@naturalcycles/datastore-lib 3.19.2 → 3.20.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
@@ -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; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/datastore-lib",
3
- "version": "3.19.2",
3
+ "version": "3.20.0",
4
4
  "description": "Opinionated library to work with Google Datastore",
5
5
  "scripts": {
6
6
  "prepare": "husky install"
@@ -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
@@ -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 }
@@ -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
- }