@naturalcycles/db-lib 10.49.0 → 10.50.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.
|
@@ -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`.
|
|
@@ -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));
|
|
@@ -451,7 +451,7 @@ export class CommonDao {
|
|
|
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
457
|
.map(async batch => {
|
|
@@ -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
|
|
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
|
-
|
|
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
|
-
|
|
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) {
|
package/package.json
CHANGED
|
@@ -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) {
|
|
@@ -602,7 +602,7 @@ export class CommonDao<
|
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
}
|