@naturalcycles/datastore-lib 3.38.1 → 3.38.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.
@@ -57,6 +57,11 @@ export declare class DatastoreDB extends BaseCommonDB implements CommonDB {
57
57
  getTables(): Promise<string[]>;
58
58
  getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
59
59
  private getPRetryOptions;
60
+ /**
61
+ * Silently rollback the transaction.
62
+ * It may happen that transaction is already committed/rolled back, so we don't want to throw an error here.
63
+ */
64
+ private rollback;
60
65
  }
61
66
  /**
62
67
  * https://cloud.google.com/datastore/docs/concepts/transactions#datastore-datastore-transactional-update-nodejs
@@ -206,9 +206,7 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
206
206
  */
207
207
  async deleteByIds(table, ids, opt = {}) {
208
208
  const keys = ids.map(id => this.key(table, id));
209
- await (0, js_lib_1.pMap)((0, js_lib_1._chunk)(keys, MAX_ITEMS),
210
- // eslint-disable-next-line @typescript-eslint/return-await
211
- async (batch) => await (opt.tx?.tx || this.ds()).delete(batch), {
209
+ await (0, js_lib_1.pMap)((0, js_lib_1._chunk)(keys, MAX_ITEMS), async (batch) => await (opt.tx?.tx || this.ds()).delete(batch), {
212
210
  concurrency: DATASTORE_RECOMMENDED_CONCURRENCY,
213
211
  });
214
212
  return ids.length;
@@ -225,7 +223,7 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
225
223
  await datastoreTx.commit();
226
224
  }
227
225
  catch (err) {
228
- await datastoreTx.rollback();
226
+ await this.rollback(datastoreTx);
229
227
  throw err;
230
228
  }
231
229
  }
@@ -381,6 +379,19 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
381
379
  },
382
380
  };
383
381
  }
382
+ /**
383
+ * Silently rollback the transaction.
384
+ * It may happen that transaction is already committed/rolled back, so we don't want to throw an error here.
385
+ */
386
+ async rollback(datastoreTx) {
387
+ try {
388
+ await datastoreTx.rollback();
389
+ }
390
+ catch (err) {
391
+ // log the error, but don't re-throw, as this should be a graceful rollback
392
+ this.cfg.logger.error(err);
393
+ }
394
+ }
384
395
  }
385
396
  exports.DatastoreDB = DatastoreDB;
386
397
  /**
@@ -1,6 +1,5 @@
1
- import { CommonKeyValueDB } from '@naturalcycles/db-lib';
1
+ import { CommonKeyValueDB, KeyValueDBTuple } from '@naturalcycles/db-lib';
2
2
  import { IncrementTuple } from '@naturalcycles/db-lib/dist/kv/commonKeyValueDB';
3
- import { KeyValueTuple } from '@naturalcycles/js-lib';
4
3
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
5
4
  import { DatastoreDB } from './datastore.db';
6
5
  import { DatastoreDBCfg } from './datastore.model';
@@ -16,12 +15,12 @@ export declare class DatastoreKeyValueDB implements CommonKeyValueDB {
16
15
  };
17
16
  ping(): Promise<void>;
18
17
  createTable(): Promise<void>;
19
- getByIds<V>(table: string, ids: string[]): Promise<KeyValueTuple<string, V>[]>;
18
+ getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]>;
20
19
  deleteByIds(table: string, ids: string[]): Promise<void>;
21
- saveBatch<V>(table: string, entries: KeyValueTuple<string, V>[]): Promise<void>;
20
+ saveBatch(table: string, entries: KeyValueDBTuple[]): Promise<void>;
22
21
  streamIds(table: string, limit?: number): ReadableTyped<string>;
23
- streamValues<V>(table: string, limit?: number): ReadableTyped<V>;
24
- streamEntries<V>(table: string, limit?: number): ReadableTyped<KeyValueTuple<string, V>>;
22
+ streamValues(table: string, limit?: number): ReadableTyped<Buffer>;
23
+ streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple>;
25
24
  count(table: string): Promise<number>;
26
25
  incrementBatch(_table: string, _entries: IncrementTuple[]): Promise<IncrementTuple[]>;
27
26
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/datastore-lib",
3
- "version": "3.38.1",
3
+ "version": "3.38.3",
4
4
  "description": "Opinionated library to work with Google Datastore",
5
5
  "scripts": {
6
6
  "prepare": "husky",
@@ -350,7 +350,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
350
350
 
351
351
  await pMap(
352
352
  _chunk(keys, MAX_ITEMS),
353
- // eslint-disable-next-line @typescript-eslint/return-await
353
+
354
354
  async batch => await ((opt.tx as DatastoreDBTransaction)?.tx || this.ds()).delete(batch),
355
355
  {
356
356
  concurrency: DATASTORE_RECOMMENDED_CONCURRENCY,
@@ -374,7 +374,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
374
374
  await fn(tx)
375
375
  await datastoreTx.commit()
376
376
  } catch (err) {
377
- await datastoreTx.rollback()
377
+ await this.rollback(datastoreTx)
378
378
  throw err
379
379
  }
380
380
  }
@@ -549,6 +549,19 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
549
549
  },
550
550
  }
551
551
  }
552
+
553
+ /**
554
+ * Silently rollback the transaction.
555
+ * It may happen that transaction is already committed/rolled back, so we don't want to throw an error here.
556
+ */
557
+ private async rollback(datastoreTx: Transaction): Promise<void> {
558
+ try {
559
+ await datastoreTx.rollback()
560
+ } catch (err) {
561
+ // log the error, but don't re-throw, as this should be a graceful rollback
562
+ this.cfg.logger.error(err)
563
+ }
564
+ }
552
565
  }
