@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.
- package/dist/adapter/cachedb/cache.db.d.ts +3 -4
- package/dist/adapter/cachedb/cache.db.js +5 -4
- package/dist/adapter/cachedb/cache.db.model.d.ts +2 -2
- package/dist/adapter/file/file.db.d.ts +15 -7
- package/dist/adapter/file/file.db.js +93 -57
- package/dist/adapter/file/localFile.persistence.plugin.js +3 -3
- package/dist/adapter/inmemory/inMemory.db.d.ts +30 -4
- package/dist/adapter/inmemory/inMemory.db.js +89 -33
- package/dist/base.common.db.d.ts +7 -10
- package/dist/base.common.db.js +11 -7
- package/dist/common.db.d.ts +56 -4
- package/dist/common.db.js +23 -0
- package/dist/commondao/common.dao.d.ts +17 -9
- package/dist/commondao/common.dao.js +82 -69
- package/dist/commondao/common.dao.model.d.ts +0 -10
- package/dist/db.model.d.ts +12 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/pipeline/dbPipelineBackup.js +4 -4
- package/dist/pipeline/dbPipelineRestore.js +2 -2
- package/dist/testing/daoTest.d.ts +2 -2
- package/dist/testing/daoTest.js +29 -39
- package/dist/testing/dbTest.d.ts +1 -39
- package/dist/testing/dbTest.js +40 -49
- package/dist/testing/index.d.ts +2 -2
- package/dist/timeseries/commonTimeSeriesDao.js +5 -6
- package/dist/transaction/dbTransaction.util.d.ts +17 -4
- package/dist/transaction/dbTransaction.util.js +46 -22
- package/dist/validation/index.js +2 -2
- package/package.json +1 -1
- package/src/adapter/cachedb/cache.db.model.ts +7 -2
- package/src/adapter/cachedb/cache.db.ts +7 -8
- package/src/adapter/file/file.db.ts +121 -69
- package/src/adapter/file/localFile.persistence.plugin.ts +4 -5
- package/src/adapter/inmemory/inMemory.db.ts +106 -33
- package/src/base.common.db.ts +20 -11
- package/src/common.db.ts +80 -3
- package/src/commondao/common.dao.model.ts +0 -11
- package/src/commondao/common.dao.ts +103 -89
- package/src/db.model.ts +15 -2
- package/src/index.ts +0 -1
- package/src/pipeline/dbPipelineBackup.ts +5 -8
- package/src/pipeline/dbPipelineRestore.ts +3 -4
- package/src/testing/daoTest.ts +32 -52
- package/src/testing/dbTest.ts +42 -119
- package/src/testing/index.ts +2 -12
- package/src/timeseries/commonTimeSeriesDao.ts +5 -6
- package/src/transaction/dbTransaction.util.ts +61 -22
- package/src/validation/index.ts +2 -2
- package/dist/transaction/dbTransaction.d.ts +0 -27
- package/dist/transaction/dbTransaction.js +0 -64
- package/src/transaction/dbTransaction.ts +0 -67
package/src/testing/dbTest.ts
CHANGED
|
@@ -1,68 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { _filterObject, _pick, _sortBy, pMap } from '@naturalcycles/js-lib'
|
|
2
2
|
import { readableToArray } from '@naturalcycles/nodejs-lib'
|
|
3
|
-
import { CommonDB } from '../common.db'
|
|
3
|
+
import { CommonDB, CommonDBType } from '../common.db'
|
|
4
4
|
import { DBIncrement, DBPatch } from '../db.model'
|
|
5
5
|
import { DBQuery } from '../query/dbQuery'
|
|
6
|
-
import { DBTransaction } from '../transaction/dbTransaction'
|
|
7
6
|
import {
|
|
8
7
|
createTestItemDBM,
|
|
9
8
|
createTestItemsDBM,
|
|
10
|
-
TestItemDBM,
|
|
11
9
|
TEST_TABLE,
|
|
10
|
+
TestItemDBM,
|
|
12
11
|
testItemDBMJsonSchema,
|
|
13
12
|
} from './test.model'
|
|
14
13
|
import { deepFreeze } from './test.util'
|
|
15
14
|
|
|
16
|
-
export interface CommonDBImplementationFeatures {
|
|
17
|
-
/**
|
|
18
|
-
* All querying functionality.
|
|
19
|
-
*/
|
|
20
|
-
querying?: boolean
|
|
21
|
-
|
|
22
|
-
dbQueryFilter?: boolean
|
|
23
|
-
dbQueryFilterIn?: boolean
|
|
24
|
-
dbQueryOrder?: boolean
|
|
25
|
-
dbQuerySelectFields?: boolean
|
|
26
|
-
insert?: boolean
|
|
27
|
-
update?: boolean
|
|
28
|
-
|
|
29
|
-
updateByQuery?: boolean
|
|
30
|
-
|
|
31
|
-
dbIncrement?: boolean
|
|
32
|
-
|
|
33
|
-
createTable?: boolean
|
|
34
|
-
tableSchemas?: boolean
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Queries should return fresh results immediately.
|
|
38
|
-
* Datastore is the one known to NOT have strong consistency for queries (not for getById though).
|
|
39
|
-
*/
|
|
40
|
-
strongConsistency?: boolean
|
|
41
|
-
|
|
42
|
-
streaming?: boolean
|
|
43
|
-
|
|
44
|
-
bufferSupport?: boolean
|
|
45
|
-
nullValues?: boolean
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Set false for SQL (relational) databases,
|
|
49
|
-
* they will return `null` for all missing properties.
|
|
50
|
-
*/
|
|
51
|
-
documentDB?: boolean
|
|
52
|
-
|
|
53
|
-
transactions?: boolean
|
|
54
|
-
}
|
|
55
|
-
|
|
56
15
|
/**
|
|
57
16
|
* All options default to `false`.
|
|
58
17
|
*/
|
|
59
18
|
export interface CommonDBImplementationQuirks {
|
|
60
|
-
/**
|
|
61
|
-
* Applicable to e.g Datastore.
|
|
62
|
-
* Time in milliseconds to wait for eventual consistency to propagate.
|
|
63
|
-
*/
|
|
64
|
-
eventualConsistencyDelay?: number
|
|
65
|
-
|
|
66
19
|
/**
|
|
67
20
|
* Example: airtableId
|
|
68
21
|
*/
|
|
@@ -74,40 +27,8 @@ export interface CommonDBImplementationQuirks {
|
|
|
74
27
|
allowBooleansAsUndefined?: boolean
|
|
75
28
|
}
|
|
76
29
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
*/
|
|
80
|
-
export function runCommonDBTest(
|
|
81
|
-
db: CommonDB,
|
|
82
|
-
features: CommonDBImplementationFeatures = {},
|
|
83
|
-
quirks: CommonDBImplementationQuirks = {},
|
|
84
|
-
): void {
|
|
85
|
-
const {
|
|
86
|
-
querying = true,
|
|
87
|
-
tableSchemas = true,
|
|
88
|
-
createTable = true,
|
|
89
|
-
dbQueryFilter = true,
|
|
90
|
-
// dbQueryFilterIn = true,
|
|
91
|
-
dbQueryOrder = true,
|
|
92
|
-
dbQuerySelectFields = true,
|
|
93
|
-
insert = true,
|
|
94
|
-
update = true,
|
|
95
|
-
updateByQuery = true,
|
|
96
|
-
dbIncrement = true,
|
|
97
|
-
streaming = true,
|
|
98
|
-
strongConsistency = true,
|
|
99
|
-
bufferSupport = true,
|
|
100
|
-
nullValues = true,
|
|
101
|
-
documentDB = true,
|
|
102
|
-
transactions = true,
|
|
103
|
-
} = features
|
|
104
|
-
|
|
105
|
-
// const {
|
|
106
|
-
// allowExtraPropertiesInResponse,
|
|
107
|
-
// allowBooleansAsUndefined,
|
|
108
|
-
// } = quirks
|
|
109
|
-
const eventualConsistencyDelay = !strongConsistency && quirks.eventualConsistencyDelay
|
|
110
|
-
|
|
30
|
+
export function runCommonDBTest(db: CommonDB, quirks: CommonDBImplementationQuirks = {}): void {
|
|
31
|
+
const { support } = db
|
|
111
32
|
const items = createTestItemsDBM(3)
|
|
112
33
|
deepFreeze(items)
|
|
113
34
|
const item1 = items[0]!
|
|
@@ -119,13 +40,13 @@ export function runCommonDBTest(
|
|
|
119
40
|
})
|
|
120
41
|
|
|
121
42
|
// CREATE TABLE, DROP
|
|
122
|
-
if (createTable) {
|
|
43
|
+
if (support.createTable) {
|
|
123
44
|
test('createTable, dropIfExists=true', async () => {
|
|
124
45
|
await db.createTable(TEST_TABLE, testItemDBMJsonSchema, { dropIfExists: true })
|
|
125
46
|
})
|
|
126
47
|
}
|
|
127
48
|
|
|
128
|
-
if (
|
|
49
|
+
if (support.queries) {
|
|
129
50
|
// DELETE ALL initially
|
|
130
51
|
test('deleteByIds test items', async () => {
|
|
131
52
|
const { rows } = await db.runQuery(queryAll().select(['id']))
|
|
@@ -139,7 +60,6 @@ export function runCommonDBTest(
|
|
|
139
60
|
|
|
140
61
|
// QUERY empty
|
|
141
62
|
test('runQuery(all), runQueryCount should return empty', async () => {
|
|
142
|
-
if (eventualConsistencyDelay) await pDelay(eventualConsistencyDelay)
|
|
143
63
|
expect((await db.runQuery(queryAll())).rows).toEqual([])
|
|
144
64
|
expect(await db.runQueryCount(queryAll())).toBe(0)
|
|
145
65
|
})
|
|
@@ -161,7 +81,7 @@ export function runCommonDBTest(
|
|
|
161
81
|
})
|
|
162
82
|
|
|
163
83
|
// SAVE
|
|
164
|
-
if (nullValues) {
|
|
84
|
+
if (support.nullValues) {
|
|
165
85
|
test('should allow to save and load null values', async () => {
|
|
166
86
|
const item3 = {
|
|
167
87
|
...createTestItemDBM(3),
|
|
@@ -175,7 +95,7 @@ export function runCommonDBTest(
|
|
|
175
95
|
})
|
|
176
96
|
}
|
|
177
97
|
|
|
178
|
-
if (
|
|
98
|
+
if (db.dbType === CommonDBType.document) {
|
|
179
99
|
test('undefined values should not be saved/loaded', async () => {
|
|
180
100
|
const item3 = {
|
|
181
101
|
...createTestItemDBM(3),
|
|
@@ -193,7 +113,7 @@ export function runCommonDBTest(
|
|
|
193
113
|
})
|
|
194
114
|
}
|
|
195
115
|
|
|
196
|
-
if (
|
|
116
|
+
if (support.updateSaveMethod) {
|
|
197
117
|
test('saveBatch UPDATE method should throw', async () => {
|
|
198
118
|
await expect(db.saveBatch(TEST_TABLE, items, { saveMethod: 'update' })).rejects.toThrow()
|
|
199
119
|
})
|
|
@@ -207,13 +127,13 @@ export function runCommonDBTest(
|
|
|
207
127
|
await expect(db.saveBatch(TEST_TABLE, [{ ...item1, id: null as any }])).rejects.toThrow()
|
|
208
128
|
})
|
|
209
129
|
|
|
210
|
-
if (
|
|
130
|
+
if (support.insertSaveMethod) {
|
|
211
131
|
test('saveBatch INSERT method should throw', async () => {
|
|
212
132
|
await expect(db.saveBatch(TEST_TABLE, items, { saveMethod: 'insert' })).rejects.toThrow()
|
|
213
133
|
})
|
|
214
134
|
}
|
|
215
135
|
|
|
216
|
-
if (
|
|
136
|
+
if (support.updateSaveMethod) {
|
|
217
137
|
test('saveBatch UPDATE method should pass', async () => {
|
|
218
138
|
await db.saveBatch(TEST_TABLE, items, { saveMethod: 'update' })
|
|
219
139
|
})
|
|
@@ -230,19 +150,18 @@ export function runCommonDBTest(
|
|
|
230
150
|
})
|
|
231
151
|
|
|
232
152
|
// QUERY
|
|
233
|
-
if (
|
|
153
|
+
if (support.queries) {
|
|
234
154
|
test('runQuery(all) should return all items', async () => {
|
|
235
|
-
if (eventualConsistencyDelay) await pDelay(eventualConsistencyDelay)
|
|
236
155
|
let { rows } = await db.runQuery(queryAll())
|
|
237
156
|
rows = _sortBy(rows, r => r.id) // because query doesn't specify order here
|
|
238
157
|
expectMatch(items, rows, quirks)
|
|
239
158
|
})
|
|
240
159
|
|
|
241
|
-
if (dbQueryFilter) {
|
|
160
|
+
if (support.dbQueryFilter) {
|
|
242
161
|
test('query even=true', async () => {
|
|
243
162
|
const q = new DBQuery<TestItemDBM>(TEST_TABLE).filter('even', '==', true)
|
|
244
163
|
let { rows } = await db.runQuery(q)
|
|
245
|
-
if (!dbQueryOrder) rows = _sortBy(rows, r => r.id)
|
|
164
|
+
if (!support.dbQueryOrder) rows = _sortBy(rows, r => r.id)
|
|
246
165
|
expectMatch(
|
|
247
166
|
items.filter(i => i.even),
|
|
248
167
|
rows,
|
|
@@ -251,7 +170,7 @@ export function runCommonDBTest(
|
|
|
251
170
|
})
|
|
252
171
|
}
|
|
253
172
|
|
|
254
|
-
if (dbQueryOrder) {
|
|
173
|
+
if (support.dbQueryOrder) {
|
|
255
174
|
test('query order by k1 desc', async () => {
|
|
256
175
|
const q = new DBQuery<TestItemDBM>(TEST_TABLE).order('k1', true)
|
|
257
176
|
const { rows } = await db.runQuery(q)
|
|
@@ -259,7 +178,7 @@ export function runCommonDBTest(
|
|
|
259
178
|
})
|
|
260
179
|
}
|
|
261
180
|
|
|
262
|
-
if (dbQuerySelectFields) {
|
|
181
|
+
if (support.dbQuerySelectFields) {
|
|
263
182
|
test('projection query with only ids', async () => {
|
|
264
183
|
const q = new DBQuery<TestItemDBM>(TEST_TABLE).select(['id'])
|
|
265
184
|
let { rows } = await db.runQuery(q)
|
|
@@ -299,7 +218,7 @@ export function runCommonDBTest(
|
|
|
299
218
|
}
|
|
300
219
|
|
|
301
220
|
// STREAM
|
|
302
|
-
if (streaming) {
|
|
221
|
+
if (support.streaming) {
|
|
303
222
|
test('streamQuery all', async () => {
|
|
304
223
|
let rows = await readableToArray(db.streamQuery(queryAll()))
|
|
305
224
|
|
|
@@ -313,7 +232,7 @@ export function runCommonDBTest(
|
|
|
313
232
|
const tables = await db.getTables()
|
|
314
233
|
// console.log({ tables })
|
|
315
234
|
|
|
316
|
-
if (tableSchemas) {
|
|
235
|
+
if (support.tableSchemas) {
|
|
317
236
|
await pMap(tables, async table => {
|
|
318
237
|
const schema = await db.getTableSchema(table)
|
|
319
238
|
// console.log(schema)
|
|
@@ -323,21 +242,19 @@ export function runCommonDBTest(
|
|
|
323
242
|
})
|
|
324
243
|
|
|
325
244
|
// DELETE BY
|
|
326
|
-
if (
|
|
245
|
+
if (support.queries && support.dbQueryFilter) {
|
|
327
246
|
test('deleteByQuery even=false', async () => {
|
|
328
247
|
const q = new DBQuery<TestItemDBM>(TEST_TABLE).filter('even', '==', false)
|
|
329
248
|
const deleted = await db.deleteByQuery(q)
|
|
330
249
|
expect(deleted).toBe(items.filter(item => !item.even).length)
|
|
331
250
|
|
|
332
|
-
if (eventualConsistencyDelay) await pDelay(eventualConsistencyDelay)
|
|
333
|
-
|
|
334
251
|
expect(await db.runQueryCount(queryAll())).toBe(1)
|
|
335
252
|
})
|
|
336
253
|
}
|
|
337
254
|
|
|
338
255
|
// BUFFER
|
|
339
|
-
if (
|
|
340
|
-
test('buffer
|
|
256
|
+
if (support.bufferValues) {
|
|
257
|
+
test('buffer values', async () => {
|
|
341
258
|
const s = 'helloWorld 1'
|
|
342
259
|
const b1 = Buffer.from(s)
|
|
343
260
|
|
|
@@ -361,7 +278,7 @@ export function runCommonDBTest(
|
|
|
361
278
|
})
|
|
362
279
|
}
|
|
363
280
|
|
|
364
|
-
if (transactions) {
|
|
281
|
+
if (support.transactions) {
|
|
365
282
|
test('transaction happy path', async () => {
|
|
366
283
|
// cleanup
|
|
367
284
|
await db.deleteByQuery(queryAll())
|
|
@@ -370,12 +287,11 @@ export function runCommonDBTest(
|
|
|
370
287
|
// save item3 with k1: k1_mod
|
|
371
288
|
// delete item2
|
|
372
289
|
// remaining: item1, item3_with_k1_mod
|
|
373
|
-
const tx =
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
await db.commitTransaction(tx)
|
|
290
|
+
const tx = await db.createTransaction()
|
|
291
|
+
await db.saveBatch(TEST_TABLE, items, { tx })
|
|
292
|
+
await db.saveBatch(TEST_TABLE, [{ ...items[2]!, k1: 'k1_mod' }], { tx })
|
|
293
|
+
await db.deleteByIds(TEST_TABLE, [items[1]!.id], { tx })
|
|
294
|
+
await tx.commit()
|
|
379
295
|
|
|
380
296
|
const { rows } = await db.runQuery(queryAll())
|
|
381
297
|
const expected = [items[0], { ...items[2]!, k1: 'k1_mod' }]
|
|
@@ -384,11 +300,18 @@ export function runCommonDBTest(
|
|
|
384
300
|
|
|
385
301
|
test('transaction rollback', async () => {
|
|
386
302
|
// It should fail on id == null
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
303
|
+
let err: any
|
|
304
|
+
|
|
305
|
+
try {
|
|
306
|
+
const tx = await db.createTransaction()
|
|
307
|
+
await db.deleteByIds(TEST_TABLE, [items[2]!.id], { tx })
|
|
308
|
+
await db.saveBatch(TEST_TABLE, [{ ...items[0]!, k1: 5, id: null as any }], { tx })
|
|
309
|
+
await tx.commit()
|
|
310
|
+
} catch (err_) {
|
|
311
|
+
err = err_
|
|
312
|
+
}
|
|
390
313
|
|
|
391
|
-
|
|
314
|
+
expect(err).toBeDefined()
|
|
392
315
|
|
|
393
316
|
const { rows } = await db.runQuery(queryAll())
|
|
394
317
|
const expected = [items[0], { ...items[2]!, k1: 'k1_mod' }]
|
|
@@ -396,7 +319,7 @@ export function runCommonDBTest(
|
|
|
396
319
|
})
|
|
397
320
|
}
|
|
398
321
|
|
|
399
|
-
if (updateByQuery) {
|
|
322
|
+
if (support.updateByQuery) {
|
|
400
323
|
test('updateByQuery simple', async () => {
|
|
401
324
|
// cleanup, reset initial data
|
|
402
325
|
await db.deleteByQuery(queryAll())
|
|
@@ -419,7 +342,7 @@ export function runCommonDBTest(
|
|
|
419
342
|
expectMatch(expected, rows, quirks)
|
|
420
343
|
})
|
|
421
344
|
|
|
422
|
-
if (dbIncrement) {
|
|
345
|
+
if (support.dbIncrement) {
|
|
423
346
|
test('updateByQuery DBIncrement', async () => {
|
|
424
347
|
// cleanup, reset initial data
|
|
425
348
|
await db.deleteByQuery(queryAll())
|
|
@@ -447,7 +370,7 @@ export function runCommonDBTest(
|
|
|
447
370
|
}
|
|
448
371
|
}
|
|
449
372
|
|
|
450
|
-
if (
|
|
373
|
+
if (support.queries) {
|
|
451
374
|
test('cleanup', async () => {
|
|
452
375
|
// CLEAN UP
|
|
453
376
|
await db.deleteByQuery(queryAll())
|
package/src/testing/index.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import { runCommonDaoTest } from './daoTest'
|
|
2
|
-
import {
|
|
3
|
-
CommonDBImplementationFeatures,
|
|
4
|
-
CommonDBImplementationQuirks,
|
|
5
|
-
runCommonDBTest,
|
|
6
|
-
} from './dbTest'
|
|
2
|
+
import { CommonDBImplementationQuirks, runCommonDBTest } from './dbTest'
|
|
7
3
|
import { runCommonKeyValueDBTest } from './keyValueDBTest'
|
|
8
4
|
import {
|
|
9
5
|
createTestItemBM,
|
|
@@ -21,13 +17,7 @@ import {
|
|
|
21
17
|
TEST_TABLE,
|
|
22
18
|
} from './test.model'
|
|
23
19
|
|
|
24
|
-
export type {
|
|
25
|
-
TestItemDBM,
|
|
26
|
-
TestItemBM,
|
|
27
|
-
TestItemTM,
|
|
28
|
-
CommonDBImplementationFeatures,
|
|
29
|
-
CommonDBImplementationQuirks,
|
|
30
|
-
}
|
|
20
|
+
export type { TestItemDBM, TestItemBM, TestItemTM, CommonDBImplementationQuirks }
|
|
31
21
|
|
|
32
22
|
export {
|
|
33
23
|
TEST_TABLE,
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { _isTruthy, ObjectWithId } from '@naturalcycles/js-lib'
|
|
2
|
-
import { DBTransaction } from '..'
|
|
3
2
|
import { DBQuery } from '../query/dbQuery'
|
|
4
3
|
import {
|
|
5
4
|
CommonTimeSeriesDaoCfg,
|
|
@@ -53,19 +52,19 @@ export class CommonTimeSeriesDao {
|
|
|
53
52
|
async commitTransaction(ops: TimeSeriesSaveBatchOp[]): Promise<void> {
|
|
54
53
|
if (!ops.length) return
|
|
55
54
|
|
|
56
|
-
const tx =
|
|
55
|
+
const tx = await this.cfg.db.createTransaction()
|
|
57
56
|
|
|
58
|
-
|
|
57
|
+
for (const op of ops) {
|
|
59
58
|
const rows: ObjectWithId[] = op.dataPoints.map(([ts, v]) => ({
|
|
60
59
|
id: String(ts), // Convert Number id into String id, as per CommonDB
|
|
61
60
|
ts, // to allow querying by ts, since querying by id is not always available (Datastore is one example)
|
|
62
61
|
v,
|
|
63
62
|
}))
|
|
64
63
|
|
|
65
|
-
|
|
66
|
-
}
|
|
64
|
+
await this.cfg.db.saveBatch(`${op.series}${_TIMESERIES_RAW}`, rows, { tx })
|
|
65
|
+
}
|
|
67
66
|
|
|
68
|
-
await
|
|
67
|
+
await tx.commit()
|
|
69
68
|
}
|
|
70
69
|
|
|
71
70
|
async deleteById(series: string, tsMillis: number): Promise<void> {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { ObjectWithId } from '@naturalcycles/js-lib'
|
|
1
2
|
import type { CommonDB } from '../common.db'
|
|
2
|
-
import { CommonDBSaveOptions,
|
|
3
|
+
import { CommonDBOptions, CommonDBSaveOptions, DBTransaction, RunQueryResult } from '../db.model'
|
|
3
4
|
import { DBQuery } from '../query/dbQuery'
|
|
4
|
-
import { DBTransaction } from './dbTransaction'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Optimizes the Transaction (list of DBOperations) to do less operations.
|
|
@@ -11,9 +11,9 @@ import { DBTransaction } from './dbTransaction'
|
|
|
11
11
|
* Currently only takes into account SaveBatch and DeleteByIds ops.
|
|
12
12
|
* Output ops are maximum 1 per entity - save or delete.
|
|
13
13
|
*/
|
|
14
|
-
export function mergeDBOperations(ops: DBOperation[]): DBOperation[] {
|
|
15
|
-
|
|
16
|
-
}
|
|
14
|
+
// export function mergeDBOperations(ops: DBOperation[]): DBOperation[] {
|
|
15
|
+
// return ops // currently "does nothing"
|
|
16
|
+
// }
|
|
17
17
|
|
|
18
18
|
// Commented out as "overly complicated"
|
|
19
19
|
/*
|
|
@@ -70,23 +70,62 @@ export function mergeDBOperations(ops: DBOperation[]): DBOperation[] {
|
|
|
70
70
|
* Does NOT actually implement a Transaction, cause partial ops application will happen
|
|
71
71
|
* in case of an error in the middle.
|
|
72
72
|
*/
|
|
73
|
-
export async function commitDBTransactionSimple(
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
): Promise<void> {
|
|
78
|
-
|
|
73
|
+
// export async function commitDBTransactionSimple(
|
|
74
|
+
// db: CommonDB,
|
|
75
|
+
// ops: DBOperation[],
|
|
76
|
+
// opt?: CommonDBSaveOptions,
|
|
77
|
+
// ): Promise<void> {
|
|
78
|
+
// // const ops = mergeDBOperations(tx.ops)
|
|
79
|
+
//
|
|
80
|
+
// for await (const op of ops) {
|
|
81
|
+
// if (op.type === 'saveBatch') {
|
|
82
|
+
// await db.saveBatch(op.table, op.rows, { ...op.opt, ...opt })
|
|
83
|
+
// } else if (op.type === 'deleteByIds') {
|
|
84
|
+
// await db.deleteByQuery(DBQuery.create(op.table).filter('id', 'in', op.ids), {
|
|
85
|
+
// ...op.opt,
|
|
86
|
+
// ...opt,
|
|
87
|
+
// })
|
|
88
|
+
// } else {
|
|
89
|
+
// throw new Error(`DBOperation not supported: ${(op as any).type}`)
|
|
90
|
+
// }
|
|
91
|
+
// }
|
|
92
|
+
// }
|
|
79
93
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Fake implementation of DBTransactionContext,
|
|
96
|
+
* which executes all operations instantly, without any Transaction involved.
|
|
97
|
+
*/
|
|
98
|
+
export class FakeDBTransaction implements DBTransaction {
|
|
99
|
+
constructor(protected db: CommonDB) {}
|
|
100
|
+
|
|
101
|
+
async commit(): Promise<void> {}
|
|
102
|
+
async rollback(): Promise<void> {}
|
|
103
|
+
|
|
104
|
+
async getByIds<ROW extends ObjectWithId>(
|
|
105
|
+
table: string,
|
|
106
|
+
ids: string[],
|
|
107
|
+
opt?: CommonDBOptions,
|
|
108
|
+
): Promise<ROW[]> {
|
|
109
|
+
return await this.db.getByIds(table, ids, opt)
|
|
110
|
+
}
|
|
111
|
+
async runQuery<ROW extends ObjectWithId>(
|
|
112
|
+
q: DBQuery<ROW>,
|
|
113
|
+
opt?: CommonDBOptions,
|
|
114
|
+
): Promise<RunQueryResult<ROW>> {
|
|
115
|
+
return await this.db.runQuery(q, opt)
|
|
116
|
+
}
|
|
117
|
+
async saveBatch<ROW extends Partial<ObjectWithId>>(
|
|
118
|
+
table: string,
|
|
119
|
+
rows: ROW[],
|
|
120
|
+
opt?: CommonDBSaveOptions<ROW>,
|
|
121
|
+
): Promise<void> {
|
|
122
|
+
return await this.db.saveBatch(table, rows, opt)
|
|
123
|
+
}
|
|
124
|
+
async deleteByIds(
|
|
125
|
+
table: string,
|
|
126
|
+
ids: string[],
|
|
127
|
+
opt?: CommonDBOptions | undefined,
|
|
128
|
+
): Promise<number> {
|
|
129
|
+
return await this.db.deleteByIds(table, ids, opt)
|
|
91
130
|
}
|
|
92
131
|
}
|
package/src/validation/index.ts
CHANGED
|
@@ -17,8 +17,8 @@ import {
|
|
|
17
17
|
} from '../query/dbQuery'
|
|
18
18
|
|
|
19
19
|
export const commonDBOptionsSchema = objectSchema<CommonDBOptions>({
|
|
20
|
-
onlyCache: booleanSchema.optional(),
|
|
21
|
-
skipCache: booleanSchema.optional(),
|
|
20
|
+
['onlyCache' as any]: booleanSchema.optional(),
|
|
21
|
+
['skipCache' as any]: booleanSchema.optional(),
|
|
22
22
|
})
|
|
23
23
|
|
|
24
24
|
export const commonDBSaveOptionsSchema = objectSchema<CommonDBSaveOptions>({
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { ObjectWithId } from '@naturalcycles/js-lib';
|
|
2
|
-
import type { CommonDB } from '../common.db';
|
|
3
|
-
import type { CommonDBSaveOptions, DBOperation } from '../db.model';
|
|
4
|
-
/**
|
|
5
|
-
* Convenience class that stores the list of DBOperations and provides a fluent API to add them.
|
|
6
|
-
*/
|
|
7
|
-
export declare class DBTransaction {
|
|
8
|
-
ops: DBOperation[];
|
|
9
|
-
protected constructor(ops?: DBOperation[]);
|
|
10
|
-
/**
|
|
11
|
-
* Convenience method.
|
|
12
|
-
*/
|
|
13
|
-
static create(ops?: DBOperation[]): DBTransaction;
|
|
14
|
-
save<ROW extends Partial<ObjectWithId>>(table: string, row: ROW): this;
|
|
15
|
-
saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[]): this;
|
|
16
|
-
deleteById(table: string, id: string): this;
|
|
17
|
-
deleteByIds(table: string, ids: string[]): this;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Extends DBTransaction by providing a convenient `commit` method that delegates
|
|
21
|
-
* to CommonDB.commitTransaction().
|
|
22
|
-
*/
|
|
23
|
-
export declare class RunnableDBTransaction extends DBTransaction {
|
|
24
|
-
db: CommonDB;
|
|
25
|
-
constructor(db: CommonDB);
|
|
26
|
-
commit<ROW extends Partial<ObjectWithId>>(opt?: CommonDBSaveOptions<ROW>): Promise<void>;
|
|
27
|
-
}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RunnableDBTransaction = exports.DBTransaction = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Convenience class that stores the list of DBOperations and provides a fluent API to add them.
|
|
6
|
-
*/
|
|
7
|
-
class DBTransaction {
|
|
8
|
-
constructor(ops = []) {
|
|
9
|
-
this.ops = ops;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Convenience method.
|
|
13
|
-
*/
|
|
14
|
-
static create(ops = []) {
|
|
15
|
-
return new DBTransaction(ops);
|
|
16
|
-
}
|
|
17
|
-
save(table, row) {
|
|
18
|
-
this.ops.push({
|
|
19
|
-
type: 'saveBatch',
|
|
20
|
-
table,
|
|
21
|
-
rows: [row],
|
|
22
|
-
});
|
|
23
|
-
return this;
|
|
24
|
-
}
|
|
25
|
-
saveBatch(table, rows) {
|
|
26
|
-
this.ops.push({
|
|
27
|
-
type: 'saveBatch',
|
|
28
|
-
table,
|
|
29
|
-
rows,
|
|
30
|
-
});
|
|
31
|
-
return this;
|
|
32
|
-
}
|
|
33
|
-
deleteById(table, id) {
|
|
34
|
-
this.ops.push({
|
|
35
|
-
type: 'deleteByIds',
|
|
36
|
-
table,
|
|
37
|
-
ids: [id],
|
|
38
|
-
});
|
|
39
|
-
return this;
|
|
40
|
-
}
|
|
41
|
-
deleteByIds(table, ids) {
|
|
42
|
-
this.ops.push({
|
|
43
|
-
type: 'deleteByIds',
|
|
44
|
-
table,
|
|
45
|
-
ids,
|
|
46
|
-
});
|
|
47
|
-
return this;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
exports.DBTransaction = DBTransaction;
|
|
51
|
-
/**
|
|
52
|
-
* Extends DBTransaction by providing a convenient `commit` method that delegates
|
|
53
|
-
* to CommonDB.commitTransaction().
|
|
54
|
-
*/
|
|
55
|
-
class RunnableDBTransaction extends DBTransaction {
|
|
56
|
-
constructor(db) {
|
|
57
|
-
super();
|
|
58
|
-
this.db = db;
|
|
59
|
-
}
|
|
60
|
-
async commit(opt) {
|
|
61
|
-
await this.db.commitTransaction(this, opt);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
exports.RunnableDBTransaction = RunnableDBTransaction;
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { ObjectWithId } from '@naturalcycles/js-lib'
|
|
2
|
-
import type { CommonDB } from '../common.db'
|
|
3
|
-
import type { CommonDBSaveOptions, DBOperation } from '../db.model'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Convenience class that stores the list of DBOperations and provides a fluent API to add them.
|
|
7
|
-
*/
|
|
8
|
-
export class DBTransaction {
|
|
9
|
-
protected constructor(public ops: DBOperation[] = []) {}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Convenience method.
|
|
13
|
-
*/
|
|
14
|
-
static create(ops: DBOperation[] = []): DBTransaction {
|
|
15
|
-
return new DBTransaction(ops)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
save<ROW extends Partial<ObjectWithId>>(table: string, row: ROW): this {
|
|
19
|
-
this.ops.push({
|
|
20
|
-
type: 'saveBatch',
|
|
21
|
-
table,
|
|
22
|
-
rows: [row],
|
|
23
|
-
})
|
|
24
|
-
return this
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[]): this {
|
|
28
|
-
this.ops.push({
|
|
29
|
-
type: 'saveBatch',
|
|
30
|
-
table,
|
|
31
|
-
rows,
|
|
32
|
-
})
|
|
33
|
-
return this
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
deleteById(table: string, id: string): this {
|
|
37
|
-
this.ops.push({
|
|
38
|
-
type: 'deleteByIds',
|
|
39
|
-
table,
|
|
40
|
-
ids: [id],
|
|
41
|
-
})
|
|
42
|
-
return this
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
deleteByIds(table: string, ids: string[]): this {
|
|
46
|
-
this.ops.push({
|
|
47
|
-
type: 'deleteByIds',
|
|
48
|
-
table,
|
|
49
|
-
ids,
|
|
50
|
-
})
|
|
51
|
-
return this
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Extends DBTransaction by providing a convenient `commit` method that delegates
|
|
57
|
-
* to CommonDB.commitTransaction().
|
|
58
|
-
*/
|
|
59
|
-
export class RunnableDBTransaction extends DBTransaction {
|
|
60
|
-
constructor(public db: CommonDB) {
|
|
61
|
-
super()
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async commit<ROW extends Partial<ObjectWithId>>(opt?: CommonDBSaveOptions<ROW>): Promise<void> {
|
|
65
|
-
await this.db.commitTransaction(this, opt)
|
|
66
|
-
}
|
|
67
|
-
}
|