wingbot-mongodb 4.0.0 → 4.1.1
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 +1 -1
- package/src/BaseStorage.js +34 -5
package/package.json
CHANGED
package/src/BaseStorage.js
CHANGED
|
@@ -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').
|
|
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
|
|
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 {...
|
|
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) {
|
|
@@ -480,6 +490,25 @@ class BaseStorage {
|
|
|
480
490
|
|
|
481
491
|
return h.digest('base64');
|
|
482
492
|
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Drop collection
|
|
496
|
+
*
|
|
497
|
+
* @returns {Promise}
|
|
498
|
+
*/
|
|
499
|
+
async drop () {
|
|
500
|
+
const db = typeof this._mongoDb === 'function'
|
|
501
|
+
? await this._mongoDb()
|
|
502
|
+
: this._mongoDb;
|
|
503
|
+
|
|
504
|
+
await db.dropCollection(this._collectionName)
|
|
505
|
+
.catch(() => {});
|
|
506
|
+
|
|
507
|
+
this._collection = null;
|
|
508
|
+
this._indexing = null;
|
|
509
|
+
this._shouldIndexBeforeRead = null;
|
|
510
|
+
this._shouldWaitForIndex = null;
|
|
511
|
+
}
|
|
483
512
|
}
|
|
484
513
|
|
|
485
514
|
module.exports = BaseStorage;
|