@naturalcycles/db-lib 10.47.0 → 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';
|
|
@@ -178,7 +178,10 @@ export class CommonDao {
|
|
|
178
178
|
q.table = opt.table || q.table;
|
|
179
179
|
let pipeline = this.cfg.db.streamQuery(q, opt);
|
|
180
180
|
if (this.cfg.compress?.keys.length) {
|
|
181
|
-
pipeline = pipeline.map(async row => await this.storageRowToDBM(row)
|
|
181
|
+
pipeline = pipeline.map(async row => await this.storageRowToDBM(row), {
|
|
182
|
+
// lowered, to reduce the total buffer size a bit for uncompressed objects
|
|
183
|
+
highWaterMark: 16,
|
|
184
|
+
});
|
|
182
185
|
}
|
|
183
186
|
const isPartialQuery = !!q._selectedFieldNames;
|
|
184
187
|
if (isPartialQuery)
|
|
@@ -192,7 +195,10 @@ export class CommonDao {
|
|
|
192
195
|
q.table = opt.table || q.table;
|
|
193
196
|
let pipeline = this.cfg.db.streamQuery(q, opt);
|
|
194
197
|
if (this.cfg.compress?.keys.length) {
|
|
195
|
-
pipeline = pipeline.map(async row => await this.storageRowToDBM(row)
|
|
198
|
+
pipeline = pipeline.map(async row => await this.storageRowToDBM(row), {
|
|
199
|
+
// lowered, to reduce the total buffer size a bit for uncompressed objects
|
|
200
|
+
highWaterMark: 16,
|
|
201
|
+
});
|
|
196
202
|
}
|
|
197
203
|
const isPartialQuery = !!q._selectedFieldNames;
|
|
198
204
|
if (isPartialQuery)
|
|
@@ -628,7 +634,12 @@ export class CommonDao {
|
|
|
628
634
|
if (!this.cfg.compress?.keys.length)
|
|
629
635
|
return row;
|
|
630
636
|
const dbm = { ...row };
|
|
631
|
-
|
|
637
|
+
if (this.cfg.compress.syncMode) {
|
|
638
|
+
this.decompressSync(dbm);
|
|
639
|
+
}
|
|
640
|
+
else {
|
|
641
|
+
await this.decompress(dbm);
|
|
642
|
+
}
|
|
632
643
|
return dbm;
|
|
633
644
|
}
|
|
634
645
|
/**
|
|
@@ -637,6 +648,13 @@ export class CommonDao {
|
|
|
637
648
|
async storageRowsToDBMs(rows) {
|
|
638
649
|
if (!this.cfg.compress?.keys.length)
|
|
639
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
|
+
}
|
|
640
658
|
return await pMap(rows, async row => await this.storageRowToDBM(row));
|
|
641
659
|
}
|
|
642
660
|
/**
|
|
@@ -665,6 +683,18 @@ export class CommonDao {
|
|
|
665
683
|
dbm.__compressed = undefined;
|
|
666
684
|
Object.assign(dbm, properties);
|
|
667
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
|
+
}
|
|
668
698
|
anyToDBM(dbm, _opt = {}) {
|
|
669
699
|
if (!dbm)
|
|
670
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
|
@@ -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 {
|
|
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,
|
|
@@ -254,7 +258,10 @@ export class CommonDao<
|
|
|
254
258
|
let pipeline = this.cfg.db.streamQuery<DBM>(q, opt)
|
|
255
259
|
|
|
256
260
|
if (this.cfg.compress?.keys.length) {
|
|
257
|
-
pipeline = pipeline.map(async row => await this.storageRowToDBM(row)
|
|
261
|
+
pipeline = pipeline.map(async row => await this.storageRowToDBM(row), {
|
|
262
|
+
// lowered, to reduce the total buffer size a bit for uncompressed objects
|
|
263
|
+
highWaterMark: 16,
|
|
264
|
+
})
|
|
258
265
|
}
|
|
259
266
|
|
|
260
267
|
const isPartialQuery = !!q._selectedFieldNames
|
|
@@ -272,7 +279,10 @@ export class CommonDao<
|
|
|
272
279
|
let pipeline = this.cfg.db.streamQuery<DBM>(q, opt)
|
|
273
280
|
|
|
274
281
|
if (this.cfg.compress?.keys.length) {
|
|
275
|
-
pipeline = pipeline.map(async row => await this.storageRowToDBM(row)
|
|
282
|
+
pipeline = pipeline.map(async row => await this.storageRowToDBM(row), {
|
|
283
|
+
// lowered, to reduce the total buffer size a bit for uncompressed objects
|
|
284
|
+
highWaterMark: 16,
|
|
285
|
+
})
|
|
276
286
|
}
|
|
277
287
|
|
|
278
288
|
const isPartialQuery = !!q._selectedFieldNames
|
|
@@ -823,7 +833,11 @@ export class CommonDao<
|
|
|
823
833
|
async storageRowToDBM(row: ObjectWithId): Promise<DBM> {
|
|
824
834
|
if (!this.cfg.compress?.keys.length) return row as DBM
|
|
825
835
|
const dbm = { ...(row as DBM) }
|
|
826
|
-
|
|
836
|
+
if (this.cfg.compress.syncMode) {
|
|
837
|
+
this.decompressSync(dbm)
|
|
838
|
+
} else {
|
|
839
|
+
await this.decompress(dbm)
|
|
840
|
+
}
|
|
827
841
|
return dbm
|
|
828
842
|
}
|
|
829
843
|
|
|
@@ -832,6 +846,13 @@ export class CommonDao<
|
|
|
832
846
|
*/
|
|
833
847
|
async storageRowsToDBMs(rows: ObjectWithId[]): Promise<DBM[]> {
|
|
834
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
|
+
}
|
|
835
856
|
return await pMap(rows, async row => await this.storageRowToDBM(row))
|
|
836
857
|
}
|
|
837
858
|
|
|
@@ -863,6 +884,19 @@ export class CommonDao<
|
|
|
863
884
|
Object.assign(dbm, properties)
|
|
864
885
|
}
|
|
865
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
|
+
|
|
866
900
|
anyToDBM(dbm: undefined, opt?: CommonDaoOptions): null
|
|
867
901
|
anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM
|
|
868
902
|
anyToDBM(dbm?: DBM, _opt: CommonDaoOptions = {}): DBM | null {
|