@naturalcycles/datastore-lib 3.25.3 → 3.26.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.
@@ -48,7 +48,7 @@ export declare class DatastoreDB extends BaseCommonDB implements CommonDB {
48
48
  getStats(table: string): Promise<DatastoreStats | undefined>;
49
49
  getStatsCount(table: string): Promise<number | undefined>;
50
50
  getTableProperties(table: string): Promise<DatastorePropertyStats[]>;
51
- mapId<T = any>(o: any, preserveKey?: boolean): T;
51
+ mapId<T extends ObjectWithId>(o: any, preserveKey?: boolean): T;
52
52
  toDatastoreEntity<T = any>(kind: string, o: T & {
53
53
  id?: string | number;
54
54
  }, excludeFromIndexes?: string[]): DatastorePayload<T>;
@@ -80,9 +80,10 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
80
80
  const DS = datastoreLib.Datastore;
81
81
  this.cachedDatastore = new DS(this.cfg);
82
82
  // Second try (will throw)
83
- const r = await (0, js_lib_1.pTimeout)(() => this.ds().get(keys), {
83
+ const r = await (0, js_lib_1.pRetry)(() => this.ds().get(keys), {
84
+ ...this.getPRetryOptions(`datastore.getByIds(${table}) second try`),
85
+ maxAttempts: 3,
84
86
  timeout: this.cfg.timeout,
85
- name: `datastore.getByIds(${table}) second try`,
86
87
  errorData: {
87
88
  // This error will be grouped ACROSS all endpoints and usages
88
89
  fingerprint: ['DATASTORE_TIMEOUT'],
@@ -92,7 +93,9 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
92
93
  }
93
94
  }
94
95
  else {
95
- rows = (await this.ds().get(keys))[0];
96
+ rows = await (0, js_lib_1.pRetry)(async () => {
97
+ return (await this.ds().get(keys))[0];
98
+ }, this.getPRetryOptions(`datastore.getByIds(${table})`));
96
99
  }
97
100
  return (rows
98
101
  .map(r => this.mapId(r))
@@ -101,7 +104,7 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
101
104
  .sort((a, b) => (a.id > b.id ? 1 : -1)));
102
105
  }
103
106
  getQueryKind(q) {
104
- if (!q || !q.kinds || !q.kinds.length)
107
+ if (!q?.kinds?.length)
105
108
  return ''; // should never be the case, but
106
109
  return q.kinds[0];
107
110
  }
@@ -246,7 +249,7 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
246
249
  }
247
250
  async getStatsCount(table) {
248
251
  const stats = await this.getStats(table);
249
- return stats && stats.count;
252
+ return stats?.count;
250
253
  }
251
254
  async getTableProperties(table) {
252
255
  const q = this.ds()
@@ -350,11 +353,9 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
350
353
  }
351
354
  else if (dtype === datastore_model_1.DatastoreType.NULL) {
352
355
  // check, maybe we can just skip this type and do nothing?
353
- if (!s.properties[name]) {
354
- s.properties[name] = {
355
- type: 'null',
356
- };
357
- }
356
+ s.properties[name] ||= {
357
+ type: 'null',
358
+ };
358
359
  }
359
360
  else {
360
361
  throw new Error(`Unknown Datastore Type '${stats.property_type}' for ${table}.${name}`);
@@ -366,9 +367,10 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
366
367
  return {
367
368
  predicate: err => RETRY_ON.some(s => err?.message?.includes(s)),
368
369
  name,
370
+ timeout: 10000,
369
371
  maxAttempts: 5,
370
372
  delay: 5000,
371
- delayMultiplier: 2,
373
+ delayMultiplier: 1.5,
372
374
  logFirstAttempt: false,
373
375
  logFailures: true,
374
376
  // logAll: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/datastore-lib",
3
- "version": "3.25.3",
3
+ "version": "3.26.0",
4
4
  "description": "Opinionated library to work with Google Datastore",
5
5
  "scripts": {
6
6
  "prepare": "husky install"
@@ -118,7 +118,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
118
118
  await this.getAllStats()
119
119
  }
120
120
 
121
- async getByIds<ROW extends ObjectWithId>(
121
+ override async getByIds<ROW extends ObjectWithId>(
122
122
  table: string,
123
123
  ids: ROW['id'][],
124
124
  _opt?: DatastoreDBOptions,
@@ -144,9 +144,10 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
144
144
  this.cachedDatastore = new DS(this.cfg)
145
145
 
146
146
  // Second try (will throw)
147
- const r = await pTimeout(() => this.ds().get(keys), {
147
+ const r = await pRetry(() => this.ds().get(keys), {
148
+ ...this.getPRetryOptions(`datastore.getByIds(${table}) second try`),
149
+ maxAttempts: 3,
148
150
  timeout: this.cfg.timeout,
149
- name: `datastore.getByIds(${table}) second try`,
150
151
  errorData: {
151
152
  // This error will be grouped ACROSS all endpoints and usages
152
153
  fingerprint: ['DATASTORE_TIMEOUT'],
@@ -155,7 +156,9 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
155
156
  rows = r[0]
156
157
  }
157
158
  } else {
158
- rows = (await this.ds().get(keys))[0]
159
+ rows = await pRetry(async () => {
160
+ return (await this.ds().get(keys))[0]
161
+ }, this.getPRetryOptions(`datastore.getByIds(${table})`))
159
162
  }
160
163
 
161
164
  return (
@@ -168,7 +171,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
168
171
  }
169
172
 
170
173
  getQueryKind(q: Query): string {
171
- if (!q || !q.kinds || !q.kinds.length) return '' // should never be the case, but
174
+ if (!q?.kinds?.length) return '' // should never be the case, but
172
175
  return q.kinds[0]!
173
176
  }
174
177
 
@@ -380,7 +383,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
380
383
 
381
384
  async getStatsCount(table: string): Promise<number | undefined> {
382
385
  const stats = await this.getStats(table)
383
- return stats && stats.count
386
+ return stats?.count
384
387
  }
385
388
 
386
389
  async getTableProperties(table: string): Promise<DatastorePropertyStats[]> {
@@ -391,7 +394,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
391
394
  return stats
392
395
  }
393
396
 
394
- mapId<T = any>(o: any, preserveKey = false): T {
397
+ mapId<T extends ObjectWithId>(o: any, preserveKey = false): T {
395
398
  if (!o) return o
396
399
  const r = {
397
400
  ...o,
@@ -497,11 +500,9 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
497
500
  s.properties[name] = {} as JsonSchemaAny
498
501
  } else if (dtype === DatastoreType.NULL) {
499
502
  // check, maybe we can just skip this type and do nothing?
500
- if (!s.properties[name]) {
501
- s.properties[name] = {
502
- type: 'null',
503
- } as JsonSchemaNull
504
- }
503
+ s.properties[name] ||= {
504
+ type: 'null',
505
+ } as JsonSchemaNull
505
506
  } else {
506
507
  throw new Error(
507
508
  `Unknown Datastore Type '${stats.property_type}' for ${table}.${name as string}`,
@@ -516,9 +517,10 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
516
517
  return {
517
518
  predicate: err => RETRY_ON.some(s => err?.message?.includes(s)),
518
519
  name,
520
+ timeout: 10_000,
519
521
  maxAttempts: 5,
520
522
  delay: 5000,
521
- delayMultiplier: 2,
523
+ delayMultiplier: 1.5,
522
524
  logFirstAttempt: false,
523
525
  logFailures: true,
524
526
  // logAll: true,