@naturalcycles/datastore-lib 3.19.2 → 3.21.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.
@@ -30,7 +30,7 @@ export declare class DatastoreDB extends BaseCommonDB implements CommonDB {
30
30
  /**
31
31
  * Returns saved entities with generated id/updated/created (non-mutating!)
32
32
  */
33
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: DatastoreDBSaveOptions<ROW>): Promise<void>;
33
+ saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: DatastoreDBSaveOptions<ROW>): Promise<void>;
34
34
  deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: DatastoreDBOptions): Promise<number>;
35
35
  /**
36
36
  * Limitation: Datastore's delete returns void, so we always return all ids here as "deleted"
@@ -13,6 +13,11 @@ const MAX_ITEMS = 500;
13
13
  const RETRY_ON = ['GOAWAY', 'UNAVAILABLE', 'UNKNOWN'];
14
14
  // Examples of errors:
15
15
  // UNKNOWN: Stream removed
16
+ const methodMap = {
17
+ insert: 'insert',
18
+ update: 'update',
19
+ upsert: 'save',
20
+ };
16
21
  /**
17
22
  * Datastore API:
18
23
  * https://googlecloudplatform.github.io/google-cloud-node/#/docs/datastore/1.0.3/datastore
@@ -147,8 +152,9 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
147
152
  */
