Unverified Commit 1b0aaa05 authored by Gabriel Engel's avatar Gabriel Engel Committed by gitbook-bot
Browse files

GitBook: [master] 3 pages modified

parent 80ccae27
......@@ -584,5 +584,7 @@
* [Fundamentals](apps-development/fundamentals/README.md)
* [App Lifecycle](apps-development/fundamentals/app-lifecycle.md)
* [Event Interfaces](apps-development/fundamentals/event-interfaces.md)
* [Recipes](apps-development/recipes/README.md)
* [Storing User Input](apps-development/recipes/storing-user-input.md)
* [Apps-Engine API Reference](https://rocketchat.github.io/Rocket.Chat.Apps-engine)
# Storing User Input
## Introduction
The Rocket.Chat Apps-Engine provides `persistenceRead: IPersistenceRead` and`persistence: IPersistence` to help you read/write data from or to the RocketChat database.
We can get `persistenceRead: IPersistenceRead` through the following way:
```typescript
// Get a persistence reader if you are using it in a method
// Here `this` means the main App class instance
const persistenceRead = this.getAccessors().reader.getPersistenceReader();
// Some methods provideds `read: IRead` parameter, so that you get a persistence
// reader through this parameter too.
const persistenceRead = read.getPersistenceRead();
```
For `persistence: IPersistence`, you can only fetch it through parameter way, 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.
```typescript
someMethod(context, read: IRead, persistence: IPersistence) {
console.log(persistence); // The only way to fetch a persistence writer object
}
```
## Examples
Let's understand its usages with some examples:
```typescript
import { IAppAccessors, IHttp, ILogger, IPersistence, IRead } from '@rocket.chat/apps-engine/definition/accessors';
import { App } from '@rocket.chat/apps-engine/definition/App';
import { IMessage, IPreMessageSentPrevent } from '@rocket.chat/apps-engine/definition/messages';
import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata';
export class MessageEventsApp extends App implements IPreMessageSentPrevent {
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
}
public async executePostMessageSent(message: IMessage, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise<void> {
// Create an association so that we can find these records next time
const associations: Array<RocketChatAssociationRecord> = [
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'post'),
];
// Persist the text of the message with associations
if (message && message.text) {
await persis.createWithAssociations(message.text, associations);
}
}
}
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment