@naturalcycles/db-lib 10.0.1 → 10.1.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.
Files changed (114) hide show
  1. package/dist/adapter/cachedb/cache.db.d.ts +7 -7
  2. package/dist/adapter/cachedb/cache.db.js +13 -16
  3. package/dist/adapter/cachedb/cache.db.model.d.ts +3 -3
  4. package/dist/adapter/cachedb/cache.db.model.js +1 -2
  5. package/dist/adapter/cachedb/index.d.ts +2 -2
  6. package/dist/adapter/cachedb/index.js +2 -5
  7. package/dist/adapter/file/file.db.d.ts +7 -7
  8. package/dist/adapter/file/file.db.js +35 -36
  9. package/dist/adapter/file/file.db.model.d.ts +3 -3
  10. package/dist/adapter/file/file.db.model.js +1 -2
  11. package/dist/adapter/file/inMemory.persistence.plugin.d.ts +3 -3
  12. package/dist/adapter/file/inMemory.persistence.plugin.js +4 -10
  13. package/dist/adapter/file/index.d.ts +3 -3
  14. package/dist/adapter/file/index.js +3 -7
  15. package/dist/adapter/file/localFile.persistence.plugin.d.ts +3 -3
  16. package/dist/adapter/file/localFile.persistence.plugin.js +12 -15
  17. package/dist/adapter/file/noop.persistence.plugin.d.ts +3 -3
  18. package/dist/adapter/file/noop.persistence.plugin.js +1 -5
  19. package/dist/adapter/inmemory/inMemory.db.d.ts +7 -5
  20. package/dist/adapter/inmemory/inMemory.db.js +54 -49
  21. package/dist/adapter/inmemory/inMemoryKeyValueDB.d.ts +4 -4
  22. package/dist/adapter/inmemory/inMemoryKeyValueDB.js +12 -15
  23. package/dist/adapter/inmemory/queryInMemory.d.ts +2 -2
  24. package/dist/adapter/inmemory/queryInMemory.js +4 -7
  25. package/dist/base.common.db.d.ts +8 -5
  26. package/dist/base.common.db.js +9 -12
  27. package/dist/cnst.js +2 -5
  28. package/dist/common.db.d.ts +9 -3
  29. package/dist/common.db.js +3 -6
  30. package/dist/commondao/common.dao.d.ts +16 -6
  31. package/dist/commondao/common.dao.js +123 -112
  32. package/dist/commondao/common.dao.model.d.ts +5 -4
  33. package/dist/commondao/common.dao.model.js +2 -5
  34. package/dist/db.model.d.ts +7 -2
  35. package/dist/db.model.js +4 -7
  36. package/dist/index.d.ts +17 -17
  37. package/dist/index.js +17 -20
  38. package/dist/kv/commonKeyValueDB.d.ts +3 -3
  39. package/dist/kv/commonKeyValueDB.js +1 -4
  40. package/dist/kv/commonKeyValueDao.d.ts +5 -5
  41. package/dist/kv/commonKeyValueDao.js +11 -14
  42. package/dist/kv/commonKeyValueDaoMemoCache.d.ts +3 -2
  43. package/dist/kv/commonKeyValueDaoMemoCache.js +5 -8
  44. package/dist/model.util.d.ts +1 -1
  45. package/dist/model.util.js +9 -15
  46. package/dist/pipeline/dbPipelineBackup.d.ts +6 -4
  47. package/dist/pipeline/dbPipelineBackup.js +27 -30
  48. package/dist/pipeline/dbPipelineCopy.d.ts +6 -4
  49. package/dist/pipeline/dbPipelineCopy.js +20 -23
  50. package/dist/pipeline/dbPipelineRestore.d.ts +6 -4
  51. package/dist/pipeline/dbPipelineRestore.js +27 -30
  52. package/dist/query/dbQuery.d.ts +5 -5
  53. package/dist/query/dbQuery.js +22 -17
  54. package/dist/testing/{dbTest.d.ts → commonDBTest.d.ts} +1 -1
  55. package/dist/testing/{dbTest.js → commonDBTest.js} +81 -62
  56. package/dist/testing/commonDaoTest.d.ts +3 -0
  57. package/dist/testing/{daoTest.js → commonDaoTest.js} +101 -38
  58. package/dist/testing/index.d.ts +7 -5
  59. package/dist/testing/index.js +6 -20
  60. package/dist/testing/keyValueDBTest.d.ts +1 -1
  61. package/dist/testing/keyValueDBTest.js +28 -31
  62. package/dist/testing/keyValueDaoTest.d.ts +1 -1
  63. package/dist/testing/keyValueDaoTest.js +8 -11
  64. package/dist/testing/test.model.d.ts +1 -1
  65. package/dist/testing/test.model.js +30 -37
  66. package/dist/testing/timeSeriesTest.util.d.ts +1 -1
  67. package/dist/testing/timeSeriesTest.util.js +3 -6
  68. package/dist/timeseries/commonTimeSeriesDao.d.ts +1 -1
  69. package/dist/timeseries/commonTimeSeriesDao.js +6 -9
  70. package/dist/timeseries/timeSeries.model.d.ts +1 -1
  71. package/dist/timeseries/timeSeries.model.js +1 -2
  72. package/dist/transaction/dbTransaction.util.d.ts +4 -3
  73. package/dist/transaction/dbTransaction.util.js +3 -5
  74. package/dist/validation/index.d.ts +2 -2
  75. package/dist/validation/index.js +25 -28
  76. package/package.json +5 -4
  77. package/src/adapter/cachedb/cache.db.model.ts +4 -4
  78. package/src/adapter/cachedb/cache.db.ts +10 -9
  79. package/src/adapter/cachedb/index.ts +3 -3
  80. package/src/adapter/file/file.db.model.ts +3 -3
  81. package/src/adapter/file/file.db.ts +12 -16
  82. package/src/adapter/file/inMemory.persistence.plugin.ts +4 -3
  83. package/src/adapter/file/index.ts +3 -3
  84. package/src/adapter/file/localFile.persistence.plugin.ts +4 -3
  85. package/src/adapter/file/noop.persistence.plugin.ts +3 -3
  86. package/src/adapter/inmemory/inMemory.db.ts +26 -27
  87. package/src/adapter/inmemory/inMemoryKeyValueDB.ts +6 -6
  88. package/src/adapter/inmemory/queryInMemory.ts +3 -2
  89. package/src/base.common.db.ts +12 -7
  90. package/src/common.db.ts +12 -4
  91. package/src/commondao/common.dao.model.ts +5 -6
  92. package/src/commondao/common.dao.ts +41 -23
  93. package/src/db.model.ts +8 -2
  94. package/src/index.ts +17 -17
  95. package/src/kv/commonKeyValueDB.ts +3 -3
  96. package/src/kv/commonKeyValueDao.ts +8 -6
  97. package/src/kv/commonKeyValueDaoMemoCache.ts +3 -8
  98. package/src/model.util.ts +2 -1
  99. package/src/pipeline/dbPipelineBackup.ts +5 -14
  100. package/src/pipeline/dbPipelineCopy.ts +6 -14
  101. package/src/pipeline/dbPipelineRestore.ts +9 -8
  102. package/src/query/dbQuery.ts +7 -12
  103. package/src/testing/{dbTest.ts → commonDBTest.ts} +34 -5
  104. package/src/testing/{daoTest.ts → commonDaoTest.ts} +89 -11
  105. package/src/testing/index.ts +7 -8
  106. package/src/testing/keyValueDBTest.ts +4 -3
  107. package/src/testing/keyValueDaoTest.ts +3 -3
  108. package/src/testing/test.model.ts +2 -7
  109. package/src/testing/timeSeriesTest.util.ts +1 -1
  110. package/src/timeseries/commonTimeSeriesDao.ts +5 -4
  111. package/src/timeseries/timeSeries.model.ts +1 -1
  112. package/src/transaction/dbTransaction.util.ts +4 -3
  113. package/src/validation/index.ts +4 -4
  114. package/dist/testing/daoTest.d.ts +0 -3
