@naturalcycles/datastore-lib 3.34.0 → 3.35.1

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,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Readable } from 'node:stream';
3
2
  import { Query } from '@google-cloud/datastore';
4
3
  import { CommonLogger } from '@naturalcycles/js-lib';
@@ -1,7 +1,7 @@
1
- import { Transaction } from '@google-cloud/datastore';
2
1
  import type { Datastore, Key, Query } from '@google-cloud/datastore';
3
- import { BaseCommonDB, CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBSupport, DBQuery, DBTransaction, DBTransactionFn, RunQueryResult, CommonDBTransactionOptions } from '@naturalcycles/db-lib';
4
- import { JsonSchemaObject, JsonSchemaRootObject, CommonLogger, ObjectWithId } from '@naturalcycles/js-lib';
2
+ import { Transaction } from '@google-cloud/datastore';
3
+ import { BaseCommonDB, CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBSupport, CommonDBTransactionOptions, DBQuery, DBTransaction, DBTransactionFn, RunQueryResult } from '@naturalcycles/db-lib';
4
+ import { CommonLogger, JsonSchemaObject, JsonSchemaRootObject, 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
  /**
@@ -5,11 +5,13 @@ const datastore_1 = require("@google-cloud/datastore");
5
5
  const db_lib_1 = require("@naturalcycles/db-lib");
6
6
  const js_lib_1 = require("@naturalcycles/js-lib");
7
7
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
8
- const DatastoreStreamReadable_1 = require("./DatastoreStreamReadable");
9
8
  const datastore_model_1 = require("./datastore.model");
9
+ const DatastoreStreamReadable_1 = require("./DatastoreStreamReadable");
10
10
  const query_util_1 = require("./query.util");
11
11
  // Datastore (also Firestore and other Google APIs) supports max 500 of items when saving/deleting, etc.
12
12
  const MAX_ITEMS = 500;
13
+ // It's an empyrical value, but anything less than infinity is better than infinity
14
+ const DATASTORE_RECOMMENDED_CONCURRENCY = 16;
13
15
  const RETRY_ON = [
14
16
  'GOAWAY',
15
17
  'UNAVAILABLE',
@@ -176,7 +178,9 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
176
178
  await save(chunks[0]);
177
179
  }
178
180
  else {
179
- await (0, js_lib_1.pMap)(chunks, async (batch) => await save(batch));
181
+ await (0, js_lib_1.pMap)(chunks, async (batch) => await save(batch), {
182
+ concurrency: DATASTORE_RECOMMENDED_CONCURRENCY,
183
+ });
180
184
  }
181
185
  }
182
186
  catch (err) {
@@ -203,7 +207,9 @@ class DatastoreDB extends db_lib_1.BaseCommonDB {
203
207
  const keys = ids.map(id => this.key(table, id));
204
208
  await (0, js_lib_1.pMap)((0, js_lib_1._chunk)(keys, MAX_ITEMS),
205
209
  // eslint-disable-next-line @typescript-eslint/return-await
206
- async (batch) => await (opt.tx?.tx || this.ds()).delete(batch));
210
+ async (batch) => await (opt.tx?.tx || this.ds()).delete(batch), {
211
+ concurrency: DATASTORE_RECOMMENDED_CONCURRENCY,
212
+ });
207
213
  return ids.length;
208
214
  }
209
215
  async runInTransaction(fn, opt = {}) {
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { CommonKeyValueDB, KeyValueDBTuple } from '@naturalcycles/db-lib';
3
2
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
4
3
  import { DatastoreDB } from './datastore.db';
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dbQueryToDatastoreQuery = void 0;
3
+ exports.dbQueryToDatastoreQuery = dbQueryToDatastoreQuery;
4
4
  const datastore_1 = require("@google-cloud/datastore");
5
5
  const FNAME_MAP = {
6
6
  id: '__key__',
@@ -41,4 +41,3 @@ function dbQueryToDatastoreQuery(dbQuery, emptyQuery) {
41
41
  }
42
42
  return q;
43
43
  }
44
- exports.dbQueryToDatastoreQuery = dbQueryToDatastoreQuery;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/datastore-lib",
3
- "version": "3.34.0",
3
+ "version": "3.35.1",
4
4
  "description": "Opinionated library to work with Google Datastore",
5
5
  "scripts": {
6
6
  "prepare": "husky"
@@ -1,5 +1,5 @@
1
- import { PropertyFilter, Transaction } from '@google-cloud/datastore'
2
1
  import type { Datastore, Key, Query } from '@google-cloud/datastore'
2
+ import { PropertyFilter, Transaction } from '@google-cloud/datastore'
3
3
  import {
4
4
  BaseCommonDB,
5
5
  CommonDB,
@@ -8,34 +8,33 @@ import {
8
8
  CommonDBSaveMethod,
9
9
  CommonDBSaveOptions,
10
10
  CommonDBSupport,
11
+ CommonDBTransactionOptions,
11
12
  DBQuery,
12
13
  DBTransaction,
13
14
  DBTransactionFn,
14
15
  RunQueryResult,
15
- CommonDBTransactionOptions,
16
16
  } from '@naturalcycles/db-lib'
17
17
  import {
18
+ _assert,
19
+ _chunk,
20
+ _omit,
21
+ CommonLogger,
22
+ commonLoggerMinLevel,
18
23
  JsonSchemaAny,
19
24
  JsonSchemaBoolean,
20
25
  JsonSchemaNull,
21
26
  JsonSchemaNumber,
22
27
  JsonSchemaObject,
28
+ JsonSchemaRootObject,
23
29
  JsonSchemaString,
30
+ ObjectWithId,
24
31
  pMap,
25
- _assert,
26
- _chunk,
27
- _omit,
28
- JsonSchemaRootObject,
29
- CommonLogger,
30
- commonLoggerMinLevel,
31
- pTimeout,
32
- pRetryFn,
33
32
  pRetry,
33
+ pRetryFn,
34
34
  PRetryOptions,
35
- ObjectWithId,
35
+ pTimeout,
36
36
  } from '@naturalcycles/js-lib'
37
- import { ReadableTyped, boldWhite } from '@naturalcycles/nodejs-lib'
38
- import { DatastoreStreamReadable } from './DatastoreStreamReadable'
37
+ import { boldWhite, ReadableTyped } from '@naturalcycles/nodejs-lib'
39
38
  import {
40
39
  DatastoreDBCfg,
41
40
  DatastoreDBOptions,
@@ -46,10 +45,13 @@ import {
46
45
  DatastoreStats,
47
46
  DatastoreType,
48
47
  } from './datastore.model'
48
+ import { DatastoreStreamReadable } from './DatastoreStreamReadable'
49
49
  import { dbQueryToDatastoreQuery } from './query.util'
50
50
 
51
51
  // Datastore (also Firestore and other Google APIs) supports max 500 of items when saving/deleting, etc.
52
52
  const MAX_ITEMS = 500
53
+ // It's an empyrical value, but anything less than infinity is better than infinity
54
+ const DATASTORE_RECOMMENDED_CONCURRENCY = 16
53
55
 
54
56
  const RETRY_ON = [
55
57
  'GOAWAY',
@@ -300,7 +302,9 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
300
302
  // Not using pMap in hope to preserve stack trace
301
303
  await save(chunks[0]!)
302
304
  } else {
303
- await pMap(chunks, async batch => await save(batch))
305
+ await pMap(chunks, async batch => await save(batch), {
306
+ concurrency: DATASTORE_RECOMMENDED_CONCURRENCY,
307
+ })
304
308
  }
305
309
  } catch (err) {
306
310
  // console.log(`datastore.save ${kind}`, { obj, entity })
@@ -347,6 +351,9 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
347
351
  _chunk(keys, MAX_ITEMS),
348
352
  // eslint-disable-next-line @typescript-eslint/return-await
349
353
  async batch => await ((opt.tx as DatastoreDBTransaction)?.tx || this.ds()).delete(batch),
354
+ {
355
+ concurrency: DATASTORE_RECOMMENDED_CONCURRENCY,
356
+ },
350
357
  )
351
358
  return ids.length
352
359
  }