wingbot-mongodb 2.22.0-alpha.4 → 3.0.0-alpha.7

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.
@@ -0,0 +1 @@
1
+ {"parent":null,"pid":52036,"argv":["/usr/local/bin/node","/Users/david/Development/wingbot-mongodb/node_modules/.bin/mocha","./test"],"execArgv":[],"cwd":"/Users/david/Development/wingbot-mongodb","time":1655802879208,"ppid":52035,"coverageFilename":"/Users/david/Development/wingbot-mongodb/.nyc_output/38fa2b60-0502-4dbc-9c7f-c478802fb2de.json","externalId":"","uuid":"38fa2b60-0502-4dbc-9c7f-c478802fb2de","files":["/Users/david/Development/wingbot-mongodb/src/BaseStorage.js","/Users/david/Development/wingbot-mongodb/src/defaultLogger.js","/Users/david/Development/wingbot-mongodb/src/AttachmentCache.js","/Users/david/Development/wingbot-mongodb/src/AuditLogStorage.js","/Users/david/Development/wingbot-mongodb/src/BotConfigStorage.js","/Users/david/Development/wingbot-mongodb/src/BotTokenStorage.js","/Users/david/Development/wingbot-mongodb/src/tokenFactory.js","/Users/david/Development/wingbot-mongodb/src/ChatLogStorage.js","/Users/david/Development/wingbot-mongodb/src/NotificationsStorage.js","/Users/david/Development/wingbot-mongodb/src/StateStorage.js"]}
@@ -1 +1 @@
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":{}}
1
+ {"processes":{"38fa2b60-0502-4dbc-9c7f-c478802fb2de":{"parent":null,"children":[]}},"files":{"/Users/david/Development/wingbot-mongodb/src/BaseStorage.js":["38fa2b60-0502-4dbc-9c7f-c478802fb2de"],"/Users/david/Development/wingbot-mongodb/src/defaultLogger.js":["38fa2b60-0502-4dbc-9c7f-c478802fb2de"],"/Users/david/Development/wingbot-mongodb/src/AttachmentCache.js":["38fa2b60-0502-4dbc-9c7f-c478802fb2de"],"/Users/david/Development/wingbot-mongodb/src/AuditLogStorage.js":["38fa2b60-0502-4dbc-9c7f-c478802fb2de"],"/Users/david/Development/wingbot-mongodb/src/BotConfigStorage.js":["38fa2b60-0502-4dbc-9c7f-c478802fb2de"],"/Users/david/Development/wingbot-mongodb/src/BotTokenStorage.js":["38fa2b60-0502-4dbc-9c7f-c478802fb2de"],"/Users/david/Development/wingbot-mongodb/src/tokenFactory.js":["38fa2b60-0502-4dbc-9c7f-c478802fb2de"],"/Users/david/Development/wingbot-mongodb/src/ChatLogStorage.js":["38fa2b60-0502-4dbc-9c7f-c478802fb2de"],"/Users/david/Development/wingbot-mongodb/src/NotificationsStorage.js":["38fa2b60-0502-4dbc-9c7f-c478802fb2de"],"/Users/david/Development/wingbot-mongodb/src/StateStorage.js":["38fa2b60-0502-4dbc-9c7f-c478802fb2de"]},"externalIds":{}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wingbot-mongodb",
3
- "version": "2.22.0-alpha.4",
3
+ "version": "3.0.0-alpha.7",
4
4
  "description": "MongoDB storage for wingbot.ai",
5
5
  "main": "src/main.js",
6
6
  "scripts": {
@@ -45,9 +45,9 @@
45
45
  "jsdoc-to-markdown": "^7.1.1",
46
46
  "jsdoc-tsimport-plugin": "^1.0.5",
47
47
  "mocha": "^9.2.2",
48
- "mongodb": "^4.5.0",
48
+ "mongodb": "^4.7.0",
49
49
  "nyc": "^15.1.0",
50
- "wingbot": "^3.30.2"
50
+ "wingbot": "^3.34.0"
51
51
  },
52
52
  "peerDependencies": {
53
53
  "mongodb": "^4.0.0"
@@ -3,6 +3,7 @@
3
3
  */
4
4
  'use strict';
5
5
 
6
+ const { ObjectId } = require('mongodb');
6
7
  const crypto = require('crypto');
7
8
  const defaultLogger = require('./defaultLogger');
8
9
 
@@ -10,6 +11,26 @@ const defaultLogger = require('./defaultLogger');
10
11
  /** @typedef {import('mongodb').Collection} Collection */
11
12
  /** @typedef {import('mongodb').CreateIndexesOptions} CreateIndexesOptions */
12
13
 
14
+ /**
15
+ *
16
+ * @param {any} obj
17
+ * @param {boolean} nested
18
+ * @param {string} [attr]
19
+ * @param {object} [ret]
20
+ * @returns {object}
21
+ */
22
+ function getNestedObjects (obj, nested, attr = null, ret = {}) {
23
+ if (typeof obj !== 'object' || !obj || nested === null || Array.isArray(obj)) {
24
+ Object.assign(ret, { [attr]: obj === undefined ? null : obj });
25
+ } else {
26
+ Object.entries(obj)
27
+ .forEach(([key, val]) => {
28
+ getNestedObjects(val, nested || null, attr ? `${attr}.${key}` : key, ret);
29
+ });
30
+ }
31
+ return ret;
32
+ }
33
+
13
34
  class BaseStorage {
14
35
 
15
36
  /**
@@ -24,7 +45,7 @@ class BaseStorage {
24
45
  *
25
46
  * class MyCoolDataStorage extends BaseStorage {
26
47
  *
27
- * constructor (mongoDb, collectionName = 'myCoolData', log = console, isCosmo = false) {
48
+ * constructor (mongoDb, collectionName, log = undefined, isCosmo = false) {
28
49
  * super(mongoDb, collectionName, log, isCosmo);
29
50
  *
30
51
  * this.addIndex({
@@ -90,21 +111,33 @@ class BaseStorage {
90
111
  }
91
112
 
92
113
  /**
114
+ * @example
115
+ * {
116
+ * _id: ObjectId.isValid(id) ? new ObjectId(input) : input
117
+ * }
93
118
  *
94
119
  * @protected
95
- * @param {string} attr
120
+ * @param {string} id
121
+ * @returns {string|ObjectId}
122
+ */
123
+ _id (id) {
124
+ return ObjectId.isValid(id) && `${id}`.length === 24
125
+ ? new ObjectId(id)
126
+ : id;
127
+ }
128
+
129
+ /**
130
+ *
131
+ * @param {string|null} attr
96
132
  * @param {{[key: string]: any}} obj
133
+ * @param {boolean} [nested]
97
134
  * @returns {{[key: string]: any}}
98
135
  */
99
- _expandObjectToSet (attr, obj) {
100
- const keys = Object.keys(obj);
101
- if (keys.length === 0) {
136
+ _expandObjectToSet (attr, obj, nested = false) {
137
+ if (Object.keys(obj).length === 0) {
102
138
  return null;
103
139
  }
104
- return keys
105
- .reduce((o, key) => Object.assign(o, {
106
- [`${attr}.${key}`]: obj[key]
107
- }), {});
140
+ return getNestedObjects(obj, nested, attr);
108
141
  }
109
142
 
110
143
  async _getOrCreateCollection (name) {
@@ -6,7 +6,7 @@
6
6
  const mongodb = require('mongodb');
7
7
  const defaultLogger = require('./defaultLogger');
8
8
 
9
- const { ObjectID } = mongodb;
9
+ const { ObjectId } = mongodb;
10
10
 
11
11
  /**
12
12
  * @typedef Target {Object}
@@ -453,8 +453,8 @@ class NotificationsStorage {
453
453
  const c = await this._getCollection(this.taksCollection);
454
454
 
455
455
  const res = await c.findOne({
456
- _id: ObjectID.isValid(taskId)
457
- ? ObjectID.createFromHexString(taskId)
456
+ _id: ObjectId.isValid(taskId)
457
+ ? ObjectId.createFromHexString(taskId)
458
458
  : taskId
459
459
  });
460
460
 
@@ -470,8 +470,8 @@ class NotificationsStorage {
470
470
  const c = await this._getCollection(this.taksCollection);
471
471
 
472
472
  const res = await c.findOneAndUpdate({
473
- _id: ObjectID.isValid(taskId)
474
- ? ObjectID.createFromHexString(taskId)
473
+ _id: ObjectId.isValid(taskId)
474
+ ? ObjectId.createFromHexString(taskId)
475
475
  : taskId
476
476
  }, {
477
477
  $set: data
@@ -607,7 +607,7 @@ class NotificationsStorage {
607
607
  });
608
608
  ret = this._mapCampaign(res.value);
609
609
  } else {
610
- const id = new ObjectID();
610
+ const id = new ObjectId();
611
611
  ret = { id: id.toHexString(), _id: id, ...campaign };
612
612
  if (updateCampaign) {
613
613
  Object.assign(ret, updateCampaign);
@@ -740,7 +740,7 @@ class NotificationsStorage {
740
740
  useCondition = {
741
741
  ...useCondition,
742
742
  _id: {
743
- $lt: ObjectID.createFromHexString(key._id)
743
+ $lt: ObjectId.createFromHexString(key._id)
744
744
  }
745
745
  };
746
746
  }
@@ -899,7 +899,7 @@ class NotificationsStorage {
899
899
  condition = {
900
900
  ...condition,
901
901
  _id: {
902
- $gt: ObjectID.createFromHexString(key._id)
902
+ $gt: ObjectId.createFromHexString(key._id)
903
903
  }
904
904
  };
905
905
  }
@@ -9,6 +9,7 @@ const defaultLogger = require('./defaultLogger');
9
9
  const USER_INDEX = 'senderId_1_pageId_1';
10
10
  const LAST_INTERACTION_INDEX = 'lastInteraction_1';
11
11
  const SEARCH = 'search-text';
12
+ const NAME = 'name_1';
12
13
 
13
14
  /**
14
15
  * @typedef {object} State
@@ -50,7 +51,12 @@ class StateStorage extends BaseStorage {
50
51
  { name: LAST_INTERACTION_INDEX }
51
52
  );
52
53
 
53
- if (!isCosmo) {
54
+ if (isCosmo) {
55
+ this.addIndex(
56
+ { name: 1 },
57
+ { name: NAME }
58
+ );
59
+ } else {
54
60
  this.addIndex(
55
61
  { '$**': 'text' },
56
62
  { name: SEARCH }
@@ -160,8 +166,12 @@ class StateStorage extends BaseStorage {
160
166
 
161
167
  if (searchStates) {
162
168
  if (this._isCosmo) {
169
+ const $regex = `^${condition.search.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')}`;
163
170
  Object.assign(useCondition, {
164
- name: { $regex: condition.search, $options: 'i' }
171
+ $or: [
172
+ { senderId: { $regex } },
173
+ { name: { $regex } }
174
+ ]
165
175
  });
166
176
  } else {
167
177
  Object.assign(useCondition, {