@naturalcycles/db-lib 10.41.0 → 10.42.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.
@@ -386,6 +386,12 @@ export class CommonDao {
386
386
  }
387
387
  prepareSaveOptions(opt) {
388
388
  let { saveMethod, assignGeneratedIds = this.cfg.assignGeneratedIds, excludeFromIndexes = this.cfg.excludeFromIndexes, } = opt;
389
+ if (this.cfg.compress?.keys) {
390
+ excludeFromIndexes ??= [];
391
+ if (!excludeFromIndexes.includes('data')) {
392
+ excludeFromIndexes.push('data');
393
+ }
394
+ }
389
395
  if (this.cfg.immutable && !opt.allowMutability && !opt.saveMethod) {
390
396
  saveMethod = 'insert';
391
397
  }
@@ -569,7 +575,7 @@ export class CommonDao {
569
575
  * Mutates `dbm`.
570
576
  */
571
577
  async compress(dbm) {
572
- if (!this.cfg.compress)
578
+ if (!this.cfg.compress?.keys.length)
573
579
  return; // No compression requested
574
580
  const { keys } = this.cfg.compress;
575
581
  const properties = _pick(dbm, keys);
@@ -584,6 +590,8 @@ export class CommonDao {
584
590
  */
585
591
  async decompress(dbm) {
586
592
  _typeCast(dbm);
593
+ if (!this.cfg.compress)
594
+ return; // Auto-compression not turned on
587
595
  if (!Buffer.isBuffer(dbm.data))
588
596
  return; // No compressed data
589
597
  // try-catch to avoid a `data` with Buffer which is not compressed, but legit data
@@ -171,11 +171,14 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
171
171
  * When specified, the listed properties will be compressed under a `data` property in the DBM.
172
172
  * If DBM already has a `data` property and you don't add it to the list, an error will be thrown.
173
173
  *
174
+ * When specified with an empty `keys` list, then compression will be skipped, but all previously compressed data
175
+ * will be decompressed, so the Dao can still work.
176
+ *
174
177
  * Compression happens after the `beforeBMToDBM` hook and before the DBM is saved to the database.
175
178
  * Decompression happens after the DBM is loaded from the database and before the `beforeDBMToBM` hook.
176
179
  */
177
180
  compress?: {
178
- keys: [keyof DBM, ...(keyof DBM)[]];
181
+ keys: (keyof DBM)[];
179
182
  };
180
183
  }
181
184
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
3
  "type": "module",
4
- "version": "10.41.0",
4
+ "version": "10.42.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@naturalcycles/nodejs-lib": "^15"
@@ -210,11 +210,14 @@ export interface CommonDaoCfg<
210
210
  * When specified, the listed properties will be compressed under a `data` property in the DBM.
211
211
  * If DBM already has a `data` property and you don't add it to the list, an error will be thrown.
212
212
  *
213
+ * When specified with an empty `keys` list, then compression will be skipped, but all previously compressed data
214
+ * will be decompressed, so the Dao can still work.
215
+ *
213
216
  * Compression happens after the `beforeBMToDBM` hook and before the DBM is saved to the database.
214
217
  * Decompression happens after the DBM is loaded from the database and before the `beforeDBMToBM` hook.
215
218
  */
216
219
  compress?: {
217
- keys: [keyof DBM, ...(keyof DBM)[]]
220
+ keys: (keyof DBM)[]
218
221
  }
219
222
  }
220
223
 
@@ -522,6 +522,14 @@ export class CommonDao<
522
522
  assignGeneratedIds = this.cfg.assignGeneratedIds,
523
523
  excludeFromIndexes = this.cfg.excludeFromIndexes,
524
524
  } = opt
525
+
526
+ if (this.cfg.compress?.keys) {
527
+ excludeFromIndexes ??= []
528
+ if (!excludeFromIndexes.includes('data' as any)) {
529
+ excludeFromIndexes.push('data' as any)
530
+ }
531
+ }
532
+
525
533
  if (this.cfg.immutable && !opt.allowMutability && !opt.saveMethod) {
526
534
  saveMethod = 'insert'
527
535
  }
@@ -758,7 +766,7 @@ export class CommonDao<
758
766
  * Mutates `dbm`.
759
767
  */
760
768
  async compress(dbm: DBM): Promise<void> {
761
- if (!this.cfg.compress) return // No compression requested
769
+ if (!this.cfg.compress?.keys.length) return // No compression requested
762
770
 
763
771
  const { keys } = this.cfg.compress
764
772
  const properties = _pick(dbm, keys)
@@ -777,6 +785,7 @@ export class CommonDao<
777
785
  */
778
786
  async decompress(dbm: DBM): Promise<void> {
779
787
  _typeCast<Compressed<DBM>>(dbm)
788
+ if (!this.cfg.compress) return // Auto-compression not turned on
780
789
  if (!Buffer.isBuffer(dbm.data)) return // No compressed data
781
790
 
782
791
  // try-catch to avoid a `data` with Buffer which is not compressed, but legit data