@naturalcycles/datastore-lib 3.32.0 → 3.32.2

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,7 +1,7 @@
1
1
  import { Transaction } from '@google-cloud/datastore';
2
2
  import type { Datastore, Key, Query } from '@google-cloud/datastore';
3
3
  import { BaseCommonDB, CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBSupport, DBQuery, DBTransaction, DBTransactionFn, RunQueryResult, CommonDBTransactionOptions } from '@naturalcycles/db-lib';
4
- import { ObjectWithId, JsonSchemaObject, JsonSchemaRootObject, CommonLogger } from '@naturalcycles/js-lib';
4
+ import { JsonSchemaObject, JsonSchemaRootObject, CommonLogger, PartialObjectWithId, Saved, ObjectWithId } 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';
7
7
  /**
@@ -22,17 +22,17 @@ export declare class DatastoreDB extends BaseCommonDB implements CommonDB {
22
22
  protected KEY: symbol;
23
23
  ds(): Datastore;
24
24
  ping(): Promise<void>;
25
- getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: DatastoreDBOptions): Promise<ROW[]>;
26
- runQuery<ROW extends ObjectWithId>(dbQuery: DBQuery<ROW>, _opt?: DatastoreDBOptions): Promise<RunQueryResult<ROW>>;
27
- runQueryCount<ROW extends ObjectWithId>(dbQuery: DBQuery<ROW>, _opt?: DatastoreDBOptions): Promise<number>;
28
- runDatastoreQuery<ROW extends ObjectWithId>(q: Query): Promise<RunQueryResult<ROW>>;
25
+ getByIds<ROW extends PartialObjectWithId>(table: string, ids: string[], opt?: DatastoreDBOptions): Promise<Saved<ROW>[]>;
26
+ runQuery<ROW extends PartialObjectWithId>(dbQuery: DBQuery<ROW>, _opt?: DatastoreDBOptions): Promise<RunQueryResult<Saved<ROW>>>;
27
+ runQueryCount<ROW extends PartialObjectWithId>(dbQuery: DBQuery<ROW>, _opt?: DatastoreDBOptions): Promise<number>;
28
+ runDatastoreQuery<ROW extends PartialObjectWithId>(q: Query): Promise<RunQueryResult<Saved<ROW>>>;
29
29
  private runQueryStream;
30
- streamQuery<ROW extends ObjectWithId>(dbQuery: DBQuery<ROW>, opt?: DatastoreDBStreamOptions): ReadableTyped<ROW>;
30
+ streamQuery<ROW extends PartialObjectWithId>(dbQuery: DBQuery<ROW>, opt?: DatastoreDBStreamOptions): ReadableTyped<ROW>;
31
31
  /**
32
32
  * Returns saved entities with generated id/updated/created (non-mutating!)
33
33
  */
