Storing User Input

Introduction

The Rocket.Chat Apps-Engine provides persistenceRead: IPersistenceRead andpersistence: IPersistence to help you read/write data from or to the RocketChat database.
We can get persistenceRead: IPersistenceRead through the following way:
1
// Get a persistence reader if you are using it in a method
2
// Here `this` means the main App class instance
3
const persistenceRead = this.getAccessors().reader.getPersistenceReader();
4
5
// Some methods provideds `read: IRead` parameter, so that you get a persistence
6
// reader through this parameter too.
7
const persistenceRead = read.getPersistenceRead();
Copied!
For persistence: IPersistence, you can only obtain it through parameter approach, which means you can not persist data within a method (typically is an event handler that you are going to implement) if the method doesn't have a persistence: IPersistence parameter.
1
function someMethod(context, read: IRead, persistence: IPersistence) {
2
console.log(persistence); // The only way to fetch a persistence writer object
3
}
Copied!

Examples

Below is a complete example to show how we can manage persistence methods with a class. Imagine that you are going to persist some messages. You can create a class called MessagePersistence or whatever name. Then you can add a series of static methods like below to add/remove/query data from the database.
1
import { IPersistence, IPersistenceRead } from '@rocket.chat/apps-engine/definition/accessors';
2
import { RocketChatAssociationModel, RocketChatAssociationRecord } from '@rocket.chat/apps-engine/definition/metadata';
3
import { IRoom } from '@rocket.chat/apps-engine/definition/rooms';
4
5
export class MessagePersistence {
6
// add a record
7
public static async persist(persis: IPersistence, room: IRoom, id: string): Promise<boolean> {
8
const associations: Array<RocketChatAssociationRecord> = [
9
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'message'),
10
new RocketChatAssociationRecord(RocketChatAssociationModel.ROOM, room.id),
11
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, id),
12
];
13
14
try {
15
await persis.updateByAssociations(associations, { id }, true);
16
} catch (err) {
17
console.warn(err);
18
return false;
19
}
20
21
return true;
22
}
23
24
// query all records within the "scope" - message
25
public static async findAll(persis: IPersistenceRead): Promise<Array<string>> {
26
const associations: Array<RocketChatAssociationRecord> = [
27
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'message'),
28
];
29
30
let result: Array<string> = [];
31
try {
32
const records: Array<{ id: string }> = (await persis.readByAssociations(associations)) as Array<{ id: string }>;
33
34
if (records.length) {
35
result = records.map(({ id }) => id);
36
}
37
} catch (err) {
38
console.warn(err);
39
}
40
41
return result;
42
}
43
44
// query all records by room within the "scope" - message
45
public static async findByRoom(persis: IPersistenceRead, room: IRoom): Promise<Array<string>> {
46
const associations: Array<RocketChatAssociationRecord> = [
47
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'message'),
48
new RocketChatAssociationRecord(RocketChatAssociationModel.ROOM, room.id),
49
];
50
51
let result: Array<string> = [];
52
try {
53
const records: Array<{ id: string }> = (await persis.readByAssociations(associations)) as Array<{ id: string }>;
54
55
if (records.length) {
56
result = records.map(({ id }) => id);
57
}
58
} catch (err) {
59
console.warn(err);
60
}
61
62
return result;
63
}
64
65
// query all records by room within the "scope" - message
66
public static async removeByRoom(persis: IPersistence, room: IRoom): Promise<boolean> {
67
const associations: Array<RocketChatAssociationRecord> = [
68
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'message'),
69
new RocketChatAssociationRecord(RocketChatAssociationModel.ROOM, room.id),
70
];
71
72
try {
73
await persis.removeByAssociations(associations);
74
} catch (err) {
75
console.warn(err);
76
return false;
77
}
78
79
return true;
80
}
81
82
// remove all records by id within the "scope" - message
83
public static async removeById(persis: IPersistence, id: string): Promise<boolean> {
84
const associations: Array<RocketChatAssociationRecord> = [
85
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'message'),
86
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, id),
87
];
88
89
try {
90
await persis.removeByAssociations(associations);
91
} catch (err) {
92
console.warn(err);
93
return false;
94
}
95
96
return true;
97
}
98
99
// remove all records within the "scope" - message
100
public static async clear(persis): Promise<boolean> {
101
const associations: Array<RocketChatAssociationRecord> = [
102
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'message'),
103
];
104
105
try {
106
await persis.removeByAssociations(associations);
107
} catch (err) {
108
console.warn(err);
109
return false;
110
}
111
112
return true;
113
}
114
}
Copied!
Last modified 6mo ago
Export as PDF
Copy link