cry-db 2.4.19 → 2.4.21

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.mjs CHANGED
@@ -70,6 +70,7 @@ export class Mongo extends Db {
70
70
  super(db, url);
71
71
  this.revisions = false;
72
72
  this.softdelete = false;
73
+ this.softarchive = false;
73
74
  this.syncSupport = false;
74
75
  this.session = undefined;
75
76
  this.emittingPublishEvents = false;
@@ -114,6 +115,17 @@ export class Mongo extends Db {
114
115
  usesSoftDelete() {
115
116
  return this.softdelete;
116
117
  }
118
+ /**
119
+ * Enable or disable archive filtering.
120
+ * When enabled, query operations exclude records with `_archived` set,
121
+ * unless `returnArchived: true` is passed in opts.
122
+ */
123
+ useArchive(enabled) {
124
+ return this.softarchive = !!enabled;
125
+ }
126
+ usesArchive() {
127
+ return this.softarchive;
128
+ }
117
129
  useAuditing(enabled) {
118
130
  return this.auditing = enabled;
119
131
  }
@@ -199,6 +211,10 @@ export class Mongo extends Db {
199
211
  if (!query._deleted)
200
212
  query._deleted = { $exists: false };
201
213
  }
214
+ if (this.softarchive && !opts.returnArchived) {
215
+ if (!query._archived)
216
+ query._archived = { $exists: false };
217
+ }
202
218
  fjLog.debug('count called', collection, query, opts);
203
219
  let ret = await this.executeTransactionally(collection, async (conn) => {
204
220
  return await conn.countDocuments(query, opts);
@@ -213,6 +229,10 @@ export class Mongo extends Db {
213
229
  if (!query._deleted)
214
230
  query._deleted = { $exists: false };
215
231
  }
232
+ if (this.softarchive && !opts.returnArchived) {
233
+ if (!query._archived)
234
+ query._archived = { $exists: false };
235
+ }
216
236
  fjLog.debug('find called', collection, query, opts);
217
237
  let ret = await this.executeTransactionally(collection, async (conn) => {
218
238
  let r = this._applyQueryOpts(conn.find(query, this._buildFindOptions(opts)), opts);
@@ -237,6 +257,10 @@ export class Mongo extends Db {
237
257
  if (!query._deleted)
238
258
  query._deleted = { $exists: false };
239
259
  }
260
+ if (this.softarchive && !opts.returnArchived) {
261
+ if (!query._archived)
262
+ query._archived = { $exists: false };
263
+ }
240
264
  fjLog.debug('findNewer called', collection, timestamp, query, opts);
241
265
  let ret = await this.executeTransactionally(collection, async (conn) => {
242
266
  let r = this._applyQueryOpts(conn.find(query, this._buildFindOptions(opts)).sort({ _ts: 1 }), opts);
@@ -245,18 +269,22 @@ export class Mongo extends Db {
245
269
  fjLog.debug('findNewer returns', ret);
246
270
  return ret;
247
271
  }
248
- async findNewerMany(spec = []) {
272
+ async findNewerMany(spec = [], defaultOpts = {}) {
249
273
  var _a;
250
274
  fjLog.debug('findNewerMany called', spec);
251
275
  const dbName = this.db; // Capture db at operation start
252
276
  let conn = await this.connect();
253
277
  const getOneColl = async (coll) => {
254
278
  let query = this._createQueryForNewer(coll.timestamp, coll.query || {});
255
- const opts = coll.opts || {};
279
+ const opts = { ...defaultOpts, ...coll.opts };
256
280
  if (this.softdelete && !opts.returnDeleted) {
257
281
  if (!query._deleted)
258
282
  query._deleted = { $exists: false };
259
283
  }
284
+ if (this.softarchive && !opts.returnArchived) {
285
+ if (!query._archived)
286
+ query._archived = { $exists: false };
287
+ }
260
288
  if (process.env.MONGO_DEBUG_FINDNEWERMANY) {
261
289
  fjLog.debug("findNewerMany <-", coll.collection, coll.timestamp, coll.query, " -> ", JSON.stringify(query));
262
290
  }
@@ -406,6 +434,10 @@ export class Mongo extends Db {
406
434
  if (!query._deleted)
407
435
  query._deleted = { $exists: false };
408
436
  }
437
+ if (this.softarchive && !opts.returnArchived) {
438
+ if (!query._archived)
439
+ query._archived = { $exists: false };
440
+ }
409
441
  // if (!query._blocked) query._blocked = { $exists: false }; // intentionally - blocked records are returned
410
442
  fjLog.debug('findOne called', collection, query, projection);
411
443
  let ret = await this.executeTransactionally(collection, async (conn) => await conn.findOne(query, { ...(projection ? { projection } : {}), ...this._sessionOpt() }), false, { operation: "findOne", collection, query, projection });
@@ -420,16 +452,19 @@ export class Mongo extends Db {
420
452
  const dbName = this.db; // Capture db at operation start
421
453
  let query = {
422
454
  _id: Mongo._toId(id),
423
- // _deleted: { $exists: false }
424
455
  };
456
+ if (this.softdelete && !opts.returnDeleted) {
457
+ query._deleted = { $exists: false };
458
+ }
459
+ if (this.softarchive && !opts.returnArchived) {
460
+ query._archived = { $exists: false };
461
+ }
425
462
  fjLog.debug('findById called', dbName, collection, id, projection);
426
463
  fjLog.trace('findById executing with query', collection, query, projection);
427
464
  let ret = await this.executeTransactionally(collection, async (conn) => {
428
465
  let r = await conn.findOne(query, { ...(projection ? { projection } : {}), ...this._sessionOpt() });
429
466
  return r;
430
467
  }, false, { operation: "findById", collection, id, projection });
431
- if ((ret === null || ret === void 0 ? void 0 : ret._deleted) && this.softdelete && !opts.returnDeleted)
432
- ret = null;
433
468
  fjLog.debug('findById returns', ret);
434
469
  return this._processReturnedObject(ret);
435
470
  }
@@ -450,6 +485,9 @@ export class Mongo extends Db {
450
485
  if (this.softdelete && !opts.returnDeleted) {
451
486
  query._deleted = { $exists: false };
452
487
  }
488
+ if (this.softarchive && !opts.returnArchived) {
489
+ query._archived = { $exists: false };
490
+ }
453
491
  let r = conn.find(query, { ...(projection ? { projection } : {}), ...this._sessionOpt() });
454
492
  return await r.toArray();
455
493
  }, false, { operation: "findByIdsInManyCollections", collection, ids, projection });
@@ -476,6 +514,9 @@ export class Mongo extends Db {
476
514
  if (this.softdelete && !opts.returnDeleted) {
477
515
  query._deleted = { $exists: false };
478
516
  }
517
+ if (this.softarchive && !opts.returnArchived) {
518
+ query._archived = { $exists: false };
519
+ }
479
520
  let r = conn.find(query, { ...(projection ? { projection } : {}), ...this._sessionOpt() });
480
521
  return await r.toArray();
481
522
  }, false, { operation: "findByIds", collection, ids, projection });
@@ -1067,6 +1108,158 @@ export class Mongo extends Db {
1067
1108
  fjLog.debug('unblockOne returns', ret);
1068
1109
  return ret;
1069
1110
  }
1111
+ /**
1112
+ * Archives a single document by setting `_archived` to the current date.
1113
+ * Increments `_rev` and updates `_ts`.
1114
+ */
1115
+ async archiveOne(collection, query) {
1116
+ let opts = {
1117
+ upsert: false,
1118
+ returnDocument: "after",
1119
+ ...this._sessionOpt()
1120
+ };
1121
+ const dbName = this.db;
1122
+ query = this.replaceIds(query);
1123
+ fjLog.debug('archiveOne called', collection, query);
1124
+ let ret = await this.executeTransactionally(collection, async (conn) => {
1125
+ query._archived = { $exists: false };
1126
+ let update = {
1127
+ $set: { _archived: new Date(), },
1128
+ $inc: { _rev: 1, },
1129
+ $currentDate: { _ts: { $type: 'timestamp' } }
1130
+ };
1131
+ let obj = await conn.findOneAndUpdate(query, update, opts);
1132
+ if (!obj || !(obj === null || obj === void 0 ? void 0 : obj._id))
1133
+ return {
1134
+ ok: false
1135
+ };
1136
+ await this._publishAndAudit('update', dbName, collection, obj);
1137
+ return obj;
1138
+ }, false, { operation: "archiveOne", collection, query });
1139
+ fjLog.debug('archiveOne returns', ret);
1140
+ return ret;
1141
+ }
1142
+ /**
1143
+ * Unarchives a single document by removing the `_archived` field.
1144
+ * Increments `_rev` and updates `_ts`.
1145
+ */
1146
+ async unarchiveOne(collection, query) {
1147
+ let opts = {
1148
+ upsert: false,
1149
+ returnDocument: "after",
1150
+ ...this._sessionOpt()
1151
+ };
1152
+ const dbName = this.db;
1153
+ query = this.replaceIds(query);
1154
+ fjLog.debug('unarchiveOne called', collection, query);
1155
+ let ret = await this.executeTransactionally(collection, async (conn) => {
1156
+ query._archived = { $exists: true };
1157
+ let update = {
1158
+ $unset: { _archived: 1 },
1159
+ $inc: { _rev: 1, },
1160
+ $currentDate: { _ts: { $type: 'timestamp' } }
1161
+ };
1162
+ let obj = await conn.findOneAndUpdate(query, update, opts);
1163
+ if (!obj || !(obj === null || obj === void 0 ? void 0 : obj._id))
1164
+ return {
1165
+ ok: false
1166
+ };
1167
+ await this._publishAndAudit('update', dbName, collection, obj);
1168
+ return obj;
1169
+ }, false, { operation: "unarchiveOne", collection, query });
1170
+ fjLog.debug('unarchiveOne returns', ret);
1171
+ return ret;
1172
+ }
1173
+ /**
1174
+ * Archives a single document found by its `_id`.
1175
+ */
1176
+ async archiveOneById(collection, id) {
1177
+ assert(collection);
1178
+ assert(id);
1179
+ return await this.archiveOne(collection, { _id: Mongo._toId(id) });
1180
+ }
1181
+ /**
1182
+ * Archives multiple documents found by their `_id`s.
1183
+ * Returns the array of archived documents.
1184
+ */
1185
+ async archiveManyByIds(collection, ids) {
1186
+ assert(collection);
1187
+ assert(ids);
1188
+ if (!ids || ids.length === 0)
1189
+ return [];
1190
+ const opts = {
1191
+ upsert: false,
1192
+ returnDocument: "after",
1193
+ ...this._sessionOpt()
1194
+ };
1195
+ const dbName = this.db;
1196
+ fjLog.debug('archiveManyByIds called', collection, ids);
1197
+ let ret = [];
1198
+ for (const id of ids) {
1199
+ let r = await this.executeTransactionally(collection, async (conn) => {
1200
+ let query = { _id: Mongo._toId(id), _archived: { $exists: false } };
1201
+ let update = {
1202
+ $set: { _archived: new Date() },
1203
+ $inc: { _rev: 1 },
1204
+ $currentDate: { _ts: { $type: 'timestamp' } }
1205
+ };
1206
+ let obj = await conn.findOneAndUpdate(query, update, opts);
1207
+ if (obj === null || obj === void 0 ? void 0 : obj._id) {
1208
+ await this._publishAndAudit('update', dbName, collection, obj);
1209
+ }
1210
+ return obj;
1211
+ }, false, { operation: "archiveManyByIds", collection, id });
1212
+ if (r === null || r === void 0 ? void 0 : r._id)
1213
+ ret.push(r);
1214
+ }
1215
+ fjLog.debug('archiveManyByIds returns', ret);
1216
+ return ret;
1217
+ }
1218
+ /**
1219
+ * Unarchives a single document found by its `_id`.
1220
+ */
1221
+ async unarchiveOneById(collection, id) {
1222
+ assert(collection);
1223
+ assert(id);
1224
+ return await this.unarchiveOne(collection, { _id: Mongo._toId(id) });
1225
+ }
1226
+ /**
1227
+ * Unarchives multiple documents found by their `_id`s.
1228
+ * Returns the array of unarchived documents.
1229
+ */
1230
+ async unarchiveManyByIds(collection, ids) {
1231
+ assert(collection);
1232
+ assert(ids);
1233
+ if (!ids || ids.length === 0)
1234
+ return [];
1235
+ const opts = {
1236
+ upsert: false,
1237
+ returnDocument: "after",
1238
+ ...this._sessionOpt()
1239
+ };
1240
+ const dbName = this.db;
1241
+ fjLog.debug('unarchiveManyByIds called', collection, ids);
1242
+ let ret = [];
1243
+ for (const id of ids) {
1244
+ let r = await this.executeTransactionally(collection, async (conn) => {
1245
+ let query = { _id: Mongo._toId(id), _archived: { $exists: true } };
1246
+ let update = {
1247
+ $unset: { _archived: 1 },
1248
+ $inc: { _rev: 1 },
1249
+ $currentDate: { _ts: { $type: 'timestamp' } }
1250
+ };
1251
+ let obj = await conn.findOneAndUpdate(query, update, opts);
1252
+ if (obj === null || obj === void 0 ? void 0 : obj._id) {
1253
+ await this._publishAndAudit('update', dbName, collection, obj);
1254
+ }
1255
+ return obj;
1256
+ }, false, { operation: "unarchiveManyByIds", collection, id });
1257
+ if (r === null || r === void 0 ? void 0 : r._id)
1258
+ ret.push(r);
1259
+ }
1260
+ fjLog.debug('unarchiveManyByIds returns', ret);
1261
+ return ret;
1262
+ }
1070
1263
  async hardDeleteOne(collection, query) {
1071
1264
  assert(collection);
1072
1265
  assert(query);