@naturalcycles/db-lib 10.47.1 → 10.48.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.
@@ -164,6 +164,10 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
164
164
  storageRowsToDBMs(rows: ObjectWithId[]): Promise<DBM[]>;
165
165
  private compress;
166
166
  private decompress;
167
+ /**
168
+ * Mutates `dbm`.
169
+ */
170
+ private decompressSync;
167
171
  anyToDBM(dbm: undefined, opt?: CommonDaoOptions): null;
168
172
  anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM;
169
173
  anyToDBMs(rows: DBM[], opt?: CommonDaoOptions): DBM[];
@@ -7,7 +7,7 @@ import { _filterUndefinedValues, _objectAssignExact, _omitWithUndefined, _pick,
7
7
  import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
8
8
  import { _objectKeys, _passthroughPredicate, _stringMapEntries, _stringMapValues, _typeCast, } from '@naturalcycles/js-lib/types';
9
9
  import { stringId } from '@naturalcycles/nodejs-lib';
10
- import { decompressZstdOrInflateToString, zstdCompress } from '@naturalcycles/nodejs-lib/zip';
10
+ import { decompressZstdOrInflateToString, decompressZstdOrInflateToStringSync, zstdCompress, } from '@naturalcycles/nodejs-lib/zip';
11
11
  import { DBLibError } from '../cnst.js';
12
12
  import { RunnableDBQuery } from '../query/dbQuery.js';
13
13
  import { CommonDaoTransaction } from './commonDaoTransaction.js';
@@ -634,7 +634,12 @@ export class CommonDao {
634
634
  if (!this.cfg.compress?.keys.length)
635
635
  return row;
636
636
  const dbm = { ...row };
637
- await this.decompress(dbm);
637
+ if (this.cfg.compress.syncMode) {
638
+ this.decompressSync(dbm);
639
+ }
640
+ else {
641
+ await this.decompress(dbm);
642
+ }
638
643
  return dbm;
639
644
  }
640
645
  /**
@@ -643,6 +648,13 @@ export class CommonDao {
643
648
  async storageRowsToDBMs(rows) {
644
649
  if (!this.cfg.compress?.keys.length)
645
650
  return rows;
651
+ if (this.cfg.compress.syncMode) {
652
+ return rows.map(row => {
653
+ const dbm = { ...row };
654
+ this.decompressSync(dbm);
655
+ return dbm;
656
+ });
657
+ }
646
658
  return await pMap(rows, async row => await this.storageRowToDBM(row));
647
659
  }
648
660
  /**
@@ -671,6 +683,18 @@ export class CommonDao {
671
683
  dbm.__compressed = undefined;
672
684
  Object.assign(dbm, properties);
673
685
  }
686
+ /**
687
+ * Mutates `dbm`.
688
+ */
689
+ decompressSync(dbm) {
690
+ _typeCast(dbm);
691
+ if (!Buffer.isBuffer(dbm.__compressed))
692
+ return; // No compressed data
693
+ const bufferString = decompressZstdOrInflateToStringSync(dbm.__compressed);
694
+ const properties = JSON.parse(bufferString);
695
+ dbm.__compressed = undefined;
696
+ Object.assign(dbm, properties);
697
+ }
674
698
  anyToDBM(dbm, _opt = {}) {
675
699
  if (!dbm)
676
700
  return null;
@@ -185,6 +185,12 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
185
185
  * Undefined will default to level 1 (not the 3, which is the zstd default)
186
186
  */
187
187
  level?: Integer;
188
+ /**
189
+ * When enabled, zstd decompression will be performed synchronously.
190
+ *
191
+ * @experimental
192
+ */
193
+ syncMode?: boolean;
188
194
  };
189
195
  }
190
196
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
3
  "type": "module",
4
- "version": "10.47.1",
4
+ "version": "10.48.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@naturalcycles/nodejs-lib": "^15"
@@ -224,6 +224,12 @@ export interface CommonDaoCfg<
224
224
  * Undefined will default to level 1 (not the 3, which is the zstd default)
225
225
  */
226
226
  level?: Integer
227
+ /**
228
+ * When enabled, zstd decompression will be performed synchronously.
229
+ *
230
+ * @experimental
231
+ */
232
+ syncMode?: boolean
227
233
  }
228
234
  }
229
235
 
@@ -27,7 +27,11 @@ import type {
27
27
  import { stringId } from '@naturalcycles/nodejs-lib'
28
28
  import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
29
29
  import type { Pipeline } from '@naturalcycles/nodejs-lib/stream'
30
- import { decompressZstdOrInflateToString, zstdCompress } from '@naturalcycles/nodejs-lib/zip'
30
+ import {
31
+ decompressZstdOrInflateToString,
32
+ decompressZstdOrInflateToStringSync,
33
+ zstdCompress,
34
+ } from '@naturalcycles/nodejs-lib/zip'
31
35
  import { DBLibError } from '../cnst.js'
32
36
  import type {
33
37
  CommonDBSaveOptions,
@@ -829,7 +833,11 @@ export class CommonDao<
829
833
  async storageRowToDBM(row: ObjectWithId): Promise<DBM> {
830
834
  if (!this.cfg.compress?.keys.length) return row as DBM
831
835
  const dbm = { ...(row as DBM) }
832
- await this.decompress(dbm)
836
+ if (this.cfg.compress.syncMode) {
837
+ this.decompressSync(dbm)
838
+ } else {
839
+ await this.decompress(dbm)
840
+ }
833
841
  return dbm
834
842
  }
835
843
 
@@ -838,6 +846,13 @@ export class CommonDao<
838
846
  */
839
847
  async storageRowsToDBMs(rows: ObjectWithId[]): Promise<DBM[]> {
840
848
  if (!this.cfg.compress?.keys.length) return rows as DBM[]
849
+ if (this.cfg.compress.syncMode) {
850
+ return rows.map(row => {
851
+ const dbm = { ...(row as DBM) }
852
+ this.decompressSync(dbm)
853
+ return dbm
854
+ })
855
+ }
841
856
  return await pMap(rows, async row => await this.storageRowToDBM(row))
842
857
  }
843
858
 
@@ -869,6 +884,19 @@ export class CommonDao<
869
884
  Object.assign(dbm, properties)
870
885
  }
871
886
 
887
+ /**
888
+ * Mutates `dbm`.
889
+ */
890
+ private decompressSync(dbm: DBM): void {
891
+ _typeCast<Compressed<DBM>>(dbm)
892
+ if (!Buffer.isBuffer(dbm.__compressed)) return // No compressed data
893
+
894
+ const bufferString = decompressZstdOrInflateToStringSync(dbm.__compressed)
895
+ const properties = JSON.parse(bufferString)
896
+ dbm.__compressed = undefined
897
+ Object.assign(dbm, properties)
898
+ }
899
+
872
900
  anyToDBM(dbm: undefined, opt?: CommonDaoOptions): null
873
901
  anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM
874
902
  anyToDBM(dbm?: DBM, _opt: CommonDaoOptions = {}): DBM | null {