@naturalcycles/db-lib 10.60.0 → 10.61.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/adapter/cachedb/cache.db.d.ts +2 -2
  2. package/dist/adapter/file/file.db.d.ts +6 -6
  3. package/dist/adapter/file/file.db.js +2 -2
  4. package/dist/adapter/file/file.db.model.d.ts +1 -1
  5. package/dist/adapter/file/inMemory.persistence.plugin.d.ts +1 -1
  6. package/dist/adapter/file/localFile.persistence.plugin.d.ts +2 -2
  7. package/dist/adapter/file/noop.persistence.plugin.d.ts +1 -1
  8. package/dist/commondao/common.dao.d.ts +24 -18
  9. package/dist/commondao/common.dao.js +43 -28
  10. package/dist/commondao/commonDaoTransaction.d.ts +3 -3
  11. package/dist/commondb/base.common.db.d.ts +3 -3
  12. package/dist/commondb/common.db.d.ts +3 -3
  13. package/dist/db.model.d.ts +2 -2
  14. package/dist/inmemory/inMemory.db.d.ts +6 -6
  15. package/dist/inmemory/inMemoryKeyValueDB.d.ts +4 -4
  16. package/dist/inmemory/queryInMemory.js +1 -1
  17. package/dist/kv/commonKeyValueDB.d.ts +4 -4
  18. package/dist/kv/commonKeyValueDao.d.ts +5 -5
  19. package/dist/query/dbQuery.d.ts +4 -4
  20. package/dist/query/dbQuery.js +2 -2
  21. package/dist/timeseries/commonTimeSeriesDao.d.ts +3 -3
  22. package/dist/transaction/dbTransaction.util.d.ts +3 -3
  23. package/package.json +1 -1
  24. package/src/adapter/cachedb/cache.db.ts +2 -2
  25. package/src/adapter/file/file.db.model.ts +1 -1
  26. package/src/adapter/file/file.db.ts +9 -7
  27. package/src/adapter/file/inMemory.persistence.plugin.ts +1 -1
  28. package/src/adapter/file/localFile.persistence.plugin.ts +2 -2
  29. package/src/adapter/file/noop.persistence.plugin.ts +1 -1
  30. package/src/commondao/common.dao.ts +73 -48
  31. package/src/commondao/commonDaoTransaction.ts +3 -3
  32. package/src/commondb/base.common.db.ts +10 -3
  33. package/src/commondb/common.db.ts +7 -3
  34. package/src/db.model.ts +2 -2
  35. package/src/inmemory/inMemory.db.ts +10 -6
  36. package/src/inmemory/inMemoryKeyValueDB.ts +7 -4
  37. package/src/inmemory/queryInMemory.ts +5 -5
  38. package/src/kv/commonKeyValueDB.ts +4 -4
  39. package/src/kv/commonKeyValueDao.ts +5 -5
  40. package/src/query/dbQuery.ts +6 -6
  41. package/src/timeseries/commonTimeSeriesDao.ts +3 -3
  42. package/src/transaction/dbTransaction.util.ts +3 -3
@@ -11,13 +11,13 @@ export declare class CommonTimeSeriesDao {
11
11
  ping(): Promise<void>;
12
12
  getSeries(): Promise<string[]>;
13
13
  save(series: string, tsMillis: number, value: number): Promise<void>;
14
- saveBatch(series: string, dataPoints: TimeSeriesDataPoint[]): Promise<void>;
14
+ saveBatch(series: string, dataPoints: readonly TimeSeriesDataPoint[]): Promise<void>;
15
15
  /**
16
16
  * All ops are executed as a single CommonDB Transaction.
17
17
  */
18
- commitTransaction(ops: TimeSeriesSaveBatchOp[]): Promise<void>;
18
+ commitTransaction(ops: readonly TimeSeriesSaveBatchOp[]): Promise<void>;
19
19
  deleteById(series: string, tsMillis: number): Promise<void>;
20
- deleteByIds(series: string, ids: number[]): Promise<void>;
20
+ deleteByIds(series: string, ids: readonly number[]): Promise<void>;
21
21
  query(q: TimeSeriesQuery): Promise<TimeSeriesDataPoint[]>;
22
22
  optimize(): Promise<void>;
23
23
  }