148
153
  async saveBatch(table, rows, opt = {}) {
149
154
  const entities = rows.map(obj => this.toDatastoreEntity(table, obj, opt.excludeFromIndexes));
155
+ const method = methodMap[opt.saveMethod || 'upsert'] || 'save';
150
156
  const save = (0, js_lib_1.pRetryFn)(async (batch) => {
151
- await (opt.tx || this.ds()).save(batch);
157
+ await (opt.tx || this.ds())[method](batch);
152
158
  }, {
153
159
  // Here we retry the GOAWAY errors that are somewhat common for Datastore
154
160
  // Currently only retrying them here in .saveBatch(), cause probably they're only thrown when saving
@@ -297,7 +303,8 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
297
303
  stats
298
304
  .filter(s => !s.property_name.includes('.') && s.property_name !== 'id') // filter out objectify's "virtual properties"
299
305
  .forEach(stats => {
300
- const { property_name: name, property_type: dtype } = stats;
306
+ const { property_type: dtype } = stats;
307
+ const name = stats.property_name;
301
308
  if (dtype === datastore_model_1.DatastoreType.Blob) {
302
309
  s.properties[name] = {
303
310
  instanceof: 'Buffer',
@@ -88,7 +88,7 @@ export interface DatastoreDBStreamOptions extends DatastoreDBOptions {
88
88
  export interface DatastoreDBOptions extends CommonDBOptions {
89
89
  tx?: Transaction;
90
90
  }
91
- export interface DatastoreDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId> extends CommonDBSaveOptions<ROW> {
91
+ export interface DatastoreDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObjectWithId> extends CommonDBSaveOptions<ROW> {
92
92
  tx?: Transaction;
93
93
  }
94
94
  export interface DatastoreStats {
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { DatastoreDB } from './datastore.db';
2
2
  import { DatastoreCredentials, DatastoreDBCfg, DatastoreDBOptions, DatastoreDBSaveOptions, DatastoreDBStreamOptions, DatastorePayload, DatastorePropertyStats, DatastoreStats, DatastoreType } from './datastore.model';
3
3
  import { DatastoreKeyValueDB, DatastoreKeyValueDBCfg } from './datastoreKeyValueDB';
4
- import { getDBAdapter } from './dbAdapter';
5
4
  export type { DatastorePayload, DatastoreDBCfg, DatastoreKeyValueDBCfg, DatastoreStats, DatastoreCredentials, DatastorePropertyStats, DatastoreDBStreamOptions, DatastoreDBOptions, DatastoreDBSaveOptions, };
6
- export { DatastoreDB, DatastoreType, getDBAdapter, DatastoreKeyValueDB };
5
+ export { DatastoreDB, DatastoreType, DatastoreKeyValueDB };
package/dist/index.js CHANGED
@@ -1,11 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DatastoreKeyValueDB = exports.getDBAdapter = exports.DatastoreType = exports.DatastoreDB = void 0;
3
+ exports.DatastoreKeyValueDB = exports.DatastoreType = exports.DatastoreDB = void 0;
4
4
  const datastore_db_1 = require("./datastore.db");
5
5
  Object.defineProperty(exports, "DatastoreDB", { enumerable: true, get: function () { return datastore_db_1.DatastoreDB; } });
6
6
  const datastore_model_1 = require("./datastore.model");
7
7
  Object.defineProperty(exports, "DatastoreType", { enumerable: true, get: function () { return datastore_model_1.DatastoreType; } });
8
8
  const datastoreKeyValueDB_1 = require("./datastoreKeyValueDB");
9
9
  Object.defineProperty(exports, "DatastoreKeyValueDB", { enumerable: true, get: function () { return datastoreKeyValueDB_1.DatastoreKeyValueDB; } });
10
- const dbAdapter_1 = require("./dbAdapter");
11
- Object.defineProperty(exports, "getDBAdapter", { enumerable: true, get: function () { return dbAdapter_1.getDBAdapter; } });
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.dbQueryToDatastoreQuery = void 0;
4
- // import Operator = google.datastore.v1.CompositeFilter.Operator
5
4
  const FNAME_MAP = {
6
5
  id: '__key__',
7
6
  };
8
7
  const OP_MAP = {
9
8
  '==': '=',
9
+ in: 'IN',
10
+ 'not-in': 'NOT_IN',
10
11
  };
11
12
  function dbQueryToDatastoreQuery(dbQuery, emptyQuery) {
12
13
  let q = emptyQuery;
package/package.json CHANGED
@@ -1,24 +1,21 @@
1
1
  {
2
2
  "name": "@naturalcycles/datastore-lib",
3
- "version": "3.19.2",
3
+ "version": "3.21.0",
4
4
  "description": "Opinionated library to work with Google Datastore",
5
5
  "scripts": {
6
6
  "prepare": "husky install"
7
7
  },
8
- "peerDependencies": {
9
- "@google-cloud/datastore": ">=6.0.0"
10
- },
11
8
  "dependencies": {
9
+ "@google-cloud/datastore": "^7.0.0",
12
10
  "@naturalcycles/db-lib": "^8.0.0",
13
11
  "@naturalcycles/js-lib": "^14.0.0",
14
12
  "@naturalcycles/nodejs-lib": "^12.0.0",
15
13
  "grpc": "^1.24.2"
16
14
  },
17
15
  "devDependencies": {
18
- "@google-cloud/datastore": "^6.0.0",
19
- "@naturalcycles/dev-lib": "^12.0.1",
16
+ "@naturalcycles/dev-lib": "^13.0.0",
20
17
  "@types/node": "^17.0.8",
21
- "jest": "^27.0.4"
18
+ "jest": "^28.1.0"
22
19
  },
23
20
  "files": [
24
21
  "dist",
@@ -3,6 +3,7 @@ import type { Datastore, Key, Query } from '@google-cloud/datastore'
3
3
  import {
4
4
  BaseCommonDB,
5
5
  CommonDB,
6
+ CommonDBSaveMethod,
6
7
  DBQuery,
7
8
  DBTransaction,
8
9
  mergeDBOperations,
@@ -48,6 +49,12 @@ const RETRY_ON = ['GOAWAY', 'UNAVAILABLE', 'UNKNOWN']
48
49
  // Examples of errors:
49
50
  // UNKNOWN: Stream removed
50
51
 
52
+ const methodMap: Record<CommonDBSaveMethod, string> = {
53
+ insert: 'insert',
54
+ update: 'update',
55
+ upsert: 'save',
56
+ }
57
+
51
58
  /**
52
59
  * Datastore API:
53
60
  * https://googlecloudplatform.github.io/google-cloud-node/#/docs/datastore/1.0.3/datastore
@@ -238,7 +245,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
238
245
  /**
239
246
  * Returns saved entities with generated id/updated/created (non-mutating!)
240
247
  */
241
- override async saveBatch<ROW extends ObjectWithId>(
248
+ override async saveBatch<ROW extends Partial<ObjectWithId>>(
242
249
  table: string,
243
250
  rows: ROW[],
244
251
  opt: DatastoreDBSaveOptions<ROW> = {},
@@ -247,9 +254,11 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
247
254
  this.toDatastoreEntity(table, obj, opt.excludeFromIndexes as string[]),
248
255
  )
249
256
 
257
+ const method = methodMap[opt.saveMethod || 'upsert'] || 'save'
258
+
250
259
  const save = pRetryFn(
251
260
  async (batch: DatastorePayload<ROW>[]) => {
252
- await (opt.tx || this.ds()).save(batch)
261
+ await (opt.tx || this.ds())[method](batch)
253
262
  },
254
263
  {
255
264
  // Here we retry the GOAWAY errors that are somewhat common for Datastore
@@ -439,7 +448,8 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
439
448
  stats
440
449
  .filter(s => !s.property_name.includes('.') && s.property_name !== 'id') // filter out objectify's "virtual properties"
441
450
  .forEach(stats => {
442
- const { property_name: name, property_type: dtype } = stats
451
+ const { property_type: dtype } = stats
452
+ const name = stats.property_name as keyof ROW
443
453
 
444
454
  if (dtype === DatastoreType.Blob) {
445
455
  s.properties[name] = {
@@ -479,7 +489,9 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
479
489
  } as JsonSchemaNull
480
490
  }
481
491
  } else {
482
- throw new Error(`Unknown Datastore Type '${stats.property_type}' for ${table}.${name}`)
492
+ throw new Error(
493
+ `Unknown Datastore Type '${stats.property_type}' for ${table}.${name as string}`,
494
+ )
483
495
  }
484
496
  })
485
497
 
@@ -102,7 +102,7 @@ export interface DatastoreDBStreamOptions extends DatastoreDBOptions {
102
102
  export interface DatastoreDBOptions extends CommonDBOptions {
103
103
  tx?: Transaction
104
104
  }
105
- export interface DatastoreDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId>
105
+ export interface DatastoreDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObjectWithId>
106
106
  extends CommonDBSaveOptions<ROW> {
107
107
  tx?: Transaction
108
108
  }
package/src/index.ts CHANGED
@@ -11,7 +11,6 @@ import {
11
11
  DatastoreType,
12
12
  } from './datastore.model'
13
13
  import { DatastoreKeyValueDB, DatastoreKeyValueDBCfg } from './datastoreKeyValueDB'
14
- import { getDBAdapter } from './dbAdapter'
15
14
 
16
15
  export type {
17
16
  DatastorePayload,
@@ -25,4 +24,4 @@ export type {
25
24
  DatastoreDBSaveOptions,
26
25
  }
27
26
 
28
- export { DatastoreDB, DatastoreType, getDBAdapter, DatastoreKeyValueDB }
27
+ export { DatastoreDB, DatastoreType, DatastoreKeyValueDB }
package/src/query.util.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { Query } from '@google-cloud/datastore'
2
2
  import { DBQuery, DBQueryFilterOperator } from '@naturalcycles/db-lib'
3
3
  import { ObjectWithId, StringMap } from '@naturalcycles/js-lib'
4
- // import Operator = google.datastore.v1.CompositeFilter.Operator
5
4
 
6
5
  const FNAME_MAP: StringMap = {
7
6
  id: '__key__',
@@ -9,6 +8,8 @@ const FNAME_MAP: StringMap = {
9
8
 
10
9
  const OP_MAP: Partial<Record<DBQueryFilterOperator, string>> = {
11
10
  '==': '=',
11
+ in: 'IN',
12
+ 'not-in': 'NOT_IN',
12
13
  }
13
14
 
14
15
  export function dbQueryToDatastoreQuery<ROW extends ObjectWithId>(
@@ -1,2 +0,0 @@
1
- import { DatastoreDB } from './datastore.db';
2
- export declare function getDBAdapter(cfgStr?: string): DatastoreDB;
package/dist/dbAdapter.js DELETED
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDBAdapter = void 0;
4
- const datastore_db_1 = require("./datastore.db");
5
- function getDBAdapter(cfgStr) {
6
- const cfg = cfgStr
7
- ? JSON.parse(cfgStr)
8
- : {
9
- useLegacyGRPC: true,
10
- };
11
- return new datastore_db_1.DatastoreDB(cfg);
12
- }
13
- exports.getDBAdapter = getDBAdapter;
package/src/dbAdapter.ts DELETED
@@ -1,11 +0,0 @@
1
- import { DatastoreDB } from './datastore.db'
2
- import { DatastoreDBCfg } from './datastore.model'
3
-
4
- export function getDBAdapter(cfgStr?: string): DatastoreDB {
5
- const cfg: DatastoreDBCfg = cfgStr
6
- ? JSON.parse(cfgStr)
7
- : {
8
- useLegacyGRPC: true,
9
- }
10
- return new DatastoreDB(cfg)
11
- }