@naturalcycles/datastore-lib 3.19.1 → 3.20.1
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.
- package/dist/datastore.db.d.ts +5 -5
- package/dist/datastore.db.js +7 -1
- package/dist/datastore.model.d.ts +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -3
- package/package.json +3 -6
- package/src/datastore.db.ts +16 -7
- package/src/datastore.model.ts +1 -1
- package/src/datastoreKeyValueDB.ts +1 -1
- package/src/index.ts +1 -2
- package/dist/dbAdapter.d.ts +0 -2
- package/dist/dbAdapter.js +0 -13
- package/src/dbAdapter.ts +0 -11
package/dist/datastore.db.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ export declare class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
20
20
|
protected KEY: symbol;
|
|
21
21
|
ds(): Datastore;
|
|
22
22
|
ping(): Promise<void>;
|
|
23
|
-
getByIds<ROW extends ObjectWithId>(table: string, ids:
|
|
23
|
+
getByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], _opt?: DatastoreDBOptions): Promise<ROW[]>;
|
|
24
24
|
getQueryKind(q: Query): string;
|
|
25
25
|
runQuery<ROW extends ObjectWithId>(dbQuery: DBQuery<ROW>, _opt?: DatastoreDBOptions): Promise<RunQueryResult<ROW>>;
|
|
26
26
|
runQueryCount<ROW extends ObjectWithId>(dbQuery: DBQuery<ROW>, _opt?: DatastoreDBOptions): Promise<number>;
|
|
@@ -30,13 +30,13 @@ 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
|
|
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"
|
|
37
37
|
* regardless if they were actually deleted or not.
|
|
38
38
|
*/
|
|
39
|
-
deleteByIds(table: string, ids:
|
|
39
|
+
deleteByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], opt?: DatastoreDBOptions): Promise<number>;
|
|
40
40
|
/**
|
|
41
41
|
* https://cloud.google.com/datastore/docs/concepts/transactions#datastore-datastore-transactional-update-nodejs
|
|
42
42
|
*/
|
|
@@ -50,9 +50,9 @@ export declare class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
50
50
|
getTableProperties(table: string): Promise<DatastorePropertyStats[]>;
|
|
51
51
|
mapId<T = any>(o: any, preserveKey?: boolean): T;
|
|
52
52
|
toDatastoreEntity<T = any>(kind: string, o: T & {
|
|
53
|
-
id?: string;
|
|
53
|
+
id?: string | number;
|
|
54
54
|
}, excludeFromIndexes?: string[]): DatastorePayload<T>;
|
|
55
|
-
key(kind: string, id: string): Key;
|
|
55
|
+
key(kind: string, id: string | number): Key;
|
|
56
56
|
getDsKey(o: any): Key | undefined;
|
|
57
57
|
getKey(key: Key): string | undefined;
|
|
58
58
|
getTables(): Promise<string[]>;
|
package/dist/datastore.db.js
CHANGED
|
@@ -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())
|
|
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,
|
|
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.
|
|
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,24 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/datastore-lib",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.20.1",
|
|
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": "^6.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
16
|
"@naturalcycles/dev-lib": "^12.0.1",
|
|
20
17
|
"@types/node": "^17.0.8",
|
|
21
|
-
"jest": "^
|
|
18
|
+
"jest": "^28.1.0"
|
|
22
19
|
},
|
|
23
20
|
"files": [
|
|
24
21
|
"dist",
|
package/src/datastore.db.ts
CHANGED
|
@@ -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
|
|
@@ -111,7 +118,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
111
118
|
|
|
112
119
|
override async getByIds<ROW extends ObjectWithId>(
|
|
113
120
|
table: string,
|
|
114
|
-
ids:
|
|
121
|
+
ids: ROW['id'][],
|
|
115
122
|
_opt?: DatastoreDBOptions,
|
|
116
123
|
): Promise<ROW[]> {
|
|
117
124
|
if (!ids.length) return []
|
|
@@ -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())
|
|
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
|
|
@@ -302,9 +311,9 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
302
311
|
* Limitation: Datastore's delete returns void, so we always return all ids here as "deleted"
|
|
303
312
|
* regardless if they were actually deleted or not.
|
|
304
313
|
*/
|
|
305
|
-
override async deleteByIds(
|
|
314
|
+
override async deleteByIds<ROW extends ObjectWithId>(
|
|
306
315
|
table: string,
|
|
307
|
-
ids:
|
|
316
|
+
ids: ROW['id'][],
|
|
308
317
|
opt: DatastoreDBOptions = {},
|
|
309
318
|
): Promise<number> {
|
|
310
319
|
const keys = ids.map(id => this.key(table, id))
|
|
@@ -386,7 +395,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
386
395
|
// if key field exists on entity, it will be used as key (prevent to duplication of numeric keyed entities)
|
|
387
396
|
toDatastoreEntity<T = any>(
|
|
388
397
|
kind: string,
|
|
389
|
-
o: T & { id?: string },
|
|
398
|
+
o: T & { id?: string | number },
|
|
390
399
|
excludeFromIndexes: string[] = [],
|
|
391
400
|
): DatastorePayload<T> {
|
|
392
401
|
const key = this.getDsKey(o) || this.key(kind, o.id!)
|
|
@@ -401,7 +410,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
401
410
|
}
|
|
402
411
|
}
|
|
403
412
|
|
|
404
|
-
key(kind: string, id: string): Key {
|
|
413
|
+
key(kind: string, id: string | number): Key {
|
|
405
414
|
_assert(id, `Cannot save "${kind}" entity without "id"`)
|
|
406
415
|
return this.ds().key([kind, String(id)])
|
|
407
416
|
}
|
package/src/datastore.model.ts
CHANGED
|
@@ -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
|
}
|
|
@@ -50,7 +50,7 @@ export class DatastoreKeyValueDB implements CommonKeyValueDB {
|
|
|
50
50
|
.limit(limit || 0)
|
|
51
51
|
|
|
52
52
|
return this.db.streamQuery<KVObject>(q).pipe(
|
|
53
|
-
transformMapSimple<ObjectWithId
|
|
53
|
+
transformMapSimple<ObjectWithId<string>, string>(objectWithId => objectWithId.id, {
|
|
54
54
|
errorMode: ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
|
|
55
55
|
}),
|
|
56
56
|
)
|
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,
|
|
27
|
+
export { DatastoreDB, DatastoreType, DatastoreKeyValueDB }
|
package/dist/dbAdapter.d.ts
DELETED
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
|
-
}
|