@naturalcycles/datastore-lib 3.25.1 → 3.25.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.
@@ -1,6 +1,6 @@
1
1
  import type { Datastore, Key, Query } from '@google-cloud/datastore';
2
2
  import { BaseCommonDB, CommonDB, DBQuery, DBTransaction, RunQueryResult } from '@naturalcycles/db-lib';
3
- import { ObjectWithId, JsonSchemaRootObject, CommonLogger } from '@naturalcycles/js-lib';
3
+ import { ObjectWithId, JsonSchemaObject, JsonSchemaRootObject, CommonLogger } from '@naturalcycles/js-lib';
4
4
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
5
5
  import { DatastoreDBCfg, DatastoreDBOptions, DatastoreDBSaveOptions, DatastoreDBStreamOptions, DatastorePayload, DatastorePropertyStats, DatastoreStats } from './datastore.model';
6
6
  /**
@@ -55,6 +55,7 @@ export declare class DatastoreDB extends BaseCommonDB implements CommonDB {
55
55
  key(kind: string, id: string | number): Key;
56
56
  getDsKey(o: any): Key | undefined;
57
57
  getKey(key: Key): string | undefined;
58
+ createTable<ROW extends ObjectWithId>(_table: string, _schema: JsonSchemaObject<ROW>): Promise<void>;
58
59
  getTables(): Promise<string[]>;
59
60
  getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
60
61
  private getPRetryOptions;
@@ -106,6 +106,13 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
106
106
  return q.kinds[0];
107
107
  }
108
108
  async runQuery(dbQuery, _opt) {
109
+ const idFilter = dbQuery._filters.find(f => f.name === 'id');
110
+ if (idFilter) {
111
+ const ids = idFilter.op === '==' ? [idFilter.val] : idFilter.val;
112
+ return {
113
+ rows: await this.getByIds(dbQuery.table, ids),
114
+ };
115
+ }
109
116
  const q = (0, query_util_1.dbQueryToDatastoreQuery)(dbQuery, this.ds().createQuery(dbQuery.table));
110
117
  const qr = await this.runDatastoreQuery(q);
111
118
  // Special case when projection query didn't specify 'id'
@@ -175,6 +182,11 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
175
182
  }
176
183
  }
177
184
  async deleteByQuery(q, opt) {
185
+ const idFilter = q._filters.find(f => f.name === 'id');
186
+ if (idFilter) {
187
+ const ids = idFilter.op === '==' ? [idFilter.val] : idFilter.val;
188
+ return await this.deleteByIds(q.table, ids, opt);
189
+ }
178
190
  const datastoreQuery = (0, query_util_1.dbQueryToDatastoreQuery)(q.select([]), this.ds().createQuery(q.table));
179
191
  const { rows } = await this.runDatastoreQuery(datastoreQuery);
180
192
  return await this.deleteByIds(q.table, rows.map(obj => obj.id), opt);
@@ -277,6 +289,7 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
277
289
  const id = key.id || key.name;
278
290
  return id?.toString();
279
291
  }
292
+ async createTable(_table, _schema) { }
280
293
  async getTables() {
281
294
  const statsArray = await this.getAllStats();
282
295
  // Filter out tables starting with `_` by default (internal Datastore tables)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/datastore-lib",
3
- "version": "3.25.1",
3
+ "version": "3.25.3",
4
4
  "description": "Opinionated library to work with Google Datastore",
5
5
  "scripts": {
6
6
  "prepare": "husky install"
@@ -118,7 +118,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
118
118
  await this.getAllStats()
119
119
  }
120
120
 
121
- override async getByIds<ROW extends ObjectWithId>(
121
+ async getByIds<ROW extends ObjectWithId>(
122
122
  table: string,
123
123
  ids: ROW['id'][],
124
124
  _opt?: DatastoreDBOptions,
@@ -176,6 +176,15 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
176
176
  dbQuery: DBQuery<ROW>,
177
177
  _opt?: DatastoreDBOptions,
178
178
  ): Promise<RunQueryResult<ROW>> {
179
+ const idFilter = dbQuery._filters.find(f => f.name === 'id')
180
+ if (idFilter) {
181
+ const ids: string[] = idFilter.op === '==' ? [idFilter.val] : idFilter.val
182
+
183
+ return {
184
+ rows: await this.getByIds(dbQuery.table, ids),
185
+ }
186
+ }
187
+
179
188
  const q = dbQueryToDatastoreQuery(dbQuery, this.ds().createQuery(dbQuery.table))
180
189
  const qr = await this.runDatastoreQuery<ROW>(q)
181
190
 
@@ -289,6 +298,12 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
289
298
  q: DBQuery<ROW>,
290
299
  opt?: DatastoreDBOptions,
291
300
  ): Promise<number> {
301
+ const idFilter = q._filters.find(f => f.name === 'id')
302
+ if (idFilter) {
303
+ const ids: string[] = idFilter.op === '==' ? [idFilter.val] : idFilter.val
304
+ return await this.deleteByIds(q.table, ids, opt)
305
+ }
306
+
292
307
  const datastoreQuery = dbQueryToDatastoreQuery(q.select([]), this.ds().createQuery(q.table))
293
308
  const { rows } = await this.runDatastoreQuery<ROW>(datastoreQuery)
294
309
  return await this.deleteByIds(
@@ -302,7 +317,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
302
317
  * Limitation: Datastore's delete returns void, so we always return all ids here as "deleted"
303
318
  * regardless if they were actually deleted or not.
304
319
  */
305
- override async deleteByIds<ROW extends ObjectWithId>(
320
+ async deleteByIds<ROW extends ObjectWithId>(
306
321
  table: string,
307
322
  ids: ROW['id'][],
308
323
  opt: DatastoreDBOptions = {},
@@ -418,6 +433,11 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
418
433
  return id?.toString()
419
434
  }
420
435
 
436
+ override async createTable<ROW extends ObjectWithId>(
437
+ _table: string,
438
+ _schema: JsonSchemaObject<ROW>,
439
+ ): Promise<void> {}
440
+
421
441
  override async getTables(): Promise<string[]> {
422
442
  const statsArray = await this.getAllStats()
423
443
  // Filter out tables starting with `_` by default (internal Datastore tables)
@@ -45,7 +45,7 @@ export class DatastoreKeyValueDB implements CommonKeyValueDB {
45
45
  }
46
46
 
47
47
  streamIds(table: string, limit?: number): ReadableTyped<string> {
48
- const q = DBQuery.create(table)
48
+ const q = DBQuery.create<KVObject>(table)
49
49
  .select(['id'])
50
50
  .limit(limit || 0)
51
51
 
@@ -63,7 +63,7 @@ export class DatastoreKeyValueDB implements CommonKeyValueDB {
63
63
 
64
64
  streamValues(table: string, limit?: number): ReadableTyped<Buffer> {
65
65
  // `select v` doesn't work for some reason
66
- const q = DBQuery.create(table).limit(limit || 0)
66
+ const q = DBQuery.create<KVObject>(table).limit(limit || 0)
67
67
 
68
68
  const stream: ReadableTyped<string> = this.db
69
69
  .streamQuery<KVObject>(q)
@@ -78,7 +78,7 @@ export class DatastoreKeyValueDB implements CommonKeyValueDB {
78
78
  }
79
79
 
80
80
  streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple> {
81
- const q = DBQuery.create(table).limit(limit || 0)
81
+ const q = DBQuery.create<KVObject>(table).limit(limit || 0)
82
82
 
83
83
  const stream: ReadableTyped<string> = this.db
84
84
  .streamQuery<KVObject>(q)