wingbot-mongodb 4.1.0 → 4.1.2

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/BaseStorage.js +20 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wingbot-mongodb",
3
- "version": "4.1.0",
3
+ "version": "4.1.2",
4
4
  "description": "MongoDB storage for wingbot.ai",
5
5
  "main": "src/main.js",
6
6
  "scripts": {
@@ -9,7 +9,7 @@ const crypto = require('crypto');
9
9
  const defaultLogger = require('./defaultLogger');
10
10
 
11
11
  /** @typedef {import('mongodb').Db} Db */
12
- /** @typedef {import('mongodb').Collection} Collection */
12
+ /** @typedef {import('mongodb').Document} Document */
13
13
  /** @typedef {import('mongodb').CreateIndexesOptions} CreateIndexesOptions */
14
14
 
15
15
  /**
@@ -49,6 +49,9 @@ function signReplacer (k, v) {
49
49
  return v;
50
50
  }
51
51
 
52
+ /**
53
+ * @template T={} {Document}
54
+ */
52
55
  class BaseStorage {
53
56
 
54
57
  static netFailuresIntervalMs = 600000; // 10 minutes
@@ -108,7 +111,7 @@ class BaseStorage {
108
111
  this._log = log;
109
112
 
110
113
  /**
111
- * @type {Collection|Promise<Collection>}
114
+ * @type {import('mongodb').Collection<T>|Promise<import('mongodb').Collection<T>>}
112
115
  */
113
116
  this._collection = null;
114
117
 
@@ -189,7 +192,7 @@ class BaseStorage {
189
192
  /**
190
193
  * Insert defalt document to DB
191
194
  *
192
- * @param {...{ _id: string|number|ObjectId }} objects
195
+ * @param {...T} objects
193
196
  */
194
197
  addFixtureDoc (...objects) {
195
198
  this._fixtures.push(...objects);
@@ -241,17 +244,24 @@ class BaseStorage {
241
244
  return getNestedObjects(obj, nested, attr);
242
245
  }
243
246
 
247
+ /**
248
+ *
249
+ * @param {string} name
250
+ * @returns {Promise<import('mongodb').Collection<T>>}
251
+ */
244
252
  async _getOrCreateCollection (name) {
245
253
  const db = typeof this._mongoDb === 'function'
246
254
  ? await this._mongoDb()
247
255
  : this._mongoDb;
248
256
 
257
+ /** @type {import('mongodb').Collection<T>} */
249
258
  let collection;
250
259
 
251
260
  if (this._isCosmo) {
252
261
  // @ts-ignore
253
262
  const collections = await db.collections();
254
263
 
264
+ // @ts-ignore
255
265
  collection = collections
256
266
  .find((c) => c.collectionName === name);
257
267
 
@@ -290,7 +300,7 @@ class BaseStorage {
290
300
  *
291
301
  * @protected
292
302
  * @param {boolean} [forRead]
293
- * @returns {Promise<Collection>}
303
+ * @returns {Promise<import('mongodb').Collection<T>>}
294
304
  */
295
305
  async _getCollection (forRead = false) {
296
306
  if (this._collection === null) {
@@ -321,7 +331,7 @@ class BaseStorage {
321
331
  /**
322
332
  *
323
333
  * @param {object[]} indexes
324
- * @param {Collection} collection
334
+ * @param {import('mongodb').Collection<T>} collection
325
335
  * @returns {Promise}
326
336
  */
327
337
  async _ensureIndexes (indexes, collection) {
@@ -456,7 +466,11 @@ class BaseStorage {
456
466
  return entries.reduce((o, key) => {
457
467
  let val = object[key];
458
468
  if (val !== null && typeof val === 'object') {
459
- val = objectClone(val);
469
+ try {
470
+ val = objectClone(val);
471
+ } catch (e) {
472
+ val = JSON.parse(JSON.stringify(val, signReplacer));
473
+ }
460
474
  }
461
475
  return Object.assign(o, { [key]: val });
462
476
  }, {});