@naturalcycles/db-lib 8.43.3 → 8.43.5

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.
Files changed (38) hide show
  1. package/dist/adapter/cachedb/cache.db.d.ts +2 -2
  2. package/dist/adapter/cachedb/cache.db.js +2 -2
  3. package/dist/adapter/cachedb/cache.db.model.d.ts +1 -1
  4. package/dist/adapter/file/file.db.d.ts +1 -1
  5. package/dist/adapter/file/localFile.persistence.plugin.js +5 -5
  6. package/dist/adapter/inmemory/inMemory.db.d.ts +1 -1
  7. package/dist/adapter/inmemory/inMemory.db.js +6 -6
  8. package/dist/adapter/inmemory/inMemoryKeyValueDB.js +4 -4
  9. package/dist/base.common.db.d.ts +1 -1
  10. package/dist/base.common.db.js +2 -2
  11. package/dist/common.db.d.ts +1 -1
  12. package/dist/commondao/common.dao.js +9 -2
  13. package/dist/commondao/common.dao.model.d.ts +1 -1
  14. package/dist/db.model.d.ts +6 -6
  15. package/dist/kv/commonKeyValueDB.d.ts +1 -1
  16. package/dist/kv/commonKeyValueDao.js +10 -2
  17. package/dist/pipeline/dbPipelineBackup.d.ts +1 -1
  18. package/dist/pipeline/dbPipelineBackup.js +2 -2
  19. package/dist/pipeline/dbPipelineRestore.js +2 -2
  20. package/dist/query/dbQuery.d.ts +1 -1
  21. package/dist/timeseries/timeSeries.model.d.ts +1 -1
  22. package/dist/transaction/dbTransaction.d.ts +3 -3
  23. package/dist/validation/index.d.ts +1 -1
  24. package/package.json +1 -1
  25. package/src/adapter/cachedb/cache.db.model.ts +1 -1
  26. package/src/adapter/cachedb/cache.db.ts +2 -2
  27. package/src/adapter/file/file.db.ts +3 -2
  28. package/src/adapter/file/localFile.persistence.plugin.ts +2 -2
  29. package/src/adapter/inmemory/inMemory.db.ts +3 -3
  30. package/src/adapter/inmemory/inMemoryKeyValueDB.ts +1 -1
  31. package/src/base.common.db.ts +2 -2
  32. package/src/common.db.ts +1 -1
  33. package/src/commondao/common.dao.ts +18 -10
  34. package/src/db.model.ts +3 -3
  35. package/src/kv/commonKeyValueDao.ts +20 -10
  36. package/src/pipeline/dbPipelineBackup.ts +1 -1
  37. package/src/pipeline/dbPipelineRestore.ts +1 -1
  38. package/src/transaction/dbTransaction.ts +3 -3
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { Readable } from 'stream';
2
+ import { Readable } from 'node:stream';
3
3
  import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
4
4
  import { BaseCommonDB } from '../../base.common.db';
5
5
  import { CommonDB } from '../../common.db';
@@ -25,7 +25,7 @@ export declare class CacheDB extends BaseCommonDB implements CommonDB {
25
25
  createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CacheDBCreateOptions): Promise<void>;
26
26
  getByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], opt?: CacheDBSaveOptions<ROW>): Promise<ROW[]>;
27
27
  deleteByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], opt?: CacheDBOptions): Promise<number>;
28
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CacheDBSaveOptions<ROW>): Promise<void>;
28
+ saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CacheDBSaveOptions<ROW>): Promise<void>;
29
29
  runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBSaveOptions<ROW>): Promise<RunQueryResult<ROW>>;
30
30
  runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>;
31
31
  streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBStreamOptions): Readable;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CacheDB = void 0;
4
- const stream_1 = require("stream");
4
+ const node_stream_1 = require("node:stream");
5
5
  const base_common_db_1 = require("../../base.common.db");
6
6
  /**
7
7
  * CommonDB implementation that proxies requests to downstream CommonDB
@@ -160,7 +160,7 @@ class CacheDB extends base_common_db_1.BaseCommonDB {
160
160
  return stream;
161
161
  }
162
162
  if (opt.skipCache || this.cfg.skipCache)
163
- return stream_1.Readable.from([]);
163
+ return node_stream_1.Readable.from([]);
164
164
  const stream = this.cfg.cacheDB.streamQuery(q, opt);
165
165
  // if (this.cfg.logCached) {
166
166
  // let count = 0
@@ -47,7 +47,7 @@ export interface CacheDBOptions {
47
47
  */
48
48
  onlyCache?: boolean;
49
49
  }
50
- export interface CacheDBSaveOptions<ROW extends ObjectWithId> extends CacheDBOptions, CommonDBSaveOptions<ROW> {
50
+ export interface CacheDBSaveOptions<ROW extends Partial<ObjectWithId>> extends CacheDBOptions, CommonDBSaveOptions<ROW> {
51
51
  }
52
52
  export interface CacheDBStreamOptions extends CacheDBOptions, CommonDBStreamOptions {
53
53
  }
@@ -22,7 +22,7 @@ export declare class FileDB extends BaseCommonDB implements CommonDB {
22
22
  ping(): Promise<void>;
23
23
  getTables(): Promise<string[]>;
24
24
  getByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], _opt?: CommonDBOptions): Promise<ROW[]>;
