cry-db 2.1.27 → 2.1.30

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/mongo.js CHANGED
@@ -4,14 +4,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.DummyExportToFixTsCompilation = void 0;
7
- const assert_1 = __importDefault(require("assert"));
8
7
  const bcrypt_1 = __importDefault(require("bcrypt"));
9
8
  const mongodb_1 = require("mongodb");
10
9
  const tiny_typed_emitter_1 = require("tiny-typed-emitter");
11
- const _1 = require(".");
12
- const db_1 = require("./db");
13
- const types_1 = require("./types");
10
+ const db_js_1 = require("./db.js");
11
+ const types_js_1 = require("./types.js");
14
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
+ };
15
20
  const saltRounds = 10;
16
21
  const TRANSACTION_OPTIONS = {
17
22
  defaultTransactionOptions: {
@@ -21,7 +26,7 @@ const TRANSACTION_OPTIONS = {
21
26
  }
22
27
  };
23
28
  exports.DummyExportToFixTsCompilation = true;
24
- class Mongo extends db_1.Db {
29
+ class Mongo extends db_js_1.Db {
25
30
  constructor(db, url) {
26
31
  super(db, url);
27
32
  this.revisions = false;
@@ -35,27 +40,18 @@ class Mongo extends db_1.Db {
35
40
  this.emitter = new tiny_typed_emitter_1.TypedEmitter();
36
41
  this.user = undefined;
37
42
  this.audit = undefined;
38
- db_1.log.debug('new Mongo:', this.url, this.db);
39
- }
40
- static newid() {
41
- return db_1.Db.newid();
42
- }
43
- static toId(id) {
44
- return db_1.Db.toId(id);
45
- }
46
- static objectid(o) {
47
- return db_1.Db.objectid(o);
43
+ db_js_1.log.debug('new Mongo:', this.url, this.db);
48
44
  }
49
45
  on(evt, listener) {
50
- db_1.log.debug("on", evt, listener);
46
+ db_js_1.log.debug("on", evt, listener);
51
47
  this.emitter.on(evt, listener);
52
48
  }
53
49
  off(evt, listener) {
54
- db_1.log.debug("off", evt, listener);
50
+ db_js_1.log.debug("off", evt, listener);
55
51
  this.emitter.off(evt, listener);
56
52
  }
57
53
  once(evt, listener) {
58
- db_1.log.debug("off", evt, listener);
54
+ db_js_1.log.debug("off", evt, listener);
59
55
  this.emitter.off(evt, listener);
60
56
  }
61
57
  setUser(username) {
@@ -125,29 +121,29 @@ class Mongo extends db_1.Db {
125
121
  return this.emittingPublishEvents;
126
122
  }
127
123
  async distinct(collection, field) {
128
- db_1.log.debug('distinct called', collection, field);
124
+ db_js_1.log.debug('distinct called', collection, field);
129
125
  let ret = await this.executeTransactionally(collection, async (conn) => {
130
126
  return await conn.distinct(field);
131
127
  }, false, { operation: "distinct", collection, field });
132
- db_1.log.debug('distinct returns', ret);
128
+ db_js_1.log.debug('distinct returns', ret);
133
129
  return ret;
134
130
  }
135
131
  async count(collection, query = {}, opts = {}) {
136
- db_1.log.debug('distinct called', collection, query, opts);
132
+ db_js_1.log.debug('distinct called', collection, query, opts);
137
133
  let ret = await this.executeTransactionally(collection, async (conn) => {
138
134
  return await conn.countDocuments(query, opts);
139
135
  }, false, { operation: "count", collection, query, opts });
140
- db_1.log.debug('count returns', ret);
136
+ db_js_1.log.debug('count returns', ret);
141
137
  return ret;
142
138
  }
143
139
  async find(collection, query = {}, opts = {}) {
144
- (0, assert_1.default)(collection);
140
+ assert(collection);
145
141
  query = this.replaceIds(query);
146
142
  if (this.softdelete) {
147
143
  if (!query._deleted)
148
144
  query._deleted = { $exists: false };
149
145
  }
150
- db_1.log.debug('find called', collection, query, opts);
146
+ db_js_1.log.debug('find called', collection, query, opts);
151
147
  let ret = await this.executeTransactionally(collection, async (conn) => {
152
148
  let optsIn = {};
153
149
  if (opts.readPreference)
@@ -168,11 +164,11 @@ class Mongo extends db_1.Db {
168
164
  let res = await r.toArray();
169
165
  return this._processReturnedObject(res);
170
166
  }, false, { operation: "find", collection, query, opts });
171
- db_1.log.debug('find returns', ret);
167
+ db_js_1.log.debug('find returns', ret);
172
168
  return ret;
173
169
  }
174
170
  async findAll(collection, query = {}, opts = {}) {
175
- (0, assert_1.default)(collection);
171
+ assert(collection);
176
172
  query = this.replaceIds(query);
177
173
  let ret = await this.executeTransactionally(collection, async (conn) => {
178
174
  let optsIn = {};
@@ -193,12 +189,12 @@ class Mongo extends db_1.Db {
193
189
  r = r.collation(opts.collation);
194
190
  return this._processReturnedObject(await r.toArray());
195
191
  }, false, { operation: "findAll", collection, query, opts });
196
- db_1.log.debug('findAll returns', ret);
192
+ db_js_1.log.debug('findAll returns', ret);
197
193
  return ret;
198
194
  }
199
195
  async findNewer(collection, timestamp, query = {}, opts = {}) {
200
196
  query = this._createQueryForNewer(timestamp, query);
201
- db_1.log.debug('findNewer called', collection, timestamp, query, opts);
197
+ db_js_1.log.debug('findNewer called', collection, timestamp, query, opts);
202
198
  let ret = await this.executeTransactionally(collection, async (conn) => {
203
199
  let optsIn = {};
204
200
  if (opts.readPreference)
@@ -220,11 +216,11 @@ class Mongo extends db_1.Db {
220
216
  r = r.collation(opts.collation);
221
217
  return this._processReturnedObject(await r.toArray());
222
218
  }, false, { operation: "findNewer", collection, timestamp, query, opts });
223
- db_1.log.debug('findNewer returns', ret);
219
+ db_js_1.log.debug('findNewer returns', ret);
224
220
  return ret;
225
221
  }
226
222
  async findNewerMany(spec = []) {
227
- db_1.log.debug('findNewerMany called', spec);
223
+ db_js_1.log.debug('findNewerMany called', spec);
228
224
  let conn = await this.connect();
229
225
  const getOneColl = async (coll) => {
230
226
  let query = this._createQueryForNewer(coll.timestamp, coll.query);
@@ -257,7 +253,7 @@ class Mongo extends db_1.Db {
257
253
  _createQueryForNewer(timestamp, query) {
258
254
  let ts = (timestamp === 1 || timestamp === "1" || timestamp === "0" || timestamp === 0)
259
255
  ? {}
260
- : { _ts: { $gt: _1.Base.timestamp(timestamp) } };
256
+ : { _ts: { $gt: base_js_1.Base.timestamp(timestamp) } };
261
257
  query = {
262
258
  ...ts,
263
259
  ...(query || {}),
@@ -267,7 +263,7 @@ class Mongo extends db_1.Db {
267
263
  }
268
264
  async findAfter(collection, csq, query = {}, opts = {}) {
269
265
  query._csq = { $gt: csq };
270
- db_1.log.debug('findAfter called', collection, csq, query, opts);
266
+ db_js_1.log.debug('findAfter called', collection, csq, query, opts);
271
267
  let ret = await this.executeTransactionally(collection, async (conn) => {
272
268
  let optsIn = {};
273
269
  if (opts.readPreference)
@@ -289,11 +285,11 @@ class Mongo extends db_1.Db {
289
285
  r = r.collation(opts.collation);
290
286
  return this._processReturnedObject(await r.toArray());
291
287
  }, false, { operation: "findNewer", collection, csq, query, opts });
292
- db_1.log.debug('findNewer returns', ret);
288
+ db_js_1.log.debug('findNewer returns', ret);
293
289
  return ret;
294
290
  }
295
291
  async findAfterMany(spec = []) {
296
- db_1.log.debug('findAfterMany called', spec);
292
+ db_js_1.log.debug('findAfterMany called', spec);
297
293
  let conn = await this.connect();
298
294
  const getOneColl = async (coll) => {
299
295
  let r = conn
@@ -326,56 +322,56 @@ class Mongo extends db_1.Db {
326
322
  }
327
323
  async findNewerFromDate(collection, date, query = {}, opts = {}) {
328
324
  let ts = new mongodb_1.Timestamp(0, new Date(date).valueOf() / 1000);
329
- db_1.log.debug('findNewerFromDate called', collection, date, query, opts);
325
+ db_js_1.log.debug('findNewerFromDate called', collection, date, query, opts);
330
326
  let ret = await Mongo.prototype.findNewer.call(this, collection, ts, query, opts); // prevent calling Repo.findNewer
331
- db_1.log.debug('findNewerFromDate returns', ret);
327
+ db_js_1.log.debug('findNewerFromDate returns', ret);
332
328
  return ret;
333
329
  }
334
330
  async findOne(collection, query, projection) {
335
- (0, assert_1.default)(collection);
336
- (0, assert_1.default)(query);
331
+ assert(collection);
332
+ assert(query);
337
333
  query = this.replaceIds(query);
338
334
  if (!query._deleted)
339
335
  query._deleted = { $exists: false };
340
336
  // if (!query._blocked) query._blocked = { $exists: false }; // intentionally - blocked records are returned
341
- db_1.log.debug('findOne called', collection, query, projection);
342
- let ret = await this.executeTransactionally(collection, async (conn) => await conn.findOne(query, { projection, session: this.session }), false, { operation: "findOne", collection, query, projection });
343
- db_1.log.debug('findOne returns', ret);
337
+ db_js_1.log.debug('findOne called', collection, query, projection);
338
+ let ret = await this.executeTransactionally(collection, async (conn) => await conn.findOne(query, { ...(projection ? { projection } : {}), ...this._sessionOpt() }), false, { operation: "findOne", collection, query, projection });
339
+ db_js_1.log.debug('findOne returns', ret);
344
340
  return this._processReturnedObject(ret);
345
341
  }
346
342
  async findById(collection, id, projection) {
347
- (0, assert_1.default)(collection);
348
- (0, assert_1.default)(id);
343
+ assert(collection);
344
+ assert(id);
349
345
  if (!id)
350
346
  return null;
351
347
  let query = {
352
348
  _id: Mongo._toId(id),
353
349
  // _deleted: { $exists: false }
354
350
  };
355
- db_1.log.debug('findById called', this.db, collection, id, projection);
356
- db_1.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);
357
353
  let ret = await this.executeTransactionally(collection, async (conn) => {
358
- let r = await conn.findOne(query, { projection, session: this.session });
354
+ let r = await conn.findOne(query, { ...(projection ? { projection } : {}), ...this._sessionOpt() });
359
355
  return r;
360
356
  }, false, { operation: "findById", collection, id, projection });
361
357
  if (ret === null || ret === void 0 ? void 0 : ret._deleted)
362
358
  ret = null;
363
- db_1.log.debug('findById returns', ret);
359
+ db_js_1.log.debug('findById returns', ret);
364
360
  return this._processReturnedObject(ret);
365
361
  }
366
362
  async updateOne(collection, query, update, options = { returnFullObject: false }) {
367
- (0, assert_1.default)(collection);
368
- (0, assert_1.default)(query);
369
- (0, assert_1.default)(update);
363
+ assert(collection);
364
+ assert(query);
365
+ assert(update);
370
366
  query = this.replaceIds(query);
371
367
  update = this.replaceIds(update);
372
368
  let opts = {
373
369
  upsert: false,
374
370
  returnDocument: "after",
375
- session: this.session,
371
+ ...this._sessionOpt()
376
372
  };
377
373
  update = await this._processUpdateObject(update);
378
- db_1.log.debug('updateOne called', collection, query, update);
374
+ db_js_1.log.debug('updateOne called', collection, query, update);
379
375
  let seqKeys = this._findSequenceKeys(update.$set);
380
376
  let obj = await this.executeTransactionally(collection, async (conn, client) => {
381
377
  update.$set = update.$set || {};
@@ -390,21 +386,21 @@ class Mongo extends db_1.Db {
390
386
  await this._publishAndAudit('update', this.db, collection, resObj);
391
387
  return resObj;
392
388
  }, !!seqKeys, { operation: "updateOne", collection, query, update, options });
393
- db_1.log.debug('updateOne returns', obj);
389
+ db_js_1.log.debug('updateOne returns', obj);
394
390
  return this._processReturnedObject(await obj);
395
391
  }
396
392
  async save(collection, update, id = undefined, options = { returnFullObject: false }) {
397
- (0, assert_1.default)(collection);
398
- (0, assert_1.default)(update);
393
+ assert(collection);
394
+ assert(update);
399
395
  update = this.replaceIds(update);
400
396
  let opts = {
401
397
  upsert: true,
402
398
  returnDocument: "after",
403
- session: this.session,
399
+ ...this._sessionOpt()
404
400
  };
405
401
  let _id = Mongo.toId(id || update._id) || Mongo.newid();
406
402
  update = await this._processUpdateObject(update);
407
- db_1.log.debug('save called', collection, id, update);
403
+ db_js_1.log.debug('save called', collection, id, update);
408
404
  let seqKeys = this._findSequenceKeys(update.$set);
409
405
  let obj = await this.executeTransactionally(collection, async (conn, client) => {
410
406
  update.$set = update.$set || {};
@@ -419,15 +415,15 @@ class Mongo extends db_1.Db {
419
415
  await this._publishAndAudit('update', this.db, collection, resObj);
420
416
  return resObj;
421
417
  }, !!seqKeys, { operation: "save", collection, _id, update, options });
422
- db_1.log.debug('save returns', obj);
418
+ db_js_1.log.debug('save returns', obj);
423
419
  return this._processReturnedObject(await obj);
424
420
  }
425
421
  async update(collection, query, update) {
426
- (0, assert_1.default)(collection);
427
- (0, assert_1.default)(query);
428
- (0, assert_1.default)(update);
422
+ assert(collection);
423
+ assert(query);
424
+ assert(update);
429
425
  if (this.syncSupport)
430
- db_1.log.warn("update does not increase _csq, avoit it.");
426
+ db_js_1.log.warn("update does not increase _csq, avoit it.");
431
427
  if (!Object.keys(update).length)
432
428
  return { n: 0, ok: false };
433
429
  query = this.replaceIds(query);
@@ -435,10 +431,10 @@ class Mongo extends db_1.Db {
435
431
  let opts = {
436
432
  upsert: false,
437
433
  returnDocument: "after",
438
- session: this.session,
434
+ ...this._sessionOpt()
439
435
  };
440
436
  update = await this._processUpdateObject(update);
441
- db_1.log.debug('update called', collection, query, update);
437
+ db_js_1.log.debug('update called', collection, query, update);
442
438
  let seqKeys = this._findSequenceKeys(update.$set);
443
439
  let obj = await this.executeTransactionally(collection, async (conn, client) => {
444
440
  update.$set = update.$set || {};
@@ -446,7 +442,7 @@ class Mongo extends db_1.Db {
446
442
  await this._processSequenceField(client, collection, update.$set, seqKeys);
447
443
  if (update.$set === undefined || Object.keys(update.$set).length === 0)
448
444
  delete update.$set;
449
- db_1.log.debug('update called', collection, query, update);
445
+ db_js_1.log.debug('update called', collection, query, update);
450
446
  let res = await conn.updateMany(query, update, opts);
451
447
  let resObj = {
452
448
  n: res.modifiedCount,
@@ -455,14 +451,14 @@ class Mongo extends db_1.Db {
455
451
  await this._publishAndAudit('updateMany', this.db, collection, resObj);
456
452
  return resObj;
457
453
  }, !!seqKeys, { operation: "update", collection, query, update });
458
- db_1.log.debug('update returns', obj);
454
+ db_js_1.log.debug('update returns', obj);
459
455
  return await obj;
460
456
  }
461
457
  async upsert(collection, query, update, options = { returnFullObject: false }) {
462
- (0, assert_1.default)(collection);
463
- (0, assert_1.default)(query);
464
- (0, assert_1.default)(update);
465
- (0, assert_1.default)(typeof update === 'object', 'update must be an object');
458
+ assert(collection);
459
+ assert(query);
460
+ assert(update);
461
+ assert(typeof update === 'object', 'update must be an object');
466
462
  // if (!Object.keys(update).length) return null;
467
463
  query = this.replaceIds(query);
468
464
  update = this.replaceIds(update);
@@ -470,12 +466,12 @@ class Mongo extends db_1.Db {
470
466
  ...options,
471
467
  upsert: true,
472
468
  returnDocument: "after",
473
- session: this.session,
469
+ ...this._sessionOpt()
474
470
  };
475
- db_1.log.debug('upsert called', collection, query, update);
471
+ db_js_1.log.debug('upsert called', collection, query, update);
476
472
  update = await this._processUpdateObject(update);
477
473
  let seqKeys = this._findSequenceKeys(update.$set);
478
- db_1.log.debug('upsert processed', collection, query, update);
474
+ db_js_1.log.debug('upsert processed', collection, query, update);
479
475
  if (Object.keys(query).length === 0)
480
476
  query._id = Mongo.newid();
481
477
  let ret = await this.executeTransactionally(collection, async (conn, client) => {
@@ -495,40 +491,41 @@ class Mongo extends db_1.Db {
495
491
  ;
496
492
  return ret;
497
493
  }, !!seqKeys, { operation: "upsert", query, update, options });
498
- db_1.log.debug('upsert returns', ret);
494
+ db_js_1.log.debug('upsert returns', ret);
499
495
  return this._processReturnedObject(await ret);
500
496
  }
501
497
  async insert(collection, insert) {
502
- (0, assert_1.default)(collection, "collection can't be null");
503
- (0, assert_1.default)(insert, "insert can't be null");
504
- (0, assert_1.default)(typeof insert === "object", "insert must be an object");
505
- db_1.log.debug('insert called', collection, insert);
498
+ assert(collection, "collection can't be null");
499
+ assert(insert, "insert can't be null");
500
+ assert(typeof insert === "object", "insert must be an object");
501
+ db_js_1.log.debug('insert called', collection, insert);
506
502
  insert = this.replaceIds(insert);
507
503
  if (this.revisions) {
508
504
  insert._rev = 1;
509
- insert._ts = _1.Base.timestamp();
505
+ insert._ts = base_js_1.Base.timestamp();
510
506
  }
511
507
  await this._processHashedKeys(insert);
512
508
  let seqKeys = this._findSequenceKeys(insert);
513
509
  let ret = await this.executeTransactionally(collection, async (conn, client) => {
514
510
  if (insert)
515
511
  insert = await this._processSequenceField(client, collection, insert, seqKeys);
516
- let obj = await conn.insertOne(insert, { session: this.session });
512
+ let obj = await conn.insertOne(insert, this._sessionOpt());
517
513
  let fullObj = { _id: obj.insertedId, ...insert };
518
514
  await this._publishAndAudit('insert', this.db, collection, fullObj);
519
515
  return fullObj;
520
516
  }, !!seqKeys, { operation: "insert", collection, insert });
521
- db_1.log.debug('insert returns', ret);
517
+ db_js_1.log.debug('insert returns', ret);
522
518
  return this._processReturnedObject(await ret);
523
519
  }
524
520
  async upsertBatch(collection, batch) {
525
- (0, assert_1.default)(collection, "collection can't be null");
526
- (0, assert_1.default)(batch, "batch can't be null");
527
- (0, assert_1.default)(batch instanceof Array, "batch must be an Array");
528
- db_1.log.debug('upsertBatch called', collection, batch);
521
+ var _a;
522
+ assert(collection, "collection can't be null");
523
+ assert(batch, "batch can't be null");
524
+ assert(batch instanceof Array, "batch must be an Array");
525
+ db_js_1.log.debug('upsertBatch called', collection, batch);
529
526
  batch = this.replaceIds(batch);
530
527
  for (let i = 0; i < batch.length; i++)
531
- await this._processHashedKeys(batch[i].update);
528
+ await this._processHashedKeys((_a = batch[i]) === null || _a === void 0 ? void 0 : _a.update);
532
529
  let ret = await this.executeTransactionally(collection, async (conn, client) => {
533
530
  var _a, _b;
534
531
  await this._processSequenceFieldForMany(client, collection, batch.map(b => b.update));
@@ -540,7 +537,7 @@ class Mongo extends db_1.Db {
540
537
  ...(opts || {}),
541
538
  upsert: true,
542
539
  returnDocument: "after",
543
- session: this.session,
540
+ ...this._sessionOpt()
544
541
  };
545
542
  update = await this._processUpdateObject(update);
546
543
  let ret = (await conn.findOneAndUpdate(query, update, options));
@@ -565,22 +562,22 @@ class Mongo extends db_1.Db {
565
562
  }
566
563
  return changes;
567
564
  }, false, { operation: "upsertBatch", collection, batch });
568
- db_1.log.debug('upsertBatch returns', ret);
565
+ db_js_1.log.debug('upsertBatch returns', ret);
569
566
  return ret;
570
567
  }
571
568
  async insertMany(collection, insert) {
572
- (0, assert_1.default)(collection, "collection can't be null");
573
- (0, assert_1.default)(insert, "insert can't be null");
574
- (0, assert_1.default)(insert instanceof Array, "insert must be an Array");
575
- db_1.log.debug('insertMany called', collection, insert);
569
+ assert(collection, "collection can't be null");
570
+ assert(insert, "insert can't be null");
571
+ assert(insert instanceof Array, "insert must be an Array");
572
+ db_js_1.log.debug('insertMany called', collection, insert);
576
573
  insert = this.replaceIds(insert);
577
574
  for (let i = 0; i < insert.length; i++)
578
575
  await this._processHashedKeys(insert[i]);
579
576
  if (this.revisions)
580
- insert.forEach(ins => { ins._rev = 1; ins._ts = _1.Base.timestamp(); });
577
+ insert.forEach(ins => { ins._rev = 1; ins._ts = base_js_1.Base.timestamp(); });
581
578
  let ret = await this.executeTransactionally(collection, async (conn, client) => {
582
579
  await this._processSequenceFieldForMany(client, collection, insert);
583
- let obj = await conn.insertMany(insert, { session: this.session });
580
+ let obj = await conn.insertMany(insert, this._sessionOpt());
584
581
  let ret = [];
585
582
  for (let ns of Object.keys(obj.insertedIds)) {
586
583
  let n = Number(ns);
@@ -597,35 +594,35 @@ class Mongo extends db_1.Db {
597
594
  }
598
595
  return ret;
599
596
  }, false, { operation: "insertMany", collection, insert });
600
- db_1.log.debug('insertMany returns', ret);
597
+ db_js_1.log.debug('insertMany returns', ret);
601
598
  return ret;
602
599
  }
603
600
  async deleteOne(collection, query) {
604
- (0, assert_1.default)(collection);
605
- (0, assert_1.default)(query);
601
+ assert(collection);
602
+ assert(query);
606
603
  query = this.replaceIds(query);
607
604
  if (!this.softdelete) {
608
605
  let opts = {
609
606
  returnDocument: "after",
610
- session: this.session,
607
+ ...this._sessionOpt()
611
608
  };
612
- db_1.log.debug('deleteOne called', collection, query);
609
+ db_js_1.log.debug('deleteOne called', collection, query);
613
610
  let ret = await this.executeTransactionally(collection, async (conn) => {
614
611
  let obj = await conn.findOneAndDelete(query, opts);
615
612
  if (obj.value)
616
613
  await this._publishAndAudit('delete', this.db, collection, obj.value);
617
614
  return obj.value;
618
615
  }, false, { operation: "deleteOne", collection, query, softdelete: this.softdelete });
619
- db_1.log.debug('deleteOne returns', ret);
616
+ db_js_1.log.debug('deleteOne returns', ret);
620
617
  return ret;
621
618
  }
622
619
  else {
623
620
  let opts = {
624
621
  upsert: true,
625
622
  returnDocument: "after",
626
- session: this.session,
623
+ ...this._sessionOpt()
627
624
  };
628
- db_1.log.debug('deleteOne called', collection, query);
625
+ db_js_1.log.debug('deleteOne called', collection, query);
629
626
  let ret = await this.executeTransactionally(collection, async (conn, client) => {
630
627
  let del = {
631
628
  $set: { _deleted: new Date() },
@@ -639,7 +636,7 @@ class Mongo extends db_1.Db {
639
636
  await this._publishAndAudit('delete', this.db, collection, obj.value);
640
637
  return obj.value;
641
638
  }, false, { operation: "deleteOne", collection, query, softdelete: this.softdelete });
642
- db_1.log.debug('deleteOne returns', ret);
639
+ db_js_1.log.debug('deleteOne returns', ret);
643
640
  return ret;
644
641
  }
645
642
  }
@@ -647,10 +644,10 @@ class Mongo extends db_1.Db {
647
644
  let opts = {
648
645
  upsert: false,
649
646
  returnDocument: "after",
650
- session: this.session,
647
+ ...this._sessionOpt()
651
648
  };
652
649
  query = this.replaceIds(query);
653
- db_1.log.debug('blockOne called', collection, query);
650
+ db_js_1.log.debug('blockOne called', collection, query);
654
651
  let ret = await this.executeTransactionally(collection, async (conn, client) => {
655
652
  query._blocked = { $exists: 0 };
656
653
  let update = {
@@ -672,17 +669,17 @@ class Mongo extends db_1.Db {
672
669
  await this._publishAndAudit('block', this.db, collection, retObj);
673
670
  return retObj;
674
671
  }, false, { operation: "blockOne", collection, query });
675
- db_1.log.debug('blockOne returns', ret);
672
+ db_js_1.log.debug('blockOne returns', ret);
676
673
  return ret;
677
674
  }
678
675
  async unblockOne(collection, query) {
679
676
  let opts = {
680
677
  upsert: false,
681
678
  returnDocument: "after",
682
- session: this.session,
679
+ ...this._sessionOpt()
683
680
  };
684
681
  query = this.replaceIds(query);
685
- db_1.log.debug('unblockOne called', collection, query);
682
+ db_js_1.log.debug('unblockOne called', collection, query);
686
683
  let ret = await this.executeTransactionally(collection, async (conn, client) => {
687
684
  query._blocked = { $exists: 1 };
688
685
  let update = {
@@ -703,18 +700,18 @@ class Mongo extends db_1.Db {
703
700
  await this._publishAndAudit('unblock', this.db, collection, retObj);
704
701
  return retObj;
705
702
  }, false, { operation: "unblockOne", collection, query });
706
- db_1.log.debug('unblockOne returns', ret);
703
+ db_js_1.log.debug('unblockOne returns', ret);
707
704
  return ret;
708
705
  }
709
706
  async hardDeleteOne(collection, query) {
710
- (0, assert_1.default)(collection);
711
- (0, assert_1.default)(query);
707
+ assert(collection);
708
+ assert(query);
712
709
  query = this.replaceIds(query);
713
710
  let opts = {
714
711
  returnDocument: "after",
715
- session: this.session,
712
+ ...this._sessionOpt()
716
713
  };
717
- db_1.log.debug('hardDeleteOne called', collection, query);
714
+ db_js_1.log.debug('hardDeleteOne called', collection, query);
718
715
  let ret = await this.executeTransactionally(collection, async (conn) => {
719
716
  let obj = await conn.findOneAndDelete(query, opts);
720
717
  if (obj.value) {
@@ -722,21 +719,21 @@ class Mongo extends db_1.Db {
722
719
  }
723
720
  return obj.value;
724
721
  }, false, { operation: "hardDeleteOne", collection, query });
725
- db_1.log.debug('hardDeleteOne returns', ret);
722
+ db_js_1.log.debug('hardDeleteOne returns', ret);
726
723
  return ret;
727
724
  }
728
725
  async delete(collection, query) {
729
- (0, assert_1.default)(collection);
730
- (0, assert_1.default)(query);
726
+ assert(collection);
727
+ assert(query);
731
728
  if (this.syncSupport)
732
- db_1.log.warn("delete does not increase _csq, avoit it.");
729
+ db_js_1.log.warn("delete does not increase _csq, avoit it.");
733
730
  query = this.replaceIds(query);
734
731
  if (!this.softdelete) {
735
732
  let opts = {
736
733
  returnDocument: "after",
737
- session: this.session,
734
+ ...this._sessionOpt()
738
735
  };
739
- db_1.log.debug('delete called', collection, query);
736
+ db_js_1.log.debug('delete called', collection, query);
740
737
  let ret = await this.executeTransactionally(collection, async (conn) => {
741
738
  let obj = await conn.deleteMany(query, opts);
742
739
  let resObj = {
@@ -746,17 +743,17 @@ class Mongo extends db_1.Db {
746
743
  await this._publishAndAudit('deleteMany', this.db, collection, resObj);
747
744
  return resObj;
748
745
  }, false, { operation: "delete", collection, query, softdelete: this.softdelete });
749
- db_1.log.debug('delete returns', ret);
746
+ db_js_1.log.debug('delete returns', ret);
750
747
  return ret;
751
748
  }
752
749
  else {
753
750
  let opts = {
754
751
  upsert: false,
755
752
  returnDocument: "after",
756
- session: this.session,
753
+ ...this._sessionOpt()
757
754
  };
758
755
  let date = new Date();
759
- db_1.log.debug('delete called', collection, query);
756
+ db_js_1.log.debug('delete called', collection, query);
760
757
  let ret = await this.executeTransactionally(collection, async (conn) => {
761
758
  let obj = await conn.updateMany(query, { $set: { _deleted: date } }, opts);
762
759
  let resObj = {
@@ -766,21 +763,21 @@ class Mongo extends db_1.Db {
766
763
  await this._publishAndAudit('deleteMany', this.db, collection, resObj);
767
764
  return resObj;
768
765
  }, false, { operation: "delete", collection, query, softdelete: this.softdelete });
769
- db_1.log.debug('delete returns', ret);
766
+ db_js_1.log.debug('delete returns', ret);
770
767
  return ret;
771
768
  }
772
769
  }
773
770
  async hardDelete(collection, query) {
774
- (0, assert_1.default)(collection);
775
- (0, assert_1.default)(query);
771
+ assert(collection);
772
+ assert(query);
776
773
  if (this.syncSupport)
777
- db_1.log.warn("hardDelete does not increase _csq, avoit it.");
774
+ db_js_1.log.warn("hardDelete does not increase _csq, avoit it.");
778
775
  query = this.replaceIds(query);
779
776
  let opts = {
780
777
  returnDocument: "after",
781
- session: this.session,
778
+ ...this._sessionOpt()
782
779
  };
783
- db_1.log.debug('hardDelete called', collection, query);
780
+ db_js_1.log.debug('hardDelete called', collection, query);
784
781
  let ret = await this.executeTransactionally(collection, async (conn) => {
785
782
  let obj = await conn.deleteMany(query, opts);
786
783
  let resObj = {
@@ -790,12 +787,12 @@ class Mongo extends db_1.Db {
790
787
  await this._publishAndAudit('deleteMany', this.db, collection, resObj);
791
788
  return resObj;
792
789
  }, false, { operation: "hardDelete", collection, query, softdelete: this.softdelete });
793
- db_1.log.debug('hardDelete returns', ret);
790
+ db_js_1.log.debug('hardDelete returns', ret);
794
791
  return ret;
795
792
  }
796
793
  async testHash(collection, query, field, unhashedValue) {
797
794
  let _field;
798
- db_1.log.debug('teshHash called', collection, query, field, unhashedValue);
795
+ db_js_1.log.debug('teshHash called', collection, query, field, unhashedValue);
799
796
  if (typeof field === "object") {
800
797
  if (Object.keys(field).length === 1)
801
798
  [_field, unhashedValue] = Object.entries(field)[0];
@@ -807,21 +804,21 @@ class Mongo extends db_1.Db {
807
804
  if (!/^__hashed__/.test(_field))
808
805
  _field = "__hashed__" + _field;
809
806
  let conn = await this.connect();
810
- let obj = await conn.db(this.db).collection(collection).findOne(query, { projection: { [_field]: 1 }, session: this.session });
807
+ let obj = await conn.db(this.db).collection(collection).findOne(query, { projection: { [_field]: 1 }, ...this._sessionOpt() });
811
808
  if (!obj || !obj[_field]) {
812
- db_1.log.debug('teshHash returns false', obj);
809
+ db_js_1.log.debug('teshHash returns false', obj);
813
810
  return false;
814
811
  }
815
812
  let res = await bcrypt_1.default.compare(unhashedValue, obj[_field].hash);
816
- db_1.log.debug('teshHash returns', res);
813
+ db_js_1.log.debug('teshHash returns', res);
817
814
  return res;
818
815
  }
819
816
  async aggregate(collection, pipeline, opts = {
820
817
  readPreference: mongodb_1.ReadPreference.SECONDARY_PREFERRED,
821
818
  }) {
822
- (0, assert_1.default)(collection);
823
- (0, assert_1.default)(pipeline instanceof Array);
824
- db_1.log.debug('aggregate called', collection, pipeline);
819
+ assert(collection);
820
+ assert(pipeline instanceof Array);
821
+ db_js_1.log.debug('aggregate called', collection, pipeline);
825
822
  pipeline = this.replaceIds(pipeline);
826
823
  if (this.session)
827
824
  opts.session = this.session;
@@ -829,14 +826,14 @@ class Mongo extends db_1.Db {
829
826
  let res = await conn.aggregate(pipeline, opts).toArray();
830
827
  return res;
831
828
  }, false, { operation: "aggregate", collection, pipeline, opts });
832
- db_1.log.debug('aggregare returns', ret);
829
+ db_js_1.log.debug('aggregare returns', ret);
833
830
  return ret;
834
831
  }
835
832
  async isUnique(collection, field, value, id) {
836
- (0, assert_1.default)(collection);
837
- (0, assert_1.default)(field);
838
- (0, assert_1.default)(value);
839
- db_1.log.debug('isUnuqie called', collection, field, value, id);
833
+ assert(collection);
834
+ assert(field);
835
+ assert(value);
836
+ db_js_1.log.debug('isUnuqie called', collection, field, value, id);
840
837
  let _id = id === null || id === void 0 ? void 0 : id.toString();
841
838
  let matches = await this.executeTransactionally(collection, async (conn) => {
842
839
  let agg = await conn.find({ [field]: value });
@@ -850,13 +847,13 @@ class Mongo extends db_1.Db {
850
847
  return false;
851
848
  }
852
849
  }
853
- db_1.log.debug('isUnuqie returns', ret);
850
+ db_js_1.log.debug('isUnuqie returns', ret);
854
851
  return ret;
855
852
  }
856
853
  async collectFieldValues(collection, field, inArray = false, opts) {
857
- (0, assert_1.default)(collection);
858
- (0, assert_1.default)(field);
859
- db_1.log.debug('collectFieldValues called', collection, field);
854
+ assert(collection);
855
+ assert(field);
856
+ db_js_1.log.debug('collectFieldValues called', collection, field);
860
857
  let pipeline = [
861
858
  { $group: { _id: '$' + field } },
862
859
  { $sort: { _id: 1 } }
@@ -872,32 +869,32 @@ class Mongo extends db_1.Db {
872
869
  return res;
873
870
  }, false, { operation: "collectFieldValues", collection, field, inArray, pipeline, opts });
874
871
  let ret = res === null || res === void 0 ? void 0 : res.map((v) => v._id);
875
- db_1.log.debug('collectFieldValues returns', ret);
872
+ db_js_1.log.debug('collectFieldValues returns', ret);
876
873
  return ret;
877
874
  }
878
875
  async dropCollection(collection) {
879
- (0, assert_1.default)(collection);
880
- db_1.log.debug('dropCollection called', this.auditCollections);
876
+ assert(collection);
877
+ db_js_1.log.debug('dropCollection called', this.auditCollections);
881
878
  let client = await this.connect();
882
879
  let existing = await client.db(this.db).collections();
883
880
  if (existing.map((c) => c.s.name).includes(collection)) {
884
881
  await client.db(this.db).dropCollection(collection);
885
882
  }
886
- db_1.log.debug('dropCollection returns');
883
+ db_js_1.log.debug('dropCollection returns');
887
884
  }
888
885
  async resetCollectionSync(collection) {
889
- (0, assert_1.default)(collection);
890
- db_1.log.debug('resetCollectionSync called for', collection);
886
+ assert(collection);
887
+ db_js_1.log.debug('resetCollectionSync called for', collection);
891
888
  let client = await this.connect();
892
889
  await client.db(this.db)
893
- .collection(types_1.SEQUENCES_COLLECTION)
890
+ .collection(types_js_1.SEQUENCES_COLLECTION)
894
891
  .findOneAndDelete({ collection });
895
- db_1.log.debug(`resetCollectionSync for ${collection} returns`);
892
+ db_js_1.log.debug(`resetCollectionSync for ${collection} returns`);
896
893
  }
897
894
  async dropCollections(collections) {
898
- (0, assert_1.default)(collections);
899
- (0, assert_1.default)(collections instanceof Array);
900
- db_1.log.debug('dropCollections called', this.auditCollections);
895
+ assert(collections);
896
+ assert(collections instanceof Array);
897
+ db_js_1.log.debug('dropCollections called', this.auditCollections);
901
898
  let client = await this.connect();
902
899
  let existing = await client.db(this.db).collections();
903
900
  for await (let collection of collections) {
@@ -905,12 +902,12 @@ class Mongo extends db_1.Db {
905
902
  await client.db(this.db).dropCollection(collection);
906
903
  }
907
904
  }
908
- db_1.log.debug('dropCollections returns');
905
+ db_js_1.log.debug('dropCollections returns');
909
906
  }
910
907
  async createCollections(collections) {
911
- (0, assert_1.default)(collections);
912
- (0, assert_1.default)(collections instanceof Array);
913
- db_1.log.debug('createCollections called', this.auditCollections);
908
+ assert(collections);
909
+ assert(collections instanceof Array);
910
+ db_js_1.log.debug('createCollections called', this.auditCollections);
914
911
  let client = await this.connect();
915
912
  let existing = await this.getCollections();
916
913
  for await (let collection of collections) {
@@ -918,21 +915,21 @@ class Mongo extends db_1.Db {
918
915
  await client.db(this.db).createCollection(collection);
919
916
  }
920
917
  }
921
- db_1.log.debug('createCollections returns');
918
+ db_js_1.log.debug('createCollections returns');
922
919
  }
923
920
  async createCollection(collection) {
924
- (0, assert_1.default)(collection);
925
- db_1.log.debug('createCollection called', collection);
921
+ assert(collection);
922
+ db_js_1.log.debug('createCollection called', collection);
926
923
  let client = await this.connect();
927
924
  let existing = await this.getCollections();
928
925
  if (!existing.includes(collection)) {
929
926
  await client.db(this.db).createCollection(collection);
930
927
  }
931
- db_1.log.debug('createCollection returns');
928
+ db_js_1.log.debug('createCollection returns');
932
929
  }
933
930
  async dbLogPurge(collection, _id) {
934
- (0, assert_1.default)(collection);
935
- db_1.log.debug('dblogPurge called', collection, _id);
931
+ assert(collection);
932
+ db_js_1.log.debug('dblogPurge called', collection, _id);
936
933
  let ret = await this.executeTransactionally(collection, async () => {
937
934
  let cond = { db: this.db, collection, };
938
935
  if (_id !== undefined)
@@ -941,18 +938,18 @@ class Mongo extends db_1.Db {
941
938
  let ret = await client
942
939
  .db(this.db)
943
940
  .collection(this.auditCollectionName)
944
- .deleteMany(cond, { session: this.session });
941
+ .deleteMany(cond, this._sessionOpt());
945
942
  return {
946
943
  ok: !!ret.acknowledged,
947
944
  n: ret.deletedCount
948
945
  };
949
946
  }, false, { operation: "dbLogPurge", collection, _id });
950
- db_1.log.debug('dblogPurge returns', ret);
947
+ db_js_1.log.debug('dblogPurge returns', ret);
951
948
  return ret;
952
949
  }
953
950
  async dbLogGet(collection, _id) {
954
- (0, assert_1.default)(collection);
955
- db_1.log.debug('dblogGet called', collection, _id);
951
+ assert(collection);
952
+ db_js_1.log.debug('dblogGet called', collection, _id);
956
953
  let ret = await this.executeTransactionally(collection, async () => {
957
954
  let cond = { db: this.db, collection };
958
955
  if (_id)
@@ -961,12 +958,12 @@ class Mongo extends db_1.Db {
961
958
  let ret = await client
962
959
  .db(this.db)
963
960
  .collection(this.auditCollectionName)
964
- .find(cond, { session: this.session })
961
+ .find(cond, this._sessionOpt())
965
962
  .sort({ _id: -1 })
966
963
  .toArray();
967
964
  return ret;
968
965
  }, false, { operation: "dbLogGet", collection, _id });
969
- db_1.log.debug('dblogGet returns', ret);
966
+ db_js_1.log.debug('dblogGet returns', ret);
970
967
  return ret;
971
968
  }
972
969
  // HELPER FUNCTIONS
@@ -1004,9 +1001,9 @@ class Mongo extends db_1.Db {
1004
1001
  return data.map(d => this.replaceIds(d));
1005
1002
  }
1006
1003
  if (typeof data == 'object' && (data === null || data === void 0 ? void 0 : data.t) && (data === null || data === void 0 ? void 0 : data.i) !== undefined)
1007
- return _1.Base.timestamp(data);
1004
+ return base_js_1.Base.timestamp(data);
1008
1005
  if (typeof data == 'object' && (data === null || data === void 0 ? void 0 : data.high) && (data === null || data === void 0 ? void 0 : data.low) !== undefined)
1009
- return _1.Base.timestamp(data);
1006
+ return base_js_1.Base.timestamp(data);
1010
1007
  if (typeof data == 'object') {
1011
1008
  for (let key in data) {
1012
1009
  data[key] = this.replaceIds(data[key]);
@@ -1029,16 +1026,16 @@ class Mongo extends db_1.Db {
1029
1026
  if (this.session)
1030
1027
  try {
1031
1028
  await this.session.endSession();
1032
- db_1.log.info("session ended");
1029
+ db_js_1.log.info("session ended");
1033
1030
  }
1034
1031
  catch (err) {
1035
- db_1.log.error(`Error ending session ${err.message}`);
1032
+ db_js_1.log.error(`Error ending session ${err.message}`);
1036
1033
  }
1037
1034
  try {
1038
1035
  await super.close();
1039
- db_1.log.info("connection closed");
1036
+ db_js_1.log.info("connection closed");
1040
1037
  }
1041
- catch ( /** intentionally */_a) { /** intentionally */ }
1038
+ catch { /** intentionally */ }
1042
1039
  this.session = undefined;
1043
1040
  }
1044
1041
  async inTransaction() {
@@ -1056,13 +1053,13 @@ class Mongo extends db_1.Db {
1056
1053
  let hadSession = !!this.session;
1057
1054
  if (!this.session) {
1058
1055
  this.session = client.startSession();
1059
- db_1.log.info("session started");
1056
+ db_js_1.log.info("session started");
1060
1057
  }
1061
1058
  let session = this.session;
1062
1059
  await session.withTransaction(async () => await funct(client, session));
1063
1060
  if (!hadSession) {
1064
1061
  session.endSession();
1065
- db_1.log.info("session ended");
1062
+ db_js_1.log.info("session ended");
1066
1063
  this.session = undefined;
1067
1064
  }
1068
1065
  return;
@@ -1078,24 +1075,24 @@ class Mongo extends db_1.Db {
1078
1075
  try {
1079
1076
  if (!this.session) {
1080
1077
  this.session = client.startSession(TRANSACTION_OPTIONS);
1081
- db_1.log.info("session started");
1078
+ db_js_1.log.info("session started");
1082
1079
  }
1083
1080
  if (!await this.inTransaction()) {
1084
1081
  await this.session.startTransaction();
1085
- db_1.log.info("transaction started");
1082
+ db_js_1.log.info("transaction started");
1086
1083
  }
1087
1084
  }
1088
1085
  catch (err) {
1089
- db_1.log.error('startTransaction error', err);
1086
+ db_js_1.log.error('startTransaction error', err);
1090
1087
  try {
1091
1088
  if (this.session) {
1092
1089
  await this.session.endSession();
1093
- db_1.log.info("session ended");
1090
+ db_js_1.log.info("session ended");
1094
1091
  }
1095
1092
  this.session = undefined;
1096
1093
  }
1097
1094
  catch (e) {
1098
- db_1.log.error("startTransaction - error in endSession", e.message || e);
1095
+ db_js_1.log.error("startTransaction - error in endSession", e.message || e);
1099
1096
  }
1100
1097
  return;
1101
1098
  }
@@ -1108,13 +1105,13 @@ class Mongo extends db_1.Db {
1108
1105
  return;
1109
1106
  let session = this.session;
1110
1107
  await session.commitTransaction();
1111
- db_1.log.info("transaction committed");
1108
+ db_js_1.log.info("transaction committed");
1112
1109
  session.endSession();
1113
1110
  this.session = undefined;
1114
- db_1.log.info("session ended");
1111
+ db_js_1.log.info("session ended");
1115
1112
  }
1116
1113
  catch (err) {
1117
- db_1.log.error(`commitTransaction error ${err.message || err}`);
1114
+ db_js_1.log.error(`commitTransaction error ${err.message || err}`);
1118
1115
  }
1119
1116
  }
1120
1117
  async abortTransaction() {
@@ -1125,13 +1122,13 @@ class Mongo extends db_1.Db {
1125
1122
  return;
1126
1123
  let session = this.session;
1127
1124
  await session.abortTransaction();
1128
- db_1.log.info("transaction aborted");
1125
+ db_js_1.log.info("transaction aborted");
1129
1126
  await session.endSession();
1130
1127
  this.session = undefined;
1131
- db_1.log.info("session ended");
1128
+ db_js_1.log.info("session ended");
1132
1129
  }
1133
1130
  catch (err) {
1134
- db_1.log.error(`abortTransaction error ${err.message || err}`);
1131
+ db_js_1.log.error(`abortTransaction error ${err.message || err}`);
1135
1132
  }
1136
1133
  }
1137
1134
  async _try_once(useTransaction, f, collection) {
@@ -1155,7 +1152,7 @@ class Mongo extends db_1.Db {
1155
1152
  if (useTransaction)
1156
1153
  await this.abortTransaction();
1157
1154
  }
1158
- catch ( /* intentionally */_a) { /* intentionally */ }
1155
+ catch { /* intentionally */ }
1159
1156
  throw err;
1160
1157
  }
1161
1158
  }
@@ -1168,9 +1165,9 @@ class Mongo extends db_1.Db {
1168
1165
  return await this._try_once(useTransaction, f, collection);
1169
1166
  }
1170
1167
  catch (err) {
1171
- db_1.log.error(`Mongo command has failed for ${this.db}.${collection} - ${(this.session ? "ROLLBACK - " : "")} ${err.message || err}`);
1172
- db_1.log.error(debugObject);
1173
- db_1.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);
1174
1171
  let x = (err || "").toString();
1175
1172
  console.log('x');
1176
1173
  let isRepeatable = x.match(/Topology is closed, please connect/i)
@@ -1179,35 +1176,36 @@ class Mongo extends db_1.Db {
1179
1176
  || x.match(/Topology closed/);
1180
1177
  if (isRepeatable) {
1181
1178
  try {
1182
- db_1.log.error("Trying to reopen connection and repeat as");
1179
+ db_js_1.log.error("Trying to reopen connection and repeat as");
1183
1180
  await this.close();
1184
1181
  // a single retry
1185
1182
  await super.connect();
1186
1183
  let ret = await this._try_once(useTransaction, f, collection);
1187
- db_1.log.error("OK - Retry succeeded.");
1188
- db_1.log.error("");
1184
+ db_js_1.log.error("OK - Retry succeeded.");
1185
+ db_js_1.log.error("");
1189
1186
  return ret;
1190
1187
  }
1191
1188
  catch (err2) {
1192
1189
  /* intentional */
1193
1190
  if (debugObject)
1194
- db_1.log.error(debugObject);
1195
- db_1.log.error(`FAIL - Retry failed: ${err2.message || err2}`);
1196
- db_1.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("");
1197
1194
  }
1198
1195
  }
1199
1196
  throw err;
1200
1197
  }
1201
1198
  }
1202
1199
  async _findLastSequenceForKey(connection, key) {
1200
+ var _a;
1203
1201
  let maxfld = await (connection
1204
- .find({}, { session: this.session })
1202
+ .find({}, this._sessionOpt())
1205
1203
  .sort({ [key]: -1 })
1206
1204
  .limit(1)
1207
1205
  .toArray());
1208
1206
  if (maxfld.length === 0)
1209
1207
  return undefined;
1210
- return parseInt(maxfld[0][key]) || 0;
1208
+ return parseInt((_a = maxfld === null || maxfld === void 0 ? void 0 : maxfld[0]) === null || _a === void 0 ? void 0 : _a[key]) || 0;
1211
1209
  }
1212
1210
  async _getNextCollectionUpdateSeqNo(collection, conn) {
1213
1211
  var _a;
@@ -1216,7 +1214,7 @@ class Mongo extends db_1.Db {
1216
1214
  returnDocument: "after",
1217
1215
  };
1218
1216
  let nextSeq = await (conn.db(this.db)
1219
- .collection(types_1.SEQUENCES_COLLECTION)
1217
+ .collection(types_js_1.SEQUENCES_COLLECTION)
1220
1218
  .findOneAndUpdate({ collection }, {
1221
1219
  $inc: { seq: 1 },
1222
1220
  $currentDate: { last: { $type: "date" }, ts: { $type: "timestamp" } }
@@ -1231,7 +1229,7 @@ class Mongo extends db_1.Db {
1231
1229
  return ((seqKeys === null || seqKeys === void 0 ? void 0 : seqKeys.length) > 0 || this.syncSupport) ? { seqKeys } : undefined;
1232
1230
  }
1233
1231
  async _processSequenceField(client, collection, insert, seqKeys) {
1234
- (0, assert_1.default)(this.client);
1232
+ assert(this.client);
1235
1233
  if (this.syncSupport) {
1236
1234
  insert._csq = (await this._getNextCollectionUpdateSeqNo(collection, client));
1237
1235
  }
@@ -1247,8 +1245,8 @@ class Mongo extends db_1.Db {
1247
1245
  return insert;
1248
1246
  }
1249
1247
  async _processSequenceFieldForMany(connection, collection, inserts) {
1250
- (0, assert_1.default)(this.client);
1251
- (0, assert_1.default)(connection);
1248
+ assert(this.client);
1249
+ assert(connection);
1252
1250
  if (!(inserts === null || inserts === void 0 ? void 0 : inserts.length))
1253
1251
  return;
1254
1252
  let seqKeys = this._findSequenceKeys(inserts[0]);
@@ -1261,7 +1259,7 @@ class Mongo extends db_1.Db {
1261
1259
  try {
1262
1260
  await this.createCollection(collection);
1263
1261
  }
1264
- catch ( /* intentionaly */_a) { /* intentionaly */ }
1262
+ catch { /* intentionaly */ }
1265
1263
  last = 0;
1266
1264
  }
1267
1265
  for (let insert of inserts) {
@@ -1343,7 +1341,7 @@ class Mongo extends db_1.Db {
1343
1341
  return toPublish;
1344
1342
  }
1345
1343
  emit(what) {
1346
- db_1.log.debug("emitting publish", what);
1344
+ db_js_1.log.debug("emitting publish", what);
1347
1345
  this.emitter.emit('publish', what);
1348
1346
  }
1349
1347
  async _writeAuditRecord(collection, operation, data, user = this.user, audit = this.audit) {
@@ -1354,7 +1352,7 @@ class Mongo extends db_1.Db {
1354
1352
  { $match: { entityid: Mongo._toId(data._id) } },
1355
1353
  { $sort: { rev: -1 } },
1356
1354
  { $limit: 1 }
1357
- ], { session: this.session })).toArray();
1355
+ ], this.session ? { session: this.session } : {})).toArray();
1358
1356
  let previousAuditRecord = previousAuditRecords.length ? previousAuditRecords[0] : { rev: 0, changes: {} };
1359
1357
  if (previousAuditRecords.length === 0)
1360
1358
  await this.createCollection(this.auditCollectionName);
@@ -1363,9 +1361,9 @@ class Mongo extends db_1.Db {
1363
1361
  let auditRecord = {
1364
1362
  db: this.db,
1365
1363
  collection: collection,
1366
- entityid: _1.Base.objectid(data._id),
1367
- rev: previousAuditRecord.rev + 1,
1368
- ts: _1.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(),
1369
1367
  on: new Date(),
1370
1368
  operation: operation,
1371
1369
  changes: dataNoId,
@@ -1374,11 +1372,14 @@ class Mongo extends db_1.Db {
1374
1372
  auditRecord.user = user;
1375
1373
  if (audit)
1376
1374
  auditRecord.audit = audit;
1377
- db_1.log.trace('AUDITING', auditRecord);
1375
+ db_js_1.log.trace('AUDITING', auditRecord);
1378
1376
  let ret = await client.db(this.db)
1379
1377
  .collection(this.auditCollectionName)
1380
- .insertOne(auditRecord, { session: this.session });
1381
- db_1.log.debug('AUDITED', auditRecord, ret.insertedId);
1378
+ .insertOne(auditRecord, this._sessionOpt());
1379
+ db_js_1.log.debug('AUDITED', auditRecord, ret.insertedId);
1380
+ }
1381
+ _sessionOpt() {
1382
+ return this.session ? { session: this.session } : {};
1382
1383
  }
1383
1384
  async _processUpdateObject(update) {
1384
1385
  await this._processHashedKeys(update);