553
566
 
554
567
  /**
@@ -1,16 +1,21 @@
1
- import { CommonKeyValueDB, commonKeyValueDBFullSupport, DBQuery } from '@naturalcycles/db-lib'
1
+ import {
2
+ CommonKeyValueDB,
3
+ commonKeyValueDBFullSupport,
4
+ DBQuery,
5
+ KeyValueDBTuple,
6
+ } from '@naturalcycles/db-lib'
2
7
  import { IncrementTuple } from '@naturalcycles/db-lib/dist/kv/commonKeyValueDB'
3
- import { AppError, KeyValueTuple, ObjectWithId } from '@naturalcycles/js-lib'
8
+ import { AppError, ObjectWithId } from '@naturalcycles/js-lib'
4
9
  import { ReadableTyped } from '@naturalcycles/nodejs-lib'
5
10
  import { DatastoreDB } from './datastore.db'
6
11
  import { DatastoreDBCfg } from './datastore.model'
7
12
 
8
- interface KVObject<V> {
13
+ interface KVObject {
9
14
  id: string
10
- v: V
15
+ v: Buffer
11
16
  }
12
17
 
13
- const excludeFromIndexes: (keyof KVObject<any>)[] = ['v']
18
+ const excludeFromIndexes: (keyof KVObject)[] = ['v']
14
19
 
15
20
  export interface DatastoreKeyValueDBCfg extends DatastoreDBCfg {}
16
21
 
@@ -32,16 +37,16 @@ export class DatastoreKeyValueDB implements CommonKeyValueDB {
32
37
 
33
38
  async createTable(): Promise<void> {}
34
39
 
35
- async getByIds<V>(table: string, ids: string[]): Promise<KeyValueTuple<string, V>[]> {
36
- return (await this.db.getByIds<KVObject<V>>(table, ids)).map(r => [r.id, r.v])
40
+ async getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]> {
41
+ return (await this.db.getByIds<KVObject>(table, ids)).map(r => [r.id, r.v])
37
42
  }
38
43
 
39
44
  async deleteByIds(table: string, ids: string[]): Promise<void> {
40
45
  await this.db.deleteByIds(table, ids)
41
46
  }
42
47
 
43
- async saveBatch<V>(table: string, entries: KeyValueTuple<string, V>[]): Promise<void> {
44
- await this.db.saveBatch<KVObject<V>>(
48
+ async saveBatch(table: string, entries: KeyValueDBTuple[]): Promise<void> {
49
+ await this.db.saveBatch<KVObject>(
45
50
  table,
46
51
  entries.map(([id, v]) => ({ id, v })),
47
52
  {
@@ -63,9 +68,9 @@ export class DatastoreKeyValueDB implements CommonKeyValueDB {
63
68
  )
64
69
  }
65
70
 
66
- streamValues<V>(table: string, limit?: number): ReadableTyped<V> {
71
+ streamValues(table: string, limit?: number): ReadableTyped<Buffer> {
67
72
  // `select v` doesn't work for some reason
68
- const q = DBQuery.create<KVObject<V>>(table).limit(limit || 0)
73
+ const q = DBQuery.create<KVObject>(table).limit(limit || 0)
69
74
 
70
75
  return (
71
76
  this.db
@@ -75,8 +80,8 @@ export class DatastoreKeyValueDB implements CommonKeyValueDB {
75
80
  )
76
81
  }
77
82
 
78
- streamEntries<V>(table: string, limit?: number): ReadableTyped<KeyValueTuple<string, V>> {
79
- const q = DBQuery.create<KVObject<V>>(table).limit(limit || 0)
83
+ streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple> {
84
+ const q = DBQuery.create<KVObject>(table).limit(limit || 0)
80
85
 
81
86
  return (
82
87
  this.db