@naturalcycles/datastore-lib 4.0.1 → 4.0.3

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.
@@ -39,6 +39,7 @@ export declare class DatastoreDB extends BaseCommonDB implements CommonDB {
39
39
  * regardless if they were actually deleted or not.
40
40
  */
41
41
  deleteByIds(table: string, ids: string[], opt?: DatastoreDBOptions): Promise<number>;
42
+ createTransaction(opt?: CommonDBTransactionOptions): Promise<DatastoreDBTransaction>;
42
43
  runInTransaction(fn: DBTransactionFn, opt?: CommonDBTransactionOptions): Promise<void>;
43
44
  getAllStats(): Promise<DatastoreStats[]>;
44
45
  /**
@@ -70,6 +71,7 @@ export declare class DatastoreDBTransaction implements DBTransaction {
70
71
  db: DatastoreDB;
71
72
  tx: Transaction;
72
73
  constructor(db: DatastoreDB, tx: Transaction);
74
+ commit(): Promise<void>;
73
75
  rollback(): Promise<void>;
74
76
  getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBReadOptions): Promise<ROW[]>;
75
77
  saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
@@ -254,6 +254,15 @@ export class DatastoreDB extends BaseCommonDB {
254
254
  });
255
255
  return ids.length;
256
256
  }
257
+ async createTransaction(opt = {}) {
258
+ const ds = await this.ds();
259
+ const { readOnly } = opt;
260
+ const datastoreTx = ds.transaction({
261
+ readOnly,
262
+ });
263
+ await datastoreTx.run();
264
+ return new DatastoreDBTransaction(this, datastoreTx);
265
+ }
257
266
  async runInTransaction(fn, opt = {}) {
258
267
  const ds = await this.ds();
259
268
  const { readOnly } = opt;
@@ -460,6 +469,9 @@ export class DatastoreDBTransaction {
460
469
  this.db = db;
461
470
  this.tx = tx;
462
471
  }
472
+ async commit() {
473
+ await this.tx.commit();
474
+ }
463
475
  async rollback() {
464
476
  try {
465
477
  await this.tx.rollback();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/datastore-lib",
3
3
  "type": "module",
4
- "version": "4.0.1",
4
+ "version": "4.0.3",
5
5
  "description": "Opinionated library to work with Google Datastore",
6
6
  "scripts": {
7
7
  "prepare": "husky",
@@ -14,11 +14,11 @@
14
14
  "dependencies": {
15
15
  "@google-cloud/datastore": "^9",
16
16
  "@naturalcycles/db-lib": "^10",
17
- "@naturalcycles/js-lib": "^14",
18
- "@naturalcycles/nodejs-lib": "^13"
17
+ "@naturalcycles/js-lib": "^15",
18
+ "@naturalcycles/nodejs-lib": "^14"
19
19
  },
20
20
  "devDependencies": {
21
- "@naturalcycles/dev-lib": "^17",
21
+ "@naturalcycles/dev-lib": "^18",
22
22
  "@types/node": "^22",
23
23
  "@vitest/coverage-v8": "^3",
24
24
  "tsx": "^4.19.3",
@@ -427,6 +427,18 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
427
427
  return ids.length
428
428
  }
429
429
 
430
+ override async createTransaction(
431
+ opt: CommonDBTransactionOptions = {},
432
+ ): Promise<DatastoreDBTransaction> {
433
+ const ds = await this.ds()
434
+ const { readOnly } = opt
435
+ const datastoreTx = ds.transaction({
436
+ readOnly,
437
+ })
438
+ await datastoreTx.run()
439
+ return new DatastoreDBTransaction(this, datastoreTx)
440
+ }
441
+
430
442
  override async runInTransaction(
431
443
  fn: DBTransactionFn,
432
444
  opt: CommonDBTransactionOptions = {},
@@ -658,6 +670,10 @@ export class DatastoreDBTransaction implements DBTransaction {
658
670
  public tx: Transaction,
659
671
  ) {}
660
672
 
673
+ async commit(): Promise<void> {
674
+ await this.tx.commit()
675
+ }
676
+
661
677
  async rollback(): Promise<void> {
662
678
  try {
663
679
  await this.tx.rollback()