wingbot-mongodb 2.19.1 → 2.20.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wingbot-mongodb",
3
- "version": "2.19.1",
3
+ "version": "2.20.0",
4
4
  "description": "MongoDB storage for wingbot.ai",
5
5
  "main": "src/main.js",
6
6
  "scripts": {
@@ -57,6 +57,19 @@ class BaseStorage {
57
57
 
58
58
  this.ignoredSignatureKeys = ['_id', 'sign'];
59
59
  this._secret = null;
60
+
61
+ this.systemIndexes = ['_id_', '_id'];
62
+
63
+ this._fixtures = [];
64
+ }
65
+
66
+ /**
67
+ * Insert defalt document to DB
68
+ *
69
+ * @param {...any} objects
70
+ */
71
+ addFixtureDoc (...objects) {
72
+ this._fixtures.push(...objects);
60
73
  }
61
74
 
62
75
  /**
@@ -83,6 +96,7 @@ class BaseStorage {
83
96
  let collection;
84
97
 
85
98
  if (this._isCosmo) {
99
+ // @ts-ignore
86
100
  const collections = await db.collections();
87
101
 
88
102
  collection = collections
@@ -134,7 +148,7 @@ class BaseStorage {
134
148
  }
135
149
 
136
150
  await existing
137
- .filter((e) => !['_id_', '_id'].includes(e.name)
151
+ .filter((e) => !this.systemIndexes.includes(e.name)
138
152
  && !indexes.some((i) => e.name === i.options.name))
139
153
  .reduce((p, e) => {
140
154
  // eslint-disable-next-line no-console
@@ -147,7 +161,7 @@ class BaseStorage {
147
161
  });
148
162
  }, Promise.resolve());
149
163
 
150
- await indexes
164
+ const updated = await indexes
151
165
  .filter((i) => !existing.some((e) => e.name === i.options.name))
152
166
  .reduce((p, i) => {
153
167
  this._log.log(`creating index ${i.name}`);
@@ -155,8 +169,23 @@ class BaseStorage {
155
169
  .then(() => collection.createIndex(i.index, i.options))
156
170
  .catch((e) => {
157
171
  this._log.error(`failed to create index ${i.options.name} on ${collection.collectionName}`, e);
158
- });
159
- }, Promise.resolve());
172
+ })
173
+ .then(() => true);
174
+ }, Promise.resolve(false));
175
+
176
+ if (updated || existing.every((i) => this.systemIndexes.includes(i.name))) {
177
+ // upsert fixtures
178
+
179
+ await this._fixtures.reduce((p, o) => p
180
+ .then(() => collection.insertOne(o))
181
+ .then(() => this._log.log(`DB> Inserted fixture doc to "${this._collectionName}"`))
182
+ .catch((e) => {
183
+ if (e.code !== 11000) {
184
+ this._log.error(`DB> failed to insert fixture doc to "${this._collectionName}"`, e);
185
+ }
186
+ }),
187
+ Promise.resolve());
188
+ }
160
189
  }
161
190
 
162
191
  async _sign (object) {
@@ -36,7 +36,7 @@ class StateStorage extends BaseStorage {
36
36
  * @param {{error:Function,log:Function}} [log] - console like logger
37
37
  * @param {boolean} isCosmo
38
38
  */
39
- constructor (mongoDb, collectionName = 'chatlogs', log = console, isCosmo = false) {
39
+ constructor (mongoDb, collectionName = 'states', log = console, isCosmo = false) {
40
40
  super(mongoDb, collectionName, log, isCosmo);
41
41
 
42
42
  this.addIndex(