@naturalcycles/db-lib 10.42.1 → 10.42.2

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,4 +1,5 @@
1
- export var DBLibError;
1
+ export { DBLibError };
2
+ var DBLibError;
2
3
  (function (DBLibError) {
3
4
  DBLibError["DB_ROW_REQUIRED"] = "DB_ROW_REQUIRED";
4
5
  DBLibError["DAO_IS_READ_ONLY"] = "DAO_IS_READ_ONLY";
@@ -162,13 +162,7 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
162
162
  * Converts multiple storage rows to DBMs.
163
163
  */
164
164
  storageRowsToDBMs(rows: ObjectWithId[]): Promise<DBM[]>;
165
- /**
166
- * Mutates `dbm`.
167
- */
168
165
  private compress;
169
- /**
170
- * Mutates `dbm`.
171
- */
172
166
  private decompress;
173
167
  anyToDBM(dbm: undefined, opt?: CommonDaoOptions): Promise<null>;
174
168
  anyToDBM(dbm?: any, opt?: CommonDaoOptions): Promise<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 = {}) {
@@ -178,28 +178,28 @@ 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
182
  }
183
183
  const isPartialQuery = !!q._selectedFieldNames;
184
184
  if (isPartialQuery)
185
185
  return pipeline;
186
186
  opt.skipValidation ??= true;
187
187
  opt.errorMode ||= ErrorMode.SUPPRESS;
188
- return pipeline.map(async (dbm) => await this.anyToDBM(dbm, opt), { errorMode: opt.errorMode });
188
+ return pipeline.map(async dbm => await this.anyToDBM(dbm, opt), { errorMode: opt.errorMode });
189
189
  }
190
190
  streamQuery(q, opt = {}) {
191
191
  this.validateQueryIndexes(q); // throws if query uses `excludeFromIndexes` property
192
192
  q.table = opt.table || q.table;
193
193
  let pipeline = this.cfg.db.streamQuery(q, opt);
194
194
  if (this.cfg.compress?.keys.length) {
195
- pipeline = pipeline.map(async (row) => await this.storageRowToDBM(row));
195
+ pipeline = pipeline.map(async row => await this.storageRowToDBM(row));
196
196
  }
197
197
  const isPartialQuery = !!q._selectedFieldNames;
198
198
  if (isPartialQuery)
199
199
  return pipeline;
200
200
  opt.skipValidation ??= true;
201
201
  opt.errorMode ||= ErrorMode.SUPPRESS;
202
- return pipeline.map(async (dbm) => await this.dbmToBM(dbm, opt), { errorMode: opt.errorMode });
202
+ return pipeline.map(async dbm => await this.dbmToBM(dbm, opt), { errorMode: opt.errorMode });
203
203
  }
204
204
  async queryIds(q, opt = {}) {
205
205
  this.validateQueryIndexes(q); // throws if query uses `excludeFromIndexes` property
@@ -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
  }
