@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.
@@ -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.useTransaction(async tx => {
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.useTransaction(async tx => {
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.useTransaction(async tx => {
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.useTransaction(async tx => {
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
  }),
@@ -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
- 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()
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
- 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()
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
- const tx = await this.cfg.db.createTransaction()
56
-
57
- for (const op of ops) {
58
- const rows: ObjectWithId[] = op.dataPoints.map(([ts, v]) => ({
59
- id: String(ts), // Convert Number id into String id, as per CommonDB
60
- ts, // to allow querying by ts, since querying by id is not always available (Datastore is one example)
61
- v,
62
- }))
63
-
64
- await this.cfg.db.saveBatch(`${op.series}${_TIMESERIES_RAW}`, rows, { tx })
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, RunQueryResult } from '../db.model'
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
- async commit(): Promise<void> {}
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
- q: DBQuery<ROW>,
113
- opt?: CommonDBOptions,
114
- ): Promise<RunQueryResult<ROW>> {
115
- return await this.db.runQuery(q, opt)
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
- return await this.db.saveBatch(table, rows, opt)
121
+ await this.db.saveBatch(table, rows, opt)
123
122
  }
124
123
  async deleteByIds(
125
124
  table: string,