@naturalcycles/datastore-lib 3.31.1 → 3.32.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.
@@ -1,6 +1,6 @@
1
1
  import { Transaction } from '@google-cloud/datastore';
2
2
  import type { Datastore, Key, Query } from '@google-cloud/datastore';
3
- import { BaseCommonDB, CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBSupport, DBQuery, DBTransaction, DBTransactionFn, RunQueryResult } from '@naturalcycles/db-lib';
3
+ import { BaseCommonDB, CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBSupport, DBQuery, DBTransaction, DBTransactionFn, RunQueryResult, CommonDBTransactionOptions } from '@naturalcycles/db-lib';
4
4
  import { ObjectWithId, JsonSchemaObject, JsonSchemaRootObject, CommonLogger } from '@naturalcycles/js-lib';
5
5
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
6
6
  import { DatastoreDBCfg, DatastoreDBOptions, DatastoreDBSaveOptions, DatastoreDBStreamOptions, DatastorePayload, DatastorePropertyStats, DatastoreStats } from './datastore.model';
@@ -38,7 +38,7 @@ export declare class DatastoreDB extends BaseCommonDB implements CommonDB {
38
38
  * regardless if they were actually deleted or not.
39
39
  */
40
40
  deleteByIds(table: string, ids: string[], opt?: DatastoreDBOptions): Promise<number>;
41
- runInTransaction(fn: DBTransactionFn): Promise<void>;
41
+ runInTransaction(fn: DBTransactionFn, opt?: CommonDBTransactionOptions): Promise<void>;
42
42
  getAllStats(): Promise<DatastoreStats[]>;
43
43
  /**
44
44
  * Returns undefined e.g when Table is non-existing
@@ -64,8 +64,7 @@ export declare class DatastoreDB extends BaseCommonDB implements CommonDB {
64
64
  export declare class DatastoreDBTransaction implements DBTransaction {
65
65
  db: DatastoreDB;
66
66
  tx: Transaction;
67
- private constructor();
68
- static create(db: DatastoreDB): Promise<DatastoreDBTransaction>;
67
+ constructor(db: DatastoreDB, tx: Transaction);
69
68
  rollback(): Promise<void>;
70
69
  getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions | undefined): Promise<ROW[]>;
71
70
  saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW> | undefined): Promise<void>;
@@ -214,14 +214,19 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
214
214
  async (batch) => await (opt.tx?.tx || this.ds()).delete(batch));
215
215
  return ids.length;
216
216
  }
217
- async runInTransaction(fn) {
218
- const tx = await DatastoreDBTransaction.create(this);
217
+ async runInTransaction(fn, opt = {}) {
218
+ const { readOnly } = opt;
219
+ const datastoreTx = this.ds().transaction({
220
+ readOnly,
221
+ });
219
222
  try {
223
+ await datastoreTx.run();
224
+ const tx = new DatastoreDBTransaction(this, datastoreTx);
220
225
  await fn(tx);
221
- await tx.tx.commit();
226
+ await datastoreTx.commit();
222
227
  }
223
228
  catch (err) {
224
- await tx.tx.rollback();
229
+ await datastoreTx.rollback();
225
230
  throw err;
226
231
  }
227
232
  }
@@ -387,11 +392,6 @@ class DatastoreDBTransaction {
387
392
  this.db = db;
388
393
  this.tx = tx;
389
394
  }
390
- static async create(db) {
391
- const tx = db.ds().transaction();
392
- await tx.run();
393
- return new DatastoreDBTransaction(db, tx);
394
- }
395
395
  async rollback() {
396
396
  await this.tx.rollback();
397
397
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/datastore-lib",
3
- "version": "3.31.1",
3
+ "version": "3.32.0",
4
4
  "description": "Opinionated library to work with Google Datastore",
5
5
  "scripts": {
6
6
  "prepare": "husky install"
@@ -13,6 +13,7 @@ import {
13
13
  DBTransaction,
14
14
  DBTransactionFn,
15
15
  RunQueryResult,
16
+ CommonDBTransactionOptions,
16
17
  } from '@naturalcycles/db-lib'
17
18
  import {
18
19
  ObjectWithId,
@@ -361,14 +362,22 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
361
362
  return ids.length
362
363
  }
363
364
 
364
- override async runInTransaction(fn: DBTransactionFn): Promise<void> {
365
- const tx = await DatastoreDBTransaction.create(this)
365
+ override async runInTransaction(
366
+ fn: DBTransactionFn,
367
+ opt: CommonDBTransactionOptions = {},
368
+ ): Promise<void> {
369
+ const { readOnly } = opt
370
+ const datastoreTx = this.ds().transaction({
371
+ readOnly,
372
+ })
366
373
 
367
374
  try {
375
+ await datastoreTx.run()
376
+ const tx = new DatastoreDBTransaction(this, datastoreTx)
368
377
  await fn(tx)
369
- await tx.tx.commit()
378
+ await datastoreTx.commit()
370
379
  } catch (err) {
371
- await tx.tx.rollback()
380
+ await datastoreTx.rollback()
372
381
  throw err
373
382
  }
374
383
  }
@@ -549,17 +558,11 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
549
558
  * https://cloud.google.com/datastore/docs/concepts/transactions#datastore-datastore-transactional-update-nodejs
550
559
  */
551
560
  export class DatastoreDBTransaction implements DBTransaction {
552
- private constructor(
561
+ constructor(
553
562
  public db: DatastoreDB,
554
563
  public tx: Transaction,
555
564
  ) {}
556
565
 
557
- static async create(db: DatastoreDB): Promise<DatastoreDBTransaction> {
558
- const tx = db.ds().transaction()
559
- await tx.run()
560
- return new DatastoreDBTransaction(db, tx)
561
- }
562
-
563
566
  async rollback(): Promise<void> {
564
567
  await this.tx.rollback()
565
568
  }