34
- saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: DatastoreDBSaveOptions<ROW>): Promise<void>;
35
- deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: DatastoreDBOptions): Promise<number>;
34
+ saveBatch<ROW extends PartialObjectWithId>(table: string, rows: ROW[], opt?: DatastoreDBSaveOptions<ROW>): Promise<void>;
35
+ deleteByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: DatastoreDBOptions): Promise<number>;
36
36
  /**
37
37
  * Limitation: Datastore's delete returns void, so we always return all ids here as "deleted"
38
38
  * regardless if they were actually deleted or not.
@@ -53,9 +53,9 @@ export declare class DatastoreDB extends BaseCommonDB implements CommonDB {
53
53
  key(kind: string, id: string | number): Key;
54
54
  getDsKey(o: any): Key | undefined;
55
55
  getKey(key: Key): string | undefined;
56
- createTable<ROW extends ObjectWithId>(_table: string, _schema: JsonSchemaObject<ROW>): Promise<void>;
56
+ createTable<ROW extends PartialObjectWithId>(_table: string, _schema: JsonSchemaObject<ROW>): Promise<void>;
57
57
  getTables(): Promise<string[]>;
58
- getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
58
+ getTableSchema<ROW extends PartialObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
59
59
  private getPRetryOptions;
60
60
  }
61
61
  /**
@@ -66,7 +66,7 @@ export declare class DatastoreDBTransaction implements DBTransaction {
66
66
  tx: Transaction;
67
67
  constructor(db: DatastoreDB, tx: Transaction);
68
68
  rollback(): Promise<void>;
69
- getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions | undefined): Promise<ROW[]>;
70
- saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW> | undefined): Promise<void>;
69
+ getByIds<ROW extends PartialObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions | undefined): Promise<ROW[]>;
70
+ saveBatch<ROW extends PartialObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW> | undefined): Promise<void>;
71
71
  deleteByIds(table: string, ids: string[], opt?: CommonDBOptions | undefined): Promise<number>;
72
72
  }
@@ -393,7 +393,13 @@ class DatastoreDBTransaction {
393
393
  this.tx = tx;
394
394
  }
395
395
  async rollback() {
396
- await this.tx.rollback();
396
+ try {
397
+ await this.tx.rollback();
398
+ }
399
+ catch (err) {
400
+ // log the error, but don't re-throw, as this should be a graceful rollback
401
+ this.db.cfg.logger.error(err);
402
+ }
397
403
  }
398
404
  async getByIds(table, ids, opt) {
399
405
  return await this.db.getByIds(table, ids, { ...opt, tx: this });
@@ -1,4 +1,4 @@
1
1
  import { Query } from '@google-cloud/datastore';
2
2
  import { DBQuery } from '@naturalcycles/db-lib';
3
- import { ObjectWithId } from '@naturalcycles/js-lib';
4
- export declare function dbQueryToDatastoreQuery<ROW extends ObjectWithId>(dbQuery: Readonly<DBQuery<ROW>>, emptyQuery: Query): Query;
3
+ import { PartialObjectWithId } from '@naturalcycles/js-lib';
4
+ export declare function dbQueryToDatastoreQuery<ROW extends PartialObjectWithId>(dbQuery: Readonly<DBQuery<ROW>>, emptyQuery: Query): Query;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/datastore-lib",
3
- "version": "3.32.0",
3
+ "version": "3.32.2",
4
4
  "description": "Opinionated library to work with Google Datastore",
5
5
  "scripts": {
6
6
  "prepare": "husky install"
@@ -16,7 +16,6 @@ import {
16
16
  CommonDBTransactionOptions,
17
17
  } from '@naturalcycles/db-lib'
18
18
  import {
19
- ObjectWithId,
20
19
  JsonSchemaAny,
21
20
  JsonSchemaBoolean,
22
21
  JsonSchemaNull,
@@ -34,6 +33,9 @@ import {
34
33
  pRetryFn,
35
34
  pRetry,
36
35
  PRetryOptions,
36
+ PartialObjectWithId,
37
+ Saved,
38
+ ObjectWithId,
37
39
  } from '@naturalcycles/js-lib'
38
40
  import { ReadableTyped, boldWhite } from '@naturalcycles/nodejs-lib'
39
41
  import { DatastoreStreamReadable } from './DatastoreStreamReadable'
@@ -133,11 +135,11 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
133
135
  await this.getAllStats()
134
136
  }
135
137
 
136
- override async getByIds<ROW extends ObjectWithId>(
138
+ override async getByIds<ROW extends PartialObjectWithId>(
137
139
  table: string,
138
140
  ids: string[],
139
141
  opt: DatastoreDBOptions = {},
140
- ): Promise<ROW[]> {
142
+ ): Promise<Saved<ROW>[]> {
141
143
  if (!ids.length) return []
142
144
  const keys = ids.map(id => this.key(table, id))
143
145
  let rows: any[]
@@ -187,7 +189,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
187
189
 
188
190
  return (
189
191
  rows
190
- .map(r => this.mapId<ROW>(r))
192
+ .map(r => this.mapId<Saved<ROW>>(r))
191
193
  // Seems like datastore .get() method doesn't return items properly sorted by input ids, so we gonna sort them here
192
194
  // same ids are not expected here
193
195
  .sort((a, b) => (a.id > b.id ? 1 : -1))
@@ -199,10 +201,10 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
199
201
  // return q.kinds[0]!
200
202
  // }
201
203
 
202
- override async runQuery<ROW extends ObjectWithId>(
204
+ override async runQuery<ROW extends PartialObjectWithId>(
203
205
  dbQuery: DBQuery<ROW>,
204
206
  _opt?: DatastoreDBOptions,
205
- ): Promise<RunQueryResult<ROW>> {
207
+ ): Promise<RunQueryResult<Saved<ROW>>> {
206
208
  const idFilter = dbQuery._filters.find(f => f.name === 'id')
207
209
  if (idFilter) {
208
210
  const ids: string[] = idFilter.op === '==' ? [idFilter.val] : idFilter.val
@@ -223,7 +225,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
223
225
  return qr
224
226
  }
225
227
 
226
- override async runQueryCount<ROW extends ObjectWithId>(
228
+ override async runQueryCount<ROW extends PartialObjectWithId>(
227
229
  dbQuery: DBQuery<ROW>,
228
230
  _opt?: DatastoreDBOptions,
229
231
  ): Promise<number> {
@@ -232,10 +234,12 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
232
234
  return entities.length
233
235
  }
234
236
 
235
- async runDatastoreQuery<ROW extends ObjectWithId>(q: Query): Promise<RunQueryResult<ROW>> {
237
+ async runDatastoreQuery<ROW extends PartialObjectWithId>(
238
+ q: Query,
239
+ ): Promise<RunQueryResult<Saved<ROW>>> {
236
240
  const [entities, queryResult] = await this.ds().runQuery(q)
237
241
 
238
- const rows = entities.map(e => this.mapId<ROW>(e))
242
+ const rows = entities.map(e => this.mapId<Saved<ROW>>(e))
239
243
 
240
244
  return {
241
245
  ...queryResult,
@@ -243,7 +247,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
243
247
  }
244
248
  }
245
249
 
246
- private runQueryStream<ROW extends ObjectWithId>(
250
+ private runQueryStream<ROW extends PartialObjectWithId>(
247
251
  q: Query,
248
252
  _opt?: DatastoreDBStreamOptions,
249
253
  ): ReadableTyped<ROW> {
@@ -274,7 +278,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
274
278
  return stream
275
279
  }
276
280
 
277
- override streamQuery<ROW extends ObjectWithId>(
281
+ override streamQuery<ROW extends PartialObjectWithId>(
278
282
  dbQuery: DBQuery<ROW>,
279
283
  opt?: DatastoreDBStreamOptions,
280
284
  ): ReadableTyped<ROW> {
@@ -287,7 +291,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
287
291
  /**
288
292
  * Returns saved entities with generated id/updated/created (non-mutating!)
289
293
  */
