@naturalcycles/db-lib 8.60.0 → 9.0.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 (52) hide show
  1. package/dist/adapter/cachedb/cache.db.d.ts +3 -4
  2. package/dist/adapter/cachedb/cache.db.js +5 -4
  3. package/dist/adapter/cachedb/cache.db.model.d.ts +2 -2
  4. package/dist/adapter/file/file.db.d.ts +15 -7
  5. package/dist/adapter/file/file.db.js +93 -57
  6. package/dist/adapter/file/localFile.persistence.plugin.js +3 -3
  7. package/dist/adapter/inmemory/inMemory.db.d.ts +30 -4
  8. package/dist/adapter/inmemory/inMemory.db.js +89 -33
  9. package/dist/base.common.db.d.ts +7 -10
  10. package/dist/base.common.db.js +11 -7
  11. package/dist/common.db.d.ts +56 -4
  12. package/dist/common.db.js +23 -0
  13. package/dist/commondao/common.dao.d.ts +17 -9
  14. package/dist/commondao/common.dao.js +82 -69
  15. package/dist/commondao/common.dao.model.d.ts +0 -10
  16. package/dist/db.model.d.ts +12 -1
  17. package/dist/index.d.ts +0 -1
  18. package/dist/index.js +0 -1
  19. package/dist/pipeline/dbPipelineBackup.js +4 -4
  20. package/dist/pipeline/dbPipelineRestore.js +2 -2
  21. package/dist/testing/daoTest.d.ts +2 -2
  22. package/dist/testing/daoTest.js +29 -39
  23. package/dist/testing/dbTest.d.ts +1 -39
  24. package/dist/testing/dbTest.js +40 -49
  25. package/dist/testing/index.d.ts +2 -2
  26. package/dist/timeseries/commonTimeSeriesDao.js +5 -6
  27. package/dist/transaction/dbTransaction.util.d.ts +17 -4
  28. package/dist/transaction/dbTransaction.util.js +46 -22
  29. package/dist/validation/index.js +2 -2
  30. package/package.json +1 -1
  31. package/src/adapter/cachedb/cache.db.model.ts +7 -2
  32. package/src/adapter/cachedb/cache.db.ts +7 -8
  33. package/src/adapter/file/file.db.ts +121 -69
  34. package/src/adapter/file/localFile.persistence.plugin.ts +4 -5
  35. package/src/adapter/inmemory/inMemory.db.ts +106 -33
  36. package/src/base.common.db.ts +20 -11
  37. package/src/common.db.ts +80 -3
  38. package/src/commondao/common.dao.model.ts +0 -11
  39. package/src/commondao/common.dao.ts +103 -89
  40. package/src/db.model.ts +15 -2
  41. package/src/index.ts +0 -1
  42. package/src/pipeline/dbPipelineBackup.ts +5 -8
  43. package/src/pipeline/dbPipelineRestore.ts +3 -4
  44. package/src/testing/daoTest.ts +32 -52
  45. package/src/testing/dbTest.ts +42 -119
  46. package/src/testing/index.ts +2 -12
  47. package/src/timeseries/commonTimeSeriesDao.ts +5 -6
  48. package/src/transaction/dbTransaction.util.ts +61 -22
  49. package/src/validation/index.ts +2 -2
  50. package/dist/transaction/dbTransaction.d.ts +0 -27
  51. package/dist/transaction/dbTransaction.js +0 -64
  52. package/src/transaction/dbTransaction.ts +0 -67
@@ -1,43 +1,8 @@
1
1
  import { CommonDB } from '../common.db';
2
- export interface CommonDBImplementationFeatures {
3
- /**
4
- * All querying functionality.
5
- */
6
- querying?: boolean;
7
- dbQueryFilter?: boolean;
8
- dbQueryFilterIn?: boolean;
9
- dbQueryOrder?: boolean;
10
- dbQuerySelectFields?: boolean;
11
- insert?: boolean;
12
- update?: boolean;
13
- updateByQuery?: boolean;
14
- dbIncrement?: boolean;
15
- createTable?: boolean;
16
- tableSchemas?: boolean;
17
- /**
18
- * Queries should return fresh results immediately.
19
- * Datastore is the one known to NOT have strong consistency for queries (not for getById though).
20
- */
21
- strongConsistency?: boolean;
22
- streaming?: boolean;
23
- bufferSupport?: boolean;
24
- nullValues?: boolean;
25
- /**
26
- * Set false for SQL (relational) databases,
27
- * they will return `null` for all missing properties.
28
- */
29
- documentDB?: boolean;
30
- transactions?: boolean;
31
- }
32
2
  /**
33
3
  * All options default to `false`.
34
4
  */