25
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], _opt?: CommonDBSaveOptions<ROW>): Promise<void>;
25
+ saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], _opt?: CommonDBSaveOptions<ROW>): Promise<void>;
26
26
  /**
27
27
  * Implementation is optimized for loading/saving _whole files_.
28
28
  */
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LocalFilePersistencePlugin = void 0;
4
- const stream_1 = require("stream");
5
- const zlib_1 = require("zlib");
4
+ const node_stream_1 = require("node:stream");
5
+ const node_zlib_1 = require("node:zlib");
6
6
  const js_lib_1 = require("@naturalcycles/js-lib");
7
7
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
8
8
  const fs = require("fs-extra");
@@ -29,7 +29,7 @@ class LocalFilePersistencePlugin {
29
29
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`;
30
30
  if (!(await fs.pathExists(filePath)))
31
31
  return [];
32
- const transformUnzip = this.cfg.gzip ? [(0, zlib_1.createUnzip)()] : [];
32
+ const transformUnzip = this.cfg.gzip ? [(0, node_zlib_1.createUnzip)()] : [];
33
33
  const rows = [];
34
34
  await (0, nodejs_lib_1._pipeline)([
35
35
  fs.createReadStream(filePath),
@@ -47,9 +47,9 @@ class LocalFilePersistencePlugin {
47
47
  await fs.ensureDir(this.cfg.storagePath);
48
48
  const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`;
49
49
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`;
50
- const transformZip = this.cfg.gzip ? [(0, zlib_1.createGzip)()] : [];
50
+ const transformZip = this.cfg.gzip ? [(0, node_zlib_1.createGzip)()] : [];
51
51
  await (0, nodejs_lib_1._pipeline)([
52
- stream_1.Readable.from(rows),
52
+ node_stream_1.Readable.from(rows),
53
53
  (0, nodejs_lib_1.transformToNDJson)(),
54
54
  ...transformZip,
55
55
  fs.createWriteStream(filePath),
@@ -53,7 +53,7 @@ export declare class InMemoryDB implements CommonDB {
53
53
  getTableSchema<ROW extends ObjectWithId>(_table: string): Promise<JsonSchemaRootObject<ROW>>;
54
54
  createTable<ROW extends ObjectWithId>(_table: string, _schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
55
55
  getByIds<ROW extends ObjectWithId>(_table: string, ids: ROW['id'][], _opt?: CommonDBOptions): Promise<ROW[]>;
56
- saveBatch<ROW extends ObjectWithId>(_table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
56
+ saveBatch<ROW extends Partial<ObjectWithId>>(_table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
57
57
  deleteByIds<ROW extends ObjectWithId>(_table: string, ids: ROW['id'][], _opt?: CommonDBOptions): Promise<number>;
58
58
  deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
59
59
  runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<RunQueryResult<ROW>>;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InMemoryDB = void 0;
4
- const stream_1 = require("stream");
5
- const zlib_1 = require("zlib");
4
+ const node_stream_1 = require("node:stream");
5
+ const node_zlib_1 = require("node:zlib");
6
6
  const js_lib_1 = require("@naturalcycles/js-lib");
7
7
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
8
8
  const colors_1 = require("@naturalcycles/nodejs-lib/dist/colors");
@@ -122,7 +122,7 @@ class InMemoryDB {
122
122
  }
123
123
  streamQuery(q, _opt) {
124
124
  const table = this.cfg.tablesPrefix + q.table;
125
- return stream_1.Readable.from((0, __1.queryInMemory)(q, Object.values(this.data[table] || {})));
125
+ return node_stream_1.Readable.from((0, __1.queryInMemory)(q, Object.values(this.data[table] || {})));
126
126
  }
127
127
  async commitTransaction(tx, opt) {
128
128
  const backup = (0, js_lib_1._deepCopy)(this.data);
@@ -156,7 +156,7 @@ class InMemoryDB {
156
156
  const { persistentStoragePath, persistZip } = this.cfg;
157
157
  const started = Date.now();
158
158
  await fs.emptyDir(persistentStoragePath);
159
- const transformZip = persistZip ? [(0, zlib_1.createGzip)()] : [];
159
+ const transformZip = persistZip ? [(0, node_zlib_1.createGzip)()] : [];
160
160
  let tables = 0;
161
161
  // infinite concurrency for now
162
162
  await (0, js_lib_1.pMap)(Object.keys(this.data), async (table) => {
@@ -166,7 +166,7 @@ class InMemoryDB {
166
166
  tables++;
167
167
  const fname = `${persistentStoragePath}/${table}.ndjson${persistZip ? '.gz' : ''}`;
168
168
  await (0, nodejs_lib_1._pipeline)([
169
- stream_1.Readable.from(rows),
169
+ node_stream_1.Readable.from(rows),
170
170
  (0, nodejs_lib_1.transformToNDJson)(),
171
171
  ...transformZip,
172
172
  fs.createWriteStream(fname),
@@ -190,7 +190,7 @@ class InMemoryDB {
190
190
  await (0, js_lib_1.pMap)(files, async (file) => {
191
191
  const fname = `${persistentStoragePath}/${file}`;
192
192
  const table = file.split('.ndjson')[0];
193
- const transformUnzip = file.endsWith('.gz') ? [(0, zlib_1.createUnzip)()] : [];
193
+ const transformUnzip = file.endsWith('.gz') ? [(0, node_zlib_1.createUnzip)()] : [];
194
194
  const rows = [];
195
195
  await (0, nodejs_lib_1._pipeline)([
196
196
  fs.createReadStream(fname),
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InMemoryKeyValueDB = void 0;
4
- const stream_1 = require("stream");
4
+ const node_stream_1 = require("node:stream");
5
5
  class InMemoryKeyValueDB {
6
6
  constructor(cfg = {}) {
7
7
  this.cfg = cfg;
@@ -26,13 +26,13 @@ class InMemoryKeyValueDB {
26
26
  entries.forEach(([id, buf]) => (this.data[table][id] = buf));
27
27
  }
28
28
  streamIds(table, limit) {
29
- return stream_1.Readable.from(Object.keys(this.data[table] || {}).slice(0, limit));
29
+ return node_stream_1.Readable.from(Object.keys(this.data[table] || {}).slice(0, limit));
30
30
  }
31
31
  streamValues(table, limit) {
32
- return stream_1.Readable.from(Object.values(this.data[table] || {}).slice(0, limit));
32
+ return node_stream_1.Readable.from(Object.values(this.data[table] || {}).slice(0, limit));
33
33
  }
34
34
  streamEntries(table, limit) {
35
- return stream_1.Readable.from(Object.entries(this.data[table] || {}).slice(0, limit));
35
+ return node_stream_1.Readable.from(Object.entries(this.data[table] || {}).slice(0, limit));
36
36
  }
37
37
  async count(table) {
38
38
  var _a;
@@ -18,7 +18,7 @@ export declare class BaseCommonDB implements CommonDB {
18
18
  getByIds<ROW extends ObjectWithId>(_table: string, _ids: ROW['id'][]): Promise<ROW[]>;
19
19
  runQuery<ROW extends ObjectWithId>(_q: DBQuery<ROW>): Promise<RunQueryResult<ROW>>;
20
20
  runQueryCount<ROW extends ObjectWithId>(_q: DBQuery<ROW>): Promise<number>;
21
- saveBatch<ROW extends ObjectWithId>(_table: string, _rows: ROW[], _opt?: CommonDBSaveOptions<ROW>): Promise<void>;
21
+ saveBatch<ROW extends Partial<ObjectWithId>>(_table: string, _rows: ROW[], _opt?: CommonDBSaveOptions<ROW>): Promise<void>;
22
22
  streamQuery<ROW extends ObjectWithId>(_q: DBQuery<ROW>): ReadableTyped<ROW>;
23
23
  /**
24
24
  * Naive implementation.
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseCommonDB = void 0;
4
- const stream_1 = require("stream");
4
+ const node_stream_1 = require("node:stream");
5
5
  const dbTransaction_util_1 = require("./transaction/dbTransaction.util");
6
6
  /**
7
7
  * No-op implementation of CommonDB interface.
@@ -39,7 +39,7 @@ class BaseCommonDB {
39
39
  }
40
40
  async saveBatch(_table, _rows, _opt) { }
41
41
  streamQuery(_q) {
42
- return stream_1.Readable.from([]);
42
+ return node_stream_1.Readable.from([]);
43
43
  }
44
44
  /**
45
45
  * Naive implementation.
@@ -41,7 +41,7 @@ export interface CommonDB {
41
41
  /**
42
42
  * rows can have missing ids only if DB supports auto-generating them (like mysql auto_increment).
43
43
  */
44
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
44
+ saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
45
45
  /**
46
46
  * Returns number of deleted items.
47
47
  * Not supported by all implementations (e.g Datastore will always return same number as number of ids).
@@ -407,7 +407,9 @@ class CommonDao {
407
407
  const stream = this.cfg.db.streamQuery(q, opt);
408
408
  if (partialQuery || opt.raw)
409
409
  return stream;
410
- return stream.pipe((0, nodejs_lib_1.transformMapSimple)(dbm => this.anyToDBM(dbm, opt), {
410
+ return stream
411
+ .on('error', err => stream.emit('error', err))
412
+ .pipe((0, nodejs_lib_1.transformMapSimple)(dbm => this.anyToDBM(dbm, opt), {
411
413
  errorMode: js_lib_1.ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
412
414
  }));
413
415
  }
@@ -433,6 +435,7 @@ class CommonDao {
433
435
  // optimization: 1 validation is enough
434
436
  // .pipe(transformMap<any, DBM>(dbm => this.anyToDBM(dbm, opt), safeOpt))
435
437
  // .pipe(transformMap<DBM, Saved<BM>>(dbm => this.dbmToBM(dbm, opt), safeOpt))
438
+ .on('error', err => stream.emit('error', err))
436
439
  .pipe((0, nodejs_lib_1.transformMap)(async (dbm) => await this.dbmToBM(dbm, opt), {
437
440
  errorMode: js_lib_1.ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
438
441
  }))
@@ -449,9 +452,13 @@ class CommonDao {
449
452
  streamQueryIds(q, opt = {}) {
450
453
  q.table = opt.table || q.table;
451
454
  opt.errorMode || (opt.errorMode = js_lib_1.ErrorMode.SUPPRESS);
452
- return this.cfg.db.streamQuery(q.select(['id']), opt).pipe((0, nodejs_lib_1.transformMapSimple)(objectWithId => objectWithId.id, {
455
+ const stream = this.cfg.db
456
+ .streamQuery(q.select(['id']), opt)
457
+ .on('error', err => stream.emit('error', err))
458
+ .pipe((0, nodejs_lib_1.transformMapSimple)(objectWithId => objectWithId.id, {
453
459
  errorMode: js_lib_1.ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
454
460
  }));
461
+ return stream;
455
462
  }
456
463
  async streamQueryIdsForEach(q, mapper, opt = {}) {
457
464
  q.table = opt.table || q.table;
@@ -210,4 +210,4 @@ export interface CommonDaoStreamOptions extends CommonDaoOptions {
210
210
  */
211
211
  errorMode?: ErrorMode;
212
212
  }
213
- export declare type CommonDaoCreateOptions = CommonDBCreateOptions;
213
+ export type CommonDaoCreateOptions = CommonDBCreateOptions;
@@ -1,4 +1,4 @@
1
- import { AnyObjectWithId, ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { ObjectWithId } from '@naturalcycles/js-lib';
2
2
  /**
3
3
  * Similar to SQL INSERT, UPDATE.
4
4
  * Insert will fail if row already exists.
@@ -7,13 +7,13 @@ import { AnyObjectWithId, ObjectWithId } from '@naturalcycles/js-lib';
7
7
  *
8
8
  * Default is Upsert.
9
9
  */
10
- export declare type CommonDBSaveMethod = 'upsert' | 'insert' | 'update';
10
+ export type CommonDBSaveMethod = 'upsert' | 'insert' | 'update';
11
11
  export interface CommonDBOptions {
12
12
  }
13
13
  /**
14
14
  * All properties default to undefined.
15
15
  */
16
- export interface CommonDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId> extends CommonDBOptions {
16
+ export interface CommonDBSaveOptions<ROW extends Partial<ObjectWithId> = any> extends CommonDBOptions {
17
17
  excludeFromIndexes?: (keyof ROW)[];
18
18
  /**
19
19
  * Default is `upsert`
@@ -27,7 +27,7 @@ export interface CommonDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId>
27
27
  */
28
28
  assignGeneratedIds?: boolean;
29
29
  }
30
- export declare type CommonDBStreamOptions = CommonDBOptions;
30
+ export type CommonDBStreamOptions = CommonDBOptions;
31
31
  export interface CommonDBCreateOptions extends CommonDBOptions {
32
32
  /**
33
33
  * Caution! If set to true - will actually DROP the table!
@@ -40,8 +40,8 @@ export interface RunQueryResult<T> {
40
40
  rows: T[];
41
41
  endCursor?: string;
42
42
  }
43
- export declare type DBOperation = DBSaveBatchOperation | DBDeleteByIdsOperation;
44
- export interface DBSaveBatchOperation<ROW extends ObjectWithId = any> {
43
+ export type DBOperation = DBSaveBatchOperation | DBDeleteByIdsOperation;
44
+ export interface DBSaveBatchOperation<ROW extends Partial<ObjectWithId> = any> {
45
45
  type: 'saveBatch';
46
46
  table: string;
47
47
  rows: ROW[];
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
3
  import { CommonDBCreateOptions } from '../db.model';
4
- export declare type KeyValueDBTuple = [key: string, value: Buffer];
4
+ export type KeyValueDBTuple = [key: string, value: Buffer];
5
5
  /**
6
6
  * Common interface for Key-Value database implementations.
7
7
  *
@@ -103,17 +103,25 @@ class CommonKeyValueDao {
103
103
  }
104
104
  // todo: consider it when readableMap supports `errorMode: SUPPRESS`
105
105
  // readableMap(this.cfg.db.streamValues(this.cfg.table, limit), async buf => await this.cfg.hooks!.mapBufferToValue(buf))
106
- return this.cfg.db.streamValues(this.cfg.table, limit).pipe((0, nodejs_lib_1.transformMap)(async (buf) => await this.cfg.hooks.mapBufferToValue(buf), {
106
+ const stream = this.cfg.db
107
+ .streamValues(this.cfg.table, limit)
108
+ .on('error', err => stream.emit('error', err))
109
+ .pipe((0, nodejs_lib_1.transformMap)(async (buf) => await this.cfg.hooks.mapBufferToValue(buf), {
107
110
  errorMode: js_lib_1.ErrorMode.SUPPRESS, // cause .pipe cannot propagate errors
108
111
  }));
112
+ return stream;
109
113
  }
110
114
  streamEntries(limit) {
111
115
  if (!this.cfg.hooks?.mapBufferToValue) {
112
116
  return this.cfg.db.streamEntries(this.cfg.table, limit);
113
117
  }
114
- return this.cfg.db.streamEntries(this.cfg.table, limit).pipe((0, nodejs_lib_1.transformMap)(async ([id, buf]) => [id, await this.cfg.hooks.mapBufferToValue(buf)], {
118
+ const stream = this.cfg.db
119
+ .streamEntries(this.cfg.table, limit)
120
+ .on('error', err => stream.emit('error', err))
121
+ .pipe((0, nodejs_lib_1.transformMap)(async ([id, buf]) => [id, await this.cfg.hooks.mapBufferToValue(buf)], {
115
122
  errorMode: js_lib_1.ErrorMode.SUPPRESS, // cause .pipe cannot propagate errors
116
123
  }));
124
+ return stream;
117
125
  }
118
126
  }
119
127
  exports.CommonKeyValueDao = CommonKeyValueDao;
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { ZlibOptions } from 'zlib';
2
+ import { ZlibOptions } from 'node:zlib';
3
3
  import { AsyncMapper, ErrorMode } from '@naturalcycles/js-lib';
4
4
  import { NDJsonStats, TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib';
5
5
  import { CommonDB } from '../common.db';
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.dbPipelineBackup = void 0;
4
- const zlib_1 = require("zlib");
4
+ const node_zlib_1 = require("node:zlib");
5
5
  const js_lib_1 = require("@naturalcycles/js-lib");
6
6
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
7
7
  const colors_1 = require("@naturalcycles/nodejs-lib/dist/colors");
@@ -65,7 +65,7 @@ async function dbPipelineBackup(opt) {
65
65
  rows++;
66
66
  }),
67
67
  (0, nodejs_lib_1.transformToNDJson)({ strict, sortObjects }),
68
- ...(gzip ? [(0, zlib_1.createGzip)(zlibOptions)] : []),
68
+ ...(gzip ? [(0, node_zlib_1.createGzip)(zlibOptions)] : []),
69
69
  fs.createWriteStream(filePath),
70
70
  ]);
71
71
  const { size: sizeBytes } = await fs.stat(filePath);
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.dbPipelineRestore = void 0;
4
- const zlib_1 = require("zlib");
4
+ const node_zlib_1 = require("node:zlib");
5
5
  const js_lib_1 = require("@naturalcycles/js-lib");
6
6
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
7
7
  const colors_1 = require("@naturalcycles/nodejs-lib/dist/colors");
@@ -68,7 +68,7 @@ async function dbPipelineRestore(opt) {
68
68
  console.log(`<< ${(0, colors_1.grey)(filePath)} ${(0, colors_1.dimWhite)((0, js_lib_1._hb)(sizeBytes))} started...`);
69
69
  await (0, nodejs_lib_1._pipeline)([
70
70
  fs.createReadStream(filePath),
71
- ...(gzip ? [(0, zlib_1.createUnzip)()] : []),
71
+ ...(gzip ? [(0, node_zlib_1.createUnzip)()] : []),
72
72
  (0, nodejs_lib_1.transformSplit)(),
73
73
  (0, nodejs_lib_1.transformJsonParse)({ strict }),
74
74
  (0, nodejs_lib_1.transformTap)(() => rows++),
@@ -22,7 +22,7 @@ import { RunQueryResult } from '../db.model';
22
22
  *
23
23
  * You may also look at queryInMemory() for its implementation (it implements all those).
24
24
  */
25
- export declare type DBQueryFilterOperator = '<' | '<=' | '==' | '!=' | '>=' | '>' | 'in' | 'not-in' | 'array-contains' | 'array-contains-any';
25
+ export type DBQueryFilterOperator = '<' | '<=' | '==' | '!=' | '>=' | '>' | 'in' | 'not-in' | 'array-contains' | 'array-contains-any';
26
26
  export declare const dbQueryFilterOperatorValues: DBQueryFilterOperator[];
27
27
  export interface DBQueryFilter<ROW extends ObjectWithId = AnyObjectWithId> {
28
28
  name: keyof ROW;
@@ -7,7 +7,7 @@ export interface CommonTimeSeriesDaoCfg {
7
7
  * Second number: value
8
8
  * null in the second position means "absense of value" (may exist in _RAW table)
9
9
  */
10
- export declare type TimeSeriesDataPoint = [number, number | null];
10
+ export type TimeSeriesDataPoint = [number, number | null];
11
11
  export interface TimeSeriesRow {
12
12
  id: number;
13
13
  ts: number;
@@ -11,8 +11,8 @@ export declare class DBTransaction {
11
11
  * Convenience method.
12
12
  */
13
13
  static create(ops?: DBOperation[]): DBTransaction;
14
- save<ROW extends ObjectWithId>(table: string, row: ROW): this;
15
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[]): this;
14
+ save<ROW extends Partial<ObjectWithId>>(table: string, row: ROW): this;
15
+ saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[]): this;
16
16
  deleteById(table: string, id: string): this;
17
17
  deleteByIds(table: string, ids: string[]): this;
18
18
  }
@@ -23,5 +23,5 @@ export declare class DBTransaction {
23
23
  export declare class RunnableDBTransaction extends DBTransaction {
24
24
  db: CommonDB;
25
25
  constructor(db: CommonDB);
26
- commit<ROW extends ObjectWithId>(opt?: CommonDBSaveOptions<ROW>): Promise<void>;
26
+ commit<ROW extends Partial<ObjectWithId>>(opt?: CommonDBSaveOptions<ROW>): Promise<void>;
27
27
  }
@@ -1,7 +1,7 @@
1
1
  import { CommonDBOptions, CommonDBSaveOptions } from '../db.model';
2
2
  import { DBQuery, DBQueryFilter, DBQueryOrder } from '../query/dbQuery';
3
3
  export declare const commonDBOptionsSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<CommonDBOptions, CommonDBOptions>;
4
- export declare const commonDBSaveOptionsSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<CommonDBSaveOptions<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>, CommonDBSaveOptions<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>>;
4
+ export declare const commonDBSaveOptionsSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<CommonDBSaveOptions<any>, CommonDBSaveOptions<any>>;
5
5
  export declare const dbQueryFilterOperatorSchema: import("@naturalcycles/nodejs-lib/dist/validation/joi/string.extensions").ExtendedStringSchema;
6
6
  export declare const dbQueryFilterSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryFilter<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>, DBQueryFilter<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>>;
7
7
  export declare const dbQueryOrderSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryOrder<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>, DBQueryOrder<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>>;
package/package.json CHANGED
@@ -41,7 +41,7 @@
41
41
  "engines": {
42
42
  "node": ">=14.15"
43
43
  },
44
- "version": "8.43.3",
44
+ "version": "8.43.5",
45
45
  "description": "Lowest Common Denominator API to supported Databases",
46
46
  "keywords": [
47
47
  "db",
@@ -57,7 +57,7 @@ export interface CacheDBOptions {
57
57
  onlyCache?: boolean
58
58
  }
59
59
 
60
- export interface CacheDBSaveOptions<ROW extends ObjectWithId>
60
+ export interface CacheDBSaveOptions<ROW extends Partial<ObjectWithId>>
61
61
  extends CacheDBOptions,
62
62
  CommonDBSaveOptions<ROW> {}
63
63
 
@@ -1,4 +1,4 @@
1
- import { Readable } from 'stream'
1
+ import { Readable } from 'node:stream'
2
2
  import {
3
3
  JsonSchemaObject,
4
4
  JsonSchemaRootObject,
@@ -147,7 +147,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
147
147
  return deletedIds
148
148
  }
149
149
 
150
- override async saveBatch<ROW extends ObjectWithId>(
150
+ override async saveBatch<ROW extends Partial<ObjectWithId>>(
151
151
  table: string,
152
152
  rows: ROW[],
153
153
  opt: CacheDBSaveOptions<ROW> = {},
@@ -15,6 +15,7 @@ import {
15
15
  _assert,
16
16
  _deepCopy,
17
17
  _stringMapEntries,
18
+ Saved,
18
19
  } from '@naturalcycles/js-lib'
19
20
  import { readableCreate, ReadableTyped } from '@naturalcycles/nodejs-lib'
20
21
  import { dimGrey } from '@naturalcycles/nodejs-lib/dist/colors'
@@ -73,7 +74,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
73
74
  return ids.map(id => byId[id]!).filter(Boolean)
74
75
  }
75
76
 
76
- override async saveBatch<ROW extends ObjectWithId>(
77
+ override async saveBatch<ROW extends Partial<ObjectWithId>>(
77
78
  table: string,
78
79
  rows: ROW[],
79
80
  _opt?: CommonDBSaveOptions<ROW>,
@@ -81,7 +82,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
81
82
  if (!rows.length) return // save some api calls
82
83
 
83
84
  // 1. Load the whole file
84
- const byId = _by(await this.loadFile<ROW>(table), r => r.id)
85
+ const byId = _by(await this.loadFile<Saved<ROW>>(table), r => r.id)
85
86
 
86
87
  // 2. Merge with new data (using ids)
87
88
  let saved = 0
@@ -1,5 +1,5 @@
1
- import { Readable } from 'stream'
2
- import { createGzip, createUnzip } from 'zlib'
1
+ import { Readable } from 'node:stream'
2
+ import { createGzip, createUnzip } from 'node:zlib'
3
3
  import { pMap, ObjectWithId } from '@naturalcycles/js-lib'
4
4
  import {
5
5
  transformJsonParse,
@@ -1,5 +1,5 @@
1
- import { Readable } from 'stream'
2
- import { createGzip, createUnzip } from 'zlib'
1
+ import { Readable } from 'node:stream'
2
+ import { createGzip, createUnzip } from 'node:zlib'
3
3
  import {
4
4
  generateJsonSchemaFromData,
5
5
  JsonSchemaObject,
@@ -152,7 +152,7 @@ export class InMemoryDB implements CommonDB {
152
152
  return ids.map(id => this.data[table]![id]).filter(Boolean) as ROW[]
153
153
  }
154
154
 
155
- async saveBatch<ROW extends ObjectWithId>(
155
+ async saveBatch<ROW extends Partial<ObjectWithId>>(
156
156
  _table: string,
157
157
  rows: ROW[],
158
158
  opt: CommonDBSaveOptions<ROW> = {},
@@ -1,4 +1,4 @@
1
- import { Readable } from 'stream'
1
+ import { Readable } from 'node:stream'
2
2
  import { StringMap } from '@naturalcycles/js-lib'
3
3
  import { ReadableTyped } from '@naturalcycles/nodejs-lib'
4
4
  import { CommonDBCreateOptions } from '../../db.model'
@@ -1,4 +1,4 @@
1
- import { Readable } from 'stream'
1
+ import { Readable } from 'node:stream'
2
2
  import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib'
3
3
  import { ReadableTyped } from '@naturalcycles/nodejs-lib'
4
4
  import { CommonDB } from './common.db'
@@ -55,7 +55,7 @@ export class BaseCommonDB implements CommonDB {
55
55
  return 0
56
56
  }
57
57
 
58
- async saveBatch<ROW extends ObjectWithId>(
58
+ async saveBatch<ROW extends Partial<ObjectWithId>>(
59
59
  _table: string,
60
60
  _rows: ROW[],
61
61
  _opt?: CommonDBSaveOptions<ROW>,
package/src/common.db.ts CHANGED
@@ -73,7 +73,7 @@ export interface CommonDB {
73
73
  /**
74
74
  * rows can have missing ids only if DB supports auto-generating them (like mysql auto_increment).
75
75
  */
76
- saveBatch<ROW extends ObjectWithId>(
76
+ saveBatch<ROW extends Partial<ObjectWithId>>(
77
77
  table: string,
78
78
  rows: ROW[],
79
79
  opt?: CommonDBSaveOptions<ROW>,
@@ -498,11 +498,13 @@ export class CommonDao<
498
498
  const stream = this.cfg.db.streamQuery<DBM>(q, opt)
499
499
  if (partialQuery || opt.raw) return stream
500
500
 
501
- return stream.pipe(
502
- transformMapSimple<any, DBM>(dbm => this.anyToDBM(dbm, opt), {
503
- errorMode: ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
504
- }),
505
- )
501
+ return stream
502
+ .on('error', err => stream.emit('error', err))
503
+ .pipe(
504
+ transformMapSimple<any, DBM>(dbm => this.anyToDBM(dbm, opt), {
505
+ errorMode: ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
506
+ }),
507
+ )
506
508
  }
507
509
 
508
510
  /**
@@ -529,6 +531,7 @@ export class CommonDao<
529
531
  // optimization: 1 validation is enough
530
532
  // .pipe(transformMap<any, DBM>(dbm => this.anyToDBM(dbm, opt), safeOpt))
531
533
  // .pipe(transformMap<DBM, Saved<BM>>(dbm => this.dbmToBM(dbm, opt), safeOpt))
534
+ .on('error', err => stream.emit('error', err))
532
535
  .pipe(
533
536
  transformMap<DBM, Saved<BM>>(async dbm => await this.dbmToBM(dbm, opt), {
534
537
  errorMode: ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
@@ -550,11 +553,16 @@ export class CommonDao<
550
553
  q.table = opt.table || q.table
551
554
  opt.errorMode ||= ErrorMode.SUPPRESS
552
555
 
553
- return this.cfg.db.streamQuery<DBM>(q.select(['id']), opt).pipe(
554
- transformMapSimple<DBM, ID>(objectWithId => objectWithId.id, {
555
- errorMode: ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
556
- }),
557
- )
556
+ const stream: ReadableTyped<ID> = this.cfg.db
557
+ .streamQuery<DBM>(q.select(['id']), opt)
558
+ .on('error', err => stream.emit('error', err))
559
+ .pipe(
560
+ transformMapSimple<DBM, ID>(objectWithId => objectWithId.id, {
561
+ errorMode: ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
562
+ }),
563
+ )
564
+
565
+ return stream
558
566
  }
559
567
 
560
568
  async streamQueryIdsForEach(
package/src/db.model.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AnyObjectWithId, ObjectWithId } from '@naturalcycles/js-lib'
1
+ import { ObjectWithId } from '@naturalcycles/js-lib'
2
2
 
3
3
  /**
4
4
  * Similar to SQL INSERT, UPDATE.
@@ -16,7 +16,7 @@ export interface CommonDBOptions {}
16
16
  /**
17
17
  * All properties default to undefined.
18
18
  */
19
- export interface CommonDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId>
19
+ export interface CommonDBSaveOptions<ROW extends Partial<ObjectWithId> = any>
20
20
  extends CommonDBOptions {
21
21
  excludeFromIndexes?: (keyof ROW)[]
22
22
 
@@ -52,7 +52,7 @@ export interface RunQueryResult<T> {
52
52
 
53
53
  export type DBOperation = DBSaveBatchOperation | DBDeleteByIdsOperation
54
54
 
55
- export interface DBSaveBatchOperation<ROW extends ObjectWithId = any> {
55
+ export interface DBSaveBatchOperation<ROW extends Partial<ObjectWithId> = any> {
56
56
  type: 'saveBatch'
57
57
  table: string
58
58
  rows: ROW[]
@@ -163,11 +163,16 @@ export class CommonKeyValueDao<T> {
163
163
 
164
164
  // todo: consider it when readableMap supports `errorMode: SUPPRESS`
165
165
  // readableMap(this.cfg.db.streamValues(this.cfg.table, limit), async buf => await this.cfg.hooks!.mapBufferToValue(buf))
166
- return this.cfg.db.streamValues(this.cfg.table, limit).pipe(
167
- transformMap(async buf => await this.cfg.hooks!.mapBufferToValue!(buf), {
168
- errorMode: ErrorMode.SUPPRESS, // cause .pipe cannot propagate errors
169
- }),
170
- )
166
+ const stream: ReadableTyped<Buffer> = this.cfg.db
167
+ .streamValues(this.cfg.table, limit)
168
+ .on('error', err => stream.emit('error', err))
169
+ .pipe(
170
+ transformMap(async buf => await this.cfg.hooks!.mapBufferToValue!(buf), {
171
+ errorMode: ErrorMode.SUPPRESS, // cause .pipe cannot propagate errors
172
+ }),
173
+ )
174
+
175
+ return stream
171
176
  }
172
177
 
173
178
  streamEntries(limit?: number): ReadableTyped<KeyValueTuple<string, T>> {
@@ -175,10 +180,15 @@ export class CommonKeyValueDao<T> {
175
180
  return this.cfg.db.streamEntries(this.cfg.table, limit)
176
181
  }
177
182
 
178
- return this.cfg.db.streamEntries(this.cfg.table, limit).pipe(
179
- transformMap(async ([id, buf]) => [id, await this.cfg.hooks!.mapBufferToValue!(buf)], {
180
- errorMode: ErrorMode.SUPPRESS, // cause .pipe cannot propagate errors
181
- }),
182
- )
183
+ const stream: ReadableTyped<KeyValueTuple<string, T>> = this.cfg.db
184
+ .streamEntries(this.cfg.table, limit)
185
+ .on('error', err => stream.emit('error', err))
186
+ .pipe(
187
+ transformMap(async ([id, buf]) => [id, await this.cfg.hooks!.mapBufferToValue!(buf)], {
188
+ errorMode: ErrorMode.SUPPRESS, // cause .pipe cannot propagate errors
189
+ }),
190
+ )
191
+
192
+ return stream
183
193
  }
184
194
  }
@@ -1,4 +1,4 @@
1
- import { createGzip, ZlibOptions } from 'zlib'
1
+ import { createGzip, ZlibOptions } from 'node:zlib'
2
2
  import {
3
3
  AppError,
4
4
  AsyncMapper,
@@ -1,4 +1,4 @@
1
- import { createUnzip } from 'zlib'
1
+ import { createUnzip } from 'node:zlib'
2
2
  import {
3
3
  AsyncMapper,
4
4
  ErrorMode,
@@ -15,7 +15,7 @@ export class DBTransaction {
15
15
  return new DBTransaction(ops)
16
16
  }
17
17
 
18
- save<ROW extends ObjectWithId>(table: string, row: ROW): this {
18
+ save<ROW extends Partial<ObjectWithId>>(table: string, row: ROW): this {
19
19
  this.ops.push({
20
20
  type: 'saveBatch',
21
21
  table,
@@ -24,7 +24,7 @@ export class DBTransaction {
24
24
  return this
25
25
  }
26
26
 
27
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[]): this {
27
+ saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[]): this {
28
28
  this.ops.push({
29
29
  type: 'saveBatch',
30
30
  table,
@@ -61,7 +61,7 @@ export class RunnableDBTransaction extends DBTransaction {
61
61
  super()
62
62
  }
63
63
 
64
- async commit<ROW extends ObjectWithId>(opt?: CommonDBSaveOptions<ROW>): Promise<void> {
64
+ async commit<ROW extends Partial<ObjectWithId>>(opt?: CommonDBSaveOptions<ROW>): Promise<void> {
65
65
  await this.db.commitTransaction(this, opt)
66
66
  }
67
67
  }