@@ -1,19 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InMemoryDBTransaction = exports.InMemoryDB = void 0;
4
- const node_stream_1 = require("node:stream");
5
- const js_lib_1 = require("@naturalcycles/js-lib");
6
- const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
7
- const __1 = require("../..");
8
- class InMemoryDB {
1
+ import { Readable } from 'node:stream';
2
+ import { _assert, _by, _deepCopy, _isEmptyObject, _since, _sortObjectDeep, _stringMapEntries, _stringMapValues, generateJsonSchemaFromData, localTime, pMap, } from '@naturalcycles/js-lib';
3
+ import { _pipeline, bufferReviver, dimGrey, fs2, yellow } from '@naturalcycles/nodejs-lib';
4
+ import { commonDBFullSupport, CommonDBType } from '../../common.db.js';
5
+ import { queryInMemory } from './queryInMemory.js';
6
+ export class InMemoryDB {
7
+ dbType = CommonDBType.document;
8
+ support = {
9
+ ...commonDBFullSupport,
10
+ timeMachine: false,
11
+ };
9
12
  constructor(cfg) {
10
- this.dbType = __1.CommonDBType.document;
11
- this.support = {
12
- ...__1.commonDBFullSupport,
13
- timeMachine: false,
14
- };
15
- // data[table][id] > {id: 'a', created: ... }
16
- this.data = {};
17
13
  this.cfg = {
18
14
  // defaults
19
15
  tablesPrefix: '',
@@ -25,12 +21,15 @@ class InMemoryDB {
25
21
  ...cfg,
26
22
  };
27
23
  }
24
+ cfg;
25
+ // data[table][id] > {id: 'a', created: ... }
26
+ data = {};
28
27
  /**
29
28
  * Returns internal "Data snapshot".
30
29
  * Deterministic - jsonSorted.
31
30
  */
32
31
  getDataSnapshot() {
33
- return (0, js_lib_1._sortObjectDeep)(this.data);
32
+ return _sortObjectDeep(this.data);
34
33
  }
35
34
  async ping() { }
36
35
  /**
@@ -56,7 +55,7 @@ class InMemoryDB {
56
55
  async getTableSchema(_table) {
57
56
  const table = this.cfg.tablesPrefix + _table;
58
57
  return {
59
- ...(0, js_lib_1.generateJsonSchemaFromData)((0, js_lib_1._stringMapValues)(this.data[table] || {})),
58
+ ...generateJsonSchemaFromData(_stringMapValues(this.data[table] || {})),
60
59
  $id: `${table}.schema.json`,
61
60
  };
62
61
  }
@@ -91,14 +90,14 @@ class InMemoryDB {
91
90
  // JSON parse/stringify (deep clone) is to:
92
91
  // 1. Not store values "by reference" (avoid mutation bugs)
93
92
  // 2. Simulate real DB that would do something like that in a transport layer anyway
94
- this.data[table][r.id] = JSON.parse(JSON.stringify(r), nodejs_lib_1.bufferReviver);
93
+ this.data[table][r.id] = JSON.parse(JSON.stringify(r), bufferReviver);
95
94
  });
96
95
  }
97
96
  async deleteByQuery(q, _opt) {
98
97
  const table = this.cfg.tablesPrefix + q.table;
99
98
  if (!this.data[table])
100
99
  return 0;
101
- const ids = (0, __1.queryInMemory)(q, Object.values(this.data[table])).map(r => r.id);
100
+ const ids = queryInMemory(q, Object.values(this.data[table])).map(r => r.id);
102
101
  return await this.deleteByIds(q.table, ids);
103
102
  }
104
103
  async deleteByIds(_table, ids, _opt) {
@@ -115,24 +114,24 @@ class InMemoryDB {
115
114
  return count;
116
115
  }
117
116
  async patchByQuery(q, patch) {
118
- if ((0, js_lib_1._isEmptyObject)(patch))
117
+ if (_isEmptyObject(patch))
119
118
  return 0;
120
119
  const table = this.cfg.tablesPrefix + q.table;
121
- const rows = (0, __1.queryInMemory)(q, Object.values(this.data[table] || {}));
120
+ const rows = queryInMemory(q, Object.values(this.data[table] || {}));
122
121
  rows.forEach(row => Object.assign(row, patch));
123
122
  return rows.length;
124
123
  }
125
124
  async runQuery(q, _opt) {
126
125
  const table = this.cfg.tablesPrefix + q.table;
127
- return { rows: (0, __1.queryInMemory)(q, Object.values(this.data[table] || {})) };
126
+ return { rows: queryInMemory(q, Object.values(this.data[table] || {})) };
128
127
  }
129
128
  async runQueryCount(q, _opt) {
130
129
  const table = this.cfg.tablesPrefix + q.table;
131
- return (0, __1.queryInMemory)(q, Object.values(this.data[table] || {})).length;
130
+ return queryInMemory(q, Object.values(this.data[table] || {})).length;
132
131
  }
133
132
  streamQuery(q, _opt) {
134
133
  const table = this.cfg.tablesPrefix + q.table;
135
- return node_stream_1.Readable.from((0, __1.queryInMemory)(q, Object.values(this.data[table] || {})));
134
+ return Readable.from(queryInMemory(q, Object.values(this.data[table] || {})));
136
135
  }
137
136
  async runInTransaction(fn, opt = {}) {
138
137
  const tx = new InMemoryDBTransaction(this, {
@@ -148,11 +147,17 @@ class InMemoryDB {
148
147
  throw err;
149
148
  }
150
149
  }
150
+ async createTransaction(opt = {}) {
151
+ return new InMemoryDBTransaction(this, {
152
+ readOnly: false,
153
+ ...opt,
154
+ });
155
+ }
151
156
  async incrementBatch(table, prop, incrementMap, _opt) {
152
157
  const tbl = this.cfg.tablesPrefix + table;
153
158
  this.data[tbl] ||= {};
154
159
  const result = {};
155
- for (const [id, by] of (0, js_lib_1._stringMapEntries)(incrementMap)) {
160
+ for (const [id, by] of _stringMapEntries(incrementMap)) {
156
161
  this.data[tbl][id] ||= { id };
157
162
  const newValue = (this.data[tbl][id][prop] || 0) + by;
158
163
  this.data[tbl][id][prop] = newValue;
@@ -164,59 +169,60 @@ class InMemoryDB {
164
169
  * Flushes all tables (all namespaces) at once.
165
170
  */
166
171
  async flushToDisk() {
167
- (0, js_lib_1._assert)(this.cfg.persistenceEnabled, 'flushToDisk() called but persistenceEnabled=false');
172
+ _assert(this.cfg.persistenceEnabled, 'flushToDisk() called but persistenceEnabled=false');
168
173
  const { persistentStoragePath, persistZip } = this.cfg;
169
- const started = js_lib_1.localTime.nowUnixMillis();
170
- await nodejs_lib_1.fs2.emptyDirAsync(persistentStoragePath);
174
+ const started = localTime.nowUnixMillis();
175
+ await fs2.emptyDirAsync(persistentStoragePath);
171
176
  let tables = 0;
172
177
  // infinite concurrency for now
173
- await (0, js_lib_1.pMap)(Object.keys(this.data), async (table) => {
178
+ await pMap(Object.keys(this.data), async (table) => {
174
179
  const rows = Object.values(this.data[table]);
175
180
  if (rows.length === 0)
176
181
  return; // 0 rows
177
182
  tables++;
178
183
  const fname = `${persistentStoragePath}/${table}.ndjson${persistZip ? '.gz' : ''}`;
179
- await (0, nodejs_lib_1._pipeline)([node_stream_1.Readable.from(rows), ...nodejs_lib_1.fs2.createWriteStreamAsNDJSON(fname)]);
184
+ await _pipeline([Readable.from(rows), ...fs2.createWriteStreamAsNDJSON(fname)]);
180
185
  });
181
- this.cfg.logger.log(`flushToDisk took ${(0, nodejs_lib_1.dimGrey)((0, js_lib_1._since)(started))} to save ${(0, nodejs_lib_1.yellow)(tables)} tables`);
186
+ this.cfg.logger.log(`flushToDisk took ${dimGrey(_since(started))} to save ${yellow(tables)} tables`);
182
187
  }
183
188
  /**
184
189
  * Restores all tables (all namespaces) at once.
185
190
  */
186
191
  async restoreFromDisk() {
187
- (0, js_lib_1._assert)(this.cfg.persistenceEnabled, 'restoreFromDisk() called but persistenceEnabled=false');
192
+ _assert(this.cfg.persistenceEnabled, 'restoreFromDisk() called but persistenceEnabled=false');
188
193
  const { persistentStoragePath } = this.cfg;
189
- const started = js_lib_1.localTime.nowUnixMillis();
190
- await nodejs_lib_1.fs2.ensureDirAsync(persistentStoragePath);
194
+ const started = localTime.nowUnixMillis();
195
+ await fs2.ensureDirAsync(persistentStoragePath);
191
196
  this.data = {}; // empty it in the beginning!
192
- const files = (await nodejs_lib_1.fs2.readdirAsync(persistentStoragePath)).filter(f => f.includes('.ndjson'));
197
+ const files = (await fs2.readdirAsync(persistentStoragePath)).filter(f => f.includes('.ndjson'));
193
198
  // infinite concurrency for now
194
- await (0, js_lib_1.pMap)(files, async (file) => {
199
+ await pMap(files, async (file) => {
195
200
  const fname = `${persistentStoragePath}/${file}`;
196
201
  const table = file.split('.ndjson')[0];
197
- const rows = await nodejs_lib_1.fs2.createReadStreamAsNDJSON(fname).toArray();
198
- this.data[table] = (0, js_lib_1._by)(rows, r => r.id);
202
+ const rows = await fs2.createReadStreamAsNDJSON(fname).toArray();
203
+ this.data[table] = _by(rows, r => r.id);
199
204
  });
200
- this.cfg.logger.log(`restoreFromDisk took ${(0, nodejs_lib_1.dimGrey)((0, js_lib_1._since)(started))} to read ${(0, nodejs_lib_1.yellow)(files.length)} tables`);
205
+ this.cfg.logger.log(`restoreFromDisk took ${dimGrey(_since(started))} to read ${yellow(files.length)} tables`);
201
206
  }
202
207
  }
203
- exports.InMemoryDB = InMemoryDB;
204
- class InMemoryDBTransaction {
208
+ export class InMemoryDBTransaction {
209
+ db;
210
+ opt;
205
211
  constructor(db, opt) {
206
212
  this.db = db;
207
213
  this.opt = opt;
208
- this.ops = [];
209
- // used to enforce forbidReadAfterWrite setting
210
- this.writeOperationHappened = false;
211
214
  }
215
+ ops = [];
216
+ // used to enforce forbidReadAfterWrite setting
217
+ writeOperationHappened = false;
212
218
  async getByIds(table, ids, opt) {
213
219
  if (this.db.cfg.forbidTransactionReadAfterWrite) {
214
- (0, js_lib_1._assert)(!this.writeOperationHappened, `InMemoryDBTransaction: read operation attempted after write operation`);
220
+ _assert(!this.writeOperationHappened, `InMemoryDBTransaction: read operation attempted after write operation`);
215
221
  }
216
222
  return await this.db.getByIds(table, ids, opt);
217
223
  }
218
224
  async saveBatch(table, rows, opt) {
219
- (0, js_lib_1._assert)(!this.opt.readOnly, `InMemoryDBTransaction: saveBatch(${table}) called in readOnly mode`);
225
+ _assert(!this.opt.readOnly, `InMemoryDBTransaction: saveBatch(${table}) called in readOnly mode`);
220
226
  this.writeOperationHappened = true;
221
227
  this.ops.push({
222
228
  type: 'saveBatch',
@@ -226,7 +232,7 @@ class InMemoryDBTransaction {
226
232
  });
227
233
  }
228
234
  async deleteByIds(table, ids, opt) {
229
- (0, js_lib_1._assert)(!this.opt.readOnly, `InMemoryDBTransaction: deleteByIds(${table}) called in readOnly mode`);
235
+ _assert(!this.opt.readOnly, `InMemoryDBTransaction: deleteByIds(${table}) called in readOnly mode`);
230
236
  this.writeOperationHappened = true;
231
237
  this.ops.push({
232
238
  type: 'deleteByIds',
@@ -237,7 +243,7 @@ class InMemoryDBTransaction {
237
243
  return ids.length;
238
244
  }
239
245
  async commit() {
240
- const backup = (0, js_lib_1._deepCopy)(this.db.data);
246
+ const backup = _deepCopy(this.db.data);
241
247
  try {
242
248
  for (const op of this.ops) {
243
249
  if (op.type === 'saveBatch') {
@@ -264,4 +270,3 @@ class InMemoryDBTransaction {
264
270
  this.ops = [];
265
271
  }
266
272
  }
267
- exports.InMemoryDBTransaction = InMemoryDBTransaction;
@@ -1,7 +1,7 @@
1
- import { StringMap } from '@naturalcycles/js-lib';
2
- import { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
- import { CommonDBCreateOptions } from '../../db.model';
4
- import { CommonKeyValueDB, IncrementTuple, KeyValueDBTuple } from '../../kv/commonKeyValueDB';
1
+ import type { StringMap } from '@naturalcycles/js-lib';
2
+ import type { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
+ import type { CommonDBCreateOptions } from '../../db.model.js';
4
+ import type { CommonKeyValueDB, IncrementTuple, KeyValueDBTuple } from '../../kv/commonKeyValueDB.js';
5
5
  export interface InMemoryKeyValueDBCfg {
6
6
  }
7
7
  export declare class InMemoryKeyValueDB implements CommonKeyValueDB {
@@ -1,17 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InMemoryKeyValueDB = void 0;
4
- const node_stream_1 = require("node:stream");
5
- const commonKeyValueDB_1 = require("../../kv/commonKeyValueDB");
6
- class InMemoryKeyValueDB {
1
+ import { Readable } from 'node:stream';
2
+ import { commonKeyValueDBFullSupport } from '../../kv/commonKeyValueDB.js';
3
+ export class InMemoryKeyValueDB {
4
+ cfg;
7
5
  constructor(cfg = {}) {
8
6
  this.cfg = cfg;
9
- this.support = {
10
- ...commonKeyValueDB_1.commonKeyValueDBFullSupport,
11
- };
12
- // data[table][id] => any (can be Buffer, or number)
13
- this.data = {};
14
7
  }
8
+ support = {
9
+ ...commonKeyValueDBFullSupport,
10
+ };
11
+ // data[table][id] => any (can be Buffer, or number)
12
+ data = {};
15
13
  async ping() { }
16
14
  async createTable(_table, _opt) { }
17
15
  async deleteByIds(table, ids) {
@@ -27,13 +25,13 @@ class InMemoryKeyValueDB {
27
25
  entries.forEach(([id, v]) => (this.data[table][id] = v));
28
26
  }
29
27
  streamIds(table, limit) {
30
- return node_stream_1.Readable.from(Object.keys(this.data[table] || {}).slice(0, limit));
28
+ return Readable.from(Object.keys(this.data[table] || {}).slice(0, limit));
31
29
  }
32
30
  streamValues(table, limit) {
33
- return node_stream_1.Readable.from(Object.values(this.data[table] || {}).slice(0, limit));
31
+ return Readable.from(Object.values(this.data[table] || {}).slice(0, limit));
34
32
  }
35
33
  streamEntries(table, limit) {
36
- return node_stream_1.Readable.from(Object.entries(this.data[table] || {}).slice(0, limit));
34
+ return Readable.from(Object.entries(this.data[table] || {}).slice(0, limit));
37
35
  }
38
36
  async count(table) {
39
37
  this.data[table] ||= {};
@@ -48,4 +46,3 @@ class InMemoryKeyValueDB {
48
46
  });
49
47
  }
50
48
  }
51
- exports.InMemoryKeyValueDB = InMemoryKeyValueDB;
@@ -1,3 +1,3 @@
1
- import { ObjectWithId } from '@naturalcycles/js-lib';
2
- import { DBQuery } from '../../query/dbQuery';
1
+ import type { ObjectWithId } from '@naturalcycles/js-lib';
2
+ import type { DBQuery } from '../../query/dbQuery.js';
3
3
  export declare function queryInMemory<ROW extends ObjectWithId>(q: DBQuery<ROW>, rows?: ROW[]): ROW[];
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.queryInMemory = queryInMemory;
4
- const js_lib_1 = require("@naturalcycles/js-lib");
1
+ import { _get, _pick } from '@naturalcycles/js-lib';
5
2
  const FILTER_FNS = {
6
3
  '==': (v, val) => v === val,
7
4
  '!=': (v, val) => v !== val,
@@ -16,18 +13,18 @@ const FILTER_FNS = {
16
13
  };
17
14
  // Important: q.table is not used in this function, so tablesPrefix is not needed.
18
15
  // But should be careful here..
19
- function queryInMemory(q, rows = []) {
16
+ export function queryInMemory(q, rows = []) {
20
17
  // .filter
21
18
  // eslint-disable-next-line unicorn/no-array-reduce
22
19
  rows = q._filters.reduce((rows, filter) => {
23
20
  return rows.filter(row => {
24
- const value = (0, js_lib_1._get)(row, filter.name);
21
+ const value = _get(row, filter.name);
25
22
  return FILTER_FNS[filter.op](value, filter.val);
26
23
  });
27
24
  }, rows);
28
25
  // .select(fieldNames)
29
26
  if (q._selectedFieldNames) {
30
- rows = rows.map(row => (0, js_lib_1._pick)(row, q._selectedFieldNames));
27
+ rows = rows.map(row => _pick(row, q._selectedFieldNames));
31
28
  }
32
29
  // todo: only one order is supported (first)
33
30
  const [order] = q._orders;
@@ -1,8 +1,10 @@
1
- import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, StringMap } from '@naturalcycles/js-lib';
2
- import { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
- import { CommonDB, CommonDBSupport, CommonDBType } from './common.db';
4
- import { CommonDBOptions, CommonDBSaveOptions, CommonDBTransactionOptions, DBTransactionFn, RunQueryResult } from './db.model';
5
- import { DBQuery } from './query/dbQuery';
1
+ import type { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, StringMap } from '@naturalcycles/js-lib';
2
+ import type { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
+ import type { CommonDB, CommonDBSupport } from './common.db.js';
4
+ import { CommonDBType } from './common.db.js';
5
+ import type { CommonDBOptions, CommonDBSaveOptions, CommonDBTransactionOptions, DBTransactionFn, RunQueryResult } from './db.model.js';
6
+ import type { DBQuery } from './query/dbQuery.js';
7
+ import { FakeDBTransaction } from './transaction/dbTransaction.util.js';
6
8
  /**
7
9
  * No-op implementation of CommonDB interface.
8
10
  * To be extended by actual implementations.
@@ -23,5 +25,6 @@ export declare class BaseCommonDB implements CommonDB {
23
25
  streamQuery<ROW extends ObjectWithId>(_q: DBQuery<ROW>): ReadableTyped<ROW>;
24
26
  deleteByIds(_table: string, _ids: string[], _opt?: CommonDBOptions): Promise<number>;
25
27
  runInTransaction(fn: DBTransactionFn, _opt?: CommonDBTransactionOptions): Promise<void>;
28
+ createTransaction(_opt?: CommonDBTransactionOptions): Promise<FakeDBTransaction>;
26
29
  incrementBatch(_table: string, _prop: string, _incrementMap: StringMap<number>, _opt?: CommonDBOptions): Promise<StringMap<number>>;
27
30
  }
@@ -1,17 +1,12 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseCommonDB = void 0;
4
- const common_db_1 = require("./common.db");
5
- const dbTransaction_util_1 = require("./transaction/dbTransaction.util");
1
+ import { CommonDBType } from './common.db.js';
2
+ import { FakeDBTransaction } from './transaction/dbTransaction.util.js';
6
3
  /**
7
4
  * No-op implementation of CommonDB interface.
8
5
  * To be extended by actual implementations.
9
6
  */
10
- class BaseCommonDB {
11
- constructor() {
12
- this.dbType = common_db_1.CommonDBType.document;
13
- this.support = {};
14
- }
7
+ export class BaseCommonDB {
8
+ dbType = CommonDBType.document;
9
+ support = {};
15
10
  async ping() {
16
11
  throw new Error('ping is not implemented');
17
12
  }
@@ -49,12 +44,14 @@ class BaseCommonDB {
49
44
  throw new Error('deleteByIds is not implemented');
50
45
  }
51
46
  async runInTransaction(fn, _opt) {
52
- const tx = new dbTransaction_util_1.FakeDBTransaction(this);
47
+ const tx = new FakeDBTransaction(this);
53
48
  await fn(tx);
54
49
  // there's no try/catch and rollback, as there's nothing to rollback
55
50
  }
51
+ async createTransaction(_opt) {
52
+ return new FakeDBTransaction(this);
53
+ }
56
54
  async incrementBatch(_table, _prop, _incrementMap, _opt) {
57
55
  throw new Error('incrementBatch is not implemented');
58
56
  }
59
57
  }
60
- exports.BaseCommonDB = BaseCommonDB;
package/dist/cnst.js CHANGED
@@ -1,10 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DBLibError = void 0;
4
- var DBLibError;
1
+ export var DBLibError;
5
2
  (function (DBLibError) {
6
3
  DBLibError["DB_ROW_REQUIRED"] = "DB_ROW_REQUIRED";
7
4
  DBLibError["DAO_IS_READ_ONLY"] = "DAO_IS_READ_ONLY";
8
5
  DBLibError["NON_UNIQUE_ID"] = "NON_UNIQUE_ID";
9
6
  DBLibError["OBJECT_IS_IMMUTABLE"] = "OBJECT_IS_IMMUTABLE";
10
- })(DBLibError || (exports.DBLibError = DBLibError = {}));
7
+ })(DBLibError || (DBLibError = {}));
@@ -1,7 +1,7 @@
1
- import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, StringMap } from '@naturalcycles/js-lib';
1
+ import type { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, StringMap } from '@naturalcycles/js-lib';
2
2
  import type { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
- import { CommonDBCreateOptions, CommonDBOptions, CommonDBReadOptions, CommonDBSaveOptions, CommonDBStreamOptions, CommonDBTransactionOptions, DBTransactionFn, RunQueryResult } from './db.model';
4
- import { DBQuery } from './query/dbQuery';
3
+ import type { CommonDBCreateOptions, CommonDBOptions, CommonDBReadOptions, CommonDBSaveOptions, CommonDBStreamOptions, CommonDBTransactionOptions, DBTransaction, DBTransactionFn, RunQueryResult } from './db.model.js';
4
+ import type { DBQuery } from './query/dbQuery.js';
5
5
  export declare enum CommonDBType {
6
6
  'document' = "document",
7
7
  'relational' = "relational"
@@ -87,6 +87,12 @@ export interface CommonDB {
87
87
  * unless specified as readOnly in CommonDBTransactionOptions.
88
88
  */
89
89
  runInTransaction: (fn: DBTransactionFn, opt?: CommonDBTransactionOptions) => Promise<void>;
90
+ /**
91
+ * Experimental API to support more manual transaction control.
92
+ *
93
+ * @experimental
94
+ */
95
+ createTransaction: (opt?: CommonDBTransactionOptions) => Promise<DBTransaction>;
90
96
  /**
91
97
  * Increments a value of a property by a given amount.
92
98
  * This is a batch operation, so it allows to increment multiple rows at once.
package/dist/common.db.js CHANGED
@@ -1,12 +1,9 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.commonDBFullSupport = exports.CommonDBType = void 0;
4
- var CommonDBType;
1
+ export var CommonDBType;
5
2
  (function (CommonDBType) {
6
3
  CommonDBType["document"] = "document";
7
4
  CommonDBType["relational"] = "relational";
8
- })(CommonDBType || (exports.CommonDBType = CommonDBType = {}));
9
- exports.commonDBFullSupport = {
5
+ })(CommonDBType || (CommonDBType = {}));
6
+ export const commonDBFullSupport = {
10
7
  queries: true,
11
8
  dbQueryFilter: true,
12
9
  dbQueryFilterIn: true,
@@ -1,9 +1,12 @@
1
- import { Transform } from 'node:stream';
2
- import { AsyncMapper, BaseDBEntity, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, StringMap, UnixTimestampMillis, Unsaved, ZodSchema } from '@naturalcycles/js-lib';
3
- import { AjvSchema, ObjectSchema, ReadableTyped } from '@naturalcycles/nodejs-lib';
4
- import { CommonDBTransactionOptions, DBTransaction, RunQueryResult } from '../db.model';
5
- import { DBQuery, RunnableDBQuery } from '../query/dbQuery';
6
- import { CommonDaoCfg, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoPatchByIdOptions, CommonDaoPatchOptions, CommonDaoReadOptions, CommonDaoSaveBatchOptions, CommonDaoSaveOptions, CommonDaoStreamDeleteOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, CommonDaoStreamSaveOptions } from './common.dao.model';
1
+ import type { Transform } from 'node:stream';
2
+ import type { AsyncMapper, BaseDBEntity, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, StringMap, UnixTimestampMillis, Unsaved } from '@naturalcycles/js-lib';
3
+ import { ZodSchema } from '@naturalcycles/js-lib/dist/zod/index.js';
4
+ import type { ObjectSchema, ReadableTyped } from '@naturalcycles/nodejs-lib';
5
+ import { AjvSchema } from '@naturalcycles/nodejs-lib';
6
+ import type { CommonDBTransactionOptions, DBTransaction, RunQueryResult } from '../db.model.js';
7
+ import type { DBQuery } from '../query/dbQuery.js';
8
+ import { RunnableDBQuery } from '../query/dbQuery.js';
9
+ import type { CommonDaoCfg, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoPatchByIdOptions, CommonDaoPatchOptions, CommonDaoReadOptions, CommonDaoSaveBatchOptions, CommonDaoSaveOptions, CommonDaoStreamDeleteOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, CommonDaoStreamSaveOptions } from './common.dao.model.js';
7
10
  /**
8
11
  * Lowest common denominator API between supported Databases.
9
12
  *
@@ -165,6 +168,7 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
165
168
  * Proxy to this.cfg.db.ping
166
169
  */
167
170
  ping(): Promise<void>;
171
+ createTransaction(opt?: CommonDBTransactionOptions): Promise<CommonDaoTransaction>;
168
172
  runInTransaction<T = void>(fn: CommonDaoTransactionFn<T>, opt?: CommonDBTransactionOptions): Promise<T>;
169
173
  /**
170
174
  * Throws if query uses a property that is in `excludeFromIndexes` list.
@@ -189,8 +193,14 @@ export declare class CommonDaoTransaction {
189
193
  tx: DBTransaction;
190
194
  private logger;
191
195
  constructor(tx: DBTransaction, logger: CommonLogger);
196
+ /**
197
+ * Commits the underlying DBTransaction.
198
+ * May throw.
199
+ */
200
+ commit(): Promise<void>;
192
201
  /**
193
202
  * Perform a graceful rollback without throwing/re-throwing any error.
203
+ * Never throws.
194
204
  */
195
205
  rollback(): Promise<void>;
196
206
  getById<BM extends BaseDBEntity, DBM extends BaseDBEntity, ID = BM['id']>(dao: CommonDao<BM, DBM, ID>, id?: ID | null, opt?: CommonDaoReadOptions): Promise<BM | null>;