@@ -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
- .map(async (bm) => {
450
+ .map(async bm => {
451
451
  this.assignIdCreatedUpdated(bm, opt);
452
452
  const dbm = await this.bmToDBM(bm, opt);
453
453
  beforeSave?.(dbm);
454
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
  }, {
@@ -572,7 +572,7 @@ export class CommonDao {
572
572
  return this.validateAndConvert(bm, 'load', opt);
573
573
  }
574
574
  async dbmsToBM(dbms, opt = {}) {
575
- return await pMap(dbms, async (dbm) => await this.dbmToBM(dbm, opt));
575
+ return await pMap(dbms, async dbm => await this.dbmToBM(dbm, opt));
576
576
  }
577
577
  async bmToDBM(bm, opt) {
578
578
  if (bm === undefined)
@@ -585,7 +585,7 @@ export class CommonDao {
585
585
  }
586
586
  async bmsToDBM(bms, opt = {}) {
587
587
  // try/catch?
588
- return await pMap(bms, async (bm) => await this.bmToDBM(bm, opt));
588
+ return await pMap(bms, async bm => await this.bmToDBM(bm, opt));
589
589
  }
590
590
  // STORAGE LAYER (compression/decompression at DB boundary)
591
591
  // These methods convert between DBM (logical model) and storage format (physical, possibly compressed).
@@ -614,7 +614,7 @@ export class CommonDao {
614
614
  async dbmsToStorageRows(dbms) {
615
615
  if (!this.cfg.compress?.keys.length)
616
616
  return dbms;
617
- return await pMap(dbms, async (dbm) => await this.dbmToStorageRow(dbm));
617
+ return await pMap(dbms, async dbm => await this.dbmToStorageRow(dbm));
618
618
  }
619
619
  /**
620
620
  * Converts a storage row back to a DBM, applying decompression if needed.
@@ -638,7 +638,7 @@ export class CommonDao {
638
638
  async storageRowsToDBMs(rows) {
639
639
  if (!this.cfg.compress?.keys.length)
640
640
  return rows;
641
- return await pMap(rows, async (row) => await this.storageRowToDBM(row));
641
+ return await pMap(rows, async row => await this.storageRowToDBM(row));
642
642
  }
643
643
  /**
644
644
  * Mutates `dbm`.
@@ -679,7 +679,7 @@ export class CommonDao {
679
679
  return dbm;
680
680
  }
681
681
  async anyToDBMs(rows, opt = {}) {
682
- return await pMap(rows, async (entity) => await this.anyToDBM(entity, opt));
682
+ return await pMap(rows, async entity => await this.anyToDBM(entity, opt));
683
683
  }
684
684
  /**
685
685
  * Returns *converted value* (NOT the same reference).
@@ -923,7 +923,7 @@ export class CommonDao {
923
923
  return;
924
924
  const { db } = inputs[0].dao.cfg;
925
925
  const dbmsByTable = {};
926
- await pMap(inputs, async (input) => {
926
+ await pMap(inputs, async input => {
927
927
  const { dao } = input;
928
928
  const { table } = dao.cfg;
929
929
  dbmsByTable[table] ||= [];
@@ -965,7 +965,7 @@ export class CommonDao {
965
965
  }
966
966
  async runInTransaction(fn, opt) {
967
967
  let r;
968
- await this.cfg.db.runInTransaction(async (tx) => {
968
+ await this.cfg.db.runInTransaction(async tx => {
969
969
  const daoTx = new CommonDaoTransaction(tx, this.cfg.logger);
970
970
  try {
971
971
  r = await fn(daoTx);
@@ -1,4 +1,5 @@
1
- export var CommonDaoLogLevel;
1
+ export { CommonDaoLogLevel };
2
+ var CommonDaoLogLevel;
2
3
  (function (CommonDaoLogLevel) {
3
4
  /**
4
5
  * Same as undefined
@@ -1,4 +1,5 @@
1
- export var CommonDBType;
1
+ export { CommonDBType };
2
+ var CommonDBType;
2
3
  (function (CommonDBType) {
3
4
  CommonDBType["document"] = "document";
4
5
  CommonDBType["relational"] = "relational";
package/dist/db.model.js CHANGED
@@ -1,9 +1,11 @@
1
- export var DBRelation;
1
+ export { DBRelation };
2
+ var DBRelation;
2
3
  (function (DBRelation) {
3
4
  DBRelation["ONE_TO_ONE"] = "ONE_TO_ONE";
4
5
  DBRelation["ONE_TO_MANY"] = "ONE_TO_MANY";
5
6
  })(DBRelation || (DBRelation = {}));
6
- export var DBModelType;
7
+ export { DBModelType };
8
+ var DBModelType;
7
9
  (function (DBModelType) {
8
10
  DBModelType["DBM"] = "DBM";
9
11
  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;
12
- increment?: boolean;
11
+ count?: boolean | undefined;
12
+ increment?: boolean | undefined;
13
13
  };
14
14
  data: StringMap<StringMap<any>>;
15
15
  ping(): Promise<void>;
@@ -7,14 +7,14 @@ import { decompressZstdOrInflateToString, deflateString, inflateToString, zstdCo
7
7
  */
8
8
  export function commonKeyValueDaoDeflatedJsonTransformer() {
9
9
  return {
10
- valueToBuffer: async (v) => await deflateString(JSON.stringify(v)),
11
- bufferToValue: async (buf) => JSON.parse(await inflateToString(buf)),
10
+ valueToBuffer: async v => await deflateString(JSON.stringify(v)),
11
+ bufferToValue: async buf => JSON.parse(await inflateToString(buf)),
12
12
  };
13
13
  }
14
14
  export function commonKeyValueDaoZstdJsonTransformer(level) {
15
15
  return {
16
- valueToBuffer: async (v) => await zstdCompress(JSON.stringify(v), level),
17
- bufferToValue: async (buf) => JSON.parse(await zstdDecompressToString(buf)),
16
+ valueToBuffer: async v => await zstdCompress(JSON.stringify(v), level),
17
+ bufferToValue: async buf => JSON.parse(await zstdDecompressToString(buf)),
18
18
  };
19
19
  }
20
20
  /**
@@ -23,8 +23,8 @@ export function commonKeyValueDaoZstdJsonTransformer(level) {
23
23
  */
24
24
  export function commonKeyValueDaoCompressedTransformer() {
25
25
  return {
26
- valueToBuffer: async (v) => await zstdCompress(JSON.stringify(v)),
27
- bufferToValue: async (buf) => JSON.parse(await decompressZstdOrInflateToString(buf)),
26
+ valueToBuffer: async v => await zstdCompress(JSON.stringify(v)),
27
+ bufferToValue: async buf => JSON.parse(await decompressZstdOrInflateToString(buf)),
28
28
  };
29
29
  }
30
30
  // todo: logging
@@ -117,7 +117,7 @@ export class CommonKeyValueDao {
117
117
  if (!transformer) {
118
118
  return this.cfg.db.streamValues(this.cfg.table, limit);
119
119
  }
120
- return this.cfg.db.streamValues(this.cfg.table, limit).map(async (buf) => {
120
+ return this.cfg.db.streamValues(this.cfg.table, limit).map(async buf => {
121
121
  try {
122
122
  return await transformer.bufferToValue(buf);
123
123
  }
@@ -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 }]);
@@ -243,7 +243,7 @@ export async function runCommonDaoTest(db, quirks = {}) {
243
243
  await dao.query().deleteByQuery();
244
244
  // Test that id, created, updated are created
245
245
  const now = localTime.nowUnix();
246
- await dao.runInTransaction(async (tx) => {
246
+ await dao.runInTransaction(async tx => {
247
247
  const row = _omit(item1, ['id', 'created', 'updated']);
248
248
  await tx.save(dao, row);
249
249
  });
@@ -252,14 +252,14 @@ export async function runCommonDaoTest(db, quirks = {}) {
252
252
  expect(loaded[0].id).toBeDefined();
253
253
  expect(loaded[0].created).toBeGreaterThanOrEqual(now);
254
254
  expect(loaded[0].updated).toBe(loaded[0].created);
255
- await dao.runInTransaction(async (tx) => {
255
+ await dao.runInTransaction(async tx => {
256
256
  await tx.deleteById(dao, loaded[0].id);
257
257
  });
258
258
  // saveBatch [item1, 2, 3]
259
259
  // save item3 with k1: k1_mod
260
260
  // delete item2
261
261
  // remaining: item1, item3_with_k1_mod
262
- await dao.runInTransaction(async (tx) => {
262
+ await dao.runInTransaction(async tx => {
263
263
  await tx.saveBatch(dao, items);
264
264
  await tx.save(dao, { ...items[2], k1: 'k1_mod' });
265
265
  await tx.deleteById(dao, items[1].id);
@@ -304,7 +304,7 @@ export async function runCommonDaoTest(db, quirks = {}) {
304
304
  const expected = await prepare();
305
305
  let err;
306
306
  try {
307
- await dao.runInTransaction(async (tx) => {
307
+ await dao.runInTransaction(async tx => {
308
308
  await tx.deleteById(dao, items[2].id);
309
309
  await tx.save(dao, { ...items[0], k1: 5 }); // it should fail here
310
310
  });
@@ -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,5 +1,3 @@
1
- import type { ObjectWithId } from '@naturalcycles/js-lib/types';
2
- import type { JsonSchemaObjectBuilder } from '@naturalcycles/nodejs-lib/ajv';
3
1
  import type { CommonDBOptions } from '../db.model.js';
4
2
  export declare const commonDBOptionsSchema: () => JsonSchemaObjectBuilder<CommonDBOptions, CommonDBOptions>;
5
3
  export declare const commonDBSaveOptionsSchema: <ROW extends ObjectWithId>() => any;
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
3
  "type": "module",
4
- "version": "10.42.1",
4
+ "version": "10.42.2",
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-0",
10
+ "@typescript/native-preview": "7.0.0-dev.20260201.1",
11
11
  "@naturalcycles/dev-lib": "18.4.2"
12
12
  },
13
13
  "files": [