wingbot-mongodb 2.22.0-alpha.1 → 2.22.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"processes":{"9818b26b-1122-4466-80dd-e8cd02a9c4cf":{"parent":null,"children":[]}},"files":{"/Users/david/Development/wingbot-mongodb/src/AttachmentCache.js":["9818b26b-1122-4466-80dd-e8cd02a9c4cf"],"/Users/david/Development/wingbot-mongodb/src/AuditLogStorage.js":["9818b26b-1122-4466-80dd-e8cd02a9c4cf"],"/Users/david/Development/wingbot-mongodb/src/BaseStorage.js":["9818b26b-1122-4466-80dd-e8cd02a9c4cf"],"/Users/david/Development/wingbot-mongodb/src/BotConfigStorage.js":["9818b26b-1122-4466-80dd-e8cd02a9c4cf"],"/Users/david/Development/wingbot-mongodb/src/BotTokenStorage.js":["9818b26b-1122-4466-80dd-e8cd02a9c4cf"],"/Users/david/Development/wingbot-mongodb/src/tokenFactory.js":["9818b26b-1122-4466-80dd-e8cd02a9c4cf"],"/Users/david/Development/wingbot-mongodb/src/ChatLogStorage.js":["9818b26b-1122-4466-80dd-e8cd02a9c4cf"],"/Users/david/Development/wingbot-mongodb/src/NotificationsStorage.js":["9818b26b-1122-4466-80dd-e8cd02a9c4cf"],"/Users/david/Development/wingbot-mongodb/src/StateStorage.js":["9818b26b-1122-4466-80dd-e8cd02a9c4cf"]},"externalIds":{}}
1
+ {"processes":{"75b2d1cb-d4fe-472b-a674-acdcb631b536":{"parent":null,"children":[]}},"files":{"/Users/david/Development/wingbot-mongodb/src/AttachmentCache.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/AuditLogStorage.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/BaseStorage.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/defaultLogger.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/BotConfigStorage.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/BotTokenStorage.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/tokenFactory.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/ChatLogStorage.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/NotificationsStorage.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/StateStorage.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"]},"externalIds":{}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wingbot-mongodb",
3
- "version": "2.22.0-alpha.1",
3
+ "version": "2.22.0-alpha.4",
4
4
  "description": "MongoDB storage for wingbot.ai",
5
5
  "main": "src/main.js",