290
- override async saveBatch<ROW extends Partial<ObjectWithId>>(
294
+ override async saveBatch<ROW extends PartialObjectWithId>(
291
295
  table: string,
292
296
  rows: ROW[],
293
297
  opt: DatastoreDBSaveOptions<ROW> = {},
@@ -324,7 +328,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
324
328
  }
325
329
  }
326
330
 
327
- override async deleteByQuery<ROW extends ObjectWithId>(
331
+ override async deleteByQuery<ROW extends PartialObjectWithId>(
328
332
  q: DBQuery<ROW>,
329
333
  opt?: DatastoreDBOptions,
330
334
  ): Promise<number> {
@@ -458,7 +462,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
458
462
  return id?.toString()
459
463
  }
460
464
 
461
- override async createTable<ROW extends ObjectWithId>(
465
+ override async createTable<ROW extends PartialObjectWithId>(
462
466
  _table: string,
463
467
  _schema: JsonSchemaObject<ROW>,
464
468
  ): Promise<void> {}
@@ -469,7 +473,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
469
473
  return statsArray.map(stats => stats.kind_name).filter(table => table && !table.startsWith('_'))
470
474
  }
471
475
 
472
- override async getTableSchema<ROW extends ObjectWithId>(
476
+ override async getTableSchema<ROW extends PartialObjectWithId>(
473
477
  table: string,
474
478
  ): Promise<JsonSchemaRootObject<ROW>> {
475
479
  const stats = await this.getTableProperties(table)
@@ -564,10 +568,15 @@ export class DatastoreDBTransaction implements DBTransaction {
564
568
  ) {}
565
569
 
566
570
  async rollback(): Promise<void> {
567
- await this.tx.rollback()
571
+ try {
572
+ await this.tx.rollback()
573
+ } catch (err) {
574
+ // log the error, but don't re-throw, as this should be a graceful rollback
575
+ this.db.cfg.logger.error(err)
576
+ }
568
577
  }
569
578
 
570
- async getByIds<ROW extends ObjectWithId>(
579
+ async getByIds<ROW extends PartialObjectWithId>(
571
580
  table: string,
572
581
  ids: string[],
573
582
  opt?: CommonDBOptions | undefined,
@@ -575,7 +584,7 @@ export class DatastoreDBTransaction implements DBTransaction {
575
584
  return await this.db.getByIds(table, ids, { ...opt, tx: this })
576
585
  }
577
586
 
578
- async saveBatch<ROW extends Partial<ObjectWithId>>(
587
+ async saveBatch<ROW extends PartialObjectWithId>(
579
588
  table: string,
580
589
  rows: ROW[],
581
590
  opt?: CommonDBSaveOptions<ROW> | undefined,
package/src/query.util.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { PropertyFilter, Query } from '@google-cloud/datastore'
2
2
  import { DBQuery, DBQueryFilterOperator } from '@naturalcycles/db-lib'
3
- import { ObjectWithId, StringMap } from '@naturalcycles/js-lib'
3
+ import { PartialObjectWithId, StringMap } from '@naturalcycles/js-lib'
4
4
 
5
5
  const FNAME_MAP: StringMap = {
6
6
  id: '__key__',
@@ -12,7 +12,7 @@ const OP_MAP: Partial<Record<DBQueryFilterOperator, string>> = {
12
12
  'not-in': 'NOT_IN',
13
13
  }
14
14
 
15
- export function dbQueryToDatastoreQuery<ROW extends ObjectWithId>(
15
+ export function dbQueryToDatastoreQuery<ROW extends PartialObjectWithId>(
16
16
  dbQuery: Readonly<DBQuery<ROW>>,
17
17
  emptyQuery: Query,
18
18
  ): Query {