@naturalcycles/datastore-lib 3.25.0 → 3.25.2

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;
@@ -33,13 +33,12 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
33
33
  }
34
34
  // @memo() // not used to be able to connect to many DBs in the same server instance
35
35
  ds() {
36
- var _a;
37
36
  if (!this.cachedDatastore) {
38
37
  (0, js_lib_1._assert)(process.env['APP_ENV'] !== 'test', 'DatastoreDB cannot be used in Test env, please use InMemoryDB');
39
38
  // Lazy-loading
40
39
  const datastoreLib = require('@google-cloud/datastore');
41
40
  const DS = datastoreLib.Datastore;
42
- (_a = this.cfg).projectId || (_a.projectId = this.cfg.credentials?.project_id || process.env['GOOGLE_CLOUD_PROJECT']);
41
+ this.cfg.projectId ||= this.cfg.credentials?.project_id || process.env['GOOGLE_CLOUD_PROJECT'];
43
42
  if (this.cfg.projectId) {
44
43
  this.cfg.logger.log(`DatastoreDB connected to ${(0, colors_1.boldWhite)(this.cfg.projectId)}`);
45
44
  }
@@ -68,7 +67,7 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
68
67
  if (this.cfg.timeout) {
69
68
  // First try
70
69
  try {
71
- const r = await (0, js_lib_1.pTimeout)(this.ds().get(keys), {
70
+ const r = await (0, js_lib_1.pTimeout)(() => this.ds().get(keys), {
72
71
  timeout: this.cfg.timeout,
73
72
  name: `datastore.getByIds(${table})`,
74
73
  });
@@ -81,7 +80,7 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
81
80
  const DS = datastoreLib.Datastore;
82
81
  this.cachedDatastore = new DS(this.cfg);
83
82
  // Second try (will throw)
84
- const r = await (0, js_lib_1.pTimeout)(this.ds().get(keys), {
83
+ const r = await (0, js_lib_1.pTimeout)(() => this.ds().get(keys), {
85
84
  timeout: this.cfg.timeout,
86
85
  name: `datastore.getByIds(${table}) second try`,
87
86
  errorData: {
@@ -107,6 +106,11 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
107
106
  return q.kinds[0];
108
107
  }
109
108
  async runQuery(dbQuery, _opt) {
109
+ if (dbQuery._ids?.length) {
110
+ return {
111
+ rows: await this.getByIds(dbQuery.table, dbQuery._ids),
112
+ };
113
+ }
110
114
  const q = (0, query_util_1.dbQueryToDatastoreQuery)(dbQuery, this.ds().createQuery(dbQuery.table));
111
115
  const qr = await this.runDatastoreQuery(q);
112
116
  // Special case when projection query didn't specify 'id'
@@ -278,6 +282,7 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
278
282
  const id = key.id || key.name;
279
283
  return id?.toString();
280
284
  }
285
+ async createTable(_table, _schema) { }
281
286
  async getTables() {
282
287
  const statsArray = await this.getAllStats();
283
288
  // Filter out tables starting with `_` by default (internal Datastore tables)
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@naturalcycles/datastore-lib",
3
- "version": "3.25.0",
3
+ "version": "3.25.2",
4
4
  "description": "Opinionated library to work with Google Datastore",
5
5
  "scripts": {
6
6
  "prepare": "husky install"
7
7
  },
8
8
  "dependencies": {
9
9
  "@google-cloud/datastore": "^7.0.0",
10
- "@naturalcycles/db-lib": "^8.0.0",
11
- "@naturalcycles/js-lib": "^14.0.0",
10
+ "@naturalcycles/db-lib": "^8.46.1",
11
+ "@naturalcycles/js-lib": "^14.116.0",
12
12
  "@naturalcycles/nodejs-lib": "^12.0.0",
13
13
  "grpc": "^1.24.2"
14
14
  },
@@ -31,7 +31,7 @@
31
31
  "main": "dist/index.js",
32
32
  "types": "dist/index.d.ts",
33
33
  "engines": {
34
- "node": ">=14.15.0"
34
+ "node": ">=16.10.0"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public"
@@ -130,7 +130,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
130
130
  if (this.cfg.timeout) {
131
131
  // First try
132
132
  try {
133
- const r = await pTimeout(this.ds().get(keys), {
133
+ const r = await pTimeout(() => this.ds().get(keys), {
134
134
  timeout: this.cfg.timeout,
135
135
  name: `datastore.getByIds(${table})`,
136
136
  })
@@ -144,7 +144,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
144
144
  this.cachedDatastore = new DS(this.cfg)
145
145
 
146
146
  // Second try (will throw)
147
- const r = await pTimeout(this.ds().get(keys), {
147
+ const r = await pTimeout(() => this.ds().get(keys), {
148
148
  timeout: this.cfg.timeout,
149
149
  name: `datastore.getByIds(${table}) second try`,
150
150
  errorData: {
@@ -176,6 +176,12 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
176
176
  dbQuery: DBQuery<ROW>,
177
177
  _opt?: DatastoreDBOptions,
178
178
  ): Promise<RunQueryResult<ROW>> {
179
+ if (dbQuery._ids?.length) {
180
+ return {
181
+ rows: await this.getByIds(dbQuery.table, dbQuery._ids),
182
+ }
183
+ }
184
+
179
185
  const q = dbQueryToDatastoreQuery(dbQuery, this.ds().createQuery(dbQuery.table))
180
186
  const qr = await this.runDatastoreQuery<ROW>(q)
181
187
 
@@ -418,6 +424,11 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
418
424
  return id?.toString()
419
425
  }
420
426
 
427
+ override async createTable<ROW extends ObjectWithId>(
428
+ _table: string,
429
+ _schema: JsonSchemaObject<ROW>,
430
+ ): Promise<void> {}
431
+
421
432
  override async getTables(): Promise<string[]> {
422
433
  const statsArray = await this.getAllStats()
423
434
  // 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)