6
6
  "scripts": {
@@ -3,7 +3,8 @@
3
3
  */
4
4
  'use strict';
5
5
 
6
- const mongodb = require('mongodb'); // eslint-disable-line no-unused-vars
6
+ /** @typedef {import('mongodb').Db} Db */
7
+ /** @typedef {import('mongodb').Collection} Collection */
7
8
 
8
9
  /**
9
10
  * Cache storage for Facebook attachments
@@ -14,7 +15,7 @@ class AttachmentCache {
14
15
 
15
16
  /**
16
17
  *
17
- * @param {mongodb.Db|{():Promise<mongodb.Db>}} mongoDb
18
+ * @param {Db|{():Promise<Db>}} mongoDb
18
19
  * @param {string} collectionName
19
20
  */
20
21
  constructor (mongoDb, collectionName = 'attachments') {
@@ -22,13 +23,13 @@ class AttachmentCache {
22
23
  this._collectionName = collectionName;
23
24
 
24
25
  /**
25
- * @type {mongodb.Collection}
26
+ * @type {Collection}
26
27
  */
27
28
  this._collection = null;
28
29
  }
29
30
 
30
31
  /**
31
- * @returns {Promise<mongodb.Collection>}
32
+ * @returns {Promise<Collection>}
32
33
  */
33
34
  async _getCollection () {
34
35
  if (this._collection === null) {
@@ -5,8 +5,7 @@
5
5
 
6
6
  const jsonwebtoken = require('jsonwebtoken');
7
7
  const BaseStorage = require('./BaseStorage');
8
-
9
- /** @typedef {import('mongodb/lib/db')} Db */
8
+ const defaultLogger = require('./defaultLogger');
10
9
 
11
10
  const LEVEL_CRITICAL = 'Critical';
12
11
  const LEVEL_IMPORTANT = 'Important';
@@ -91,6 +90,9 @@ const TYPE_INFO = 'Info';
91
90
  * @returns {Promise}
92
91
  */
93
92
 
93
+ /** @typedef {import('mongodb').Db} Db */
94
+ /** @typedef {import('mongodb').Collection} Collection */
95
+
94
96
  /**
95
97
  * Storage for audit logs with signatures chain
96
98
  */
@@ -105,7 +107,7 @@ class AuditLogStorage extends BaseStorage {
105
107
  * @param {string|Promise<string>} [secret]
106
108
  * @param {string|Promise<string>} [jwtVerifier]
107
109
  */
108
- constructor (mongoDb, collectionName = 'auditlog', log = console, isCosmo = false, secret = null, jwtVerifier = null) {
110
+ constructor (mongoDb, collectionName = 'auditlog', log = defaultLogger, isCosmo = false, secret = null, jwtVerifier = null) {
109
111
  super(mongoDb, collectionName, log, isCosmo);
110
112
 
111
113
  this.addIndex({
@@ -3,11 +3,12 @@
3
3
  */
4
4
  'use strict';
5
5
 
6
- const mongodb = require('mongodb'); // eslint-disable-line no-unused-vars
7
6
  const crypto = require('crypto');
7
+ const defaultLogger = require('./defaultLogger');
8
8
 
9
- /** @typedef {import('mongodb/lib/db')} Db */
10
- /** @typedef {import('mongodb/lib/collection')} Collection */
9
+ /** @typedef {import('mongodb').Db} Db */
10
+ /** @typedef {import('mongodb').Collection} Collection */
11
+ /** @typedef {import('mongodb').CreateIndexesOptions} CreateIndexesOptions */
11
12
 
12
13
  class BaseStorage {
13
14
 
@@ -42,7 +43,7 @@ class BaseStorage {
42
43
  *
43
44
  * }
44
45
  */
45
- constructor (mongoDb, collectionName, log = console, isCosmo = false) {
46
+ constructor (mongoDb, collectionName, log = defaultLogger, isCosmo = false) {
46
47
  this._mongoDb = mongoDb;
47
48
  this._collectionName = collectionName;
48
49
  this._isCosmo = isCosmo;
@@ -76,7 +77,7 @@ class BaseStorage {
76
77
  * Add custom indexing rule
77
78
  *
78
79
  * @param {object} index
79
- * @param {mongodb.IndexOptions} options
80
+ * @param {CreateIndexesOptions} options
80
81
  */
81
82
  addIndex (index, options) {
82
83
  if (!options.name) {
@@ -88,6 +89,24 @@ class BaseStorage {
88
89
  });
89
90
  }
90
91
 
92
+ /**
93
+ *
94
+ * @protected
95
+ * @param {string} attr
96
+ * @param {{[key: string]: any}} obj
97
+ * @returns {{[key: string]: any}}
98
+ */
99
+ _expandObjectToSet (attr, obj) {
100
+ const keys = Object.keys(obj);
101
+ if (keys.length === 0) {
102
+ return null;
103
+ }
104
+ return keys
105
+ .reduce((o, key) => Object.assign(o, {
106
+ [`${attr}.${key}`]: obj[key]
107
+ }), {});
108
+ }
109
+
91
110
  async _getOrCreateCollection (name) {
92
111
  const db = typeof this._mongoDb === 'function'
93
112
  ? await this._mongoDb()
@@ -209,7 +228,7 @@ class BaseStorage {
209
228
 
210
229
  /**
211
230
  *
212
- * @private
231
+ * @protected
213
232
  * @template T
214
233
  * @param {T} object
215
234
  * @returns {T}
@@ -11,8 +11,8 @@ try {
11
11
  // noop
12
12
  }
13
13
 
14
- /** @typedef {import('mongodb/lib/db')} Db */
15
- /** @typedef {import('mongodb/lib/collection')} Collection */
14
+ /** @typedef {import('mongodb').Db} Db */
15
+ /** @typedef {import('mongodb').Collection} Collection */
16
16
 
17
17
  const CONFIG_ID = 'config';
18
18
 
@@ -3,7 +3,6 @@
3
3
  */
4
4
  'use strict';
5
5
 
6
- const mongodb = require('mongodb'); // eslint-disable-line no-unused-vars
7
6
  const tokenFactory = require('./tokenFactory');
8
7
 
9
8
  const TOKEN_INDEX = 'token-index';
@@ -16,6 +15,9 @@ const USER_INDEX = 'user-page-index';
16
15
  * @prop {string} token
17
16
  */
18
17
 
18
+ /** @typedef {import('mongodb').Db} Db */
19
+ /** @typedef {import('mongodb').Collection} Collection */
20
+
19
21
  /**
20
22
  * Storage for webview tokens
21
23
  *
@@ -25,7 +27,7 @@ class BotTokenStorage {
25
27
 
26
28
  /**
27
29
  *
28
- * @param {mongodb.Db|{():Promise<mongodb.Db>}} mongoDb
30
+ * @param {Db|{():Promise<Db>}} mongoDb
29
31
  * @param {string} collectionName
30
32
  */
31
33
  constructor (mongoDb, collectionName = 'tokens') {
@@ -33,13 +35,13 @@ class BotTokenStorage {
33
35
  this._collectionName = collectionName;
34
36
 
35
37
  /**
36
- * @type {mongodb.Collection}
38
+ * @type {Collection}
37
39
  */
38
40
  this._collection = null;
39
41
  }
40
42
 
41
43
  /**
42
- * @returns {Promise<mongodb.Collection>}
44
+ * @returns {Promise<Collection>}
43
45
  */
44
46
  async _getCollection () {
45
47
  if (this._collection === null) {
@@ -60,8 +62,7 @@ class BotTokenStorage {
60
62
  token: 1
61
63
  }, {
62
64
  unique: true,
63
- name: TOKEN_INDEX,
64
- dropDups: true
65
+ name: TOKEN_INDEX
65
66
  });
66
67
  }
67
68
  try {
@@ -75,8 +76,7 @@ class BotTokenStorage {
75
76
  pageId: 1
76
77
  }, {
77
78
  unique: true,
78
- name: USER_INDEX,
79
- dropDups: true
79
+ name: USER_INDEX
80
80
  });
81
81
  }
82
82
  }
@@ -4,11 +4,12 @@
4
4
  'use strict';
5
5
 
6
6
  const BaseStorage = require('./BaseStorage');
7
+ const defaultLogger = require('./defaultLogger');
7
8
 
8
9
  const PAGE_SENDER_TIMESTAMP = 'pageId_1_senderId_1_timestamp_-1';
9
10
  const TIMESTAMP = 'timestamp_1';
10
11
 
11
- /** @typedef {import('mongodb/lib/db')} Db */
12
+ /** @typedef {import('mongodb').Db} Db */
12
13
 
13
14
  /**
14
15
  * Storage for conversation logs
@@ -25,7 +26,7 @@ class ChatLogStorage extends BaseStorage {
25
26
  * @param {boolean} [isCosmo]
26
27
  * @param {string|Promise<string>} [secret]
27
28
  */
28
- constructor (mongoDb, collectionName = 'chatlogs', log = console, isCosmo = false, secret = null) {
29
+ constructor (mongoDb, collectionName = 'chatlogs', log = defaultLogger, isCosmo = false, secret = null) {
29
30
  super(mongoDb, collectionName, log, isCosmo);
30
31
 
31
32
  this.addIndex({
@@ -4,6 +4,7 @@
4
4
  'use strict';
5
5
 
6
6
  const mongodb = require('mongodb');
7
+ const defaultLogger = require('./defaultLogger');
7
8
 
8
9
  const { ObjectID } = mongodb;
9
10
 
@@ -84,7 +85,7 @@ class NotificationsStorage {
84
85
  * @param {{error:Function,log:Function}} [log] - console like logger
85
86
  * @param {boolean} isCosmo
86
87
  */
87
- constructor (mongoDb, collectionsPrefix = '', log = console, isCosmo = false) {
88
+ constructor (mongoDb, collectionsPrefix = '', log = defaultLogger, isCosmo = false) {
88
89
  this._mongoDb = mongoDb;
89
90
 
90
91
  this.taksCollection = `${collectionsPrefix}notification-tasks`;
@@ -3,8 +3,8 @@
3
3
  */
4
4
  'use strict';
5
5
 
6
- const mongodb = require('mongodb'); // eslint-disable-line no-unused-vars
7
6
  const BaseStorage = require('./BaseStorage');
7
+ const defaultLogger = require('./defaultLogger');
8
8
 
9
9
  const USER_INDEX = 'senderId_1_pageId_1';
10
10
  const LAST_INTERACTION_INDEX = 'lastInteraction_1';
@@ -22,6 +22,8 @@ const SEARCH = 'search-text';
22
22
  * @prop {string} [search]
23
23
  */
24
24
 
25
+ /** @typedef {import('mongodb').Db} Db */
26
+
25
27
  /**
26
28
  * Storage for chat states
27
29
  *
@@ -31,17 +33,17 @@ class StateStorage extends BaseStorage {
31
33
 
32
34
  /**
33
35
  *
34
- * @param {mongodb.Db|{():Promise<mongodb.Db>}} mongoDb
36
+ * @param {Db|{():Promise<Db>}} mongoDb
35
37
  * @param {string} collectionName
36
38
  * @param {{error:Function,log:Function}} [log] - console like logger
37
39
  * @param {boolean} isCosmo
38
40
  */
39
- constructor (mongoDb, collectionName = 'states', log = console, isCosmo = false) {
41
+ constructor (mongoDb, collectionName = 'states', log = defaultLogger, isCosmo = false) {
40
42
  super(mongoDb, collectionName, log, isCosmo);
41
43
 
42
44
  this.addIndex(
43
45
  { senderId: 1, pageId: 1 },
44
- { name: USER_INDEX, unique: true, dropDups: true }
46
+ { name: USER_INDEX, unique: true }
45
47
  );
46
48
  this.addIndex(
47
49
  { lastInteraction: isCosmo ? 1 : -1 },
@@ -77,7 +79,9 @@ class StateStorage extends BaseStorage {
77
79
  */
78
80
  async getState (senderId, pageId) {
79
81
  const c = await this._getCollection();
80
- return c.findOne({ senderId, pageId }, { projection: { _id: 0 } });
82
+ const doc = await c.findOne({ senderId, pageId }, { projection: { _id: 0 } });
83
+ // @ts-ignore
84
+ return doc;
81
85
  }
82
86
 
83
87
  /**
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @author {David Menger}
3
+ */
4
+ 'use strict';
5
+
6
+ function log (...args) {
7
+ const mapped = args.map((a) => (typeof a === 'string' ? a : JSON.stringify(a, null, 2)));
8
+ process.stderr.write(` + ${mapped.join(' ')}\n`);
9
+ }
10
+
11
+ const defaultLogger = {
12
+ log,
13
+ error: (...args) => log('ERROR:', ...args),
14
+ info: log,
15
+ warn: (...args) => log('WARN:', ...args),
16
+ debug: log
17
+ };
18
+
19
+ module.exports = defaultLogger;