@naturalcycles/db-lib 9.0.0 → 9.1.1
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/file/file.db.d.ts +1 -12
- package/dist/adapter/file/file.db.js +79 -69
- package/dist/adapter/inmemory/inMemory.db.d.ts +7 -4
- package/dist/adapter/inmemory/inMemory.db.js +33 -38
- package/dist/base.common.db.d.ts +2 -2
- package/dist/base.common.db.js +4 -2
- package/dist/common.db.d.ts +7 -3
- package/dist/commondao/common.dao.d.ts +19 -8
- package/dist/commondao/common.dao.js +46 -54
- package/dist/db.model.d.ts +18 -1
- package/dist/testing/daoTest.js +4 -4
- package/dist/testing/dbTest.js +10 -10
- package/dist/timeseries/commonTimeSeriesDao.js +10 -10
- package/dist/transaction/dbTransaction.util.d.ts +1 -4
- package/dist/transaction/dbTransaction.util.js +8 -5
- package/package.json +1 -1
- package/src/adapter/file/file.db.ts +8 -14
- package/src/adapter/inmemory/inMemory.db.ts +44 -41
- package/src/base.common.db.ts +5 -3
- package/src/common.db.ts +7 -3
- package/src/commondao/common.dao.ts +60 -57
- package/src/db.model.ts +20 -1
- package/src/testing/daoTest.ts +4 -4
- package/src/testing/dbTest.ts +10 -10
- package/src/timeseries/commonTimeSeriesDao.ts +11 -13
- package/src/transaction/dbTransaction.util.ts +9 -10
package/src/testing/daoTest.ts
CHANGED
|
@@ -265,7 +265,7 @@ export function runCommonDaoTest(db: CommonDB, quirks: CommonDBImplementationQui
|
|
|
265
265
|
// Test that id, created, updated are created
|
|
266
266
|
const now = localTimeNow().unix()
|
|
267
267
|
|
|
268
|
-
await dao.
|
|
268
|
+
await dao.runInTransaction(async tx => {
|
|
269
269
|
const row = _omit(item1, ['id', 'created', 'updated'])
|
|
270
270
|
await tx.save(dao, row)
|
|
271
271
|
})
|
|
@@ -276,7 +276,7 @@ export function runCommonDaoTest(db: CommonDB, quirks: CommonDBImplementationQui
|
|
|
276
276
|
expect(loaded[0]!.created).toBeGreaterThanOrEqual(now)
|
|
277
277
|
expect(loaded[0]!.updated).toBe(loaded[0]!.created)
|
|
278
278
|
|
|
279
|
-
await dao.
|
|
279
|
+
await dao.runInTransaction(async tx => {
|
|
280
280
|
await tx.deleteById(dao, loaded[0]!.id)
|
|
281
281
|
})
|
|
282
282
|
|
|
@@ -284,7 +284,7 @@ export function runCommonDaoTest(db: CommonDB, quirks: CommonDBImplementationQui
|
|
|
284
284
|
// save item3 with k1: k1_mod
|
|
285
285
|
// delete item2
|
|
286
286
|
// remaining: item1, item3_with_k1_mod
|
|
287
|
-
await dao.
|
|
287
|
+
await dao.runInTransaction(async tx => {
|
|
288
288
|
await tx.saveBatch(dao, items)
|
|
289
289
|
await tx.save(dao, { ...items[2]!, k1: 'k1_mod' })
|
|
290
290
|
await tx.deleteById(dao, items[1]!.id)
|
|
@@ -297,7 +297,7 @@ export function runCommonDaoTest(db: CommonDB, quirks: CommonDBImplementationQui
|
|
|
297
297
|
|
|
298
298
|
test('transaction rollback', async () => {
|
|
299
299
|
await expect(
|
|
300
|
-
dao.
|
|
300
|
+
dao.runInTransaction(async tx => {
|
|
301
301
|
await tx.deleteById(dao, items[2]!.id)
|
|
302
302
|
await tx.save(dao, { ...items[0]!, k1: 5 as any }) // it should fail here
|
|
303
303
|
}),
|
package/src/testing/dbTest.ts
CHANGED
|
@@ -287,11 +287,11 @@ export function runCommonDBTest(db: CommonDB, quirks: CommonDBImplementationQuir
|
|
|
287
287
|
// save item3 with k1: k1_mod
|
|
288
288
|
// delete item2
|
|
289
289
|
// remaining: item1, item3_with_k1_mod
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
290
|
+
await db.runInTransaction(async tx => {
|
|
291
|
+
await tx.saveBatch(TEST_TABLE, items)
|
|
292
|
+
await tx.saveBatch(TEST_TABLE, [{ ...items[2]!, k1: 'k1_mod' }])
|
|
293
|
+
await tx.deleteByIds(TEST_TABLE, [items[1]!.id])
|
|
294
|
+
})
|
|
295
295
|
|
|
296
296
|
const { rows } = await db.runQuery(queryAll())
|
|
297
297
|
const expected = [items[0], { ...items[2]!, k1: 'k1_mod' }]
|
|
@@ -299,14 +299,14 @@ export function runCommonDBTest(db: CommonDB, quirks: CommonDBImplementationQuir
|
|
|
299
299
|
})
|
|
300
300
|
|
|
301
301
|
test('transaction rollback', async () => {
|
|
302
|
-
// It should fail on id == null
|
|
303
302
|
let err: any
|
|
304
303
|
|
|
305
304
|
try {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
305
|
+
await db.runInTransaction(async tx => {
|
|
306
|
+
await tx.deleteByIds(TEST_TABLE, [items[2]!.id])
|
|
307
|
+
// It should fail on id == null
|
|
308
|
+
await tx.saveBatch(TEST_TABLE, [{ ...items[0]!, k1: 5, id: null as any }])
|
|
309
|
+
})
|
|
310
310
|
} catch (err_) {
|
|
311
311
|
err = err_
|
|
312
312
|
}
|
|
@@ -52,19 +52,17 @@ export class CommonTimeSeriesDao {
|
|
|
52
52
|
async commitTransaction(ops: TimeSeriesSaveBatchOp[]): Promise<void> {
|
|
53
53
|
if (!ops.length) return
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
await tx.commit()
|
|
55
|
+
await this.cfg.db.runInTransaction(async tx => {
|
|
56
|
+
for (const op of ops) {
|
|
57
|
+
const rows: ObjectWithId[] = op.dataPoints.map(([ts, v]) => ({
|
|
58
|
+
id: String(ts), // Convert Number id into String id, as per CommonDB
|
|
59
|
+
ts, // to allow querying by ts, since querying by id is not always available (Datastore is one example)
|
|
60
|
+
v,
|
|
61
|
+
}))
|
|
62
|
+
|
|
63
|
+
await tx.saveBatch(`${op.series}${_TIMESERIES_RAW}`, rows)
|
|
64
|
+
}
|
|
65
|
+
})
|
|
68
66
|
}
|
|
69
67
|
|
|
70
68
|
async deleteById(series: string, tsMillis: number): Promise<void> {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { ObjectWithId } from '@naturalcycles/js-lib'
|
|
2
2
|
import type { CommonDB } from '../common.db'
|
|
3
|
-
import { CommonDBOptions, CommonDBSaveOptions, DBTransaction
|
|
4
|
-
import { DBQuery } from '../query/dbQuery'
|
|
3
|
+
import { CommonDBOptions, CommonDBSaveOptions, DBTransaction } from '../db.model'
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Optimizes the Transaction (list of DBOperations) to do less operations.
|
|
@@ -98,7 +97,7 @@ export function mergeDBOperations(ops: DBOperation[]): DBOperation[] {
|
|
|
98
97
|
export class FakeDBTransaction implements DBTransaction {
|
|
99
98
|
constructor(protected db: CommonDB) {}
|
|
100
99
|
|
|
101
|
-
|
|
100
|
+
// no-op
|
|
102
101
|
async rollback(): Promise<void> {}
|
|
103
102
|
|
|
104
103
|
async getByIds<ROW extends ObjectWithId>(
|
|
@@ -108,18 +107,18 @@ export class FakeDBTransaction implements DBTransaction {
|
|
|
108
107
|
): Promise<ROW[]> {
|
|
109
108
|
return await this.db.getByIds(table, ids, opt)
|
|
110
109
|
}
|
|
111
|
-
async runQuery<ROW extends ObjectWithId>(
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
): Promise<RunQueryResult<ROW>> {
|
|
115
|
-
|
|
116
|
-
}
|
|
110
|
+
// async runQuery<ROW extends ObjectWithId>(
|
|
111
|
+
// q: DBQuery<ROW>,
|
|
112
|
+
// opt?: CommonDBOptions,
|
|
113
|
+
// ): Promise<RunQueryResult<ROW>> {
|
|
114
|
+
// return await this.db.runQuery(q, opt)
|
|
115
|
+
// }
|
|
117
116
|
async saveBatch<ROW extends Partial<ObjectWithId>>(
|
|
118
117
|
table: string,
|
|
119
118
|
rows: ROW[],
|
|
120
119
|
opt?: CommonDBSaveOptions<ROW>,
|
|
121
120
|
): Promise<void> {
|
|
122
|
-
|
|
121
|
+
await this.db.saveBatch(table, rows, opt)
|
|
123
122
|
}
|
|
124
123
|
async deleteByIds(
|
|
125
124
|
table: string,
|