cry-db 2.1.29 → 2.1.32
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/dist/base.js +33 -32
- package/dist/base.js.map +1 -0
- package/dist/db.js +18 -12
- package/dist/db.js.map +1 -0
- package/dist/index.js +40 -6
- package/dist/index.js.map +1 -0
- package/dist/mongo.d.ts.map +1 -1
- package/dist/mongo.js +182 -175
- package/dist/mongo.js.map +1 -0
- package/dist/repo.js +17 -11
- package/dist/repo.js.map +1 -0
- package/dist/types.js +5 -1
- package/dist/types.js.map +1 -0
- package/package.json +1 -2
package/dist/mongo.js
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DummyExportToFixTsCompilation = void 0;
|
|
7
|
+
const bcrypt_1 = __importDefault(require("bcrypt"));
|
|
8
|
+
const mongodb_1 = require("mongodb");
|
|
9
|
+
const tiny_typed_emitter_1 = require("tiny-typed-emitter");
|
|
10
|
+
const db_js_1 = require("./db.js");
|
|
11
|
+
const types_js_1 = require("./types.js");
|
|
12
|
+
const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
|
|
13
|
+
const base_js_1 = require("./base.js");
|
|
14
|
+
const assert = (cond, msg) => {
|
|
15
|
+
if (!cond) {
|
|
16
|
+
console.log("assert failed", cond || msg);
|
|
17
|
+
throw new Error(msg || cond);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
9
20
|
const saltRounds = 10;
|
|
10
21
|
const TRANSACTION_OPTIONS = {
|
|
11
22
|
defaultTransactionOptions: {
|
|
12
|
-
readPreference: new ReadPreference("primary"),
|
|
13
|
-
readConcern: new ReadConcern("local"),
|
|
14
|
-
writeConcern: new WriteConcern("majority")
|
|
23
|
+
readPreference: new mongodb_1.ReadPreference("primary"),
|
|
24
|
+
readConcern: new mongodb_1.ReadConcern("local"),
|
|
25
|
+
writeConcern: new mongodb_1.WriteConcern("majority")
|
|
15
26
|
}
|
|
16
27
|
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
revisions;
|
|
20
|
-
softdelete;
|
|
21
|
-
session;
|
|
22
|
-
emittingPublishEvents;
|
|
23
|
-
auditing;
|
|
24
|
-
auditCollectionName;
|
|
25
|
-
auditedCollections;
|
|
26
|
-
emitter;
|
|
27
|
-
user;
|
|
28
|
-
audit;
|
|
29
|
-
syncSupport;
|
|
28
|
+
exports.DummyExportToFixTsCompilation = true;
|
|
29
|
+
class Mongo extends db_js_1.Db {
|
|
30
30
|
constructor(db, url) {
|
|
31
31
|
super(db, url);
|
|
32
32
|
this.revisions = false;
|
|
@@ -37,21 +37,21 @@ export default class Mongo extends Db {
|
|
|
37
37
|
this.auditing = false;
|
|
38
38
|
this.auditCollectionName = "dblog";
|
|
39
39
|
this.auditedCollections = this.auditCollections(process.env.AUDIT_COLLECTIONS || []);
|
|
40
|
-
this.emitter = new TypedEmitter();
|
|
40
|
+
this.emitter = new tiny_typed_emitter_1.TypedEmitter();
|
|
41
41
|
this.user = undefined;
|
|
42
42
|
this.audit = undefined;
|
|
43
|
-
log.debug('new Mongo:', this.url, this.db);
|
|
43
|
+
db_js_1.log.debug('new Mongo:', this.url, this.db);
|
|
44
44
|
}
|
|
45
45
|
on(evt, listener) {
|
|
46
|
-
log.debug("on", evt, listener);
|
|
46
|
+
db_js_1.log.debug("on", evt, listener);
|
|
47
47
|
this.emitter.on(evt, listener);
|
|
48
48
|
}
|
|
49
49
|
off(evt, listener) {
|
|
50
|
-
log.debug("off", evt, listener);
|
|
50
|
+
db_js_1.log.debug("off", evt, listener);
|
|
51
51
|
this.emitter.off(evt, listener);
|
|
52
52
|
}
|
|
53
53
|
once(evt, listener) {
|
|
54
|
-
log.debug("off", evt, listener);
|
|
54
|
+
db_js_1.log.debug("off", evt, listener);
|
|
55
55
|
this.emitter.off(evt, listener);
|
|
56
56
|
}
|
|
57
57
|
setUser(username) {
|
|
@@ -121,19 +121,19 @@ export default class Mongo extends Db {
|
|
|
121
121
|
return this.emittingPublishEvents;
|
|
122
122
|
}
|
|
123
123
|
async distinct(collection, field) {
|
|
124
|
-
log.debug('distinct called', collection, field);
|
|
124
|
+
db_js_1.log.debug('distinct called', collection, field);
|
|
125
125
|
let ret = await this.executeTransactionally(collection, async (conn) => {
|
|
126
126
|
return await conn.distinct(field);
|
|
127
127
|
}, false, { operation: "distinct", collection, field });
|
|
128
|
-
log.debug('distinct returns', ret);
|
|
128
|
+
db_js_1.log.debug('distinct returns', ret);
|
|
129
129
|
return ret;
|
|
130
130
|
}
|
|
131
131
|
async count(collection, query = {}, opts = {}) {
|
|
132
|
-
log.debug('distinct called', collection, query, opts);
|
|
132
|
+
db_js_1.log.debug('distinct called', collection, query, opts);
|
|
133
133
|
let ret = await this.executeTransactionally(collection, async (conn) => {
|
|
134
134
|
return await conn.countDocuments(query, opts);
|
|
135
135
|
}, false, { operation: "count", collection, query, opts });
|
|
136
|
-
log.debug('count returns', ret);
|
|
136
|
+
db_js_1.log.debug('count returns', ret);
|
|
137
137
|
return ret;
|
|
138
138
|
}
|
|
139
139
|
async find(collection, query = {}, opts = {}) {
|
|
@@ -143,7 +143,7 @@ export default class Mongo extends Db {
|
|
|
143
143
|
if (!query._deleted)
|
|
144
144
|
query._deleted = { $exists: false };
|
|
145
145
|
}
|
|
146
|
-
log.debug('find called', collection, query, opts);
|
|
146
|
+
db_js_1.log.debug('find called', collection, query, opts);
|
|
147
147
|
let ret = await this.executeTransactionally(collection, async (conn) => {
|
|
148
148
|
let optsIn = {};
|
|
149
149
|
if (opts.readPreference)
|
|
@@ -164,7 +164,7 @@ export default class Mongo extends Db {
|
|
|
164
164
|
let res = await r.toArray();
|
|
165
165
|
return this._processReturnedObject(res);
|
|
166
166
|
}, false, { operation: "find", collection, query, opts });
|
|
167
|
-
log.debug('find returns', ret);
|
|
167
|
+
db_js_1.log.debug('find returns', ret);
|
|
168
168
|
return ret;
|
|
169
169
|
}
|
|
170
170
|
async findAll(collection, query = {}, opts = {}) {
|
|
@@ -189,12 +189,12 @@ export default class Mongo extends Db {
|
|
|
189
189
|
r = r.collation(opts.collation);
|
|
190
190
|
return this._processReturnedObject(await r.toArray());
|
|
191
191
|
}, false, { operation: "findAll", collection, query, opts });
|
|
192
|
-
log.debug('findAll returns', ret);
|
|
192
|
+
db_js_1.log.debug('findAll returns', ret);
|
|
193
193
|
return ret;
|
|
194
194
|
}
|
|
195
195
|
async findNewer(collection, timestamp, query = {}, opts = {}) {
|
|
196
196
|
query = this._createQueryForNewer(timestamp, query);
|
|
197
|
-
log.debug('findNewer called', collection, timestamp, query, opts);
|
|
197
|
+
db_js_1.log.debug('findNewer called', collection, timestamp, query, opts);
|
|
198
198
|
let ret = await this.executeTransactionally(collection, async (conn) => {
|
|
199
199
|
let optsIn = {};
|
|
200
200
|
if (opts.readPreference)
|
|
@@ -216,11 +216,11 @@ export default class Mongo extends Db {
|
|
|
216
216
|
r = r.collation(opts.collation);
|
|
217
217
|
return this._processReturnedObject(await r.toArray());
|
|
218
218
|
}, false, { operation: "findNewer", collection, timestamp, query, opts });
|
|
219
|
-
log.debug('findNewer returns', ret);
|
|
219
|
+
db_js_1.log.debug('findNewer returns', ret);
|
|
220
220
|
return ret;
|
|
221
221
|
}
|
|
222
222
|
async findNewerMany(spec = []) {
|
|
223
|
-
log.debug('findNewerMany called', spec);
|
|
223
|
+
db_js_1.log.debug('findNewerMany called', spec);
|
|
224
224
|
let conn = await this.connect();
|
|
225
225
|
const getOneColl = async (coll) => {
|
|
226
226
|
let query = this._createQueryForNewer(coll.timestamp, coll.query);
|
|
@@ -253,7 +253,7 @@ export default class Mongo extends Db {
|
|
|
253
253
|
_createQueryForNewer(timestamp, query) {
|
|
254
254
|
let ts = (timestamp === 1 || timestamp === "1" || timestamp === "0" || timestamp === 0)
|
|
255
255
|
? {}
|
|
256
|
-
: { _ts: { $gt: Base.timestamp(timestamp) } };
|
|
256
|
+
: { _ts: { $gt: base_js_1.Base.timestamp(timestamp) } };
|
|
257
257
|
query = {
|
|
258
258
|
...ts,
|
|
259
259
|
...(query || {}),
|
|
@@ -263,7 +263,7 @@ export default class Mongo extends Db {
|
|
|
263
263
|
}
|
|
264
264
|
async findAfter(collection, csq, query = {}, opts = {}) {
|
|
265
265
|
query._csq = { $gt: csq };
|
|
266
|
-
log.debug('findAfter called', collection, csq, query, opts);
|
|
266
|
+
db_js_1.log.debug('findAfter called', collection, csq, query, opts);
|
|
267
267
|
let ret = await this.executeTransactionally(collection, async (conn) => {
|
|
268
268
|
let optsIn = {};
|
|
269
269
|
if (opts.readPreference)
|
|
@@ -285,11 +285,11 @@ export default class Mongo extends Db {
|
|
|
285
285
|
r = r.collation(opts.collation);
|
|
286
286
|
return this._processReturnedObject(await r.toArray());
|
|
287
287
|
}, false, { operation: "findNewer", collection, csq, query, opts });
|
|
288
|
-
log.debug('findNewer returns', ret);
|
|
288
|
+
db_js_1.log.debug('findNewer returns', ret);
|
|
289
289
|
return ret;
|
|
290
290
|
}
|
|
291
291
|
async findAfterMany(spec = []) {
|
|
292
|
-
log.debug('findAfterMany called', spec);
|
|
292
|
+
db_js_1.log.debug('findAfterMany called', spec);
|
|
293
293
|
let conn = await this.connect();
|
|
294
294
|
const getOneColl = async (coll) => {
|
|
295
295
|
let r = conn
|
|
@@ -321,10 +321,10 @@ export default class Mongo extends Db {
|
|
|
321
321
|
return out;
|
|
322
322
|
}
|
|
323
323
|
async findNewerFromDate(collection, date, query = {}, opts = {}) {
|
|
324
|
-
let ts = new Timestamp(0, new Date(date).valueOf() / 1000);
|
|
325
|
-
log.debug('findNewerFromDate called', collection, date, query, opts);
|
|
324
|
+
let ts = new mongodb_1.Timestamp(0, new Date(date).valueOf() / 1000);
|
|
325
|
+
db_js_1.log.debug('findNewerFromDate called', collection, date, query, opts);
|
|
326
326
|
let ret = await Mongo.prototype.findNewer.call(this, collection, ts, query, opts); // prevent calling Repo.findNewer
|
|
327
|
-
log.debug('findNewerFromDate returns', ret);
|
|
327
|
+
db_js_1.log.debug('findNewerFromDate returns', ret);
|
|
328
328
|
return ret;
|
|
329
329
|
}
|
|
330
330
|
async findOne(collection, query, projection) {
|
|
@@ -334,9 +334,9 @@ export default class Mongo extends Db {
|
|
|
334
334
|
if (!query._deleted)
|
|
335
335
|
query._deleted = { $exists: false };
|
|
336
336
|
// if (!query._blocked) query._blocked = { $exists: false }; // intentionally - blocked records are returned
|
|
337
|
-
log.debug('findOne called', collection, query, projection);
|
|
337
|
+
db_js_1.log.debug('findOne called', collection, query, projection);
|
|
338
338
|
let ret = await this.executeTransactionally(collection, async (conn) => await conn.findOne(query, { ...(projection ? { projection } : {}), ...this._sessionOpt() }), false, { operation: "findOne", collection, query, projection });
|
|
339
|
-
log.debug('findOne returns', ret);
|
|
339
|
+
db_js_1.log.debug('findOne returns', ret);
|
|
340
340
|
return this._processReturnedObject(ret);
|
|
341
341
|
}
|
|
342
342
|
async findById(collection, id, projection) {
|
|
@@ -348,15 +348,15 @@ export default class Mongo extends Db {
|
|
|
348
348
|
_id: Mongo._toId(id),
|
|
349
349
|
// _deleted: { $exists: false }
|
|
350
350
|
};
|
|
351
|
-
log.debug('findById called', this.db, collection, id, projection);
|
|
352
|
-
log.trace('findById executing with query', collection, query, projection);
|
|
351
|
+
db_js_1.log.debug('findById called', this.db, collection, id, projection);
|
|
352
|
+
db_js_1.log.trace('findById executing with query', collection, query, projection);
|
|
353
353
|
let ret = await this.executeTransactionally(collection, async (conn) => {
|
|
354
354
|
let r = await conn.findOne(query, { ...(projection ? { projection } : {}), ...this._sessionOpt() });
|
|
355
355
|
return r;
|
|
356
356
|
}, false, { operation: "findById", collection, id, projection });
|
|
357
|
-
if (ret
|
|
357
|
+
if (ret === null || ret === void 0 ? void 0 : ret._deleted)
|
|
358
358
|
ret = null;
|
|
359
|
-
log.debug('findById returns', ret);
|
|
359
|
+
db_js_1.log.debug('findById returns', ret);
|
|
360
360
|
return this._processReturnedObject(ret);
|
|
361
361
|
}
|
|
362
362
|
async updateOne(collection, query, update, options = { returnFullObject: false }) {
|
|
@@ -371,7 +371,7 @@ export default class Mongo extends Db {
|
|
|
371
371
|
...this._sessionOpt()
|
|
372
372
|
};
|
|
373
373
|
update = await this._processUpdateObject(update);
|
|
374
|
-
log.debug('updateOne called', collection, query, update);
|
|
374
|
+
db_js_1.log.debug('updateOne called', collection, query, update);
|
|
375
375
|
let seqKeys = this._findSequenceKeys(update.$set);
|
|
376
376
|
let obj = await this.executeTransactionally(collection, async (conn, client) => {
|
|
377
377
|
update.$set = update.$set || {};
|
|
@@ -382,11 +382,11 @@ export default class Mongo extends Db {
|
|
|
382
382
|
let res = await conn.findOneAndUpdate(query, update, opts);
|
|
383
383
|
if (!res.value)
|
|
384
384
|
return null;
|
|
385
|
-
let resObj = this._removeUnchanged(res.value, update, !!options
|
|
385
|
+
let resObj = this._removeUnchanged(res.value, update, !!(options === null || options === void 0 ? void 0 : options.returnFullObject));
|
|
386
386
|
await this._publishAndAudit('update', this.db, collection, resObj);
|
|
387
387
|
return resObj;
|
|
388
388
|
}, !!seqKeys, { operation: "updateOne", collection, query, update, options });
|
|
389
|
-
log.debug('updateOne returns', obj);
|
|
389
|
+
db_js_1.log.debug('updateOne returns', obj);
|
|
390
390
|
return this._processReturnedObject(await obj);
|
|
391
391
|
}
|
|
392
392
|
async save(collection, update, id = undefined, options = { returnFullObject: false }) {
|
|
@@ -400,7 +400,7 @@ export default class Mongo extends Db {
|
|
|
400
400
|
};
|
|
401
401
|
let _id = Mongo.toId(id || update._id) || Mongo.newid();
|
|
402
402
|
update = await this._processUpdateObject(update);
|
|
403
|
-
log.debug('save called', collection, id, update);
|
|
403
|
+
db_js_1.log.debug('save called', collection, id, update);
|
|
404
404
|
let seqKeys = this._findSequenceKeys(update.$set);
|
|
405
405
|
let obj = await this.executeTransactionally(collection, async (conn, client) => {
|
|
406
406
|
update.$set = update.$set || {};
|
|
@@ -411,11 +411,11 @@ export default class Mongo extends Db {
|
|
|
411
411
|
let res = await conn.findOneAndUpdate({ _id }, update, opts);
|
|
412
412
|
if (!res.value)
|
|
413
413
|
return null;
|
|
414
|
-
let resObj = this._removeUnchanged(res.value, update, !!options
|
|
414
|
+
let resObj = this._removeUnchanged(res.value, update, !!(options === null || options === void 0 ? void 0 : options.returnFullObject));
|
|
415
415
|
await this._publishAndAudit('update', this.db, collection, resObj);
|
|
416
416
|
return resObj;
|
|
417
417
|
}, !!seqKeys, { operation: "save", collection, _id, update, options });
|
|
418
|
-
log.debug('save returns', obj);
|
|
418
|
+
db_js_1.log.debug('save returns', obj);
|
|
419
419
|
return this._processReturnedObject(await obj);
|
|
420
420
|
}
|
|
421
421
|
async update(collection, query, update) {
|
|
@@ -423,7 +423,7 @@ export default class Mongo extends Db {
|
|
|
423
423
|
assert(query);
|
|
424
424
|
assert(update);
|
|
425
425
|
if (this.syncSupport)
|
|
426
|
-
log.warn("update does not increase _csq, avoit it.");
|
|
426
|
+
db_js_1.log.warn("update does not increase _csq, avoit it.");
|
|
427
427
|
if (!Object.keys(update).length)
|
|
428
428
|
return { n: 0, ok: false };
|
|
429
429
|
query = this.replaceIds(query);
|
|
@@ -434,7 +434,7 @@ export default class Mongo extends Db {
|
|
|
434
434
|
...this._sessionOpt()
|
|
435
435
|
};
|
|
436
436
|
update = await this._processUpdateObject(update);
|
|
437
|
-
log.debug('update called', collection, query, update);
|
|
437
|
+
db_js_1.log.debug('update called', collection, query, update);
|
|
438
438
|
let seqKeys = this._findSequenceKeys(update.$set);
|
|
439
439
|
let obj = await this.executeTransactionally(collection, async (conn, client) => {
|
|
440
440
|
update.$set = update.$set || {};
|
|
@@ -442,7 +442,7 @@ export default class Mongo extends Db {
|
|
|
442
442
|
await this._processSequenceField(client, collection, update.$set, seqKeys);
|
|
443
443
|
if (update.$set === undefined || Object.keys(update.$set).length === 0)
|
|
444
444
|
delete update.$set;
|
|
445
|
-
log.debug('update called', collection, query, update);
|
|
445
|
+
db_js_1.log.debug('update called', collection, query, update);
|
|
446
446
|
let res = await conn.updateMany(query, update, opts);
|
|
447
447
|
let resObj = {
|
|
448
448
|
n: res.modifiedCount,
|
|
@@ -451,7 +451,7 @@ export default class Mongo extends Db {
|
|
|
451
451
|
await this._publishAndAudit('updateMany', this.db, collection, resObj);
|
|
452
452
|
return resObj;
|
|
453
453
|
}, !!seqKeys, { operation: "update", collection, query, update });
|
|
454
|
-
log.debug('update returns', obj);
|
|
454
|
+
db_js_1.log.debug('update returns', obj);
|
|
455
455
|
return await obj;
|
|
456
456
|
}
|
|
457
457
|
async upsert(collection, query, update, options = { returnFullObject: false }) {
|
|
@@ -468,13 +468,14 @@ export default class Mongo extends Db {
|
|
|
468
468
|
returnDocument: "after",
|
|
469
469
|
...this._sessionOpt()
|
|
470
470
|
};
|
|
471
|
-
log.debug('upsert called', collection, query, update);
|
|
471
|
+
db_js_1.log.debug('upsert called', collection, query, update);
|
|
472
472
|
update = await this._processUpdateObject(update);
|
|
473
473
|
let seqKeys = this._findSequenceKeys(update.$set);
|
|
474
|
-
log.debug('upsert processed', collection, query, update);
|
|
474
|
+
db_js_1.log.debug('upsert processed', collection, query, update);
|
|
475
475
|
if (Object.keys(query).length === 0)
|
|
476
476
|
query._id = Mongo.newid();
|
|
477
477
|
let ret = await this.executeTransactionally(collection, async (conn, client) => {
|
|
478
|
+
var _a;
|
|
478
479
|
update.$set = update.$set || {};
|
|
479
480
|
if (seqKeys)
|
|
480
481
|
await this._processSequenceField(client, collection, update.$set, seqKeys);
|
|
@@ -482,26 +483,26 @@ export default class Mongo extends Db {
|
|
|
482
483
|
delete update.$set;
|
|
483
484
|
let ret = await conn.findOneAndUpdate(query, update, opts);
|
|
484
485
|
if (ret.value) {
|
|
485
|
-
let oper = ret.lastErrorObject
|
|
486
|
-
let retObj = oper === "insert" ? ret.value : this._removeUnchanged(ret.value, update, !!options
|
|
486
|
+
let oper = ((_a = ret.lastErrorObject) === null || _a === void 0 ? void 0 : _a.updatedExisting) ? "update" : "insert";
|
|
487
|
+
let retObj = oper === "insert" ? ret.value : this._removeUnchanged(ret.value, update, !!(options === null || options === void 0 ? void 0 : options.returnFullObject));
|
|
487
488
|
await this._publishAndAudit(oper, this.db, collection, retObj);
|
|
488
489
|
return retObj;
|
|
489
490
|
}
|
|
490
491
|
;
|
|
491
492
|
return ret;
|
|
492
493
|
}, !!seqKeys, { operation: "upsert", query, update, options });
|
|
493
|
-
log.debug('upsert returns', ret);
|
|
494
|
+
db_js_1.log.debug('upsert returns', ret);
|
|
494
495
|
return this._processReturnedObject(await ret);
|
|
495
496
|
}
|
|
496
497
|
async insert(collection, insert) {
|
|
497
498
|
assert(collection, "collection can't be null");
|
|
498
499
|
assert(insert, "insert can't be null");
|
|
499
500
|
assert(typeof insert === "object", "insert must be an object");
|
|
500
|
-
log.debug('insert called', collection, insert);
|
|
501
|
+
db_js_1.log.debug('insert called', collection, insert);
|
|
501
502
|
insert = this.replaceIds(insert);
|
|
502
503
|
if (this.revisions) {
|
|
503
504
|
insert._rev = 1;
|
|
504
|
-
insert._ts = Base.timestamp();
|
|
505
|
+
insert._ts = base_js_1.Base.timestamp();
|
|
505
506
|
}
|
|
506
507
|
await this._processHashedKeys(insert);
|
|
507
508
|
let seqKeys = this._findSequenceKeys(insert);
|
|
@@ -513,18 +514,20 @@ export default class Mongo extends Db {
|
|
|
513
514
|
await this._publishAndAudit('insert', this.db, collection, fullObj);
|
|
514
515
|
return fullObj;
|
|
515
516
|
}, !!seqKeys, { operation: "insert", collection, insert });
|
|
516
|
-
log.debug('insert returns', ret);
|
|
517
|
+
db_js_1.log.debug('insert returns', ret);
|
|
517
518
|
return this._processReturnedObject(await ret);
|
|
518
519
|
}
|
|
519
520
|
async upsertBatch(collection, batch) {
|
|
521
|
+
var _a;
|
|
520
522
|
assert(collection, "collection can't be null");
|
|
521
523
|
assert(batch, "batch can't be null");
|
|
522
524
|
assert(batch instanceof Array, "batch must be an Array");
|
|
523
|
-
log.debug('upsertBatch called', collection, batch);
|
|
525
|
+
db_js_1.log.debug('upsertBatch called', collection, batch);
|
|
524
526
|
batch = this.replaceIds(batch);
|
|
525
527
|
for (let i = 0; i < batch.length; i++)
|
|
526
|
-
await this._processHashedKeys(batch[i]
|
|
528
|
+
await this._processHashedKeys((_a = batch[i]) === null || _a === void 0 ? void 0 : _a.update);
|
|
527
529
|
let ret = await this.executeTransactionally(collection, async (conn, client) => {
|
|
530
|
+
var _a, _b;
|
|
528
531
|
await this._processSequenceFieldForMany(client, collection, batch.map(b => b.update));
|
|
529
532
|
let publications = [];
|
|
530
533
|
let changes = [];
|
|
@@ -538,9 +541,9 @@ export default class Mongo extends Db {
|
|
|
538
541
|
};
|
|
539
542
|
update = await this._processUpdateObject(update);
|
|
540
543
|
let ret = (await conn.findOneAndUpdate(query, update, options));
|
|
541
|
-
if (ret
|
|
542
|
-
let oper = ret.lastErrorObject
|
|
543
|
-
let retObj = oper === "insert" ? ret.value : this._removeUnchanged(ret.value, update, !!opts
|
|
544
|
+
if ((_a = ret === null || ret === void 0 ? void 0 : ret.value) === null || _a === void 0 ? void 0 : _a._id) {
|
|
545
|
+
let oper = ((_b = ret.lastErrorObject) === null || _b === void 0 ? void 0 : _b.updatedExisting) ? "update" : "insert";
|
|
546
|
+
let retObj = oper === "insert" ? ret.value : this._removeUnchanged(ret.value, update, !!(opts === null || opts === void 0 ? void 0 : opts.returnFullObject));
|
|
544
547
|
publications.push(await this._publishAndAudit(oper, this.db, collection, retObj, true));
|
|
545
548
|
changes.push(retObj);
|
|
546
549
|
}
|
|
@@ -553,25 +556,25 @@ export default class Mongo extends Db {
|
|
|
553
556
|
operation: "batch",
|
|
554
557
|
db: this.db,
|
|
555
558
|
collection,
|
|
556
|
-
data: publications.map(p => p
|
|
559
|
+
data: publications.map(p => p === null || p === void 0 ? void 0 : p.payload).filter(p => !!p)
|
|
557
560
|
}
|
|
558
561
|
});
|
|
559
562
|
}
|
|
560
563
|
return changes;
|
|
561
564
|
}, false, { operation: "upsertBatch", collection, batch });
|
|
562
|
-
log.debug('upsertBatch returns', ret);
|
|
565
|
+
db_js_1.log.debug('upsertBatch returns', ret);
|
|
563
566
|
return ret;
|
|
564
567
|
}
|
|
565
568
|
async insertMany(collection, insert) {
|
|
566
569
|
assert(collection, "collection can't be null");
|
|
567
570
|
assert(insert, "insert can't be null");
|
|
568
571
|
assert(insert instanceof Array, "insert must be an Array");
|
|
569
|
-
log.debug('insertMany called', collection, insert);
|
|
572
|
+
db_js_1.log.debug('insertMany called', collection, insert);
|
|
570
573
|
insert = this.replaceIds(insert);
|
|
571
574
|
for (let i = 0; i < insert.length; i++)
|
|
572
575
|
await this._processHashedKeys(insert[i]);
|
|
573
576
|
if (this.revisions)
|
|
574
|
-
insert.forEach(ins => { ins._rev = 1; ins._ts = Base.timestamp(); });
|
|
577
|
+
insert.forEach(ins => { ins._rev = 1; ins._ts = base_js_1.Base.timestamp(); });
|
|
575
578
|
let ret = await this.executeTransactionally(collection, async (conn, client) => {
|
|
576
579
|
await this._processSequenceFieldForMany(client, collection, insert);
|
|
577
580
|
let obj = await conn.insertMany(insert, this._sessionOpt());
|
|
@@ -591,7 +594,7 @@ export default class Mongo extends Db {
|
|
|
591
594
|
}
|
|
592
595
|
return ret;
|
|
593
596
|
}, false, { operation: "insertMany", collection, insert });
|
|
594
|
-
log.debug('insertMany returns', ret);
|
|
597
|
+
db_js_1.log.debug('insertMany returns', ret);
|
|
595
598
|
return ret;
|
|
596
599
|
}
|
|
597
600
|
async deleteOne(collection, query) {
|
|
@@ -603,14 +606,14 @@ export default class Mongo extends Db {
|
|
|
603
606
|
returnDocument: "after",
|
|
604
607
|
...this._sessionOpt()
|
|
605
608
|
};
|
|
606
|
-
log.debug('deleteOne called', collection, query);
|
|
609
|
+
db_js_1.log.debug('deleteOne called', collection, query);
|
|
607
610
|
let ret = await this.executeTransactionally(collection, async (conn) => {
|
|
608
611
|
let obj = await conn.findOneAndDelete(query, opts);
|
|
609
612
|
if (obj.value)
|
|
610
613
|
await this._publishAndAudit('delete', this.db, collection, obj.value);
|
|
611
614
|
return obj.value;
|
|
612
615
|
}, false, { operation: "deleteOne", collection, query, softdelete: this.softdelete });
|
|
613
|
-
log.debug('deleteOne returns', ret);
|
|
616
|
+
db_js_1.log.debug('deleteOne returns', ret);
|
|
614
617
|
return ret;
|
|
615
618
|
}
|
|
616
619
|
else {
|
|
@@ -619,7 +622,7 @@ export default class Mongo extends Db {
|
|
|
619
622
|
returnDocument: "after",
|
|
620
623
|
...this._sessionOpt()
|
|
621
624
|
};
|
|
622
|
-
log.debug('deleteOne called', collection, query);
|
|
625
|
+
db_js_1.log.debug('deleteOne called', collection, query);
|
|
623
626
|
let ret = await this.executeTransactionally(collection, async (conn, client) => {
|
|
624
627
|
let del = {
|
|
625
628
|
$set: { _deleted: new Date() },
|
|
@@ -633,7 +636,7 @@ export default class Mongo extends Db {
|
|
|
633
636
|
await this._publishAndAudit('delete', this.db, collection, obj.value);
|
|
634
637
|
return obj.value;
|
|
635
638
|
}, false, { operation: "deleteOne", collection, query, softdelete: this.softdelete });
|
|
636
|
-
log.debug('deleteOne returns', ret);
|
|
639
|
+
db_js_1.log.debug('deleteOne returns', ret);
|
|
637
640
|
return ret;
|
|
638
641
|
}
|
|
639
642
|
}
|
|
@@ -644,7 +647,7 @@ export default class Mongo extends Db {
|
|
|
644
647
|
...this._sessionOpt()
|
|
645
648
|
};
|
|
646
649
|
query = this.replaceIds(query);
|
|
647
|
-
log.debug('blockOne called', collection, query);
|
|
650
|
+
db_js_1.log.debug('blockOne called', collection, query);
|
|
648
651
|
let ret = await this.executeTransactionally(collection, async (conn, client) => {
|
|
649
652
|
query._blocked = { $exists: 0 };
|
|
650
653
|
let update = {
|
|
@@ -666,7 +669,7 @@ export default class Mongo extends Db {
|
|
|
666
669
|
await this._publishAndAudit('block', this.db, collection, retObj);
|
|
667
670
|
return retObj;
|
|
668
671
|
}, false, { operation: "blockOne", collection, query });
|
|
669
|
-
log.debug('blockOne returns', ret);
|
|
672
|
+
db_js_1.log.debug('blockOne returns', ret);
|
|
670
673
|
return ret;
|
|
671
674
|
}
|
|
672
675
|
async unblockOne(collection, query) {
|
|
@@ -676,7 +679,7 @@ export default class Mongo extends Db {
|
|
|
676
679
|
...this._sessionOpt()
|
|
677
680
|
};
|
|
678
681
|
query = this.replaceIds(query);
|
|
679
|
-
log.debug('unblockOne called', collection, query);
|
|
682
|
+
db_js_1.log.debug('unblockOne called', collection, query);
|
|
680
683
|
let ret = await this.executeTransactionally(collection, async (conn, client) => {
|
|
681
684
|
query._blocked = { $exists: 1 };
|
|
682
685
|
let update = {
|
|
@@ -697,7 +700,7 @@ export default class Mongo extends Db {
|
|
|
697
700
|
await this._publishAndAudit('unblock', this.db, collection, retObj);
|
|
698
701
|
return retObj;
|
|
699
702
|
}, false, { operation: "unblockOne", collection, query });
|
|
700
|
-
log.debug('unblockOne returns', ret);
|
|
703
|
+
db_js_1.log.debug('unblockOne returns', ret);
|
|
701
704
|
return ret;
|
|
702
705
|
}
|
|
703
706
|
async hardDeleteOne(collection, query) {
|
|
@@ -708,7 +711,7 @@ export default class Mongo extends Db {
|
|
|
708
711
|
returnDocument: "after",
|
|
709
712
|
...this._sessionOpt()
|
|
710
713
|
};
|
|
711
|
-
log.debug('hardDeleteOne called', collection, query);
|
|
714
|
+
db_js_1.log.debug('hardDeleteOne called', collection, query);
|
|
712
715
|
let ret = await this.executeTransactionally(collection, async (conn) => {
|
|
713
716
|
let obj = await conn.findOneAndDelete(query, opts);
|
|
714
717
|
if (obj.value) {
|
|
@@ -716,21 +719,21 @@ export default class Mongo extends Db {
|
|
|
716
719
|
}
|
|
717
720
|
return obj.value;
|
|
718
721
|
}, false, { operation: "hardDeleteOne", collection, query });
|
|
719
|
-
log.debug('hardDeleteOne returns', ret);
|
|
722
|
+
db_js_1.log.debug('hardDeleteOne returns', ret);
|
|
720
723
|
return ret;
|
|
721
724
|
}
|
|
722
725
|
async delete(collection, query) {
|
|
723
726
|
assert(collection);
|
|
724
727
|
assert(query);
|
|
725
728
|
if (this.syncSupport)
|
|
726
|
-
log.warn("delete does not increase _csq, avoit it.");
|
|
729
|
+
db_js_1.log.warn("delete does not increase _csq, avoit it.");
|
|
727
730
|
query = this.replaceIds(query);
|
|
728
731
|
if (!this.softdelete) {
|
|
729
732
|
let opts = {
|
|
730
733
|
returnDocument: "after",
|
|
731
734
|
...this._sessionOpt()
|
|
732
735
|
};
|
|
733
|
-
log.debug('delete called', collection, query);
|
|
736
|
+
db_js_1.log.debug('delete called', collection, query);
|
|
734
737
|
let ret = await this.executeTransactionally(collection, async (conn) => {
|
|
735
738
|
let obj = await conn.deleteMany(query, opts);
|
|
736
739
|
let resObj = {
|
|
@@ -740,7 +743,7 @@ export default class Mongo extends Db {
|
|
|
740
743
|
await this._publishAndAudit('deleteMany', this.db, collection, resObj);
|
|
741
744
|
return resObj;
|
|
742
745
|
}, false, { operation: "delete", collection, query, softdelete: this.softdelete });
|
|
743
|
-
log.debug('delete returns', ret);
|
|
746
|
+
db_js_1.log.debug('delete returns', ret);
|
|
744
747
|
return ret;
|
|
745
748
|
}
|
|
746
749
|
else {
|
|
@@ -750,7 +753,7 @@ export default class Mongo extends Db {
|
|
|
750
753
|
...this._sessionOpt()
|
|
751
754
|
};
|
|
752
755
|
let date = new Date();
|
|
753
|
-
log.debug('delete called', collection, query);
|
|
756
|
+
db_js_1.log.debug('delete called', collection, query);
|
|
754
757
|
let ret = await this.executeTransactionally(collection, async (conn) => {
|
|
755
758
|
let obj = await conn.updateMany(query, { $set: { _deleted: date } }, opts);
|
|
756
759
|
let resObj = {
|
|
@@ -760,7 +763,7 @@ export default class Mongo extends Db {
|
|
|
760
763
|
await this._publishAndAudit('deleteMany', this.db, collection, resObj);
|
|
761
764
|
return resObj;
|
|
762
765
|
}, false, { operation: "delete", collection, query, softdelete: this.softdelete });
|
|
763
|
-
log.debug('delete returns', ret);
|
|
766
|
+
db_js_1.log.debug('delete returns', ret);
|
|
764
767
|
return ret;
|
|
765
768
|
}
|
|
766
769
|
}
|
|
@@ -768,13 +771,13 @@ export default class Mongo extends Db {
|
|
|
768
771
|
assert(collection);
|
|
769
772
|
assert(query);
|
|
770
773
|
if (this.syncSupport)
|
|
771
|
-
log.warn("hardDelete does not increase _csq, avoit it.");
|
|
774
|
+
db_js_1.log.warn("hardDelete does not increase _csq, avoit it.");
|
|
772
775
|
query = this.replaceIds(query);
|
|
773
776
|
let opts = {
|
|
774
777
|
returnDocument: "after",
|
|
775
778
|
...this._sessionOpt()
|
|
776
779
|
};
|
|
777
|
-
log.debug('hardDelete called', collection, query);
|
|
780
|
+
db_js_1.log.debug('hardDelete called', collection, query);
|
|
778
781
|
let ret = await this.executeTransactionally(collection, async (conn) => {
|
|
779
782
|
let obj = await conn.deleteMany(query, opts);
|
|
780
783
|
let resObj = {
|
|
@@ -784,12 +787,12 @@ export default class Mongo extends Db {
|
|
|
784
787
|
await this._publishAndAudit('deleteMany', this.db, collection, resObj);
|
|
785
788
|
return resObj;
|
|
786
789
|
}, false, { operation: "hardDelete", collection, query, softdelete: this.softdelete });
|
|
787
|
-
log.debug('hardDelete returns', ret);
|
|
790
|
+
db_js_1.log.debug('hardDelete returns', ret);
|
|
788
791
|
return ret;
|
|
789
792
|
}
|
|
790
793
|
async testHash(collection, query, field, unhashedValue) {
|
|
791
794
|
let _field;
|
|
792
|
-
log.debug('teshHash called', collection, query, field, unhashedValue);
|
|
795
|
+
db_js_1.log.debug('teshHash called', collection, query, field, unhashedValue);
|
|
793
796
|
if (typeof field === "object") {
|
|
794
797
|
if (Object.keys(field).length === 1)
|
|
795
798
|
[_field, unhashedValue] = Object.entries(field)[0];
|
|
@@ -803,19 +806,19 @@ export default class Mongo extends Db {
|
|
|
803
806
|
let conn = await this.connect();
|
|
804
807
|
let obj = await conn.db(this.db).collection(collection).findOne(query, { projection: { [_field]: 1 }, ...this._sessionOpt() });
|
|
805
808
|
if (!obj || !obj[_field]) {
|
|
806
|
-
log.debug('teshHash returns false', obj);
|
|
809
|
+
db_js_1.log.debug('teshHash returns false', obj);
|
|
807
810
|
return false;
|
|
808
811
|
}
|
|
809
|
-
let res = await
|
|
810
|
-
log.debug('teshHash returns', res);
|
|
812
|
+
let res = await bcrypt_1.default.compare(unhashedValue, obj[_field].hash);
|
|
813
|
+
db_js_1.log.debug('teshHash returns', res);
|
|
811
814
|
return res;
|
|
812
815
|
}
|
|
813
816
|
async aggregate(collection, pipeline, opts = {
|
|
814
|
-
readPreference: ReadPreference.SECONDARY_PREFERRED,
|
|
817
|
+
readPreference: mongodb_1.ReadPreference.SECONDARY_PREFERRED,
|
|
815
818
|
}) {
|
|
816
819
|
assert(collection);
|
|
817
820
|
assert(pipeline instanceof Array);
|
|
818
|
-
log.debug('aggregate called', collection, pipeline);
|
|
821
|
+
db_js_1.log.debug('aggregate called', collection, pipeline);
|
|
819
822
|
pipeline = this.replaceIds(pipeline);
|
|
820
823
|
if (this.session)
|
|
821
824
|
opts.session = this.session;
|
|
@@ -823,15 +826,15 @@ export default class Mongo extends Db {
|
|
|
823
826
|
let res = await conn.aggregate(pipeline, opts).toArray();
|
|
824
827
|
return res;
|
|
825
828
|
}, false, { operation: "aggregate", collection, pipeline, opts });
|
|
826
|
-
log.debug('aggregare returns', ret);
|
|
829
|
+
db_js_1.log.debug('aggregare returns', ret);
|
|
827
830
|
return ret;
|
|
828
831
|
}
|
|
829
832
|
async isUnique(collection, field, value, id) {
|
|
830
833
|
assert(collection);
|
|
831
834
|
assert(field);
|
|
832
835
|
assert(value);
|
|
833
|
-
log.debug('isUnuqie called', collection, field, value, id);
|
|
834
|
-
let _id = id
|
|
836
|
+
db_js_1.log.debug('isUnuqie called', collection, field, value, id);
|
|
837
|
+
let _id = id === null || id === void 0 ? void 0 : id.toString();
|
|
835
838
|
let matches = await this.executeTransactionally(collection, async (conn) => {
|
|
836
839
|
let agg = await conn.find({ [field]: value });
|
|
837
840
|
let res = await agg.toArray();
|
|
@@ -844,13 +847,13 @@ export default class Mongo extends Db {
|
|
|
844
847
|
return false;
|
|
845
848
|
}
|
|
846
849
|
}
|
|
847
|
-
log.debug('isUnuqie returns', ret);
|
|
850
|
+
db_js_1.log.debug('isUnuqie returns', ret);
|
|
848
851
|
return ret;
|
|
849
852
|
}
|
|
850
853
|
async collectFieldValues(collection, field, inArray = false, opts) {
|
|
851
854
|
assert(collection);
|
|
852
855
|
assert(field);
|
|
853
|
-
log.debug('collectFieldValues called', collection, field);
|
|
856
|
+
db_js_1.log.debug('collectFieldValues called', collection, field);
|
|
854
857
|
let pipeline = [
|
|
855
858
|
{ $group: { _id: '$' + field } },
|
|
856
859
|
{ $sort: { _id: 1 } }
|
|
@@ -865,33 +868,33 @@ export default class Mongo extends Db {
|
|
|
865
868
|
let res = await agg.toArray();
|
|
866
869
|
return res;
|
|
867
870
|
}, false, { operation: "collectFieldValues", collection, field, inArray, pipeline, opts });
|
|
868
|
-
let ret = res
|
|
869
|
-
log.debug('collectFieldValues returns', ret);
|
|
871
|
+
let ret = res === null || res === void 0 ? void 0 : res.map((v) => v._id);
|
|
872
|
+
db_js_1.log.debug('collectFieldValues returns', ret);
|
|
870
873
|
return ret;
|
|
871
874
|
}
|
|
872
875
|
async dropCollection(collection) {
|
|
873
876
|
assert(collection);
|
|
874
|
-
log.debug('dropCollection called', this.auditCollections);
|
|
877
|
+
db_js_1.log.debug('dropCollection called', this.auditCollections);
|
|
875
878
|
let client = await this.connect();
|
|
876
879
|
let existing = await client.db(this.db).collections();
|
|
877
880
|
if (existing.map((c) => c.s.name).includes(collection)) {
|
|
878
881
|
await client.db(this.db).dropCollection(collection);
|
|
879
882
|
}
|
|
880
|
-
log.debug('dropCollection returns');
|
|
883
|
+
db_js_1.log.debug('dropCollection returns');
|
|
881
884
|
}
|
|
882
885
|
async resetCollectionSync(collection) {
|
|
883
886
|
assert(collection);
|
|
884
|
-
log.debug('resetCollectionSync called for', collection);
|
|
887
|
+
db_js_1.log.debug('resetCollectionSync called for', collection);
|
|
885
888
|
let client = await this.connect();
|
|
886
889
|
await client.db(this.db)
|
|
887
|
-
.collection(SEQUENCES_COLLECTION)
|
|
890
|
+
.collection(types_js_1.SEQUENCES_COLLECTION)
|
|
888
891
|
.findOneAndDelete({ collection });
|
|
889
|
-
log.debug(`resetCollectionSync for ${collection} returns`);
|
|
892
|
+
db_js_1.log.debug(`resetCollectionSync for ${collection} returns`);
|
|
890
893
|
}
|
|
891
894
|
async dropCollections(collections) {
|
|
892
895
|
assert(collections);
|
|
893
896
|
assert(collections instanceof Array);
|
|
894
|
-
log.debug('dropCollections called', this.auditCollections);
|
|
897
|
+
db_js_1.log.debug('dropCollections called', this.auditCollections);
|
|
895
898
|
let client = await this.connect();
|
|
896
899
|
let existing = await client.db(this.db).collections();
|
|
897
900
|
for await (let collection of collections) {
|
|
@@ -899,12 +902,12 @@ export default class Mongo extends Db {
|
|
|
899
902
|
await client.db(this.db).dropCollection(collection);
|
|
900
903
|
}
|
|
901
904
|
}
|
|
902
|
-
log.debug('dropCollections returns');
|
|
905
|
+
db_js_1.log.debug('dropCollections returns');
|
|
903
906
|
}
|
|
904
907
|
async createCollections(collections) {
|
|
905
908
|
assert(collections);
|
|
906
909
|
assert(collections instanceof Array);
|
|
907
|
-
log.debug('createCollections called', this.auditCollections);
|
|
910
|
+
db_js_1.log.debug('createCollections called', this.auditCollections);
|
|
908
911
|
let client = await this.connect();
|
|
909
912
|
let existing = await this.getCollections();
|
|
910
913
|
for await (let collection of collections) {
|
|
@@ -912,21 +915,21 @@ export default class Mongo extends Db {
|
|
|
912
915
|
await client.db(this.db).createCollection(collection);
|
|
913
916
|
}
|
|
914
917
|
}
|
|
915
|
-
log.debug('createCollections returns');
|
|
918
|
+
db_js_1.log.debug('createCollections returns');
|
|
916
919
|
}
|
|
917
920
|
async createCollection(collection) {
|
|
918
921
|
assert(collection);
|
|
919
|
-
log.debug('createCollection called', collection);
|
|
922
|
+
db_js_1.log.debug('createCollection called', collection);
|
|
920
923
|
let client = await this.connect();
|
|
921
924
|
let existing = await this.getCollections();
|
|
922
925
|
if (!existing.includes(collection)) {
|
|
923
926
|
await client.db(this.db).createCollection(collection);
|
|
924
927
|
}
|
|
925
|
-
log.debug('createCollection returns');
|
|
928
|
+
db_js_1.log.debug('createCollection returns');
|
|
926
929
|
}
|
|
927
930
|
async dbLogPurge(collection, _id) {
|
|
928
931
|
assert(collection);
|
|
929
|
-
log.debug('dblogPurge called', collection, _id);
|
|
932
|
+
db_js_1.log.debug('dblogPurge called', collection, _id);
|
|
930
933
|
let ret = await this.executeTransactionally(collection, async () => {
|
|
931
934
|
let cond = { db: this.db, collection, };
|
|
932
935
|
if (_id !== undefined)
|
|
@@ -941,12 +944,12 @@ export default class Mongo extends Db {
|
|
|
941
944
|
n: ret.deletedCount
|
|
942
945
|
};
|
|
943
946
|
}, false, { operation: "dbLogPurge", collection, _id });
|
|
944
|
-
log.debug('dblogPurge returns', ret);
|
|
947
|
+
db_js_1.log.debug('dblogPurge returns', ret);
|
|
945
948
|
return ret;
|
|
946
949
|
}
|
|
947
950
|
async dbLogGet(collection, _id) {
|
|
948
951
|
assert(collection);
|
|
949
|
-
log.debug('dblogGet called', collection, _id);
|
|
952
|
+
db_js_1.log.debug('dblogGet called', collection, _id);
|
|
950
953
|
let ret = await this.executeTransactionally(collection, async () => {
|
|
951
954
|
let cond = { db: this.db, collection };
|
|
952
955
|
if (_id)
|
|
@@ -960,7 +963,7 @@ export default class Mongo extends Db {
|
|
|
960
963
|
.toArray();
|
|
961
964
|
return ret;
|
|
962
965
|
}, false, { operation: "dbLogGet", collection, _id });
|
|
963
|
-
log.debug('dblogGet returns', ret);
|
|
966
|
+
db_js_1.log.debug('dblogGet returns', ret);
|
|
964
967
|
return ret;
|
|
965
968
|
}
|
|
966
969
|
// HELPER FUNCTIONS
|
|
@@ -978,9 +981,9 @@ export default class Mongo extends Db {
|
|
|
978
981
|
return undefined;
|
|
979
982
|
if (typeof data === "symbol")
|
|
980
983
|
return data.toString();
|
|
981
|
-
if (data instanceof ObjectId)
|
|
984
|
+
if (data instanceof mongodb_1.ObjectId)
|
|
982
985
|
return data;
|
|
983
|
-
if (data instanceof Timestamp)
|
|
986
|
+
if (data instanceof mongodb_1.Timestamp)
|
|
984
987
|
return data;
|
|
985
988
|
if (data instanceof Date)
|
|
986
989
|
return data;
|
|
@@ -990,17 +993,17 @@ export default class Mongo extends Db {
|
|
|
990
993
|
return data;
|
|
991
994
|
if (data instanceof String)
|
|
992
995
|
return data;
|
|
993
|
-
if (typeof data === "string" && data
|
|
994
|
-
return new ObjectId(data);
|
|
996
|
+
if (typeof data === "string" && (data === null || data === void 0 ? void 0 : data.match(/^[0-9a-f]{24,24}$/g)))
|
|
997
|
+
return new mongodb_1.ObjectId(data);
|
|
995
998
|
if (typeof data === "string")
|
|
996
999
|
return data;
|
|
997
1000
|
if (data instanceof Array) {
|
|
998
1001
|
return data.map(d => this.replaceIds(d));
|
|
999
1002
|
}
|
|
1000
|
-
if (typeof data == 'object' && data
|
|
1001
|
-
return Base.timestamp(data);
|
|
1002
|
-
if (typeof data == 'object' && data
|
|
1003
|
-
return Base.timestamp(data);
|
|
1003
|
+
if (typeof data == 'object' && (data === null || data === void 0 ? void 0 : data.t) && (data === null || data === void 0 ? void 0 : data.i) !== undefined)
|
|
1004
|
+
return base_js_1.Base.timestamp(data);
|
|
1005
|
+
if (typeof data == 'object' && (data === null || data === void 0 ? void 0 : data.high) && (data === null || data === void 0 ? void 0 : data.low) !== undefined)
|
|
1006
|
+
return base_js_1.Base.timestamp(data);
|
|
1004
1007
|
if (typeof data == 'object') {
|
|
1005
1008
|
for (let key in data) {
|
|
1006
1009
|
data[key] = this.replaceIds(data[key]);
|
|
@@ -1023,14 +1026,14 @@ export default class Mongo extends Db {
|
|
|
1023
1026
|
if (this.session)
|
|
1024
1027
|
try {
|
|
1025
1028
|
await this.session.endSession();
|
|
1026
|
-
log.info("session ended");
|
|
1029
|
+
db_js_1.log.info("session ended");
|
|
1027
1030
|
}
|
|
1028
1031
|
catch (err) {
|
|
1029
|
-
log.error(`Error ending session ${err.message}`);
|
|
1032
|
+
db_js_1.log.error(`Error ending session ${err.message}`);
|
|
1030
1033
|
}
|
|
1031
1034
|
try {
|
|
1032
1035
|
await super.close();
|
|
1033
|
-
log.info("connection closed");
|
|
1036
|
+
db_js_1.log.info("connection closed");
|
|
1034
1037
|
}
|
|
1035
1038
|
catch { /** intentionally */ }
|
|
1036
1039
|
this.session = undefined;
|
|
@@ -1050,13 +1053,13 @@ export default class Mongo extends Db {
|
|
|
1050
1053
|
let hadSession = !!this.session;
|
|
1051
1054
|
if (!this.session) {
|
|
1052
1055
|
this.session = client.startSession();
|
|
1053
|
-
log.info("session started");
|
|
1056
|
+
db_js_1.log.info("session started");
|
|
1054
1057
|
}
|
|
1055
1058
|
let session = this.session;
|
|
1056
1059
|
await session.withTransaction(async () => await funct(client, session));
|
|
1057
1060
|
if (!hadSession) {
|
|
1058
1061
|
session.endSession();
|
|
1059
|
-
log.info("session ended");
|
|
1062
|
+
db_js_1.log.info("session ended");
|
|
1060
1063
|
this.session = undefined;
|
|
1061
1064
|
}
|
|
1062
1065
|
return;
|
|
@@ -1072,24 +1075,24 @@ export default class Mongo extends Db {
|
|
|
1072
1075
|
try {
|
|
1073
1076
|
if (!this.session) {
|
|
1074
1077
|
this.session = client.startSession(TRANSACTION_OPTIONS);
|
|
1075
|
-
log.info("session started");
|
|
1078
|
+
db_js_1.log.info("session started");
|
|
1076
1079
|
}
|
|
1077
1080
|
if (!await this.inTransaction()) {
|
|
1078
1081
|
await this.session.startTransaction();
|
|
1079
|
-
log.info("transaction started");
|
|
1082
|
+
db_js_1.log.info("transaction started");
|
|
1080
1083
|
}
|
|
1081
1084
|
}
|
|
1082
1085
|
catch (err) {
|
|
1083
|
-
log.error('startTransaction error', err);
|
|
1086
|
+
db_js_1.log.error('startTransaction error', err);
|
|
1084
1087
|
try {
|
|
1085
1088
|
if (this.session) {
|
|
1086
1089
|
await this.session.endSession();
|
|
1087
|
-
log.info("session ended");
|
|
1090
|
+
db_js_1.log.info("session ended");
|
|
1088
1091
|
}
|
|
1089
1092
|
this.session = undefined;
|
|
1090
1093
|
}
|
|
1091
1094
|
catch (e) {
|
|
1092
|
-
log.error("startTransaction - error in endSession", e.message || e);
|
|
1095
|
+
db_js_1.log.error("startTransaction - error in endSession", e.message || e);
|
|
1093
1096
|
}
|
|
1094
1097
|
return;
|
|
1095
1098
|
}
|
|
@@ -1102,13 +1105,13 @@ export default class Mongo extends Db {
|
|
|
1102
1105
|
return;
|
|
1103
1106
|
let session = this.session;
|
|
1104
1107
|
await session.commitTransaction();
|
|
1105
|
-
log.info("transaction committed");
|
|
1108
|
+
db_js_1.log.info("transaction committed");
|
|
1106
1109
|
session.endSession();
|
|
1107
1110
|
this.session = undefined;
|
|
1108
|
-
log.info("session ended");
|
|
1111
|
+
db_js_1.log.info("session ended");
|
|
1109
1112
|
}
|
|
1110
1113
|
catch (err) {
|
|
1111
|
-
log.error(`commitTransaction error ${err.message || err}`);
|
|
1114
|
+
db_js_1.log.error(`commitTransaction error ${err.message || err}`);
|
|
1112
1115
|
}
|
|
1113
1116
|
}
|
|
1114
1117
|
async abortTransaction() {
|
|
@@ -1119,13 +1122,13 @@ export default class Mongo extends Db {
|
|
|
1119
1122
|
return;
|
|
1120
1123
|
let session = this.session;
|
|
1121
1124
|
await session.abortTransaction();
|
|
1122
|
-
log.info("transaction aborted");
|
|
1125
|
+
db_js_1.log.info("transaction aborted");
|
|
1123
1126
|
await session.endSession();
|
|
1124
1127
|
this.session = undefined;
|
|
1125
|
-
log.info("session ended");
|
|
1128
|
+
db_js_1.log.info("session ended");
|
|
1126
1129
|
}
|
|
1127
1130
|
catch (err) {
|
|
1128
|
-
log.error(`abortTransaction error ${err.message || err}`);
|
|
1131
|
+
db_js_1.log.error(`abortTransaction error ${err.message || err}`);
|
|
1129
1132
|
}
|
|
1130
1133
|
}
|
|
1131
1134
|
async _try_once(useTransaction, f, collection) {
|
|
@@ -1162,9 +1165,9 @@ export default class Mongo extends Db {
|
|
|
1162
1165
|
return await this._try_once(useTransaction, f, collection);
|
|
1163
1166
|
}
|
|
1164
1167
|
catch (err) {
|
|
1165
|
-
log.error(`Mongo command has failed for ${this.db}.${collection} - ${(this.session ? "ROLLBACK - " : "")} ${err.message || err}`);
|
|
1166
|
-
log.error(debugObject);
|
|
1167
|
-
log.debug(err);
|
|
1168
|
+
db_js_1.log.error(`Mongo command has failed for ${this.db}.${collection} - ${(this.session ? "ROLLBACK - " : "")} ${err.message || err}`);
|
|
1169
|
+
db_js_1.log.error(debugObject);
|
|
1170
|
+
db_js_1.log.debug(err);
|
|
1168
1171
|
let x = (err || "").toString();
|
|
1169
1172
|
console.log('x');
|
|
1170
1173
|
let isRepeatable = x.match(/Topology is closed, please connect/i)
|
|
@@ -1173,27 +1176,28 @@ export default class Mongo extends Db {
|
|
|
1173
1176
|
|| x.match(/Topology closed/);
|
|
1174
1177
|
if (isRepeatable) {
|
|
1175
1178
|
try {
|
|
1176
|
-
log.error("Trying to reopen connection and repeat as");
|
|
1179
|
+
db_js_1.log.error("Trying to reopen connection and repeat as");
|
|
1177
1180
|
await this.close();
|
|
1178
1181
|
// a single retry
|
|
1179
1182
|
await super.connect();
|
|
1180
1183
|
let ret = await this._try_once(useTransaction, f, collection);
|
|
1181
|
-
log.error("OK - Retry succeeded.");
|
|
1182
|
-
log.error("");
|
|
1184
|
+
db_js_1.log.error("OK - Retry succeeded.");
|
|
1185
|
+
db_js_1.log.error("");
|
|
1183
1186
|
return ret;
|
|
1184
1187
|
}
|
|
1185
1188
|
catch (err2) {
|
|
1186
1189
|
/* intentional */
|
|
1187
1190
|
if (debugObject)
|
|
1188
|
-
log.error(debugObject);
|
|
1189
|
-
log.error(`FAIL - Retry failed: ${err2.message || err2}`);
|
|
1190
|
-
log.error("");
|
|
1191
|
+
db_js_1.log.error(debugObject);
|
|
1192
|
+
db_js_1.log.error(`FAIL - Retry failed: ${err2.message || err2}`);
|
|
1193
|
+
db_js_1.log.error("");
|
|
1191
1194
|
}
|
|
1192
1195
|
}
|
|
1193
1196
|
throw err;
|
|
1194
1197
|
}
|
|
1195
1198
|
}
|
|
1196
1199
|
async _findLastSequenceForKey(connection, key) {
|
|
1200
|
+
var _a;
|
|
1197
1201
|
let maxfld = await (connection
|
|
1198
1202
|
.find({}, this._sessionOpt())
|
|
1199
1203
|
.sort({ [key]: -1 })
|
|
@@ -1201,34 +1205,35 @@ export default class Mongo extends Db {
|
|
|
1201
1205
|
.toArray());
|
|
1202
1206
|
if (maxfld.length === 0)
|
|
1203
1207
|
return undefined;
|
|
1204
|
-
return parseInt(maxfld
|
|
1208
|
+
return parseInt((_a = maxfld === null || maxfld === void 0 ? void 0 : maxfld[0]) === null || _a === void 0 ? void 0 : _a[key]) || 0;
|
|
1205
1209
|
}
|
|
1206
1210
|
async _getNextCollectionUpdateSeqNo(collection, conn) {
|
|
1211
|
+
var _a;
|
|
1207
1212
|
let opts = {
|
|
1208
1213
|
upsert: true,
|
|
1209
1214
|
returnDocument: "after",
|
|
1210
1215
|
};
|
|
1211
1216
|
let nextSeq = await (conn.db(this.db)
|
|
1212
|
-
.collection(SEQUENCES_COLLECTION)
|
|
1217
|
+
.collection(types_js_1.SEQUENCES_COLLECTION)
|
|
1213
1218
|
.findOneAndUpdate({ collection }, {
|
|
1214
1219
|
$inc: { seq: 1 },
|
|
1215
1220
|
$currentDate: { last: { $type: "date" }, ts: { $type: "timestamp" } }
|
|
1216
1221
|
}, opts));
|
|
1217
1222
|
conn.db(this.db).collection(collection);
|
|
1218
|
-
return nextSeq
|
|
1223
|
+
return ((_a = nextSeq === null || nextSeq === void 0 ? void 0 : nextSeq.value) === null || _a === void 0 ? void 0 : _a.seq) || 1;
|
|
1219
1224
|
}
|
|
1220
1225
|
_findSequenceKeys(object) {
|
|
1221
1226
|
if (!object)
|
|
1222
1227
|
return;
|
|
1223
1228
|
let seqKeys = Object.keys(object).filter(key => object[key] === 'SEQ_NEXT' || object[key] === 'SEQ_LAST');
|
|
1224
|
-
return (seqKeys
|
|
1229
|
+
return ((seqKeys === null || seqKeys === void 0 ? void 0 : seqKeys.length) > 0 || this.syncSupport) ? { seqKeys } : undefined;
|
|
1225
1230
|
}
|
|
1226
1231
|
async _processSequenceField(client, collection, insert, seqKeys) {
|
|
1227
1232
|
assert(this.client);
|
|
1228
1233
|
if (this.syncSupport) {
|
|
1229
1234
|
insert._csq = (await this._getNextCollectionUpdateSeqNo(collection, client));
|
|
1230
1235
|
}
|
|
1231
|
-
for await (let seqKey of seqKeys
|
|
1236
|
+
for await (let seqKey of (seqKeys === null || seqKeys === void 0 ? void 0 : seqKeys.seqKeys) || []) {
|
|
1232
1237
|
let last = await this._findLastSequenceForKey(client.db(this.db).collection(collection), seqKey);
|
|
1233
1238
|
if (last === undefined) {
|
|
1234
1239
|
await this.createCollection(collection);
|
|
@@ -1242,13 +1247,13 @@ export default class Mongo extends Db {
|
|
|
1242
1247
|
async _processSequenceFieldForMany(connection, collection, inserts) {
|
|
1243
1248
|
assert(this.client);
|
|
1244
1249
|
assert(connection);
|
|
1245
|
-
if (!inserts
|
|
1250
|
+
if (!(inserts === null || inserts === void 0 ? void 0 : inserts.length))
|
|
1246
1251
|
return;
|
|
1247
1252
|
let seqKeys = this._findSequenceKeys(inserts[0]);
|
|
1248
1253
|
let seq = 0;
|
|
1249
1254
|
if (this.syncSupport)
|
|
1250
1255
|
seq = await this._getNextCollectionUpdateSeqNo(collection, connection);
|
|
1251
|
-
for await (let seqKey of seqKeys
|
|
1256
|
+
for await (let seqKey of (seqKeys === null || seqKeys === void 0 ? void 0 : seqKeys.seqKeys) || []) {
|
|
1252
1257
|
let last = await this._findLastSequenceForKey(connection.db(this.db).collection(collection), seqKey);
|
|
1253
1258
|
if (last === undefined) {
|
|
1254
1259
|
try {
|
|
@@ -1305,7 +1310,7 @@ export default class Mongo extends Db {
|
|
|
1305
1310
|
async _publishAndAudit(operation, db, collection, dataToPublish, noEmit) {
|
|
1306
1311
|
if (!dataToPublish._id && !["deleteMany", "updateMany"].includes(operation))
|
|
1307
1312
|
throw new Error(`_publishAndAudit requires _id for ${operation}`);
|
|
1308
|
-
let data =
|
|
1313
|
+
let data = (0, lodash_clonedeep_1.default)(dataToPublish);
|
|
1309
1314
|
if (data._id && /[0-9a-f]{24,24}/i.test(data._id.toString()))
|
|
1310
1315
|
data._id = data._id.toHexString();
|
|
1311
1316
|
let toPublish = undefined;
|
|
@@ -1336,7 +1341,7 @@ export default class Mongo extends Db {
|
|
|
1336
1341
|
return toPublish;
|
|
1337
1342
|
}
|
|
1338
1343
|
emit(what) {
|
|
1339
|
-
log.debug("emitting publish", what);
|
|
1344
|
+
db_js_1.log.debug("emitting publish", what);
|
|
1340
1345
|
this.emitter.emit('publish', what);
|
|
1341
1346
|
}
|
|
1342
1347
|
async _writeAuditRecord(collection, operation, data, user = this.user, audit = this.audit) {
|
|
@@ -1356,9 +1361,9 @@ export default class Mongo extends Db {
|
|
|
1356
1361
|
let auditRecord = {
|
|
1357
1362
|
db: this.db,
|
|
1358
1363
|
collection: collection,
|
|
1359
|
-
entityid: Base.objectid(data._id),
|
|
1360
|
-
rev: (previousAuditRecord
|
|
1361
|
-
ts: Base.timestamp(),
|
|
1364
|
+
entityid: base_js_1.Base.objectid(data._id),
|
|
1365
|
+
rev: ((previousAuditRecord === null || previousAuditRecord === void 0 ? void 0 : previousAuditRecord.rev) || 0) + 1,
|
|
1366
|
+
ts: base_js_1.Base.timestamp(),
|
|
1362
1367
|
on: new Date(),
|
|
1363
1368
|
operation: operation,
|
|
1364
1369
|
changes: dataNoId,
|
|
@@ -1367,11 +1372,11 @@ export default class Mongo extends Db {
|
|
|
1367
1372
|
auditRecord.user = user;
|
|
1368
1373
|
if (audit)
|
|
1369
1374
|
auditRecord.audit = audit;
|
|
1370
|
-
log.trace('AUDITING', auditRecord);
|
|
1375
|
+
db_js_1.log.trace('AUDITING', auditRecord);
|
|
1371
1376
|
let ret = await client.db(this.db)
|
|
1372
1377
|
.collection(this.auditCollectionName)
|
|
1373
1378
|
.insertOne(auditRecord, this._sessionOpt());
|
|
1374
|
-
log.debug('AUDITED', auditRecord, ret.insertedId);
|
|
1379
|
+
db_js_1.log.debug('AUDITED', auditRecord, ret.insertedId);
|
|
1375
1380
|
}
|
|
1376
1381
|
_sessionOpt() {
|
|
1377
1382
|
return this.session ? { session: this.session } : {};
|
|
@@ -1413,8 +1418,8 @@ export default class Mongo extends Db {
|
|
|
1413
1418
|
for await (let key of Object.keys(update)) {
|
|
1414
1419
|
let shouldBeHashed = /^__hashed_(.+)$/.test(key);
|
|
1415
1420
|
if (shouldBeHashed) {
|
|
1416
|
-
let salt = await
|
|
1417
|
-
let hash = await
|
|
1421
|
+
let salt = await bcrypt_1.default.genSalt(saltRounds);
|
|
1422
|
+
let hash = await bcrypt_1.default.hash(update[key], salt);
|
|
1418
1423
|
update[key] = { salt, hash };
|
|
1419
1424
|
}
|
|
1420
1425
|
}
|
|
@@ -1438,4 +1443,6 @@ export default class Mongo extends Db {
|
|
|
1438
1443
|
return ret;
|
|
1439
1444
|
}
|
|
1440
1445
|
}
|
|
1446
|
+
exports.default = Mongo;
|
|
1441
1447
|
module.exports = Mongo;
|
|
1448
|
+
//# sourceMappingURL=mongo.js.map
|