MongoDB storage for wingbot.ai
npm install wingbot-mongodbContains storage for tokens, chat states, bot config and chat logs.
-----------------
Storage for chat states
Storage for webview tokens
Storage for conversation logs
Storage for wingbot.ai conversation config
Cache storage for Facebook attachments
Storage for audit logs with signatures chain
objectobjectobjectmodule:mongodb/lib/dbmodule:mongodb/lib/dbmodule:mongodb/lib/collectionmodule:mongodb/lib/dbobjectobjectobjectobjectPromise.<boolean>JWT Verifier
objectPromiseAudit Log Callback
ObjectObjectobjectObjectmodule:mongodb/lib/dbmodule:mongodb/lib/collectionKind: global class
* StateStorage
* [new StateStorage(mongoDb, collectionName, [log], isCosmo)](#new_StateStorage_new)
* ~~.addCustomIndex(index, options)~~
* .getState(senderId, pageId) ⇒ Promise.<(State\|null)>
* [.getOrCreateAndLock(senderId, pageId, [defaultState], [timeout])](#StateStorage+getOrCreateAndLock) ⇒ Promise.<object>
* .getStates(condition, limit, lastKey) ⇒ Promise.<{Array.<data:State>, lastKey:string}>
* .saveState(state) ⇒ Promise.<object>
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| mongoDb | mongodb.Db \| Object | | |
| collectionName | string | "states" | |
| [log] | Object | | console like logger |
| isCosmo | boolean | false | |
Add custom indexing rule
Kind: instance method of StateStorage
| Param | Type |
| --- | --- |
| index | object |
| options | object |
| options.name | string |
StateStorage | Param | Type |
| --- | --- |
| senderId | string |
| pageId | string |
Kind: instance method of StateStorage
Returns: Promise.<object> - - conversation state
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| senderId | string | | sender identifier |
| pageId | string | | page identifier |
| [defaultState] | object | | default state of the conversation |
| [timeout] | number | 300 | given default state |
StateStorage | Param | Type | Default |
| --- | --- | --- |
| condition | StateCondition | |
| limit | number | 20 |
| lastKey | string | null |
Kind: instance method of StateStorage
| Param | Type | Description |
| --- | --- | --- |
| state | object | conversation state |
Kind: global class
* BotTokenStorage
* new BotTokenStorage(mongoDb, collectionName)
* ._collection : mongodb.Collection
* ._getCollection() ⇒ Promise.<mongodb.Collection>
* .findByToken(token) ⇒ Promise.<(Token\|null)>
* .getOrCreateToken(senderId, pageId, createToken) ⇒ Promise.<(Token\|null)>
| Param | Type | Default |
| --- | --- | --- |
| mongoDb | mongodb.Db \| Object | |
| collectionName | string | "tokens" |
BotTokenStorage BotTokenStorage BotTokenStorage | Param | Type |
| --- | --- |
| token | string |
BotTokenStorage | Param | Type |
| --- | --- |
| senderId | string |
| pageId | string |
| createToken | Object |
Kind: global class
* ChatLogStorage
* [new ChatLogStorage(mongoDb, collectionName, [log], [isCosmo], [secret])](#new_ChatLogStorage_new)
* [.getInteractions(senderId, pageId, [limit], [endAt], [startAt])](#ChatLogStorage+getInteractions) ⇒ Promise.<Array.<object>>
* [.log(senderId, responses, request, [metadata])](#ChatLogStorage+log) ⇒ Promise
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| mongoDb | Db \| Object | | |
| collectionName | string | "chatlogs" | |
| [log] | Object | | console like logger |
| [isCosmo] | boolean | false | |
| [secret] | string \| Promise.<string> | null | |
Kind: instance method of ChatLogStorage
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| senderId | string | | |
| pageId | string | | |
| [limit] | number | 10 | |
| [endAt] | number | | iterate backwards to history |
| [startAt] | number | | iterate forward to last interaction |
Kind: instance method of ChatLogStorage
| Param | Type | Description |
| --- | --- | --- |
| senderId | string | |
| responses | Array.<object> | list of sent responses |
| request | object | event request |
| [metadata] | object | request metadata |
Kind: global class
* BotConfigStorage
* new BotConfigStorage(mongoDb, collectionName)
* ._collection : Collection
* ._getCollection() ⇒ Promise.<Collection>
* [.api([onUpdate], [acl])](#BotConfigStorage+api) ⇒ Object
* .invalidateConfig() ⇒ Promise
* .getConfigTimestamp() ⇒ Promise.<number>
* [.updateConfig(newConfig, [id])](#BotConfigStorage+updateConfig) ⇒ Promise.<T>
* .setConfig(id, newConfig)
* [.getConfig([id])](#BotConfigStorage+getConfig) ⇒ Promise.<(object\|null)>
| Param | Type | Default |
| --- | --- | --- |
| mongoDb | Db \| Object | |
| collectionName | string | "botconfig" |
BotConfigStorage BotConfigStorage Kind: instance method of BotConfigStorage
| Param | Type | Description |
| --- | --- | --- |
| [onUpdate] | function | async update handler function |
| [acl] | function \| Array.<string> | acl configuration |
Kind: instance method of BotConfigStorage
BotConfigStorage BotConfigStorage | Param | Type |
| --- | --- |
| newConfig | T |
| [id] | string |
BotConfigStorage | Param | Type |
| --- | --- |
| id | string |
| newConfig | object |
BotConfigStorage | Param | Type |
| --- | --- |
| [id] | string |
Kind: global class
* AttachmentCache
* new AttachmentCache(mongoDb, collectionName)
* ._collection : mongodb.Collection
* ._getCollection() ⇒ Promise.<mongodb.Collection>
* .findAttachmentByUrl(url) ⇒ Promise.<(number\|null)>
* .saveAttachmentId(url, attachmentId) ⇒ Promise
| Param | Type | Default |
| --- | --- | --- |
| mongoDb | mongodb.Db \| Object | |
| collectionName | string | "attachments" |
AttachmentCache AttachmentCache AttachmentCache | Param | Type |
| --- | --- |
| url | string |
AttachmentCache | Param | Type |
| --- | --- |
| url | string |
| attachmentId | number |
Kind: global class
* AuditLogStorage
* [new AuditLogStorage(mongoDb, collectionName, [log], [isCosmo], [secret], [jwtVerifier])](#new_AuditLogStorage_new)
* ._jwtVerify : JwtVerifier
* .callback : AuditLogCallback
* [.log(event, user, [meta], [wid], [type], [level], [date])](#AuditLogStorage+log) ⇒ Promise
* [.list([wid], [fromSeq], [limit])](#AuditLogStorage+list) ⇒ Promise.<Array.<LogEntry>>
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| mongoDb | Db \| Object | | |
| collectionName | string | "auditlog" | |
| [log] | Object | | console like logger |
| [isCosmo] | boolean | false | |
| [secret] | string \| Promise.<string> | null | |
| [jwtVerifier] | string \| Promise.<string> | null | |
AuditLogStorage AuditLogStorage Kind: instance method of AuditLogStorage
| Param | Type | Description |
| --- | --- | --- |
| event | TrackingEvent | |
| user | User | |
| [meta] | Meta | |
| [wid] | string | workspace ID |
| [type] | string | |
| [level] | string | |
| [date] | Date | |
AuditLogStorage | Param | Type | Default | Description |
| --- | --- | --- | --- |
| [wid] | string | | workspace id |
| [fromSeq] | number | 0 | for paging |
| [limit] | number | 40 | |
* NotificationsStorage
* [new NotificationsStorage(mongoDb, collectionsPrefix, [log], isCosmo)](#new_NotificationsStorage_new)
* ._collections : Map.<string, Promise.<mongodb.Collection>>
* ._getCollection(collectionName) ⇒ Promise.<mongodb.Collection>
* .pushTasks(tasks) ⇒ Promise.<Array.<Task>>
* [.getUnsuccessfulSubscribersByCampaign(campaignId, [sentWithoutReaction], [pageId])](#NotificationsStorage+getUnsuccessfulSubscribersByCampaign)
* .getTaskById(taskId) ⇒ Promise.<(Task\|null)>
* .updateTask(taskId, data)
* .getSentTask(pageId, senderId, campaignId) ⇒ Promise.<(Task\|null)>
* .getSentCampagnIds(pageId, senderId, checkCampaignIds) ⇒ Promise.<Array.<string>>
* .updateTasksByWatermark(senderId, pageId, watermark, eventType, ts) ⇒ Promise.<Array.<Task>>
* [.upsertCampaign(campaign, [updateCampaign])](#NotificationsStorage+upsertCampaign) ⇒ Promise.<Campaign>
* .removeCampaign(campaignId) ⇒ Promise
* .incrementCampaign(campaignId, increment) ⇒ Promise
* .updateCampaign(campaignId, data) ⇒ Promise.<(Campaign\|null)>
* [.popCampaign([now])](#NotificationsStorage+popCampaign) ⇒ Promise.<(Campaign\|null)>
* .getCampaignById(campaignId) ⇒ Promise.<(null\|Campaign)>
* .getCampaignByIds(campaignIds) ⇒ Promise.<Array.<Campaign>>
* [.getCampaigns(condition, [limit], [lastKey])](#NotificationsStorage+getCampaigns) ⇒ Promise.<{Array.<data:Campaign>, lastKey:string}>
* .subscribe(senderId, pageId, tag) ⇒ Promise
* [.unsubscribe(senderId, pageId, [tag])](#NotificationsStorage+unsubscribe) ⇒ Promise.<Array.<string>>
* [.getSubscribtionsCount(include, exclude, [pageId])](#NotificationsStorage+getSubscribtionsCount) ⇒ Promise.<number>
* [.getSubscribtions(include, exclude, limit, [pageId], lastKey)](#NotificationsStorage+getSubscribtions) ⇒ Promise.<{data: Array.<Target>, lastKey: string}>
* .getSenderSubscribtions(senderId, pageId) ⇒ Promise.<Array.<string>>
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| mongoDb | mongodb.Db \| Object | | |
| collectionsPrefix | string | | |
| [log] | Object | | console like logger |
| isCosmo | boolean | false | |
NotificationsStorage NotificationsStorage | Param | Type |
| --- | --- |
| collectionName | string |
NotificationsStorage | Param | Type |
| --- | --- |
| tasks | object |
NotificationsStorage | Param | Type | Default |
| --- | --- | --- |
| campaignId | string | |
| [sentWithoutReaction] | boolean | false |
| [pageId] | string | null |
Kind: instance method of NotificationsStorage
| Param | Type |
| --- | --- |
| taskId | string |
NotificationsStorage | Param | Type |
| --- | --- |
| taskId | string |
| data | object |
Kind: instance method of NotificationsStorage
| Param | Type |
| --- | --- |
| pageId | string |
| senderId | string |
| campaignId | string |
NotificationsStorage | Param | Type |
| --- | --- |
| pageId | string |
| senderId | string |
| checkCampaignIds | Array.<string> |
NotificationsStorage | Param | Type |
| --- | --- |
| senderId | string |
| pageId | string |
| watermark | number |
| eventType | 'read' \| 'delivery' |
| ts | number |
NotificationsStorage | Param | Type | Default |
| --- | --- | --- |
| campaign | object | |
| [updateCampaign] | object | |
NotificationsStorage | Param | Type |
| --- | --- |
| campaignId | string |
NotificationsStorage | Param | Type |
| --- | --- |
| campaignId | string |
| increment | object |
NotificationsStorage | Param | Type |
| --- | --- |
| campaignId | string |
| data | object |
NotificationsStorage | Param | Type |
| --- | --- |
| [now] | number |
NotificationsStorage | Param | Type |
| --- | --- |
| campaignId | string |
NotificationsStorage | Param | Type |
| --- | --- |
| campaignIds | Array.<string> |
NotificationsStorage | Param | Type | Default |
| --- | --- | --- |
| condition | object | |
| [limit] | number | |
| [lastKey] | object | |
NotificationsStorage | Param | Type |
| --- | --- |
| senderId | string |
| pageId | string |
| tag | string |
NotificationsStorage | Param | Type | Default |
| --- | --- | --- |
| senderId | string | |
| pageId | string | |
| [tag] | string | null |
NotificationsStorage | Param | Type | Default |
| --- | --- | --- |
| include | Array.<string> | |
| exclude | Array.<string> | |
| [pageId] | string | null |
NotificationsStorage | Param | Type | Default |
| --- | --- | --- |
| include | Array.<string> | |
| exclude | Array.<string> | |
| limit | number | |
| [pageId] | string | null |
| lastKey | \* | |
NotificationsStorage | Param | Type |
| --- | --- |
| senderId | string |
| pageId | string |
* BaseStorage
* [new BaseStorage(mongoDb, collectionName, [log], [isCosmo])](#new_BaseStorage_new)
* ._collection : Collection \| Promise.<Collection>
* .addFixtureDoc(...objects)
* .addIndex(index, options)
* ._getCollection() ⇒ Promise.<Collection>
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| mongoDb | Db \| Object | | |
| collectionName | string | | |
| [log] | Object | | console like logger |
| [isCosmo] | boolean | false | |
Example
``javascript
const { BaseStorage } = require('winbot-mongodb');
class MyCoolDataStorage extends BaseStorage {
constructor (mongoDb, collectionName = 'myCoolData', log = console, isCosmo = false) {
super(mongoDb, collectionName, log, isCosmo);
this.addIndex({
foo: -1
}, {
name: 'foo_1'
});
this.addIndex({
bar: -1,
baz: 1
}, {
name: 'bar_-1_baz_1'
});
}
BaseStorage Kind: instance method of BaseStorage
| Param | Type |
| --- | --- |
| ...objects | any |
Kind: instance method of BaseStorage
| Param | Type |
| --- | --- |
| index | object |
| options | mongodb.IndexOptions |
Kind: instance method of BaseStorage
object| Name | Type |
| --- | --- |
| senderId | string |
| pageId | string |
| state | object |
object| Name | Type |
| --- | --- |
| [search] | string |
object| Name | Type |
| --- | --- |
| senderId | string |
| pageId | string |
| token | string |
module:mongodb/lib/dbmodule:mongodb/lib/dbmodule:mongodb/lib/collectionmodule:mongodb/lib/dbobject| Name | Type | Default |
| --- | --- | --- |
| [type] | string | "'audit'" |
| category | string | |
| action | string | |
| [label] | string | |
| [payload] | object | |
object| Name | Type | Description |
| --- | --- | --- |
| [id] | string | |
| [senderId] | string | |
| [pageId] | string | |
| [jwt] | string | jwt to check the authorship |
object| Name | Type | Description |
| --- | --- | --- |
| [ip] | string | |
| [ua] | string | |
| [ro] | string | referrer || origin |
object| Name | Type | Default | Description |
| --- | --- | --- | --- |
| date | string | | ISO date |
| delta | number | | time skew in ms if there was a write conflict |
| [eventType] | string | "'audit'" | |
| category | string | | |
| action | string | | |
| [label] | string | | |
| [payload] | object | | |
| level | string | | (Critical|Important|Debug) |
| ok | boolean | | signature matches |
| seq | number | | sequence number |
| type | string | | (Error|Warn|Info) |
| user | User | | |
| wid | string | | workspace id |
| meta | Meta | | |
Promise.<boolean>Kind: global typedef
| Param | Type |
| --- | --- |
| token | string |
| userId | string |
| [user] | User |
object| Name | Type | Default | Description |
| --- | --- | --- | --- |
| date | string | | ISO date |
| [eventType] | string | "'audit'" | |
| category | string | | |
| action | string | | |
| [label] | string | | |
| [payload] | object | | |
| level | string | | (Critical|Important|Debug) |
| type | string | | (Error|Warn|Info) |
| user | User | | |
| wid | string | | workspace id |
| meta | Meta | | |
PromiseKind: global typedef
| Param | Type |
| --- | --- |
| entry | AuditLogEntry |
Object| Name | Type |
| --- | --- |
| senderId | string |
| pageId | string |
Object| Name | Type |
| --- | --- |
| senderId | string |
| pageId | string |
| subs | Array.<string> |
object| Name | Type | Description |
| --- | --- | --- |
| id | string | |
| name | string | Tatgeting |
| include | Array.<string> | |
| exclude | Array.<string> | Stats |
| sent | number | |
| succeeded | number | |
| failed | number | |
| unsubscribed | number | |
| delivery | number | |
| read | number | |
| notSent | number | |
| leaved | number | |
| queued | number | Interaction |
| action | string | |
| [data] | object | Setup |
| sliding | boolean | |
| slide | number | |
| slideRound | number | |
| active | boolean | |
| in24hourWindow | boolean | |
| startAt | number | |
Object| Name | Type | Description |
| --- | --- | --- |
| id | string | |
| pageId | string | |
| senderId | string | |
| campaignId | string | |
| enqueue | number | |
| [read] | number | |
| [delivery] | number | |
| [sent] | number | |
| [insEnqueue] | number | |
| [reaction] | boolean | user reacted |
| [leaved] | number | time the event was not sent because user left |
module:mongodb/lib/dbmodule:mongodb/lib/collection