@@ -23,7 +23,7 @@ export declare class FakeDBTransaction implements DBTransaction {
23
23
  constructor(db: CommonDB);
24
24
  commit(): Promise<void>;
25
25
  rollback(): Promise<void>;
26
- getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>;
27
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
28
- deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number>;
26
+ getByIds<ROW extends ObjectWithId>(table: string, ids: readonly string[], opt?: CommonDBOptions): Promise<ROW[]>;
27
+ saveBatch<ROW extends ObjectWithId>(table: string, rows: readonly ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
28
+ deleteByIds(table: string, ids: readonly string[], opt?: CommonDBOptions): Promise<number>;
29
29
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
3
  "type": "module",
4
- "version": "10.60.0",
4
+ "version": "10.61.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@naturalcycles/nodejs-lib": "^15"
@@ -78,7 +78,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
78
78
 
79
79
  override async getByIds<ROW extends ObjectWithId>(
80
80
  table: string,
81
- ids: string[],
81
+ ids: readonly string[],
82
82
  opt: CacheDBSaveOptions<ROW> = {},
83
83
  ): Promise<ROW[]> {
84
84
  const resultMap: StringMap<ROW> = {}
@@ -124,7 +124,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
124
124
 
125
125
  override async saveBatch<ROW extends ObjectWithId>(
126
126
  table: string,
127
- rows: ROW[],
127
+ rows: readonly ROW[],
128
128
  opt: CacheDBSaveOptions<ROW> = {},
129
129
  ): Promise<void> {
130
130
  if (!opt.onlyCache && !this.cfg.onlyCache) {
@@ -7,7 +7,7 @@ export interface FileDBPersistencePlugin {
7
7
  ping: () => Promise<void>
8
8
  getTables: () => Promise<string[]>
9
9
  loadFile: <ROW extends ObjectWithId>(table: string) => Promise<ROW[]>
10
- saveFiles: (ops: DBSaveBatchOperation<any>[]) => Promise<void>
10
+ saveFiles: (ops: readonly DBSaveBatchOperation<any>[]) => Promise<void>
11
11
  }
12
12
 
13
13
  export interface FileDBCfg {
@@ -72,7 +72,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
72
72
 
73
73
  override async getByIds<ROW extends ObjectWithId>(
74
74
  table: string,
75
- ids: string[],
75
+ ids: readonly string[],
76
76
  _opt?: CommonDBOptions,
77
77
  ): Promise<ROW[]> {
78
78
  const byId = _by(await this.loadFile<ROW>(table), r => r.id)
@@ -81,7 +81,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
81
81
 
82
82
  override async saveBatch<ROW extends ObjectWithId>(
83
83
  table: string,
84
- rows: ROW[],
84
+ rows: readonly ROW[],
85
85
  _opt?: CommonDBSaveOptions<ROW>,
86
86
  ): Promise<void> {
87
87
  if (!rows.length) return // save some api calls
@@ -154,7 +154,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
154
154
 
155
155
  override async deleteByIds(
156
156
  table: string,
157
- ids: string[],
157
+ ids: readonly string[],
158
158
  _opt?: CommonDBOptions,
159
159
  ): Promise<number> {
160
160
  const byId = _by(await this.loadFile(table), r => r.id)
@@ -190,7 +190,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
190
190
  }
191
191
 
192
192
  // wrapper, to handle logging, sorting rows before saving
193
- async saveFile<ROW extends ObjectWithId>(table: string, _rows: ROW[]): Promise<void> {
193
+ async saveFile<ROW extends ObjectWithId>(table: string, _rows: readonly ROW[]): Promise<void> {
194
194
  // if (!_rows.length) return // NO, it should be able to save file with 0 rows!
195
195
 
196
196
  // Sort the rows, if needed
@@ -202,7 +202,9 @@ export class FileDB extends BaseCommonDB implements CommonDB {
202
202
  this.logFinished(started, op)
203
203
  }
204
204
 
205
- async saveFiles<ROW extends ObjectWithId>(ops: DBSaveBatchOperation<ROW>[]): Promise<void> {
205
+ async saveFiles<ROW extends ObjectWithId>(
206
+ ops: readonly DBSaveBatchOperation<ROW>[],
207
+ ): Promise<void> {
206
208
  if (!ops.length) return
207
209
  const op =
208
210
  `saveFiles ${ops.length} op(s):\n` + ops.map(o => `${o.table} (${o.rows.length})`).join('\n')
@@ -215,8 +217,8 @@ export class FileDB extends BaseCommonDB implements CommonDB {
215
217
  // return new FileDBTransaction(this)
216
218
  // }
217
219
 
218
- sortRows<ROW extends ObjectWithId>(rows: ROW[]): ROW[] {
219
- rows = rows.map(r => _filterUndefinedValues(r))
220
+ sortRows<ROW extends ObjectWithId>(inputRows: readonly ROW[]): ROW[] {
221
+ const rows = inputRows.map(r => _filterUndefinedValues(r))
220
222
 
221
223
  if (this.cfg.sortOnSave) {
222
224
  _sortBy(rows, r => r[this.cfg.sortOnSave!.name as keyof ROW] as string, { mutate: true })
@@ -23,7 +23,7 @@ export class InMemoryPersistencePlugin implements FileDBPersistencePlugin {
23
23
  return Object.values(this.data[table] || ({} as any))
24
24
  }
25
25
 
26
- async saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void> {
26
+ async saveFiles(ops: readonly DBSaveBatchOperation<any>[]): Promise<void> {
27
27
  ops.forEach(op => {
28
28
  this.data[op.table] = _by(op.rows, r => r.id)
29
29
  })
@@ -49,11 +49,11 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
49
49
  return await Pipeline.fromNDJsonFile<ROW>(filePath).toArray()
50
50
  }
51
51
 
52
- async saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void> {
52
+ async saveFiles(ops: readonly DBSaveBatchOperation<any>[]): Promise<void> {
53
53
  await pMap(ops, async op => await this.saveFile(op.table, op.rows), { concurrency: 32 })
54
54
  }
55
55
 
56
- async saveFile<ROW extends ObjectWithId>(table: string, rows: ROW[]): Promise<void> {
56
+ async saveFile<ROW extends ObjectWithId>(table: string, rows: readonly ROW[]): Promise<void> {
57
57
  await fs2.ensureDirAsync(this.cfg.storagePath)
58
58
  const ext = `ndjson${this.cfg.zst ? '.zst' : ''}`
59
59
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`
@@ -13,5 +13,5 @@ export class NoopPersistencePlugin implements FileDBPersistencePlugin {
13
13
  return []
14
14
  }
15
15
 
16
- async saveFiles(_ops: DBSaveBatchOperation<any>[]): Promise<void> {}
16
+ async saveFiles(_ops: readonly DBSaveBatchOperation<any>[]): Promise<void> {}
17
17
  }
@@ -141,18 +141,18 @@ export class CommonDao<
141
141
  return this.anyToDBM(row, opt)
142
142
  }
143
143
 
144
- async getByIds(ids: ID[], opt: CommonDaoReadOptions = {}): Promise<BM[]> {
144
+ async getByIds(ids: readonly ID[], opt: CommonDaoReadOptions = {}): Promise<BM[]> {
145
145
  const dbms = await this.loadByIds(ids, opt)
146
146
  return this.dbmsToBM(dbms, opt)
147
147
  }
148
148
 
149
- async getByIdsAsDBM(ids: ID[], opt: CommonDaoReadOptions = {}): Promise<DBM[]> {
149
+ async getByIdsAsDBM(ids: readonly ID[], opt: CommonDaoReadOptions = {}): Promise<DBM[]> {
150
150
  const rows = await this.loadByIds(ids, opt)
151
151
  return this.anyToDBMs(rows)
152
152
  }
153
153
 
154
154
  // DRY private method
155
- private async loadByIds(ids: ID[], opt: CommonDaoReadOptions = {}): Promise<DBM[]> {
155
+ private async loadByIds(ids: readonly ID[], opt: CommonDaoReadOptions = {}): Promise<DBM[]> {
156
156
  if (!ids.length) return []
157
157
  const table = opt.table || this.cfg.table
158
158
  const rows = await (opt.tx || this.cfg.db).getByIds<DBM>(table, ids, opt)
@@ -203,7 +203,10 @@ export class CommonDao<
203
203
  * Does deduplication by id.
204
204
  * Order is not guaranteed, as queries run in parallel.
205
205
  */
206
- async runUnionQueries(queries: DBQuery<DBM>[], opt?: CommonDaoReadOptions): Promise<BM[]> {
206
+ async runUnionQueries(
207
+ queries: readonly DBQuery<DBM>[],
208
+ opt?: CommonDaoReadOptions,
209
+ ): Promise<BM[]> {
207
210
  const results = (
208
211
  await pMap(queries, async q => (await this.runQueryExtended(q, opt)).rows)
209
212
  ).flat()
@@ -214,8 +217,7 @@ export class CommonDao<
214
217
  q: DBQuery<DBM>,
215
218
  opt: CommonDaoReadOptions = {},
216
219
  ): Promise<RunQueryResult<BM>> {
217
- this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
218
- q.table = opt.table || q.table
220
+ q = this.prepareQuery(q, opt) // throws if query uses `excludeFromIndexes` property
219
221
  const { rows: rawRows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt)
220
222
  const isPartialQuery = !!q._selectedFieldNames
221
223
  const rows = isPartialQuery ? rawRows : this.storageRowsToDBM(rawRows)
@@ -235,8 +237,7 @@ export class CommonDao<
235
237
  q: DBQuery<DBM>,
236
238
  opt: CommonDaoReadOptions = {},
237
239
  ): Promise<RunQueryResult<DBM>> {
238
- this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
239
- q.table = opt.table || q.table
240
+ q = this.prepareQuery(q, opt) // throws if query uses `excludeFromIndexes` property
240
241
  const { rows: rawRows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt)
241
242
  const isPartialQuery = !!q._selectedFieldNames
242
243
  const rows = isPartialQuery ? rawRows : this.storageRowsToDBM(rawRows)
@@ -245,14 +246,12 @@ export class CommonDao<
245
246
  }
246
247
 
247
248
  async runQueryCount(q: DBQuery<DBM>, opt: CommonDaoReadOptions = {}): Promise<number> {
248
- this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
249
- q.table = opt.table || q.table
249
+ q = this.prepareQuery(q, opt) // throws if query uses `excludeFromIndexes` property
250
250
  return await this.cfg.db.runQueryCount(q, opt)
251
251
  }
252
252
 
253
253
  streamQueryAsDBM(q: DBQuery<DBM>, opt: CommonDaoStreamOptions<DBM> = {}): Pipeline<DBM> {
254
- this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
255
- q.table = opt.table || q.table
254
+ q = this.prepareQuery(q, opt) // throws if query uses `excludeFromIndexes` property
256
255
  let pipeline = this.cfg.db.streamQuery<DBM>(q, opt)
257
256
 
258
257
  if (this.cfg.compress?.keys.length) {
@@ -262,15 +261,17 @@ export class CommonDao<
262
261
  const isPartialQuery = !!q._selectedFieldNames
263
262
  if (isPartialQuery) return pipeline
264
263
 
265
- opt.skipValidation ??= true
266
- opt.errorMode ||= ErrorMode.SUPPRESS
264
+ opt = {
265
+ ...opt,
266
+ skipValidation: opt.skipValidation ?? true,
267
+ errorMode: opt.errorMode || ErrorMode.SUPPRESS,
268
+ }
267
269
 
268
270
  return pipeline.mapSync(dbm => this.anyToDBM(dbm, opt), { errorMode: opt.errorMode })
269
271
  }
270
272
 
271
273
  streamQuery(q: DBQuery<DBM>, opt: CommonDaoStreamOptions<BM> = {}): Pipeline<BM> {
272
- this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
273
- q.table = opt.table || q.table
274
+ q = this.prepareQuery(q, opt) // throws if query uses `excludeFromIndexes` property
274
275
  let pipeline = this.cfg.db.streamQuery<DBM>(q, opt)
275
276
 
276
277
  if (this.cfg.compress?.keys.length) {
@@ -280,25 +281,28 @@ export class CommonDao<
280
281
  const isPartialQuery = !!q._selectedFieldNames
281
282
  if (isPartialQuery) return pipeline as any as Pipeline<BM>
282
283
 
283
- opt.skipValidation ??= true
284
- opt.errorMode ||= ErrorMode.SUPPRESS
284
+ opt = {
285
+ ...opt,
286
+ skipValidation: opt.skipValidation ?? true,
287
+ errorMode: opt.errorMode || ErrorMode.SUPPRESS,
288
+ }
285
289
 
286
290
  return pipeline.mapSync(dbm => this.dbmToBM(dbm, opt), { errorMode: opt.errorMode })
287
291
  }
288
292
 
289
293
  async queryIds(q: DBQuery<DBM>, opt: CommonDaoReadOptions = {}): Promise<ID[]> {
290
- this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
291
- q.table = opt.table || q.table
292
- const { rows } = await this.cfg.db.runQuery(q.select(['id']), opt)
294
+ q = this.prepareQuery(q, opt) // throws if query uses `excludeFromIndexes` property
295
+ const { rows } = await this.cfg.db.runQuery(q.clone().select(['id']), opt)
293
296
  return rows.map(r => r.id as ID)
294
297
  }
295
298
 
296
299
  streamQueryIds(q: DBQuery<DBM>, opt: CommonDaoStreamOptions<ID> = {}): Pipeline<ID> {
297
- this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
298
- q.table = opt.table || q.table
299
- opt.errorMode ||= ErrorMode.SUPPRESS
300
+ q = this.prepareQuery(q, opt) // throws if query uses `excludeFromIndexes` property
301
+ opt = { ...opt, errorMode: opt.errorMode || ErrorMode.SUPPRESS }
300
302
 
301
- return this.cfg.db.streamQuery(q.select(['id']), opt).mapSync((r: ObjectWithId) => r.id as ID)
303
+ return this.cfg.db
304
+ .streamQuery(q.clone().select(['id']), opt)
305
+ .mapSync((r: ObjectWithId) => r.id as ID)
302
306
  }
303
307
 
304
308
  /**
@@ -501,7 +505,10 @@ export class CommonDao<
501
505
  return validDbm
502
506
  }
503
507
 
504
- async saveBatch(bms: Unsaved<BM>[], opt: CommonDaoSaveBatchOptions<DBM> = {}): Promise<BM[]> {
508
+ async saveBatch(
509
+ bms: readonly Unsaved<BM>[],
510
+ opt: CommonDaoSaveBatchOptions<DBM> = {},
511
+ ): Promise<BM[]> {
505
512
  if (!bms.length) return []
506
513
  this.requireWriteAccess()
507
514
  bms.forEach(bm => this.assignIdCreatedUpdated(bm, opt))
@@ -523,7 +530,7 @@ export class CommonDao<
523
530
  }
524
531
 
525
532
  async saveBatchAsDBM(
526
- dbms: Unsaved<DBM>[],
533
+ dbms: readonly Unsaved<DBM>[],
527
534
  opt: CommonDaoSaveBatchOptions<DBM> = {},
528
535
  ): Promise<DBM[]> {
529
536
  if (!dbms.length) return []
@@ -590,8 +597,11 @@ export class CommonDao<
590
597
  this.requireWriteAccess()
591
598
 
592
599
  const table = opt.table || this.cfg.table
593
- opt.skipValidation ??= true
594
- opt.errorMode ||= ErrorMode.SUPPRESS
600
+ opt = {
601
+ ...opt,
602
+ skipValidation: opt.skipValidation ?? true,
603
+ errorMode: opt.errorMode || ErrorMode.SUPPRESS,
604
+ }
595
605
 
596
606
  const saveOptions = this.prepareSaveOptions(opt)
597
607
  const { beforeSave } = this.cfg.hooks!
@@ -635,7 +645,7 @@ export class CommonDao<
635
645
  return await this.deleteByIds([id], opt)
636
646
  }
637
647
 
638
- async deleteByIds(ids: ID[], opt: CommonDaoOptions = {}): Promise<number> {
648
+ async deleteByIds(ids: readonly ID[], opt: CommonDaoOptions = {}): Promise<number> {
639
649
  if (!ids.length) return 0
640
650
  this.requireWriteAccess()
641
651
  this.requireObjectMutability(opt)
@@ -652,17 +662,16 @@ export class CommonDao<
652
662
  q: DBQuery<DBM>,
653
663
  opt: CommonDaoStreamDeleteOptions<DBM> = {},
654
664
  ): Promise<number> {
655
- this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
665
+ q = this.prepareQuery(q, opt) // throws if query uses `excludeFromIndexes` property
656
666
  this.requireWriteAccess()
657
667
  this.requireObjectMutability(opt)
658
- q.table = opt.table || q.table
659
668
  let deleted = 0
660
669
 
661
670
  if (opt.chunkSize) {
662
671
  const { chunkSize, chunkConcurrency = 8 } = opt
663
672
 
664
673
  await this.cfg.db
665
- .streamQuery<DBM>(q.select(['id']), opt)
674
+ .streamQuery<DBM>(q.clone().select(['id']), opt)
666
675
  .mapSync(r => r.id)
667
676
  .chunk(chunkSize)
668
677
  .map(
@@ -691,7 +700,11 @@ export class CommonDao<
691
700
  return deleted
692
701
  }
693
702
 
694
- async patchByIds(ids: ID[], patch: Partial<DBM>, opt: CommonDaoOptions = {}): Promise<number> {
703
+ async patchByIds(
704
+ ids: readonly ID[],
705
+ patch: Partial<DBM>,
706
+ opt: CommonDaoOptions = {},
707
+ ): Promise<number> {
695
708
  if (!ids.length) return 0
696
709
  this.assertNotCompressed(Object.keys(patch), 'patchByIds')
697
710
  return await this.patchByQuery(this.query().filterIn('id', ids), patch, opt)
@@ -702,11 +715,10 @@ export class CommonDao<
702
715
  patch: Partial<DBM>,
703
716
  opt: CommonDaoOptions = {},
704
717
  ): Promise<number> {
705
- this.validateQuery(q) // throws if query uses `excludeFromIndexes` property
718
+ q = this.prepareQuery(q, opt) // throws if query uses `excludeFromIndexes` property
706
719
  this.assertNotCompressed(Object.keys(patch), 'patchByQuery')
707
720
  this.requireWriteAccess()
708
721
  this.requireObjectMutability(opt)
709
- q.table = opt.table || q.table
710
722
  return await this.cfg.db.patchByQuery(q, patch, opt)
711
723
  }
712
724
 
@@ -761,7 +773,7 @@ export class CommonDao<
761
773
  return this.validateAndConvert(bm, 'load', opt)
762
774
  }
763
775
 
764
- dbmsToBM(dbms: DBM[], opt: CommonDaoOptions = {}): BM[] {
776
+ dbmsToBM(dbms: readonly DBM[], opt: CommonDaoOptions = {}): BM[] {
765
777
  return dbms.map(dbm => this.dbmToBM(dbm, opt))
766
778
  }
767
779
 
@@ -783,7 +795,7 @@ export class CommonDao<
783
795
  return dbm
784
796
  }
785
797
 
786
- bmsToDBM(bms: BM[], opt: CommonDaoOptions = {}): DBM[] {
798
+ bmsToDBM(bms: readonly BM[], opt: CommonDaoOptions = {}): DBM[] {
787
799
  return bms.map(bm => this.bmToDBM(bm, opt))
788
800
  }
789
801
 
@@ -812,7 +824,7 @@ export class CommonDao<
812
824
  /**
813
825
  * Converts multiple DBMs to storage rows.
814
826
  */
815
- async dbmsToStorageRows(dbms: DBM[]): Promise<ObjectWithId[]> {
827
+ async dbmsToStorageRows(dbms: readonly DBM[]): Promise<readonly ObjectWithId[]> {
816
828
  if (!this.cfg.compress?.keys.length) return dbms
817
829
  return await pMap(dbms, async dbm => {
818
830
  const row = { ...dbm }
@@ -840,7 +852,7 @@ export class CommonDao<
840
852
  /**
841
853
  * Converts multiple storage rows to DBMs.
842
854
  */
843
- storageRowsToDBM(rows: ObjectWithId[]): DBM[] {
855
+ storageRowsToDBM(rows: readonly ObjectWithId[]): DBM[] {
844
856
  if (!this.cfg.compress?.keys.length) return rows as DBM[]
845
857
  return rows.map(row => {
846
858
  const dbm = { ...(row as DBM) }
@@ -883,7 +895,7 @@ export class CommonDao<
883
895
  Object.assign(dbm, properties)
884
896
  }
885
897
 
886
- private assertNotCompressed(keys: (keyof DBM | string)[] | undefined, op: string): void {
898
+ private assertNotCompressed(keys: readonly (keyof DBM | string)[] | undefined, op: string): void {
887
899
  if (!keys) return
888
900
 
889
901
  const compressKeys = this.cfg.compress?.keys
@@ -911,7 +923,7 @@ export class CommonDao<
911
923
  return dbm
912
924
  }
913
925
 
914
- anyToDBMs(rows: DBM[], opt: CommonDaoOptions = {}): DBM[] {
926
+ anyToDBMs(rows: readonly DBM[], opt: CommonDaoOptions = {}): DBM[] {
915
927
  return rows.map(entity => this.anyToDBM(entity, opt))
916
928
  }
917
929
 
@@ -980,14 +992,14 @@ export class CommonDao<
980
992
  }
981
993
  }
982
994
 
983
- withIds(ids: ID[]): DaoWithIds<CommonDao<BM, DBM, ID>> {
995
+ withIds(ids: readonly ID[]): DaoWithIds<CommonDao<BM, DBM, ID>> {
984
996
  return {
985
997
  dao: this,
986
998
  ids,
987
999
  }
988
1000
  }
989
1001
 
990
- withRowsToSave(rows: Unsaved<BM>[]): DaoWithRows<CommonDao<BM, DBM, ID>> {
1002
+ withRowsToSave(rows: readonly Unsaved<BM>[]): DaoWithRows<CommonDao<BM, DBM, ID>> {
991
1003
  return {
992
1004
  dao: this,
993
1005
  rows: rows as any,
@@ -1109,7 +1121,7 @@ export class CommonDao<
1109
1121
  * @experimental
1110
1122
  */
1111
1123
  static async multiDelete(
1112
- inputs: (DaoWithId<AnyDao> | DaoWithIds<AnyDao>)[],
1124
+ inputs: readonly (DaoWithId<AnyDao> | DaoWithIds<AnyDao>)[],
1113
1125
  opt: CommonDaoOptions = {},
1114
1126
  ): Promise<NonNegativeInteger> {
1115
1127
  if (!inputs.length) return 0
@@ -1133,7 +1145,7 @@ export class CommonDao<
1133
1145
  }
1134
1146
 
1135
1147
  static async multiSave(
1136
- inputs: (DaoWithRows<AnyDao> | DaoWithRow<AnyDao>)[],
1148
+ inputs: readonly (DaoWithRows<AnyDao> | DaoWithRow<AnyDao>)[],
1137
1149
  opt: CommonDaoSaveBatchOptions<any> = {},
1138
1150
  ): Promise<void> {
1139
1151
  if (!inputs.length) return
@@ -1231,6 +1243,19 @@ export class CommonDao<
1231
1243
  })
1232
1244
  }
1233
1245
 
1246
+ /**
1247
+ * Validates the query (throws if it uses `excludeFromIndexes` or compressed properties),
1248
+ * and applies `opt.table` override (if any).
1249
+ * The table override is applied on a clone, to not mutate the input query.
1250
+ */
1251
+ private prepareQuery(q: DBQuery<DBM>, opt: CommonDaoOptions): DBQuery<DBM> {
1252
+ this.validateQuery(q)
1253
+ if (!opt.table || opt.table === q.table) return q
1254
+ const q2 = q.clone()
1255
+ q2.table = opt.table
1256
+ return q2
1257
+ }
1258
+
1234
1259
  private validateQuery(q: DBQuery<DBM>): void {
1235
1260
  this.validateQueryIndexes(q)
1236
1261
  this.validateQueryCompression(q)
@@ -1295,7 +1320,7 @@ export type CommonDaoTransactionFn<T = void> = (tx: CommonDaoTransaction) => Pro
1295
1320
 
1296
1321
  export interface DaoWithIds<DAO extends AnyDao> {
1297
1322
  dao: DAO
1298
- ids: string[]
1323
+ ids: readonly string[]
1299
1324
  }
1300
1325
 
1301
1326
  export interface DaoWithId<DAO extends AnyDao> {
@@ -1305,7 +1330,7 @@ export interface DaoWithId<DAO extends AnyDao> {
1305
1330
 
1306
1331
  export interface DaoWithRows<DAO extends AnyDao, BM = InferBM<DAO>> {
1307
1332
  dao: DAO
1308
- rows: Unsaved<BM>[]
1333
+ rows: readonly Unsaved<BM>[]
1309
1334
  }
1310
1335
 
1311
1336
  export interface DaoWithRow<DAO extends AnyDao, BM = InferBM<DAO>> {
@@ -51,7 +51,7 @@ export class CommonDaoTransaction {
51
51
 
52
52
  async getByIds<BM extends BaseDBEntity, DBM extends BaseDBEntity, ID extends string = BM['id']>(
53
53
  dao: CommonDao<BM, DBM, ID>,
54
- ids: ID[],
54
+ ids: readonly ID[],
55
55
  opt?: CommonDaoReadOptions,
56
56
  ): Promise<BM[]> {
57
57
  return await dao.getByIds(ids, { ...opt, tx: this.tx })
@@ -81,7 +81,7 @@ export class CommonDaoTransaction {
81
81
 
82
82
  async saveBatch<BM extends BaseDBEntity, DBM extends BaseDBEntity>(
83
83
  dao: CommonDao<BM, DBM>,
84
- bms: Unsaved<BM>[],
84
+ bms: readonly Unsaved<BM>[],
85
85
  opt?: CommonDaoSaveBatchOptions<DBM>,
86
86
  ): Promise<BM[]> {
87
87
  return await dao.saveBatch(bms, { ...opt, tx: this.tx })
@@ -116,7 +116,7 @@ export class CommonDaoTransaction {
116
116
 
117
117
  async deleteByIds<DAO extends AnyDao>(
118
118
  dao: DAO,
119
- ids: InferID<DAO>[],
119
+ ids: readonly InferID<DAO>[],
120
120
  opt?: CommonDaoOptions,
121
121
  ): Promise<number> {
122
122
  return await dao.deleteByIds(ids, { ...opt, tx: this.tx })
@@ -43,7 +43,10 @@ export class BaseCommonDB implements CommonDB {
43
43
  // no-op
44
44
  }
45
45
 
46
- async getByIds<ROW extends ObjectWithId>(_table: string, _ids: string[]): Promise<ROW[]> {
46
+ async getByIds<ROW extends ObjectWithId>(
47
+ _table: string,
48
+ _ids: readonly string[],
49
+ ): Promise<ROW[]> {
47
50
  throw new Error('getByIds is not implemented')
48
51
  }
49
52
 
@@ -78,7 +81,7 @@ export class BaseCommonDB implements CommonDB {
78
81
 
79
82
  async saveBatch<ROW extends ObjectWithId>(
80
83
  _table: string,
81
- _rows: ROW[],
84
+ _rows: readonly ROW[],
82
85
  _opt?: CommonDBSaveOptions<ROW>,
83
86
  ): Promise<void> {
84
87
  throw new Error('saveBatch is not implemented')
@@ -88,7 +91,11 @@ export class BaseCommonDB implements CommonDB {
88
91
  throw new Error('streamQuery is not implemented')
89
92
  }
90
93
 
91
- async deleteByIds(_table: string, _ids: string[], _opt?: CommonDBOptions): Promise<number> {
94
+ async deleteByIds(
95
+ _table: string,
96
+ _ids: readonly string[],
97
+ _opt?: CommonDBOptions,
98
+ ): Promise<number> {
92
99
  throw new Error('deleteByIds is not implemented')
93
100
  }
94
101
 
@@ -63,7 +63,7 @@ export interface CommonDB {
63
63
  */
64
64
  getByIds: <ROW extends ObjectWithId>(
65
65
  table: string,
66
- ids: string[],
66
+ ids: readonly string[],
67
67
  opt?: CommonDBReadOptions,
68
68
  ) => Promise<ROW[]>
69
69
 
@@ -113,7 +113,7 @@ export interface CommonDB {
113
113
  */
114
114
  saveBatch: <ROW extends ObjectWithId>(
115
115
  table: string,
116
- rows: ROW[],
116
+ rows: readonly ROW[],
117
117
  opt?: CommonDBSaveOptions<ROW>,
118
118
  ) => Promise<void>
119
119
 
@@ -156,7 +156,11 @@ export interface CommonDB {
156
156
  * Returns number of deleted items.
157
157
  * Not supported by all implementations (e.g Datastore will always return same number as number of ids).
158
158
  */
159
- deleteByIds: (table: string, ids: string[], opt?: CommonDBOptions) => Promise<NonNegativeInteger>
159
+ deleteByIds: (
160
+ table: string,
161
+ ids: readonly string[],
162
+ opt?: CommonDBOptions,
163
+ ) => Promise<NonNegativeInteger>
160
164
 
161
165
  /**
162
166
  * Deletes rows from multiple tables at once.
package/src/db.model.ts CHANGED
@@ -107,14 +107,14 @@ export type DBOperation = DBSaveBatchOperation<any> | DBDeleteByIdsOperation
107
107
  export interface DBSaveBatchOperation<ROW extends ObjectWithId> {
108
108
  type: 'saveBatch'
109
109
  table: string
110
- rows: ROW[]
110
+ rows: readonly ROW[]
111
111
  opt?: CommonDBSaveOptions<ROW>
112
112
  }
113
113
 
114
114
  export interface DBDeleteByIdsOperation {
115
115
  type: 'deleteByIds'
116
116
  table: string
117
- ids: string[]
117
+ ids: readonly string[]
118
118
  opt?: CommonDBOptions
119
119
  }
120
120
 
@@ -135,7 +135,7 @@ export class InMemoryDB implements CommonDB {
135
135
 
136
136
  async getByIds<ROW extends ObjectWithId>(
137
137
  _table: string,
138
- ids: string[],
138
+ ids: readonly string[],
139
139
  _opt?: CommonDBOptions,
140
140
  ): Promise<ROW[]> {
141
141
  const table = this.cfg.tablesPrefix + _table
@@ -159,7 +159,7 @@ export class InMemoryDB implements CommonDB {
159
159
 
160
160
  async saveBatch<ROW extends ObjectWithId>(
161
161
  _table: string,
162
- rows: ROW[],
162
+ rows: readonly ROW[],
163
163
  opt: CommonDBSaveOptions<ROW> = {},
164
164
  ): Promise<void> {
165
165
  const table = this.cfg.tablesPrefix + _table
@@ -220,7 +220,11 @@ export class InMemoryDB implements CommonDB {
220
220
  return await this.deleteByIds(q.table, ids)
221
221
  }
222
222
 
223
- async deleteByIds(_table: string, ids: string[], _opt?: CommonDBOptions): Promise<number> {
223
+ async deleteByIds(
224
+ _table: string,
225
+ ids: readonly string[],
226
+ _opt?: CommonDBOptions,
227
+ ): Promise<number> {
224
228
  const table = this.cfg.tablesPrefix + _table
225
229
  if (!this.data[table]) return 0
226
230
 
@@ -335,7 +339,7 @@ export class InMemoryDBTransaction implements DBTransaction {
335
339
 
336
340
  async getByIds<ROW extends ObjectWithId>(
337
341
  table: string,
338
- ids: string[],
342
+ ids: readonly string[],
339
343
  opt?: CommonDBOptions,
340
344
  ): Promise<ROW[]> {
341
345
  if (this.db.cfg.forbidTransactionReadAfterWrite) {
@@ -350,7 +354,7 @@ export class InMemoryDBTransaction implements DBTransaction {
350
354
 
351
355
  async saveBatch<ROW extends ObjectWithId>(
352
356
  table: string,
353
- rows: ROW[],
357
+ rows: readonly ROW[],
354
358
  opt?: CommonDBSaveOptions<ROW>,
355
359
  ): Promise<void> {
356
360
  _assert(
@@ -368,7 +372,7 @@ export class InMemoryDBTransaction implements DBTransaction {
368
372
  })
369
373
  }
370
374
 
371
- async deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number> {
375
+ async deleteByIds(table: string, ids: readonly string[], opt?: CommonDBOptions): Promise<number> {
372
376
  _assert(
373
377
  !this.opt.readOnly,
374
378
  `InMemoryDBTransaction: deleteByIds(${table}) called in readOnly mode`,