@naturalcycles/db-lib 10.49.1 → 10.51.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.
@@ -28,7 +28,7 @@ export class LocalFilePersistencePlugin {
28
28
  return await Pipeline.fromNDJsonFile(filePath).toArray();
29
29
  }
30
30
  async saveFiles(ops) {
31
- await pMap(ops, async op => await this.saveFile(op.table, op.rows), { concurrency: 32 });
31
+ await pMap(ops, async (op) => await this.saveFile(op.table, op.rows), { concurrency: 32 });
32
32
  }
33
33
  async saveFile(table, rows) {
34
34
  await fs2.ensureDirAsync(this.cfg.storagePath);
package/dist/cnst.js CHANGED
@@ -1,5 +1,4 @@
1
- export { DBLibError };
2
- var DBLibError;
1
+ export var DBLibError;
3
2
  (function (DBLibError) {
4
3
  DBLibError["DB_ROW_REQUIRED"] = "DB_ROW_REQUIRED";
5
4
  DBLibError["DAO_IS_READ_ONLY"] = "DAO_IS_READ_ONLY";
@@ -143,11 +143,11 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
143
143
  * const storageRow = await dao.dbmToStorageRow(dbm)
144
144
  * await db.saveBatch(table, [storageRow])
145
145
  */
146
- dbmToStorageRow(dbm: DBM): ObjectWithId;
146
+ dbmToStorageRow(dbm: DBM): Promise<ObjectWithId>;
147
147
  /**
148
148
  * Converts multiple DBMs to storage rows.
149
149
  */
150
- dbmsToStorageRows(dbms: DBM[]): ObjectWithId[];
150
+ dbmsToStorageRows(dbms: DBM[]): Promise<ObjectWithId[]>;
151
151
  /**
152
152
  * Converts a storage row back to a DBM, applying decompression if needed.
153
153
  *
@@ -162,9 +162,6 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
162
162
  * Converts multiple storage rows to DBMs.
163
163
  */
164
164
  storageRowsToDBM(rows: ObjectWithId[]): DBM[];
165
- /**
166
- * Mutates `dbm`.
167
- */
168
165
  private compress;
169
166
  /**
170
167
  * Mutates `dbm`.
@@ -140,7 +140,7 @@ export class CommonDao {
140
140
  * Order is not guaranteed, as queries run in parallel.
141
141
  */
142
142
  async runUnionQueries(queries, opt) {
143
- const results = (await pMap(queries, async q => (await this.runQueryExtended(q, opt)).rows)).flat();
143
+ const results = (await pMap(queries, async (q) => (await this.runQueryExtended(q, opt)).rows)).flat();
144
144
  return _uniqBy(results, r => r.id);
145
145
  }
146
146
  async runQueryExtended(q, opt = {}) {
@@ -274,7 +274,7 @@ export class CommonDao {
274
274
  * Like patchById, but runs all operations within a Transaction.
275
275
  */
276
276
  async patchByIdInTransaction(id, patch, opt) {
277
- return await this.runInTransaction(async daoTx => {
277
+ return await this.runInTransaction(async (daoTx) => {
278
278
  return await this.patchById(id, patch, { ...opt, tx: daoTx.tx });
279
279
  });
280
280
  }
@@ -326,7 +326,7 @@ export class CommonDao {
326
326
  * Like patch, but runs all operations within a Transaction.
327
327
  */
328
328
  async patchInTransaction(bm, patch, opt) {
329
- return await this.runInTransaction(async daoTx => {
329
+ return await this.runInTransaction(async (daoTx) => {
330
330
  return await this.patch(bm, patch, { ...opt, tx: daoTx.tx });
331
331
  });
332
332
  }
@@ -351,7 +351,7 @@ export class CommonDao {
351
351
  this.cfg.hooks.beforeSave?.(dbm);
352
352
  const table = opt.table || this.cfg.table;
353
353
  const saveOptions = this.prepareSaveOptions(opt);
354
- const row = this.dbmToStorageRow(dbm);
354
+ const row = await this.dbmToStorageRow(dbm);
355
355
  await (opt.tx || this.cfg.db).saveBatch(table, [row], saveOptions);
356
356
  if (saveOptions.assignGeneratedIds) {
357
357
  bm.id = dbm.id;
@@ -365,7 +365,7 @@ export class CommonDao {
365
365
  this.cfg.hooks.beforeSave?.(validDbm);
366
366
  const table = opt.table || this.cfg.table;
367
367
  const saveOptions = this.prepareSaveOptions(opt);
368
- const row = this.dbmToStorageRow(validDbm);
368
+ const row = await this.dbmToStorageRow(validDbm);
369
369
  await (opt.tx || this.cfg.db).saveBatch(table, [row], saveOptions);
370
370
  if (saveOptions.assignGeneratedIds) {
371
371
  dbm.id = validDbm.id;
@@ -383,7 +383,7 @@ export class CommonDao {
383
383
  }
384
384
  const table = opt.table || this.cfg.table;
385
385
  const saveOptions = this.prepareSaveOptions(opt);
386
- const rows = this.dbmsToStorageRows(dbms);
386
+ const rows = await this.dbmsToStorageRows(dbms);
387
387
  await (opt.tx || this.cfg.db).saveBatch(table, rows, saveOptions);
388
388
  if (saveOptions.assignGeneratedIds) {
389
389
  dbms.forEach((dbm, i) => (bms[i].id = dbm.id));
@@ -401,7 +401,7 @@ export class CommonDao {
401
401
  }
402
402
  const table = opt.table || this.cfg.table;
403
403
  const saveOptions = this.prepareSaveOptions(opt);
404
- const rows = this.dbmsToStorageRows(validDbms);
404
+ const rows = await this.dbmsToStorageRows(validDbms);
405
405
  await (opt.tx || this.cfg.db).saveBatch(table, rows, saveOptions);
406
406
  if (saveOptions.assignGeneratedIds) {
407
407
  validDbms.forEach((dbm, i) => (dbms[i].id = dbm.id));
@@ -447,14 +447,14 @@ export class CommonDao {
447
447
  const { beforeSave } = this.cfg.hooks;
448
448
  const { chunkSize = 500, chunkConcurrency = 32, errorMode } = opt;
449
449
  await p
450
- .mapSync(bm => {
450
+ .map(async (bm) => {
451
451
  this.assignIdCreatedUpdated(bm, opt);
452
452
  const dbm = this.bmToDBM(bm, opt);
453
453
  beforeSave?.(dbm);
454
- return this.dbmToStorageRow(dbm);
454
+ return await this.dbmToStorageRow(dbm);
455
455
  }, { errorMode })
456
456
  .chunk(chunkSize)
457
- .map(async batch => {
457
+ .map(async (batch) => {
458
458
  await this.cfg.db.saveBatch(table, batch, saveOptions);
459
459
  return batch;
460
460
  }, {
@@ -501,7 +501,7 @@ export class CommonDao {
501
501
  .streamQuery(q.select(['id']), opt)
502
502
  .mapSync(r => r.id)
503
503
  .chunk(chunkSize)
504
- .map(async ids => {
504
+ .map(async (ids) => {
505
505
  await this.cfg.db.deleteByIds(q.table, ids, opt);
506
506
  deleted += ids.length;
507
507
  }, {
@@ -600,22 +600,22 @@ export class CommonDao {
600
600
  * const storageRow = await dao.dbmToStorageRow(dbm)
601
601
  * await db.saveBatch(table, [storageRow])
602
602
  */
603
- dbmToStorageRow(dbm) {
603
+ async dbmToStorageRow(dbm) {
604
604
  if (!this.cfg.compress?.keys.length)
605
605
  return dbm;
606
606
  const row = { ...dbm };
607
- this.compress(row);
607
+ await this.compress(row);
608
608
  return row;
609
609
  }
610
610
  /**
611
611
  * Converts multiple DBMs to storage rows.
612
612
  */
613
- dbmsToStorageRows(dbms) {
613
+ async dbmsToStorageRows(dbms) {
614
614
  if (!this.cfg.compress?.keys.length)
615
615
  return dbms;
616
- return dbms.map(dbm => {
616
+ return await pMap(dbms, async (dbm) => {
617
617
  const row = { ...dbm };
618
- this.compress(row);
618
+ await this.compress(row);
619
619
  return row;
620
620
  });
621
621
  }
@@ -650,13 +650,15 @@ export class CommonDao {
650
650
  /**
651
651
  * Mutates `dbm`.
652
652
  */
653
- compress(dbm) {
653
+ async compress(dbm) {
654
654
  if (!this.cfg.compress?.keys.length)
655
655
  return; // No compression requested
656
656
  const { keys, level = 1 } = this.cfg.compress;
657
657
  const properties = _pick(dbm, keys);
658
658
  const bufferString = JSON.stringify(properties);
659
- const __compressed = zip2.zstdCompressSync(bufferString, level);
659
+ // Unlike `decompress`, we're testing to use async zstd compression.
660
+ // Async Decompression leaks memory severely. But Compression seems fine.
661
+ const __compressed = await zip2.zstdCompress(bufferString, level);
660
662
  _omitWithUndefined(dbm, _objectKeys(properties), { mutate: true });
661
663
  Object.assign(dbm, { __compressed });
662
664
  }
@@ -860,7 +862,7 @@ export class CommonDao {
860
862
  return;
861
863
  const { db } = inputs[0].dao.cfg;
862
864
  const dbmsByTable = {};
863
- for (const input of inputs) {
865
+ await pMap(inputs, async (input) => {
864
866
  const { dao } = input;
865
867
  const { table } = dao.cfg;
866
868
  dbmsByTable[table] ||= [];
@@ -880,7 +882,7 @@ export class CommonDao {
880
882
  dao.assignIdCreatedUpdated(row, opt);
881
883
  const dbm = dao.bmToDBM(row, opt);
882
884
  dao.cfg.hooks.beforeSave?.(dbm);
883
- const storageRow = dao.dbmToStorageRow(dbm);
885
+ const storageRow = await dao.dbmToStorageRow(dbm);
884
886
  dbmsByTable[table].push(storageRow);
885
887
  }
886
888
  else {
@@ -890,10 +892,10 @@ export class CommonDao {
890
892
  if (dao.cfg.hooks.beforeSave) {
891
893
  dbms.forEach(dbm => dao.cfg.hooks.beforeSave(dbm));
892
894
  }
893
- const storageRows = dao.dbmsToStorageRows(dbms);
895
+ const storageRows = await dao.dbmsToStorageRows(dbms);
894
896
  dbmsByTable[table].push(...storageRows);
895
897
  }
896
- }
898
+ });
897
899
  await db.multiSave(dbmsByTable);
898
900
  }
899
901
  async createTransaction(opt) {
@@ -902,7 +904,7 @@ export class CommonDao {
902
904
  }
903
905
  async runInTransaction(fn, opt) {
904
906
  let r;
905
- await this.cfg.db.runInTransaction(async tx => {
907
+ await this.cfg.db.runInTransaction(async (tx) => {
906
908
  const daoTx = new CommonDaoTransaction(tx, this.cfg.logger);
907
909
  try {
908
910
  r = await fn(daoTx);
@@ -1,5 +1,4 @@
1
- export { CommonDaoLogLevel };
2
- var CommonDaoLogLevel;
1
+ export var CommonDaoLogLevel;
3
2
  (function (CommonDaoLogLevel) {
4
3
  /**
5
4
  * Same as undefined
@@ -1,5 +1,4 @@
1
- export { CommonDBType };
2
- var CommonDBType;
1
+ export var CommonDBType;
3
2
  (function (CommonDBType) {
4
3
  CommonDBType["document"] = "document";
5
4
  CommonDBType["relational"] = "relational";
package/dist/db.model.js CHANGED
@@ -1,11 +1,9 @@
1
- export { DBRelation };
2
- var DBRelation;
1
+ export var DBRelation;
3
2
  (function (DBRelation) {
4
3
  DBRelation["ONE_TO_ONE"] = "ONE_TO_ONE";
5
4
  DBRelation["ONE_TO_MANY"] = "ONE_TO_MANY";
6
5
  })(DBRelation || (DBRelation = {}));
7
- export { DBModelType };
8
- var DBModelType;
6
+ export var DBModelType;
9
7
  (function (DBModelType) {
10
8
  DBModelType["DBM"] = "DBM";
11
9
  DBModelType["BM"] = "BM";
@@ -8,8 +8,8 @@ export declare class InMemoryKeyValueDB implements CommonKeyValueDB {
8
8
  cfg: InMemoryKeyValueDBCfg;
9
9
  constructor(cfg?: InMemoryKeyValueDBCfg);
10
10
  support: {
11
- count?: boolean | undefined;
12
- increment?: boolean | undefined;
11
+ count?: boolean;
12
+ increment?: boolean;
13
13
  };
14
14
  data: StringMap<StringMap<any>>;
15
15
  ping(): Promise<void>;
@@ -1,4 +1,4 @@
1
1
  export * from './commonKeyValueDao.js';
2
2
  export * from './commonKeyValueDaoMemoCache.js';
3
3
  export * from './commonKeyValueDB.js';
4
- export * from './commonSyncKeyValueDB.js';
4
+ export type * from './commonSyncKeyValueDB.js';
package/dist/kv/index.js CHANGED
@@ -1,4 +1,3 @@
1
1
  export * from './commonKeyValueDao.js';
2
2
  export * from './commonKeyValueDaoMemoCache.js';
3
3
  export * from './commonKeyValueDB.js';
4
- export * from './commonSyncKeyValueDB.js';
@@ -23,7 +23,7 @@ export async function dbPipelineBackup(opt) {
23
23
  tables ||= await db.getTables();
24
24
  console.log(`${yellow(tables.length)} ${boldWhite('table(s)')}:\n` + tables.join('\n'));
25
25
  const statsPerTable = {};
26
- await pMap(tables, async table => {
26
+ await pMap(tables, async (table) => {
27
27
  let q = DBQuery.create(table).limit(limit);
28
28
  const sinceUpdated = opt.sinceUpdatedPerTable?.[table] ?? opt.sinceUpdated;
29
29
  if (sinceUpdated) {
@@ -19,7 +19,7 @@ export async function dbPipelineCopy(opt) {
19
19
  tables ||= await dbInput.getTables();
20
20
  console.log(`${yellow(tables.length)} ${boldWhite('table(s)')}:\n` + tables.join('\n'));
21
21
  const statsPerTable = {};
22
- await pMap(tables, async table => {
22
+ await pMap(tables, async (table) => {
23
23
  let q = DBQuery.create(table).limit(limit);
24
24
  if (sinceUpdated) {
25
25
  q = q.filter('updated', '>=', sinceUpdated);
@@ -43,7 +43,7 @@ export async function dbPipelineCopy(opt) {
43
43
  .flattenIfNeeded()
44
44
  .tapSync(() => rows++)
45
45
  .chunk(chunkSize)
46
- .forEach(async dbms => {
46
+ .forEach(async (dbms) => {
47
47
  await dbOutput.saveBatch(table, dbms, saveOptions);
48
48
  });
49
49
  const stats = NDJsonStats.create({
@@ -48,7 +48,7 @@ export async function dbPipelineRestore(opt) {
48
48
  console.log(`${yellow(tables.length)} ${boldWhite('table(s)')}:\n`, sizeStrByTable);
49
49
  // const schemaByTable: Record<string, CommonSchema> = {}
50
50
  if (recreateTables) {
51
- await pMap(tables, async table => {
51
+ await pMap(tables, async (table) => {
52
52
  const schemaFilePath = `${inputDirPath}/${table}.schema.json`;
53
53
  if (!fs2.pathExists(schemaFilePath)) {
54
54
  console.warn(`${schemaFilePath} does not exist!`);
@@ -58,7 +58,7 @@ export async function dbPipelineRestore(opt) {
58
58
  await db.createTable(table, schema, { dropIfExists: true });
59
59
  });
60
60
  }
61
- await pMap(tables, async table => {
61
+ await pMap(tables, async (table) => {
62
62
  const zst = tablesToCompress.has(table);
63
63
  const filePath = `${inputDirPath}/${table}.ndjson` + (zst ? '.zst' : '');
64
64
  const saveOptions = saveOptionsPerTable[table] || {};
@@ -82,7 +82,7 @@ export async function dbPipelineRestore(opt) {
82
82
  })
83
83
  .flattenIfNeeded()
84
84
  .chunk(chunkSize)
85
- .forEach(async dbms => {
85
+ .forEach(async (dbms) => {
86
86
  await db.saveBatch(table, dbms, saveOptions);
87
87
  });
88
88
  const stats = NDJsonStats.create({
@@ -169,7 +169,7 @@ export async function runCommonDBTest(db, quirks = {}) {
169
169
  const tables = await db.getTables();
170
170
  // console.log({ tables })
171
171
  if (support.tableSchemas) {
172
- await pMap(tables, async table => {
172
+ await pMap(tables, async (table) => {
173
173
  const schema = await db.getTableSchema(table);
174
174
  // console.log(schema)
175
175
  expect(schema.$id).toBe(`${table}.schema.json`);
@@ -227,7 +227,7 @@ export async function runCommonDBTest(db, quirks = {}) {
227
227
  // save item3 with k1: k1_mod
228
228
  // delete item2
229
229
  // remaining: item1, item3_with_k1_mod
230
- await db.runInTransaction(async tx => {
230
+ await db.runInTransaction(async (tx) => {
231
231
  await tx.saveBatch(TEST_TABLE, items);
232
232
  await tx.saveBatch(TEST_TABLE, [{ ...items[2], k1: 'k1_mod' }]);
233
233
  await tx.deleteByIds(TEST_TABLE, [items[1].id]);
@@ -252,7 +252,7 @@ export async function runCommonDBTest(db, quirks = {}) {
252
252
  const expected = await prepare();
253
253
  let err;
254
254
  try {
255
- await db.runInTransaction(async tx => {
255
+ await db.runInTransaction(async (tx) => {
256
256
  await tx.deleteByIds(TEST_TABLE, [items[2].id]);
257
257
  // It should fail on id == null
258
258
  await tx.saveBatch(TEST_TABLE, [{ ...items[0], k1: 5, id: null }]);
@@ -242,7 +242,7 @@ export async function runCommonDaoTest(db, quirks = {}) {
242
242
  await dao.query().deleteByQuery();
243
243
  // Test that id, created, updated are created
244
244
  const now = localTime.nowUnix();
245
- await dao.runInTransaction(async tx => {
245
+ await dao.runInTransaction(async (tx) => {
246
246
  const row = _omit(item1, ['id', 'created', 'updated']);
247
247
  await tx.save(dao, row);
248
248
  });
@@ -251,14 +251,14 @@ export async function runCommonDaoTest(db, quirks = {}) {
251
251
  expect(loaded[0].id).toBeDefined();
252
252
  expect(loaded[0].created).toBeGreaterThanOrEqual(now);
253
253
  expect(loaded[0].updated).toBe(loaded[0].created);
254
- await dao.runInTransaction(async tx => {
254
+ await dao.runInTransaction(async (tx) => {
255
255
  await tx.deleteById(dao, loaded[0].id);
256
256
  });
257
257
  // saveBatch [item1, 2, 3]
258
258
  // save item3 with k1: k1_mod
259
259
  // delete item2
260
260
  // remaining: item1, item3_with_k1_mod
261
- await dao.runInTransaction(async tx => {
261
+ await dao.runInTransaction(async (tx) => {
262
262
  await tx.saveBatch(dao, items);
263
263
  await tx.save(dao, { ...items[2], k1: 'k1_mod' });
264
264
  await tx.deleteById(dao, items[1].id);
@@ -303,7 +303,7 @@ export async function runCommonDaoTest(db, quirks = {}) {
303
303
  const expected = await prepare();
304
304
  let err;
305
305
  try {
306
- await dao.runInTransaction(async tx => {
306
+ await dao.runInTransaction(async (tx) => {
307
307
  await tx.deleteById(dao, items[2].id);
308
308
  await tx.save(dao, { ...items[0], k1: 5 }); // it should fail here
309
309
  });
@@ -40,7 +40,7 @@ export class CommonTimeSeriesDao {
40
40
  async commitTransaction(ops) {
41
41
  if (!ops.length)
42
42
  return;
43
- await this.cfg.db.runInTransaction(async tx => {
43
+ await this.cfg.db.runInTransaction(async (tx) => {
44
44
  for (const op of ops) {
45
45
  const rows = op.dataPoints.map(([ts, v]) => ({
46
46
  id: String(ts), // Convert Number id into String id, as per CommonDB
@@ -1,3 +1,5 @@
1
+ import type { ObjectWithId } from '@naturalcycles/js-lib/types';
2
+ import { JBuilder } from '@naturalcycles/nodejs-lib/ajv';
1
3
  import type { CommonDBOptions } from '../db.model.js';
2
4
  export declare const commonDBOptionsSchema: () => JBuilder<CommonDBOptions, false>;
3
5
  export declare const commonDBSaveOptionsSchema: <ROW extends ObjectWithId>() => any;
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
3
  "type": "module",
4
- "version": "10.49.1",
4
+ "version": "10.51.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@naturalcycles/nodejs-lib": "^15"
8
8
  },
9
9
  "devDependencies": {
10
- "@typescript/native-preview": "7.0.0-dev.20260301.1",
11
- "@naturalcycles/dev-lib": "18.4.2"
10
+ "@typescript/native-preview": "7.0.0-dev.20260401.1",
11
+ "@naturalcycles/dev-lib": "20.42.0"
12
12
  },
13
13
  "files": [
14
14
  "dist",
@@ -471,7 +471,7 @@ export class CommonDao<
471
471
  const table = opt.table || this.cfg.table
472
472
  const saveOptions = this.prepareSaveOptions(opt)
473
473
 
474
- const row = this.dbmToStorageRow(dbm)
474
+ const row = await this.dbmToStorageRow(dbm)
475
475
  await (opt.tx || this.cfg.db).saveBatch(table, [row], saveOptions)
476
476
 
477
477
  if (saveOptions.assignGeneratedIds) {
@@ -489,7 +489,7 @@ export class CommonDao<
489
489
  const table = opt.table || this.cfg.table
490
490
  const saveOptions = this.prepareSaveOptions(opt)
491
491
 
492
- const row = this.dbmToStorageRow(validDbm)
492
+ const row = await this.dbmToStorageRow(validDbm)
493
493
  await (opt.tx || this.cfg.db).saveBatch(table, [row], saveOptions)
494
494
 
495
495
  if (saveOptions.assignGeneratedIds) {
@@ -510,7 +510,7 @@ export class CommonDao<
510
510
  const table = opt.table || this.cfg.table
511
511
  const saveOptions = this.prepareSaveOptions(opt)
512
512
 
513
- const rows = this.dbmsToStorageRows(dbms)
513
+ const rows = await this.dbmsToStorageRows(dbms)
514
514
  await (opt.tx || this.cfg.db).saveBatch(table, rows, saveOptions)
515
515
 
516
516
  if (saveOptions.assignGeneratedIds) {
@@ -534,7 +534,7 @@ export class CommonDao<
534
534
  const table = opt.table || this.cfg.table
535
535
  const saveOptions = this.prepareSaveOptions(opt)
536
536
 
537
- const rows = this.dbmsToStorageRows(validDbms)
537
+ const rows = await this.dbmsToStorageRows(validDbms)
538
538
  await (opt.tx || this.cfg.db).saveBatch(table, rows, saveOptions)
539
539
 
540
540
  if (saveOptions.assignGeneratedIds) {
@@ -597,12 +597,12 @@ export class CommonDao<
597
597
  const { chunkSize = 500, chunkConcurrency = 32, errorMode } = opt
598
598
 
599
599
  await p
600
- .mapSync(
601
- bm => {
600
+ .map(
601
+ async bm => {
602
602
  this.assignIdCreatedUpdated(bm, opt)
603
603
  const dbm = this.bmToDBM(bm, opt)
604
604
  beforeSave?.(dbm)
605
- return this.dbmToStorageRow(dbm)
605
+ return await this.dbmToStorageRow(dbm)
606
606
  },
607
607
  { errorMode },
608
608
  )
@@ -796,21 +796,21 @@ export class CommonDao<
796
796
  * const storageRow = await dao.dbmToStorageRow(dbm)
797
797
  * await db.saveBatch(table, [storageRow])
798
798
  */
799
- dbmToStorageRow(dbm: DBM): ObjectWithId {
799
+ async dbmToStorageRow(dbm: DBM): Promise<ObjectWithId> {
800
800
  if (!this.cfg.compress?.keys.length) return dbm
801
801
  const row = { ...dbm }
802
- this.compress(row)
802
+ await this.compress(row)
803
803
  return row
804
804
  }
805
805
 
806
806
  /**
807
807
  * Converts multiple DBMs to storage rows.
808
808
  */
809
- dbmsToStorageRows(dbms: DBM[]): ObjectWithId[] {
809
+ async dbmsToStorageRows(dbms: DBM[]): Promise<ObjectWithId[]> {
810
810
  if (!this.cfg.compress?.keys.length) return dbms
811
- return dbms.map(dbm => {
811
+ return await pMap(dbms, async dbm => {
812
812
  const row = { ...dbm }
813
- this.compress(row)
813
+ await this.compress(row)
814
814
  return row
815
815
  })
816
816
  }
@@ -846,13 +846,15 @@ export class CommonDao<
846
846
  /**
847
847
  * Mutates `dbm`.
848
848
  */
849
- private compress(dbm: DBM): void {
849
+ private async compress(dbm: DBM): Promise<void> {
850
850
  if (!this.cfg.compress?.keys.length) return // No compression requested
851
851
 
852
852
  const { keys, level = 1 } = this.cfg.compress
853
853
  const properties = _pick(dbm, keys)
854
854
  const bufferString = JSON.stringify(properties)
855
- const __compressed = zip2.zstdCompressSync(bufferString, level)
855
+ // Unlike `decompress`, we're testing to use async zstd compression.
856
+ // Async Decompression leaks memory severely. But Compression seems fine.
857
+ const __compressed = await zip2.zstdCompress(bufferString, level)
856
858
  _omitWithUndefined(dbm as any, _objectKeys(properties), { mutate: true })
857
859
  Object.assign(dbm, { __compressed })
858
860
  }
@@ -1113,7 +1115,7 @@ export class CommonDao<
1113
1115
  if (!inputs.length) return
1114
1116
  const { db } = inputs[0]!.dao.cfg
1115
1117
  const dbmsByTable: StringMap<any[]> = {}
1116
- for (const input of inputs) {
1118
+ await pMap(inputs, async input => {
1117
1119
  const { dao } = input
1118
1120
  const { table } = dao.cfg
1119
1121
  dbmsByTable[table] ||= []
@@ -1136,7 +1138,7 @@ export class CommonDao<
1136
1138
  dao.assignIdCreatedUpdated(row, opt)
1137
1139
  const dbm = dao.bmToDBM(row, opt)
1138
1140
  dao.cfg.hooks!.beforeSave?.(dbm)
1139
- const storageRow = dao.dbmToStorageRow(dbm)
1141
+ const storageRow = await dao.dbmToStorageRow(dbm)
1140
1142
  dbmsByTable[table].push(storageRow)
1141
1143
  } else {
1142
1144
  // Plural
@@ -1145,10 +1147,10 @@ export class CommonDao<
1145
1147
  if (dao.cfg.hooks!.beforeSave) {
1146
1148
  dbms.forEach(dbm => dao.cfg.hooks!.beforeSave!(dbm))
1147
1149
  }
1148
- const storageRows = dao.dbmsToStorageRows(dbms)
1150
+ const storageRows = await dao.dbmsToStorageRows(dbms)
1149
1151
  dbmsByTable[table].push(...storageRows)
1150
1152
  }
1151
- }
1153
+ })
1152
1154
 
1153
1155
  await db.multiSave(dbmsByTable)
1154
1156
  }
package/src/kv/index.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from './commonKeyValueDao.js'
2
2
  export * from './commonKeyValueDaoMemoCache.js'
3
3
  export * from './commonKeyValueDB.js'
4
- export * from './commonSyncKeyValueDB.js'
4
+ export type * from './commonSyncKeyValueDB.js'