35
5
  export interface CommonDBImplementationQuirks {
36
- /**
37
- * Applicable to e.g Datastore.
38
- * Time in milliseconds to wait for eventual consistency to propagate.
39
- */
40
- eventualConsistencyDelay?: number;
41
6
  /**
42
7
  * Example: airtableId
43
8
  */
@@ -47,8 +12,5 @@ export interface CommonDBImplementationQuirks {
47
12
  */
48
13
  allowBooleansAsUndefined?: boolean;
49
14
  }
50
- /**
51
- * All unclaimed features will default to 'true'
52
- */
53
- export declare function runCommonDBTest(db: CommonDB, features?: CommonDBImplementationFeatures, quirks?: CommonDBImplementationQuirks): void;
15
+ export declare function runCommonDBTest(db: CommonDB, quirks?: CommonDBImplementationQuirks): void;
54
16
  export declare function expectMatch(expected: any[], actual: any[], quirks: CommonDBImplementationQuirks): void;
@@ -3,23 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.expectMatch = exports.runCommonDBTest = void 0;
4
4
  const js_lib_1 = require("@naturalcycles/js-lib");
5
5
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
6
+ const common_db_1 = require("../common.db");
6
7
  const db_model_1 = require("../db.model");
7
8
  const dbQuery_1 = require("../query/dbQuery");
8
- const dbTransaction_1 = require("../transaction/dbTransaction");
9
9
  const test_model_1 = require("./test.model");
10
10
  const test_util_1 = require("./test.util");
11
- /**
12
- * All unclaimed features will default to 'true'
13
- */
14
- function runCommonDBTest(db, features = {}, quirks = {}) {
15
- const { querying = true, tableSchemas = true, createTable = true, dbQueryFilter = true,
16
- // dbQueryFilterIn = true,
17
- dbQueryOrder = true, dbQuerySelectFields = true, insert = true, update = true, updateByQuery = true, dbIncrement = true, streaming = true, strongConsistency = true, bufferSupport = true, nullValues = true, documentDB = true, transactions = true, } = features;
18
- // const {
19
- // allowExtraPropertiesInResponse,
20
- // allowBooleansAsUndefined,
21
- // } = quirks
22
- const eventualConsistencyDelay = !strongConsistency && quirks.eventualConsistencyDelay;
11
+ function runCommonDBTest(db, quirks = {}) {
12
+ const { support } = db;
23
13
  const items = (0, test_model_1.createTestItemsDBM)(3);
24
14
  (0, test_util_1.deepFreeze)(items);
25
15
  const item1 = items[0];
@@ -28,12 +18,12 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
28
18
  await db.ping();
29
19
  });
30
20
  // CREATE TABLE, DROP
31
- if (createTable) {
21
+ if (support.createTable) {
32
22
  test('createTable, dropIfExists=true', async () => {
33
23
  await db.createTable(test_model_1.TEST_TABLE, test_model_1.testItemDBMJsonSchema, { dropIfExists: true });
34
24
  });
35
25
  }
36
- if (querying) {
26
+ if (support.queries) {
37
27
  // DELETE ALL initially
38
28
  test('deleteByIds test items', async () => {
39
29
  const { rows } = await db.runQuery(queryAll().select(['id']));
@@ -41,8 +31,6 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
41
31
  });
42
32
  // QUERY empty
43
33
  test('runQuery(all), runQueryCount should return empty', async () => {
44
- if (eventualConsistencyDelay)
45
- await (0, js_lib_1.pDelay)(eventualConsistencyDelay);
46
34
  expect((await db.runQuery(queryAll())).rows).toEqual([]);
47
35
  expect(await db.runQueryCount(queryAll())).toBe(0);
48
36
  });
@@ -60,7 +48,7 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
60
48
  expect(await db.getByIds(test_model_1.TEST_TABLE, ['abc', 'abcd'])).toEqual([]);
61
49
  });
62
50
  // SAVE
63
- if (nullValues) {
51
+ if (support.nullValues) {
64
52
  test('should allow to save and load null values', async () => {
65
53
  const item3 = {
66
54
  ...(0, test_model_1.createTestItemDBM)(3),
@@ -73,7 +61,7 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
73
61
  expect(item3Loaded.k2).toBeNull();
74
62
  });
75
63
  }
76
- if (documentDB) {
64
+ if (db.dbType === common_db_1.CommonDBType.document) {
77
65
  test('undefined values should not be saved/loaded', async () => {
78
66
  const item3 = {
79
67
  ...(0, test_model_1.createTestItemDBM)(3),
@@ -89,7 +77,7 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
89
77
  expect(Object.keys(item3Loaded)).not.toContain('k2');
90
78
  });
91
79
  }
92
- if (update) {
80
+ if (support.updateSaveMethod) {
93
81
  test('saveBatch UPDATE method should throw', async () => {
94
82
  await expect(db.saveBatch(test_model_1.TEST_TABLE, items, { saveMethod: 'update' })).rejects.toThrow();
95
83
  });
@@ -100,12 +88,12 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
100
88
  test('saveBatch should throw on null id', async () => {
101
89
  await expect(db.saveBatch(test_model_1.TEST_TABLE, [{ ...item1, id: null }])).rejects.toThrow();
102
90
  });
103
- if (insert) {
91
+ if (support.insertSaveMethod) {
104
92
  test('saveBatch INSERT method should throw', async () => {
105
93
  await expect(db.saveBatch(test_model_1.TEST_TABLE, items, { saveMethod: 'insert' })).rejects.toThrow();
106
94
  });
107
95
  }
108
- if (update) {
96
+ if (support.updateSaveMethod) {
109
97
  test('saveBatch UPDATE method should pass', async () => {
110
98
  await db.saveBatch(test_model_1.TEST_TABLE, items, { saveMethod: 'update' });
111
99
  });
@@ -116,31 +104,29 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
116
104
  expectMatch(items, (0, js_lib_1._sortBy)(rows, r => r.id), quirks);
117
105
  });
118
106
  // QUERY
119
- if (querying) {
107
+ if (support.queries) {
120
108
  test('runQuery(all) should return all items', async () => {
121
- if (eventualConsistencyDelay)
122
- await (0, js_lib_1.pDelay)(eventualConsistencyDelay);
123
109
  let { rows } = await db.runQuery(queryAll());
124
110
  rows = (0, js_lib_1._sortBy)(rows, r => r.id); // because query doesn't specify order here
125
111
  expectMatch(items, rows, quirks);
126
112
  });
127
- if (dbQueryFilter) {
113
+ if (support.dbQueryFilter) {
128
114
  test('query even=true', async () => {
129
115
  const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).filter('even', '==', true);
130
116
  let { rows } = await db.runQuery(q);
131
- if (!dbQueryOrder)
117
+ if (!support.dbQueryOrder)
132
118
  rows = (0, js_lib_1._sortBy)(rows, r => r.id);
133
119
  expectMatch(items.filter(i => i.even), rows, quirks);
134
120
  });
135
121
  }
136
- if (dbQueryOrder) {
122
+ if (support.dbQueryOrder) {
137
123
  test('query order by k1 desc', async () => {
138
124
  const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).order('k1', true);
139
125
  const { rows } = await db.runQuery(q);
140
126
  expectMatch([...items].reverse(), rows, quirks);
141
127
  });
142
128
  }
143
- if (dbQuerySelectFields) {
129
+ if (support.dbQuerySelectFields) {
144
130
  test('projection query with only ids', async () => {
145
131
  const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).select(['id']);
146
132
  let { rows } = await db.runQuery(q);
@@ -164,7 +150,7 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
164
150
  });
165
151
  }
166
152
  // STREAM
167
- if (streaming) {
153
+ if (support.streaming) {
168
154
  test('streamQuery all', async () => {
169
155
  let rows = await (0, nodejs_lib_1.readableToArray)(db.streamQuery(queryAll()));
170
156
  rows = (0, js_lib_1._sortBy)(rows, r => r.id); // cause order is not specified in DBQuery
@@ -175,7 +161,7 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
175
161
  test('getTables, getTableSchema (if supported)', async () => {
176
162
  const tables = await db.getTables();
177
163
  // console.log({ tables })
178
- if (tableSchemas) {
164
+ if (support.tableSchemas) {
179
165
  await (0, js_lib_1.pMap)(tables, async (table) => {
180
166
  const schema = await db.getTableSchema(table);
181
167
  // console.log(schema)
@@ -184,19 +170,17 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
184
170
  }
185
171
  });
186
172
  // DELETE BY
187
- if (querying && dbQueryFilter) {
173
+ if (support.queries && support.dbQueryFilter) {
188
174
  test('deleteByQuery even=false', async () => {
189
175
  const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).filter('even', '==', false);
190
176
  const deleted = await db.deleteByQuery(q);
191
177
  expect(deleted).toBe(items.filter(item => !item.even).length);
192
- if (eventualConsistencyDelay)
193
- await (0, js_lib_1.pDelay)(eventualConsistencyDelay);
194
178
  expect(await db.runQueryCount(queryAll())).toBe(1);
195
179
  });
196
180
  }
197
181
  // BUFFER
198
- if (bufferSupport) {
199
- test('buffer support', async () => {
182
+ if (support.bufferValues) {
183
+ test('buffer values', async () => {
200
184
  const s = 'helloWorld 1';
201
185
  const b1 = Buffer.from(s);
202
186
  const item = {
@@ -218,7 +202,7 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
218
202
  expect(b1Loaded.toString()).toBe(s);
219
203
  });
220
204
  }
221
- if (transactions) {
205
+ if (support.transactions) {
222
206
  test('transaction happy path', async () => {
223
207
  // cleanup
224
208
  await db.deleteByQuery(queryAll());
@@ -226,27 +210,34 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
226
210
  // save item3 with k1: k1_mod
227
211
  // delete item2
228
212
  // remaining: item1, item3_with_k1_mod
229
- const tx = dbTransaction_1.DBTransaction.create()
230
- .saveBatch(test_model_1.TEST_TABLE, items)
231
- .save(test_model_1.TEST_TABLE, { ...items[2], k1: 'k1_mod' })
232
- .deleteById(test_model_1.TEST_TABLE, items[1].id);
233
- await db.commitTransaction(tx);
213
+ const tx = await db.createTransaction();
214
+ await db.saveBatch(test_model_1.TEST_TABLE, items, { tx });
215
+ await db.saveBatch(test_model_1.TEST_TABLE, [{ ...items[2], k1: 'k1_mod' }], { tx });
216
+ await db.deleteByIds(test_model_1.TEST_TABLE, [items[1].id], { tx });
217
+ await tx.commit();
234
218
  const { rows } = await db.runQuery(queryAll());
235
219
  const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
236
220
  expectMatch(expected, rows, quirks);
237
221
  });
238
222
  test('transaction rollback', async () => {
239
223
  // It should fail on id == null
240
- const tx = dbTransaction_1.DBTransaction.create()
241
- .deleteById(test_model_1.TEST_TABLE, items[2].id)
242
- .save(test_model_1.TEST_TABLE, { ...items[0], k1: 5, id: null });
243
- await expect(db.commitTransaction(tx)).rejects.toThrow();
224
+ let err;
225
+ try {
226
+ const tx = await db.createTransaction();
227
+ await db.deleteByIds(test_model_1.TEST_TABLE, [items[2].id], { tx });
228
+ await db.saveBatch(test_model_1.TEST_TABLE, [{ ...items[0], k1: 5, id: null }], { tx });
229
+ await tx.commit();
230
+ }
231
+ catch (err_) {
232
+ err = err_;
233
+ }
234
+ expect(err).toBeDefined();
244
235
  const { rows } = await db.runQuery(queryAll());
245
236
  const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
246
237
  expectMatch(expected, rows, quirks);
247
238
  });
248
239
  }
249
- if (updateByQuery) {
240
+ if (support.updateByQuery) {
250
241
  test('updateByQuery simple', async () => {
251
242
  // cleanup, reset initial data
252
243
  await db.deleteByQuery(queryAll());
@@ -265,7 +256,7 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
265
256
  });
266
257
  expectMatch(expected, rows, quirks);
267
258
  });
268
- if (dbIncrement) {
259
+ if (support.dbIncrement) {
269
260
  test('updateByQuery DBIncrement', async () => {
270
261
  // cleanup, reset initial data
271
262
  await db.deleteByQuery(queryAll());
@@ -286,7 +277,7 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
286
277
  });
287
278
  }
288
279
  }
289
- if (querying) {
280
+ if (support.queries) {
290
281
  test('cleanup', async () => {
291
282
  // CLEAN UP
292
283
  await db.deleteByQuery(queryAll());
@@ -1,6 +1,6 @@
1
1
  import { runCommonDaoTest } from './daoTest';
2
- import { CommonDBImplementationFeatures, CommonDBImplementationQuirks, runCommonDBTest } from './dbTest';
2
+ import { CommonDBImplementationQuirks, runCommonDBTest } from './dbTest';
3
3
  import { runCommonKeyValueDBTest } from './keyValueDBTest';
4
4
  import { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, TestItemBM, testItemBMJsonSchema, testItemBMSchema, TestItemDBM, testItemDBMJsonSchema, testItemDBMSchema, TestItemTM, testItemTMSchema, TEST_TABLE } from './test.model';
5
- export type { TestItemDBM, TestItemBM, TestItemTM, CommonDBImplementationFeatures, CommonDBImplementationQuirks, };
5
+ export type { TestItemDBM, TestItemBM, TestItemTM, CommonDBImplementationQuirks };
6
6
  export { TEST_TABLE, createTestItemDBM, createTestItemBM, createTestItemsDBM, createTestItemsBM, testItemDBMSchema, testItemBMSchema, testItemTMSchema, testItemBMJsonSchema, testItemDBMJsonSchema, runCommonDBTest, runCommonDaoTest, runCommonKeyValueDBTest, };
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CommonTimeSeriesDao = void 0;
4
4
  const js_lib_1 = require("@naturalcycles/js-lib");
5
- const __1 = require("..");
6
5
  const dbQuery_1 = require("../query/dbQuery");
7
6
  const _TIMESERIES_RAW = '_TIMESERIES_RAW';
8
7
  /**
@@ -43,16 +42,16 @@ class CommonTimeSeriesDao {
43
42
  async commitTransaction(ops) {
44
43
  if (!ops.length)
45
44
  return;
46
- const tx = __1.DBTransaction.create();
47
- ops.forEach(op => {
45
+ const tx = await this.cfg.db.createTransaction();
46
+ for (const op of ops) {
48
47
  const rows = op.dataPoints.map(([ts, v]) => ({
49
48
  id: String(ts), // Convert Number id into String id, as per CommonDB
50
49
  ts, // to allow querying by ts, since querying by id is not always available (Datastore is one example)
51
50
  v,
52
51
  }));
53
- tx.saveBatch(`${op.series}${_TIMESERIES_RAW}`, rows);
54
- });
55
- await this.cfg.db.commitTransaction(tx);
52
+ await this.cfg.db.saveBatch(`${op.series}${_TIMESERIES_RAW}`, rows, { tx });
53
+ }
54
+ await tx.commit();
56
55
  }
57
56
  async deleteById(series, tsMillis) {
58
57
  await this.deleteByIds(series, [tsMillis]);
@@ -1,6 +1,7 @@
1
+ import { ObjectWithId } from '@naturalcycles/js-lib';
1
2
  import type { CommonDB } from '../common.db';
2
- import { CommonDBSaveOptions, DBOperation } from '../db.model';
3
- import { DBTransaction } from './dbTransaction';
3
+ import { CommonDBOptions, CommonDBSaveOptions, DBTransaction, RunQueryResult } from '../db.model';
4
+ import { DBQuery } from '../query/dbQuery';
4
5
  /**
5
6
  * Optimizes the Transaction (list of DBOperations) to do less operations.
6
7
  * E.g if you save id1 first and then delete it - this function will turn it into a no-op (self-eliminate).
@@ -9,10 +10,22 @@ import { DBTransaction } from './dbTransaction';
9
10
  * Currently only takes into account SaveBatch and DeleteByIds ops.
10
11
  * Output ops are maximum 1 per entity - save or delete.
11
12
  */
12
- export declare function mergeDBOperations(ops: DBOperation[]): DBOperation[];
13
13
  /**
14
14
  * Naive implementation of "Transaction" which just executes all operations one-by-one.
15
15
  * Does NOT actually implement a Transaction, cause partial ops application will happen
16
16
  * in case of an error in the middle.
17
17
  */
18
- export declare function commitDBTransactionSimple(db: CommonDB, tx: DBTransaction, opt?: CommonDBSaveOptions): Promise<void>;
18
+ /**
19
+ * Fake implementation of DBTransactionContext,
20
+ * which executes all operations instantly, without any Transaction involved.
21
+ */
22
+ export declare class FakeDBTransaction implements DBTransaction {
23
+ protected db: CommonDB;
24
+ constructor(db: CommonDB);
25
+ commit(): Promise<void>;
26
+ rollback(): Promise<void>;
27
+ getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>;
28
+ runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions): Promise<RunQueryResult<ROW>>;
29
+ saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
30
+ deleteByIds(table: string, ids: string[], opt?: CommonDBOptions | undefined): Promise<number>;
31
+ }
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.commitDBTransactionSimple = exports.mergeDBOperations = void 0;
4
- const dbQuery_1 = require("../query/dbQuery");
3
+ exports.FakeDBTransaction = void 0;
5
4
  /**
6
5
  * Optimizes the Transaction (list of DBOperations) to do less operations.
7
6
  * E.g if you save id1 first and then delete it - this function will turn it into a no-op (self-eliminate).
@@ -10,10 +9,9 @@ const dbQuery_1 = require("../query/dbQuery");
10
9
  * Currently only takes into account SaveBatch and DeleteByIds ops.
11
10
  * Output ops are maximum 1 per entity - save or delete.
12
11
  */
13
- function mergeDBOperations(ops) {
14
- return ops; // currently "does nothing"
15
- }
16
- exports.mergeDBOperations = mergeDBOperations;
12
+ // export function mergeDBOperations(ops: DBOperation[]): DBOperation[] {
13
+ // return ops // currently "does nothing"
14
+ // }
17
15
  // Commented out as "overly complicated"
18
16
  /*
19
17
  export function mergeDBOperations(ops: DBOperation[]): DBOperation[] {
@@ -68,21 +66,47 @@ export function mergeDBOperations(ops: DBOperation[]): DBOperation[] {
68
66
  * Does NOT actually implement a Transaction, cause partial ops application will happen
69
67
  * in case of an error in the middle.
70
68
  */
71
- async function commitDBTransactionSimple(db, tx, opt) {
72
- // const ops = mergeDBOperations(tx.ops)
73
- for await (const op of tx.ops) {
74
- if (op.type === 'saveBatch') {
75
- await db.saveBatch(op.table, op.rows, { ...op.opt, ...opt });
76
- }
77
- else if (op.type === 'deleteByIds') {
78
- await db.deleteByQuery(dbQuery_1.DBQuery.create(op.table).filter('id', 'in', op.ids), {
79
- ...op.opt,
80
- ...opt,
81
- });
82
- }
83
- else {
84
- throw new Error(`DBOperation not supported: ${op.type}`);
85
- }
69
+ // export async function commitDBTransactionSimple(
70
+ // db: CommonDB,
71
+ // ops: DBOperation[],
72
+ // opt?: CommonDBSaveOptions,
73
+ // ): Promise<void> {
74
+ // // const ops = mergeDBOperations(tx.ops)
75
+ //
76
+ // for await (const op of ops) {
77
+ // if (op.type === 'saveBatch') {
78
+ // await db.saveBatch(op.table, op.rows, { ...op.opt, ...opt })
79
+ // } else if (op.type === 'deleteByIds') {
80
+ // await db.deleteByQuery(DBQuery.create(op.table).filter('id', 'in', op.ids), {
81
+ // ...op.opt,
82
+ // ...opt,
83
+ // })
84
+ // } else {
85
+ // throw new Error(`DBOperation not supported: ${(op as any).type}`)
86
+ // }
87
+ // }
88
+ // }
89
+ /**
90
+ * Fake implementation of DBTransactionContext,
91
+ * which executes all operations instantly, without any Transaction involved.
92
+ */
93
+ class FakeDBTransaction {
94
+ constructor(db) {
95
+ this.db = db;
96
+ }
97
+ async commit() { }
98
+ async rollback() { }
99
+ async getByIds(table, ids, opt) {
100
+ return await this.db.getByIds(table, ids, opt);
101
+ }
102
+ async runQuery(q, opt) {
103
+ return await this.db.runQuery(q, opt);
104
+ }
105
+ async saveBatch(table, rows, opt) {
106
+ return await this.db.saveBatch(table, rows, opt);
107
+ }
108
+ async deleteByIds(table, ids, opt) {
109
+ return await this.db.deleteByIds(table, ids, opt);
86
110
  }
87
111
  }
88
- exports.commitDBTransactionSimple = commitDBTransactionSimple;
112
+ exports.FakeDBTransaction = FakeDBTransaction;
@@ -4,8 +4,8 @@ exports.dbQuerySchema = exports.dbQueryOrderSchema = exports.dbQueryFilterSchema
4
4
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
5
5
  const dbQuery_1 = require("../query/dbQuery");
6
6
  exports.commonDBOptionsSchema = (0, nodejs_lib_1.objectSchema)({
7
- onlyCache: nodejs_lib_1.booleanSchema.optional(),
8
- skipCache: nodejs_lib_1.booleanSchema.optional(),
7
+ ['onlyCache']: nodejs_lib_1.booleanSchema.optional(),
8
+ ['skipCache']: nodejs_lib_1.booleanSchema.optional(),
9
9
  });
10
10
  exports.commonDBSaveOptionsSchema = (0, nodejs_lib_1.objectSchema)({
11
11
  excludeFromIndexes: (0, nodejs_lib_1.arraySchema)(nodejs_lib_1.stringSchema).optional(),
package/package.json CHANGED
@@ -40,7 +40,7 @@
40
40
  "engines": {
41
41
  "node": ">=18.12"
42
42
  },
43
- "version": "8.60.0",
43
+ "version": "9.0.0",
44
44
  "description": "Lowest Common Denominator API to supported Databases",
45
45
  "keywords": [
46
46
  "db",
@@ -1,6 +1,11 @@
1
1
  import { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib'
2
2
  import { CommonDB } from '../../common.db'
3
- import { CommonDBCreateOptions, CommonDBSaveOptions, CommonDBStreamOptions } from '../../db.model'
3
+ import {
4
+ CommonDBCreateOptions,
5
+ CommonDBOptions,
6
+ CommonDBSaveOptions,
7
+ CommonDBStreamOptions,
8
+ } from '../../db.model'
4
9
 
5
10
  export interface CacheDBCfg {
6
11
  name: string
@@ -45,7 +50,7 @@ export interface CacheDBCfg {
45
50
  logger?: CommonLogger
46
51
  }
47
52
 
48
- export interface CacheDBOptions {
53
+ export interface CacheDBOptions extends CommonDBOptions {
49
54
  /**
50
55
  * @default false
51
56
  */
@@ -7,10 +7,9 @@ import {
7
7
  StringMap,
8
8
  } from '@naturalcycles/js-lib'
9
9
  import { BaseCommonDB } from '../../base.common.db'
10
- import { CommonDB } from '../../common.db'
11
- import { CommonDBOptions, DBPatch, RunQueryResult } from '../../db.model'
10
+ import { CommonDB, commonDBFullSupport, CommonDBSupport } from '../../common.db'
11
+ import { DBPatch, RunQueryResult } from '../../db.model'
12
12
  import { DBQuery } from '../../query/dbQuery'
13
- import { DBTransaction } from '../../transaction/dbTransaction'
14
13
  import {
15
14
  CacheDBCfg,
16
15
  CacheDBCreateOptions,
@@ -26,6 +25,11 @@ import {
26
25
  * Queries always hit downstream (unless `onlyCache` is passed)
27
26
  */
28
27
  export class CacheDB extends BaseCommonDB implements CommonDB {
28
+ override support: CommonDBSupport = {
29
+ ...commonDBFullSupport,
30
+ transactions: false,
31
+ }
32
+
29
33
  constructor(cfg: CacheDBCfg) {
30
34
  super()
31
35
  this.cfg = {
@@ -284,9 +288,4 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
284
288
 
285
289
  return updated || 0
286
290
  }
287
-
288
- override async commitTransaction(tx: DBTransaction, opt?: CommonDBOptions): Promise<void> {
289
- await this.cfg.downstreamDB.commitTransaction(tx, opt)
290
- await this.cfg.cacheDB.commitTransaction(tx, opt)
291
- }
292
291
  }