@naturalcycles/datastore-lib 3.32.2 → 3.33.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.
- package/dist/DatastoreStreamReadable.js +1 -1
- package/dist/datastore.db.d.ts +12 -12
- package/dist/datastore.db.js +6 -11
- package/dist/datastore.model.d.ts +1 -1
- package/dist/datastoreKeyValueDB.d.ts +1 -1
- package/dist/datastoreKeyValueDB.js +12 -23
- package/dist/query.util.d.ts +2 -2
- package/package.json +2 -2
- package/src/datastore.db.ts +22 -33
- package/src/datastore.model.ts +1 -1
- package/src/datastoreKeyValueDB.ts +22 -35
- package/src/query.util.ts +2 -2
package/dist/datastore.db.d.ts
CHANGED
|
@@ -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 { JsonSchemaObject, JsonSchemaRootObject, CommonLogger,
|
|
4
|
+
import { JsonSchemaObject, JsonSchemaRootObject, CommonLogger, 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
|
|
26
|
-
runQuery<ROW extends
|
|
27
|
-
runQueryCount<ROW extends
|
|
28
|
-
runDatastoreQuery<ROW extends
|
|
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>>;
|
|
29
29
|
private runQueryStream;
|
|
30
|
-
streamQuery<ROW extends
|
|
30
|
+
streamQuery<ROW extends ObjectWithId>(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
|
|
35
|
-
deleteByQuery<ROW extends
|
|
34
|
+
saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: DatastoreDBSaveOptions<ROW>): Promise<void>;
|
|
35
|
+
deleteByQuery<ROW extends ObjectWithId>(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
|
|
56
|
+
createTable<ROW extends ObjectWithId>(_table: string, _schema: JsonSchemaObject<ROW>): Promise<void>;
|
|
57
57
|
getTables(): Promise<string[]>;
|
|
58
|
-
getTableSchema<ROW extends
|
|
58
|
+
getTableSchema<ROW extends ObjectWithId>(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
|
|
70
|
-
saveBatch<ROW extends
|
|
69
|
+
getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions | undefined): Promise<ROW[]>;
|
|
70
|
+
saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW> | undefined): Promise<void>;
|
|
71
71
|
deleteByIds(table: string, ids: string[], opt?: CommonDBOptions | undefined): Promise<number>;
|
|
72
72
|
}
|
package/dist/datastore.db.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DatastoreDBTransaction = exports.DatastoreDB = void 0;
|
|
4
|
-
const node_stream_1 = require("node:stream");
|
|
5
4
|
const datastore_1 = require("@google-cloud/datastore");
|
|
6
5
|
const db_lib_1 = require("@naturalcycles/db-lib");
|
|
7
6
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
@@ -135,8 +134,9 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
|
|
|
135
134
|
}
|
|
136
135
|
async runQueryCount(dbQuery, _opt) {
|
|
137
136
|
const q = (0, query_util_1.dbQueryToDatastoreQuery)(dbQuery.select([]), this.ds().createQuery(dbQuery.table));
|
|
138
|
-
const
|
|
139
|
-
|
|
137
|
+
const aq = this.ds().createAggregationQuery(q).count('count');
|
|
138
|
+
const [entities] = await this.ds().runAggregationQuery(aq);
|
|
139
|
+
return entities[0]?.count;
|
|
140
140
|
}
|
|
141
141
|
async runDatastoreQuery(q) {
|
|
142
142
|
const [entities, queryResult] = await this.ds().runQuery(q);
|
|
@@ -154,13 +154,8 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
|
|
|
154
154
|
const stream = (opt.experimentalCursorStream
|
|
155
155
|
? new DatastoreStreamReadable_1.DatastoreStreamReadable(q, opt, (0, js_lib_1.commonLoggerMinLevel)(this.cfg.logger, opt.debug ? 'log' : 'warn'))
|
|
156
156
|
: this.ds().runQueryStream(q))
|
|
157
|
-
.on('error', err => stream.emit('error', err))
|
|
158
|
-
.
|
|
159
|
-
objectMode: true,
|
|
160
|
-
transform: (chunk, _enc, cb) => {
|
|
161
|
-
cb(null, this.mapId(chunk));
|
|
162
|
-
},
|
|
163
|
-
}));
|
|
157
|
+
// .on('error', err => stream.emit('error', err))
|
|
158
|
+
.map(chunk => this.mapId(chunk));
|
|
164
159
|
return stream;
|
|
165
160
|
}
|
|
166
161
|
streamQuery(dbQuery, opt) {
|
|
@@ -369,7 +364,7 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
|
|
|
369
364
|
return {
|
|
370
365
|
predicate: err => RETRY_ON.some(s => err?.message?.toLowerCase()?.includes(s)),
|
|
371
366
|
name,
|
|
372
|
-
timeout:
|
|
367
|
+
timeout: 20_000,
|
|
373
368
|
maxAttempts: 5,
|
|
374
369
|
delay: 5000,
|
|
375
370
|
delayMultiplier: 1.5,
|
|
@@ -101,7 +101,7 @@ export interface DatastoreDBStreamOptions extends DatastoreDBOptions {
|
|
|
101
101
|
}
|
|
102
102
|
export interface DatastoreDBOptions extends CommonDBOptions {
|
|
103
103
|
}
|
|
104
|
-
export interface DatastoreDBSaveOptions<ROW extends
|
|
104
|
+
export interface DatastoreDBSaveOptions<ROW extends ObjectWithId> extends CommonDBSaveOptions<ROW> {
|
|
105
105
|
}
|
|
106
106
|
export interface DatastoreStats {
|
|
107
107
|
composite_index_count: number;
|
|
@@ -17,5 +17,5 @@ export declare class DatastoreKeyValueDB implements CommonKeyValueDB {
|
|
|
17
17
|
streamIds(table: string, limit?: number): ReadableTyped<string>;
|
|
18
18
|
streamValues(table: string, limit?: number): ReadableTyped<Buffer>;
|
|
19
19
|
streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple>;
|
|
20
|
-
count(
|
|
20
|
+
count(table: string): Promise<number>;
|
|
21
21
|
}
|
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DatastoreKeyValueDB = void 0;
|
|
4
4
|
const db_lib_1 = require("@naturalcycles/db-lib");
|
|
5
|
-
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
|
-
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
7
5
|
const datastore_db_1 = require("./datastore.db");
|
|
8
6
|
const excludeFromIndexes = ['v'];
|
|
9
7
|
class DatastoreKeyValueDB {
|
|
@@ -30,38 +28,29 @@ class DatastoreKeyValueDB {
|
|
|
30
28
|
const q = db_lib_1.DBQuery.create(table)
|
|
31
29
|
.select(['id'])
|
|
32
30
|
.limit(limit || 0);
|
|
33
|
-
|
|
31
|
+
return (this.db
|
|
34
32
|
.streamQuery(q)
|
|
35
|
-
.on('error', err => stream.emit('error', err))
|
|
36
|
-
.
|
|
37
|
-
errorMode: js_lib_1.ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
|
|
38
|
-
}));
|
|
39
|
-
return stream;
|
|
33
|
+
// .on('error', err => stream.emit('error', err))
|
|
34
|
+
.map(r => r.id));
|
|
40
35
|
}
|
|
41
36
|
streamValues(table, limit) {
|
|
42
37
|
// `select v` doesn't work for some reason
|
|
43
38
|
const q = db_lib_1.DBQuery.create(table).limit(limit || 0);
|
|
44
|
-
|
|
39
|
+
return (this.db
|
|
45
40
|
.streamQuery(q)
|
|
46
|
-
.on('error', err => stream.emit('error', err))
|
|
47
|
-
.
|
|
48
|
-
errorMode: js_lib_1.ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
|
|
49
|
-
}));
|
|
50
|
-
return stream;
|
|
41
|
+
// .on('error', err => stream.emit('error', err))
|
|
42
|
+
.map(r => r.v));
|
|
51
43
|
}
|
|
52
44
|
streamEntries(table, limit) {
|
|
53
45
|
const q = db_lib_1.DBQuery.create(table).limit(limit || 0);
|
|
54
|
-
|
|
46
|
+
return (this.db
|
|
55
47
|
.streamQuery(q)
|
|
56
|
-
.on('error', err => stream.emit('error', err))
|
|
57
|
-
.
|
|
58
|
-
errorMode: js_lib_1.ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
|
|
59
|
-
}));
|
|
60
|
-
return stream;
|
|
48
|
+
// .on('error', err => stream.emit('error', err))
|
|
49
|
+
.map(r => [r.id, r.v]));
|
|
61
50
|
}
|
|
62
|
-
async count(
|
|
63
|
-
|
|
64
|
-
return
|
|
51
|
+
async count(table) {
|
|
52
|
+
const q = db_lib_1.DBQuery.create(table);
|
|
53
|
+
return await this.db.runQueryCount(q);
|
|
65
54
|
}
|
|
66
55
|
}
|
|
67
56
|
exports.DatastoreKeyValueDB = DatastoreKeyValueDB;
|
package/dist/query.util.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Query } from '@google-cloud/datastore';
|
|
2
2
|
import { DBQuery } from '@naturalcycles/db-lib';
|
|
3
|
-
import {
|
|
4
|
-
export declare function dbQueryToDatastoreQuery<ROW extends
|
|
3
|
+
import { ObjectWithId } from '@naturalcycles/js-lib';
|
|
4
|
+
export declare function dbQueryToDatastoreQuery<ROW extends ObjectWithId>(dbQuery: Readonly<DBQuery<ROW>>, emptyQuery: Query): Query;
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/datastore-lib",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.33.0",
|
|
4
4
|
"description": "Opinionated library to work with Google Datastore",
|
|
5
5
|
"scripts": {
|
|
6
|
-
"prepare": "husky
|
|
6
|
+
"prepare": "husky"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"@google-cloud/datastore": "^8.0.0",
|
package/src/datastore.db.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Transform } from 'node:stream'
|
|
2
1
|
import { PropertyFilter, Transaction } from '@google-cloud/datastore'
|
|
3
2
|
import type { Datastore, Key, Query } from '@google-cloud/datastore'
|
|
4
3
|
import {
|
|
@@ -33,8 +32,6 @@ import {
|
|
|
33
32
|
pRetryFn,
|
|
34
33
|
pRetry,
|
|
35
34
|
PRetryOptions,
|
|
36
|
-
PartialObjectWithId,
|
|
37
|
-
Saved,
|
|
38
35
|
ObjectWithId,
|
|
39
36
|
} from '@naturalcycles/js-lib'
|
|
40
37
|
import { ReadableTyped, boldWhite } from '@naturalcycles/nodejs-lib'
|
|
@@ -135,11 +132,11 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
135
132
|
await this.getAllStats()
|
|
136
133
|
}
|
|
137
134
|
|
|
138
|
-
override async getByIds<ROW extends
|
|
135
|
+
override async getByIds<ROW extends ObjectWithId>(
|
|
139
136
|
table: string,
|
|
140
137
|
ids: string[],
|
|
141
138
|
opt: DatastoreDBOptions = {},
|
|
142
|
-
): Promise<
|
|
139
|
+
): Promise<ROW[]> {
|
|
143
140
|
if (!ids.length) return []
|
|
144
141
|
const keys = ids.map(id => this.key(table, id))
|
|
145
142
|
let rows: any[]
|
|
@@ -189,7 +186,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
189
186
|
|
|
190
187
|
return (
|
|
191
188
|
rows
|
|
192
|
-
.map(r => this.mapId<
|
|
189
|
+
.map(r => this.mapId<ROW>(r))
|
|
193
190
|
// Seems like datastore .get() method doesn't return items properly sorted by input ids, so we gonna sort them here
|
|
194
191
|
// same ids are not expected here
|
|
195
192
|
.sort((a, b) => (a.id > b.id ? 1 : -1))
|
|
@@ -201,10 +198,10 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
201
198
|
// return q.kinds[0]!
|
|
202
199
|
// }
|
|
203
200
|
|
|
204
|
-
override async runQuery<ROW extends
|
|
201
|
+
override async runQuery<ROW extends ObjectWithId>(
|
|
205
202
|
dbQuery: DBQuery<ROW>,
|
|
206
203
|
_opt?: DatastoreDBOptions,
|
|
207
|
-
): Promise<RunQueryResult<
|
|
204
|
+
): Promise<RunQueryResult<ROW>> {
|
|
208
205
|
const idFilter = dbQuery._filters.find(f => f.name === 'id')
|
|
209
206
|
if (idFilter) {
|
|
210
207
|
const ids: string[] = idFilter.op === '==' ? [idFilter.val] : idFilter.val
|
|
@@ -225,21 +222,20 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
225
222
|
return qr
|
|
226
223
|
}
|
|
227
224
|
|
|
228
|
-
override async runQueryCount<ROW extends
|
|
225
|
+
override async runQueryCount<ROW extends ObjectWithId>(
|
|
229
226
|
dbQuery: DBQuery<ROW>,
|
|
230
227
|
_opt?: DatastoreDBOptions,
|
|
231
228
|
): Promise<number> {
|
|
232
229
|
const q = dbQueryToDatastoreQuery(dbQuery.select([]), this.ds().createQuery(dbQuery.table))
|
|
233
|
-
const
|
|
234
|
-
|
|
230
|
+
const aq = this.ds().createAggregationQuery(q).count('count')
|
|
231
|
+
const [entities] = await this.ds().runAggregationQuery(aq)
|
|
232
|
+
return entities[0]?.count
|
|
235
233
|
}
|
|
236
234
|
|
|
237
|
-
async runDatastoreQuery<ROW extends
|
|
238
|
-
q: Query,
|
|
239
|
-
): Promise<RunQueryResult<Saved<ROW>>> {
|
|
235
|
+
async runDatastoreQuery<ROW extends ObjectWithId>(q: Query): Promise<RunQueryResult<ROW>> {
|
|
240
236
|
const [entities, queryResult] = await this.ds().runQuery(q)
|
|
241
237
|
|
|
242
|
-
const rows = entities.map(e => this.mapId<
|
|
238
|
+
const rows = entities.map(e => this.mapId<ROW>(e))
|
|
243
239
|
|
|
244
240
|
return {
|
|
245
241
|
...queryResult,
|
|
@@ -247,7 +243,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
247
243
|
}
|
|
248
244
|
}
|
|
249
245
|
|
|
250
|
-
private runQueryStream<ROW extends
|
|
246
|
+
private runQueryStream<ROW extends ObjectWithId>(
|
|
251
247
|
q: Query,
|
|
252
248
|
_opt?: DatastoreDBStreamOptions,
|
|
253
249
|
): ReadableTyped<ROW> {
|
|
@@ -258,27 +254,20 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
258
254
|
|
|
259
255
|
const stream: ReadableTyped<ROW> = (
|
|
260
256
|
opt.experimentalCursorStream
|
|
261
|
-
? new DatastoreStreamReadable(
|
|
257
|
+
? new DatastoreStreamReadable<ROW>(
|
|
262
258
|
q,
|
|
263
259
|
opt,
|
|
264
260
|
commonLoggerMinLevel(this.cfg.logger, opt.debug ? 'log' : 'warn'),
|
|
265
261
|
)
|
|
266
262
|
: this.ds().runQueryStream(q)
|
|
267
263
|
)
|
|
268
|
-
.on('error', err => stream.emit('error', err))
|
|
269
|
-
.
|
|
270
|
-
new Transform({
|
|
271
|
-
objectMode: true,
|
|
272
|
-
transform: (chunk, _enc, cb) => {
|
|
273
|
-
cb(null, this.mapId(chunk))
|
|
274
|
-
},
|
|
275
|
-
}),
|
|
276
|
-
)
|
|
264
|
+
// .on('error', err => stream.emit('error', err))
|
|
265
|
+
.map(chunk => this.mapId(chunk))
|
|
277
266
|
|
|
278
267
|
return stream
|
|
279
268
|
}
|
|
280
269
|
|
|
281
|
-
override streamQuery<ROW extends
|
|
270
|
+
override streamQuery<ROW extends ObjectWithId>(
|
|
282
271
|
dbQuery: DBQuery<ROW>,
|
|
283
272
|
opt?: DatastoreDBStreamOptions,
|
|
284
273
|
): ReadableTyped<ROW> {
|
|
@@ -291,7 +280,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
291
280
|
/**
|
|
292
281
|
* Returns saved entities with generated id/updated/created (non-mutating!)
|
|
293
282
|
*/
|
|
294
|
-
override async saveBatch<ROW extends
|
|
283
|
+
override async saveBatch<ROW extends ObjectWithId>(
|
|
295
284
|
table: string,
|
|
296
285
|
rows: ROW[],
|
|
297
286
|
opt: DatastoreDBSaveOptions<ROW> = {},
|
|
@@ -328,7 +317,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
328
317
|
}
|
|
329
318
|
}
|
|
330
319
|
|
|
331
|
-
override async deleteByQuery<ROW extends
|
|
320
|
+
override async deleteByQuery<ROW extends ObjectWithId>(
|
|
332
321
|
q: DBQuery<ROW>,
|
|
333
322
|
opt?: DatastoreDBOptions,
|
|
334
323
|
): Promise<number> {
|
|
@@ -462,7 +451,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
462
451
|
return id?.toString()
|
|
463
452
|
}
|
|
464
453
|
|
|
465
|
-
override async createTable<ROW extends
|
|
454
|
+
override async createTable<ROW extends ObjectWithId>(
|
|
466
455
|
_table: string,
|
|
467
456
|
_schema: JsonSchemaObject<ROW>,
|
|
468
457
|
): Promise<void> {}
|
|
@@ -473,7 +462,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
473
462
|
return statsArray.map(stats => stats.kind_name).filter(table => table && !table.startsWith('_'))
|
|
474
463
|
}
|
|
475
464
|
|
|
476
|
-
override async getTableSchema<ROW extends
|
|
465
|
+
override async getTableSchema<ROW extends ObjectWithId>(
|
|
477
466
|
table: string,
|
|
478
467
|
): Promise<JsonSchemaRootObject<ROW>> {
|
|
479
468
|
const stats = await this.getTableProperties(table)
|
|
@@ -576,7 +565,7 @@ export class DatastoreDBTransaction implements DBTransaction {
|
|
|
576
565
|
}
|
|
577
566
|
}
|
|
578
567
|
|
|
579
|
-
async getByIds<ROW extends
|
|
568
|
+
async getByIds<ROW extends ObjectWithId>(
|
|
580
569
|
table: string,
|
|
581
570
|
ids: string[],
|
|
582
571
|
opt?: CommonDBOptions | undefined,
|
|
@@ -584,7 +573,7 @@ export class DatastoreDBTransaction implements DBTransaction {
|
|
|
584
573
|
return await this.db.getByIds(table, ids, { ...opt, tx: this })
|
|
585
574
|
}
|
|
586
575
|
|
|
587
|
-
async saveBatch<ROW extends
|
|
576
|
+
async saveBatch<ROW extends ObjectWithId>(
|
|
588
577
|
table: string,
|
|
589
578
|
rows: ROW[],
|
|
590
579
|
opt?: CommonDBSaveOptions<ROW> | undefined,
|
package/src/datastore.model.ts
CHANGED
|
@@ -116,7 +116,7 @@ export interface DatastoreDBStreamOptions extends DatastoreDBOptions {
|
|
|
116
116
|
|
|
117
117
|
export interface DatastoreDBOptions extends CommonDBOptions {}
|
|
118
118
|
|
|
119
|
-
export interface DatastoreDBSaveOptions<ROW extends
|
|
119
|
+
export interface DatastoreDBSaveOptions<ROW extends ObjectWithId>
|
|
120
120
|
extends CommonDBSaveOptions<ROW> {}
|
|
121
121
|
|
|
122
122
|
export interface DatastoreStats {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CommonKeyValueDB, DBQuery, KeyValueDBTuple } from '@naturalcycles/db-lib'
|
|
2
|
-
import {
|
|
3
|
-
import { ReadableTyped, transformMapSimple } from '@naturalcycles/nodejs-lib'
|
|
2
|
+
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
4
3
|
import { DatastoreDB } from './datastore.db'
|
|
5
4
|
import { DatastoreDBCfg } from './datastore.model'
|
|
6
5
|
|
|
@@ -49,51 +48,39 @@ export class DatastoreKeyValueDB implements CommonKeyValueDB {
|
|
|
49
48
|
.select(['id'])
|
|
50
49
|
.limit(limit || 0)
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}),
|
|
59
|
-
)
|
|
60
|
-
|
|
61
|
-
return stream
|
|
51
|
+
return (
|
|
52
|
+
this.db
|
|
53
|
+
.streamQuery(q)
|
|
54
|
+
// .on('error', err => stream.emit('error', err))
|
|
55
|
+
.map(r => r.id)
|
|
56
|
+
)
|
|
62
57
|
}
|
|
63
58
|
|
|
64
59
|
streamValues(table: string, limit?: number): ReadableTyped<Buffer> {
|
|
65
60
|
// `select v` doesn't work for some reason
|
|
66
61
|
const q = DBQuery.create<KVObject>(table).limit(limit || 0)
|
|
67
62
|
|
|
68
|
-
|
|
69
|
-
.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}),
|
|
75
|
-
)
|
|
76
|
-
|
|
77
|
-
return stream
|
|
63
|
+
return (
|
|
64
|
+
this.db
|
|
65
|
+
.streamQuery(q)
|
|
66
|
+
// .on('error', err => stream.emit('error', err))
|
|
67
|
+
.map(r => r.v)
|
|
68
|
+
)
|
|
78
69
|
}
|
|
79
70
|
|
|
80
71
|
streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple> {
|
|
81
72
|
const q = DBQuery.create<KVObject>(table).limit(limit || 0)
|
|
82
73
|
|
|
83
|
-
|
|
84
|
-
.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}),
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
return stream
|
|
74
|
+
return (
|
|
75
|
+
this.db
|
|
76
|
+
.streamQuery(q)
|
|
77
|
+
// .on('error', err => stream.emit('error', err))
|
|
78
|
+
.map(r => [r.id, r.v] as KeyValueDBTuple)
|
|
79
|
+
)
|
|
93
80
|
}
|
|
94
81
|
|
|
95
|
-
async count(
|
|
96
|
-
|
|
97
|
-
return
|
|
82
|
+
async count(table: string): Promise<number> {
|
|
83
|
+
const q = DBQuery.create<KVObject>(table)
|
|
84
|
+
return await this.db.runQueryCount(q)
|
|
98
85
|
}
|
|
99
86
|
}
|
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 {
|
|
3
|
+
import { ObjectWithId, 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
|
|
15
|
+
export function dbQueryToDatastoreQuery<ROW extends ObjectWithId>(
|
|
16
16
|
dbQuery: Readonly<DBQuery<ROW>>,
|
|
17
17
|
emptyQuery: Query,
|
|
18
18
|
): Query {
|