@naturalcycles/datastore-lib 3.27.1 → 3.27.3
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.
|
@@ -56,7 +56,7 @@ class DatastoreStreamReadable extends node_stream_1.Readable {
|
|
|
56
56
|
});
|
|
57
57
|
}
|
|
58
58
|
catch (err) {
|
|
59
|
-
console.
|
|
59
|
+
console.log(`DatastoreStreamReadable error!\n`, {
|
|
60
60
|
table: this.table,
|
|
61
61
|
rowsRetrieved: this.rowsRetrieved,
|
|
62
62
|
}, err);
|
package/dist/datastore.db.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DatastoreDB = void 0;
|
|
4
4
|
const node_stream_1 = require("node:stream");
|
|
5
|
+
const datastore_1 = require("@google-cloud/datastore");
|
|
5
6
|
const db_lib_1 = require("@naturalcycles/db-lib");
|
|
6
7
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
7
8
|
const colors_1 = require("@naturalcycles/nodejs-lib/dist/colors");
|
|
@@ -20,6 +21,7 @@ const RETRY_ON = [
|
|
|
20
21
|
].map(s => s.toLowerCase());
|
|
21
22
|
// Examples of errors:
|
|
22
23
|
// UNKNOWN: Stream removed
|
|
24
|
+
const DATASTORE_TIMEOUT = 'DATASTORE_TIMEOUT';
|
|
23
25
|
const methodMap = {
|
|
24
26
|
insert: 'insert',
|
|
25
27
|
update: 'update',
|
|
@@ -90,7 +92,7 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
|
|
|
90
92
|
timeout: this.cfg.timeout,
|
|
91
93
|
errorData: {
|
|
92
94
|
// This error will be grouped ACROSS all endpoints and usages
|
|
93
|
-
fingerprint: [
|
|
95
|
+
fingerprint: [DATASTORE_TIMEOUT],
|
|
94
96
|
},
|
|
95
97
|
});
|
|
96
98
|
rows = r[0];
|
|
@@ -246,7 +248,11 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
|
|
|
246
248
|
* Returns undefined e.g when Table is non-existing
|
|
247
249
|
*/
|
|
248
250
|
async getStats(table) {
|
|
249
|
-
const q = this.ds()
|
|
251
|
+
const q = this.ds()
|
|
252
|
+
.createQuery('__Stat_Kind__')
|
|
253
|
+
// .filter('kind_name', table)
|
|
254
|
+
.filter(new datastore_1.PropertyFilter('kind_name', '=', table))
|
|
255
|
+
.limit(1);
|
|
250
256
|
const [statsArray] = await this.ds().runQuery(q);
|
|
251
257
|
const [stats] = statsArray;
|
|
252
258
|
return stats;
|
|
@@ -258,7 +264,8 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
|
|
|
258
264
|
async getTableProperties(table) {
|
|
259
265
|
const q = this.ds()
|
|
260
266
|
.createQuery('__Stat_PropertyType_PropertyName_Kind__')
|
|
261
|
-
.filter('kind_name', table)
|
|
267
|
+
// .filter('kind_name', table)
|
|
268
|
+
.filter(new datastore_1.PropertyFilter('kind_name', '=', table));
|
|
262
269
|
const [stats] = await this.ds().runQuery(q);
|
|
263
270
|
return stats;
|
|
264
271
|
}
|
|
@@ -379,6 +386,9 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
|
|
|
379
386
|
logFailures: true,
|
|
380
387
|
// logAll: true,
|
|
381
388
|
logger: this.cfg.logger,
|
|
389
|
+
errorData: {
|
|
390
|
+
fingerprint: [DATASTORE_TIMEOUT],
|
|
391
|
+
},
|
|
382
392
|
};
|
|
383
393
|
}
|
|
384
394
|
}
|
package/dist/datastore.model.js
CHANGED
|
@@ -12,4 +12,4 @@ var DatastoreType;
|
|
|
12
12
|
DatastoreType["DATE_TIME"] = "Date/Time";
|
|
13
13
|
DatastoreType["Boolean"] = "Boolean";
|
|
14
14
|
DatastoreType["NULL"] = "NULL";
|
|
15
|
-
})(DatastoreType
|
|
15
|
+
})(DatastoreType || (exports.DatastoreType = DatastoreType = {}));
|
package/dist/query.util.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.dbQueryToDatastoreQuery = void 0;
|
|
4
|
+
const datastore_1 = require("@google-cloud/datastore");
|
|
4
5
|
const FNAME_MAP = {
|
|
5
6
|
id: '__key__',
|
|
6
7
|
};
|
|
@@ -13,7 +14,10 @@ function dbQueryToDatastoreQuery(dbQuery, emptyQuery) {
|
|
|
13
14
|
let q = emptyQuery;
|
|
14
15
|
// filter
|
|
15
16
|
// eslint-disable-next-line unicorn/no-array-reduce
|
|
16
|
-
q = dbQuery._filters.reduce(
|
|
17
|
+
q = dbQuery._filters.reduce(
|
|
18
|
+
// keeping "previous syntax" commented out
|
|
19
|
+
// (q, f) => q.filter(f.name as string, OP_MAP[f.op] || (f.op as any), f.val),
|
|
20
|
+
(q, f) => q.filter(new datastore_1.PropertyFilter(f.name, OP_MAP[f.op] || f.op, f.val)), q);
|
|
17
21
|
// limit
|
|
18
22
|
q = q.limit(dbQuery._limitValue || 0);
|
|
19
23
|
// order
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/datastore-lib",
|
|
3
|
-
"version": "3.27.
|
|
3
|
+
"version": "3.27.3",
|
|
4
4
|
"description": "Opinionated library to work with Google Datastore",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"prepare": "husky install"
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@naturalcycles/dev-lib": "^13.0.0",
|
|
16
|
-
"@types/node": "^
|
|
16
|
+
"@types/node": "^20.4.1",
|
|
17
17
|
"jest": "^29.0.3"
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
@@ -17,7 +17,11 @@ export class DatastoreStreamReadable<T = any> extends Readable implements Readab
|
|
|
17
17
|
|
|
18
18
|
private opt: DatastoreDBStreamOptions & { batchSize: number }
|
|
19
19
|
|
|
20
|
-
constructor(
|
|
20
|
+
constructor(
|
|
21
|
+
private q: Query,
|
|
22
|
+
opt: DatastoreDBStreamOptions,
|
|
23
|
+
private logger: CommonLogger,
|
|
24
|
+
) {
|
|
21
25
|
super({ objectMode: true })
|
|
22
26
|
|
|
23
27
|
this.opt = {
|
|
@@ -74,7 +78,7 @@ export class DatastoreStreamReadable<T = any> extends Readable implements Readab
|
|
|
74
78
|
},
|
|
75
79
|
)
|
|
76
80
|
} catch (err) {
|
|
77
|
-
console.
|
|
81
|
+
console.log(
|
|
78
82
|
`DatastoreStreamReadable error!\n`,
|
|
79
83
|
{
|
|
80
84
|
table: this.table,
|
package/src/datastore.db.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Transform } from 'node:stream'
|
|
2
|
+
import { PropertyFilter } from '@google-cloud/datastore'
|
|
2
3
|
import type { Datastore, Key, Query } from '@google-cloud/datastore'
|
|
3
4
|
import {
|
|
4
5
|
BaseCommonDB,
|
|
@@ -58,6 +59,8 @@ const RETRY_ON = [
|
|
|
58
59
|
// Examples of errors:
|
|
59
60
|
// UNKNOWN: Stream removed
|
|
60
61
|
|
|
62
|
+
const DATASTORE_TIMEOUT = 'DATASTORE_TIMEOUT'
|
|
63
|
+
|
|
61
64
|
const methodMap: Record<CommonDBSaveMethod, string> = {
|
|
62
65
|
insert: 'insert',
|
|
63
66
|
update: 'update',
|
|
@@ -78,7 +81,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
78
81
|
}
|
|
79
82
|
}
|
|
80
83
|
|
|
81
|
-
|
|
84
|
+
cfg: DatastoreDBCfg & { logger: CommonLogger }
|
|
82
85
|
|
|
83
86
|
private cachedDatastore?: Datastore
|
|
84
87
|
|
|
@@ -153,15 +156,18 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
153
156
|
timeout: this.cfg.timeout,
|
|
154
157
|
errorData: {
|
|
155
158
|
// This error will be grouped ACROSS all endpoints and usages
|
|
156
|
-
fingerprint: [
|
|
159
|
+
fingerprint: [DATASTORE_TIMEOUT],
|
|
157
160
|
},
|
|
158
161
|
})
|
|
159
162
|
rows = r[0]
|
|
160
163
|
}
|
|
161
164
|
} else {
|
|
162
|
-
rows = await pRetry(
|
|
163
|
-
|
|
164
|
-
|
|
165
|
+
rows = await pRetry(
|
|
166
|
+
async () => {
|
|
167
|
+
return (await this.ds().get(keys))[0]
|
|
168
|
+
},
|
|
169
|
+
this.getPRetryOptions(`datastore.getByIds(${table})`),
|
|
170
|
+
)
|
|
165
171
|
}
|
|
166
172
|
|
|
167
173
|
return (
|
|
@@ -277,9 +283,12 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
277
283
|
|
|
278
284
|
const method = methodMap[opt.saveMethod || 'upsert'] || 'save'
|
|
279
285
|
|
|
280
|
-
const save = pRetryFn(
|
|
281
|
-
|
|
282
|
-
|
|
286
|
+
const save = pRetryFn(
|
|
287
|
+
async (batch: DatastorePayload<ROW>[]) => {
|
|
288
|
+
await (opt.tx || this.ds())[method](batch)
|
|
289
|
+
},
|
|
290
|
+
this.getPRetryOptions(`DatastoreLib.saveBatch(${table})`),
|
|
291
|
+
)
|
|
283
292
|
|
|
284
293
|
try {
|
|
285
294
|
const chunks = _chunk(entities, MAX_ITEMS)
|
|
@@ -378,7 +387,11 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
378
387
|
* Returns undefined e.g when Table is non-existing
|
|
379
388
|
*/
|
|
380
389
|
async getStats(table: string): Promise<DatastoreStats | undefined> {
|
|
381
|
-
const q = this.ds()
|
|
390
|
+
const q = this.ds()
|
|
391
|
+
.createQuery('__Stat_Kind__')
|
|
392
|
+
// .filter('kind_name', table)
|
|
393
|
+
.filter(new PropertyFilter('kind_name', '=', table))
|
|
394
|
+
.limit(1)
|
|
382
395
|
const [statsArray] = await this.ds().runQuery(q)
|
|
383
396
|
const [stats] = statsArray
|
|
384
397
|
return stats
|
|
@@ -392,7 +405,8 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
392
405
|
async getTableProperties(table: string): Promise<DatastorePropertyStats[]> {
|
|
393
406
|
const q = this.ds()
|
|
394
407
|
.createQuery('__Stat_PropertyType_PropertyName_Kind__')
|
|
395
|
-
.filter('kind_name', table)
|
|
408
|
+
// .filter('kind_name', table)
|
|
409
|
+
.filter(new PropertyFilter('kind_name', '=', table))
|
|
396
410
|
const [stats] = await this.ds().runQuery(q)
|
|
397
411
|
return stats
|
|
398
412
|
}
|
|
@@ -528,6 +542,9 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
528
542
|
logFailures: true,
|
|
529
543
|
// logAll: true,
|
|
530
544
|
logger: this.cfg.logger,
|
|
545
|
+
errorData: {
|
|
546
|
+
fingerprint: [DATASTORE_TIMEOUT],
|
|
547
|
+
},
|
|
531
548
|
}
|
|
532
549
|
}
|
|
533
550
|
}
|
package/src/query.util.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Query } from '@google-cloud/datastore'
|
|
1
|
+
import { PropertyFilter, Query } from '@google-cloud/datastore'
|
|
2
2
|
import { DBQuery, DBQueryFilterOperator } from '@naturalcycles/db-lib'
|
|
3
3
|
import { ObjectWithId, StringMap } from '@naturalcycles/js-lib'
|
|
4
4
|
|
|
@@ -21,7 +21,9 @@ export function dbQueryToDatastoreQuery<ROW extends ObjectWithId>(
|
|
|
21
21
|
// filter
|
|
22
22
|
// eslint-disable-next-line unicorn/no-array-reduce
|
|
23
23
|
q = dbQuery._filters.reduce(
|
|
24
|
-
|
|
24
|
+
// keeping "previous syntax" commented out
|
|
25
|
+
// (q, f) => q.filter(f.name as string, OP_MAP[f.op] || (f.op as any), f.val),
|
|
26
|
+
(q, f) => q.filter(new PropertyFilter(f.name as string, OP_MAP[f.op] || (f.op as any), f.val)),
|
|
25
27
|
q,
|
|
26
28
|
)
|
|
27
29
|
|