@naturalcycles/datastore-lib 3.32.3 → 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.
@@ -80,7 +80,7 @@ class DatastoreStreamReadable extends node_stream_1.Readable {
80
80
  delay: 5000,
81
81
  delayMultiplier: 2,
82
82
  logger: this.logger,
83
- timeout: 120000, // 2 minutes
83
+ timeout: 120_000, // 2 minutes
84
84
  });
85
85
  }
86
86
  catch (err) {
@@ -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 [entities] = await this.ds().runQuery(q);
139
- return entities.length;
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
- .pipe(new node_stream_1.Transform({
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: 20000,
367
+ timeout: 20_000,
373
368
  maxAttempts: 5,
374
369
  delay: 5000,
375
370
  delayMultiplier: 1.5,
@@ -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(_table: string): Promise<number>;
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
- const stream = this.db
31
+ return (this.db
34
32
  .streamQuery(q)
35
- .on('error', err => stream.emit('error', err))
36
- .pipe((0, nodejs_lib_1.transformMapSimple)(objectWithId => objectWithId.id, {
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
- const stream = this.db
39
+ return (this.db
45
40
  .streamQuery(q)
46
- .on('error', err => stream.emit('error', err))
47
- .pipe((0, nodejs_lib_1.transformMapSimple)(obj => obj.v, {
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
- const stream = this.db
46
+ return (this.db
55
47
  .streamQuery(q)
56
- .on('error', err => stream.emit('error', err))
57
- .pipe((0, nodejs_lib_1.transformMapSimple)(obj => [obj.id, obj.v], {
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(_table) {
63
- this.db.cfg.logger.warn(`DatastoreKeyValueDB.count is not supported`);
64
- return 0;
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/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@naturalcycles/datastore-lib",
3
- "version": "3.32.3",
3
+ "version": "3.33.0",
4
4
  "description": "Opinionated library to work with Google Datastore",
5
5
  "scripts": {
6
- "prepare": "husky install"
6
+ "prepare": "husky"
7
7
  },
8
8
  "dependencies": {
9
9
  "@google-cloud/datastore": "^8.0.0",
@@ -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 {
@@ -228,8 +227,9 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
228
227
  _opt?: DatastoreDBOptions,
229
228
  ): Promise<number> {
230
229
  const q = dbQueryToDatastoreQuery(dbQuery.select([]), this.ds().createQuery(dbQuery.table))
231
- const [entities] = await this.ds().runQuery(q)
232
- return entities.length
230
+ const aq = this.ds().createAggregationQuery(q).count('count')
231
+ const [entities] = await this.ds().runAggregationQuery(aq)
232
+ return entities[0]?.count
233
233
  }
234
234
 
235
235
  async runDatastoreQuery<ROW extends ObjectWithId>(q: Query): Promise<RunQueryResult<ROW>> {
@@ -254,22 +254,15 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
254
254
 
255
255
  const stream: ReadableTyped<ROW> = (
256
256
  opt.experimentalCursorStream
257
- ? new DatastoreStreamReadable(
257
+ ? new DatastoreStreamReadable<ROW>(
258
258
  q,
259
259
  opt,
260
260
  commonLoggerMinLevel(this.cfg.logger, opt.debug ? 'log' : 'warn'),
261
261
  )
262
262
  : this.ds().runQueryStream(q)
263
263
  )
264
- .on('error', err => stream.emit('error', err))
265
- .pipe(
266
- new Transform({
267
- objectMode: true,
268
- transform: (chunk, _enc, cb) => {
269
- cb(null, this.mapId(chunk))
270
- },
271
- }),
272
- )
264
+ // .on('error', err => stream.emit('error', err))
265
+ .map(chunk => this.mapId(chunk))
273
266
 
274
267
  return stream
275
268
  }
@@ -1,6 +1,5 @@
1
1
  import { CommonKeyValueDB, DBQuery, KeyValueDBTuple } from '@naturalcycles/db-lib'
2
- import { ErrorMode, ObjectWithId } from '@naturalcycles/js-lib'
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
- const stream: ReadableTyped<string> = this.db
53
- .streamQuery<KVObject>(q)
54
- .on('error', err => stream.emit('error', err))
55
- .pipe(
56
- transformMapSimple<ObjectWithId, string>(objectWithId => objectWithId.id, {
57
- errorMode: ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
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
- const stream: ReadableTyped<string> = this.db
69
- .streamQuery<KVObject>(q)
70
- .on('error', err => stream.emit('error', err))
71
- .pipe(
72
- transformMapSimple<{ v: Buffer }, Buffer>(obj => obj.v, {
73
- errorMode: ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
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
- const stream: ReadableTyped<string> = this.db
84
- .streamQuery<KVObject>(q)
85
- .on('error', err => stream.emit('error', err))
86
- .pipe(
87
- transformMapSimple<KVObject, KeyValueDBTuple>(obj => [obj.id, obj.v], {
88
- errorMode: ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
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(_table: string): Promise<number> {
96
- this.db.cfg.logger.warn(`DatastoreKeyValueDB.count is not supported`)
97
- return 0
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
  }