@naturalcycles/firestore-lib 1.7.1 → 1.8.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.
@@ -1,6 +1,6 @@
1
1
  import { Firestore, Query, Transaction } from '@google-cloud/firestore';
2
2
  import { BaseCommonDB, CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, CommonDBSupport, CommonDBTransactionOptions, DBQuery, DBTransaction, DBTransactionFn, RunQueryResult } from '@naturalcycles/db-lib';
3
- import { ObjectWithId } from '@naturalcycles/js-lib';
3
+ import { ObjectWithId, StringMap } from '@naturalcycles/js-lib';
4
4
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
5
5
  export interface FirestoreDBCfg {
6
6
  firestore: Firestore;
@@ -26,6 +26,10 @@ export declare class FirestoreDB extends BaseCommonDB implements CommonDB {
26
26
  deleteByIds(table: string, ids: string[], opt?: FirestoreDBOptions): Promise<number>;
27
27
  private querySnapshotToArray;
28
28
  runInTransaction(fn: DBTransactionFn, opt?: CommonDBTransactionOptions): Promise<void>;
29
+ /**
30
+ * Caveat: it always returns an empty object, not the actual incrementMap.
31
+ */
32
+ incrementBatch(table: string, prop: string, incrementMap: StringMap<number>, _opt?: CommonDBOptions): Promise<StringMap<number>>;
29
33
  ping(): Promise<void>;
30
34
  getTables(): Promise<string[]>;
31
35
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FirestoreDBTransaction = exports.FirestoreDB = exports.RollbackError = void 0;
4
+ const firestore_1 = require("@google-cloud/firestore");
4
5
  const db_lib_1 = require("@naturalcycles/db-lib");
5
6
  const js_lib_1 = require("@naturalcycles/js-lib");
6
7
  const firestore_util_1 = require("./firestore.util");
@@ -22,7 +23,7 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
22
23
  this.cfg = cfg;
23
24
  this.support = {
24
25
  ...db_lib_1.commonDBFullSupport,
25
- updateByQuery: false,
26
+ patchByQuery: false, // todo: can be implemented
26
27
  tableSchemas: false,
27
28
  };
28
29
  }
@@ -162,6 +163,21 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
162
163
  throw err;
163
164
  }
164
165
  }
166
+ /**
167
+ * Caveat: it always returns an empty object, not the actual incrementMap.
168
+ */
169
+ async incrementBatch(table, prop, incrementMap, _opt) {
170
+ const { firestore } = this.cfg;
171
+ const col = firestore.collection(table);
172
+ const batch = firestore.batch();
173
+ for (const [id, increment] of (0, js_lib_1._stringMapEntries)(incrementMap)) {
174
+ batch.set(col.doc((0, firestore_util_1.escapeDocId)(id)), {
175
+ [prop]: firestore_1.FieldValue.increment(increment),
176
+ }, { merge: true });
177
+ }
178
+ await batch.commit();
179
+ return {};
180
+ }
165
181
  async ping() {
166
182
  // no-op now
167
183
  }
@@ -1,15 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.unescapeDocId = exports.escapeDocId = void 0;
3
+ exports.escapeDocId = escapeDocId;
4
+ exports.unescapeDocId = unescapeDocId;
4
5
  const SLASH = '_SLASH_';
5
6
  function escapeDocId(docId) {
6
7
  if (typeof docId === 'number')
7
8
  return String(docId);
8
9
  return docId.replaceAll('/', SLASH);
9
10
  }
10
- exports.escapeDocId = escapeDocId;
11
11
  function unescapeDocId(docId) {
12
12
  // if (typeof docId === 'number') return docId
13
13
  return docId.replaceAll(SLASH, '/');
14
14
  }
15
- exports.unescapeDocId = unescapeDocId;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dbQueryToFirestoreQuery = void 0;
3
+ exports.dbQueryToFirestoreQuery = dbQueryToFirestoreQuery;
4
4
  // Map DBQueryFilterOp to WhereFilterOp
5
5
  // Currently it's fully aligned!
6
6
  const OP_MAP = {
@@ -27,4 +27,3 @@ function dbQueryToFirestoreQuery(dbQuery, emptyQuery) {
27
27
  }
28
28
  return q;
29
29
  }
30
- exports.dbQueryToFirestoreQuery = dbQueryToFirestoreQuery;
package/package.json CHANGED
@@ -1,7 +1,12 @@
1
1
  {
2
2
  "name": "@naturalcycles/firestore-lib",
3
3
  "scripts": {
4
- "prepare": "husky"
4
+ "prepare": "husky",
5
+ "build": "dev-lib build",
6
+ "test": "dev-lib test",
7
+ "lint": "dev-lib lint",
8
+ "bt": "dev-lib bt",
9
+ "lbt": "dev-lib lbt"
5
10
  },
6
11
  "dependencies": {
7
12
  "@google-cloud/firestore": "^7.0.0",
@@ -10,9 +15,9 @@
10
15
  "@naturalcycles/nodejs-lib": "^13.1.0"
11
16
  },
12
17
  "devDependencies": {
13
- "@naturalcycles/dev-lib": "^13.0.1",
18
+ "@naturalcycles/dev-lib": "^15.22.0",
14
19
  "@naturalcycles/test-lib": "^1.0.3",
15
- "@types/node": "^20.1.2",
20
+ "@types/node": "^22.7.5",
16
21
  "dotenv": "^16.0.0",
17
22
  "firebase-admin": "^12.0.0",
18
23
  "jest": "^29.1.2"
@@ -37,7 +42,7 @@
37
42
  "engines": {
38
43
  "node": ">=18.12.0"
39
44
  },
40
- "version": "1.7.1",
45
+ "version": "1.8.0",
41
46
  "description": "Firestore implementation of CommonDB interface",
42
47
  "author": "Natural Cycles Team",
43
48
  "license": "MIT"
@@ -1,4 +1,5 @@
1
1
  import {
2
+ FieldValue,
2
3
  Firestore,
3
4
  Query,
4
5
  QueryDocumentSnapshot,
@@ -21,13 +22,15 @@ import {
21
22
  RunQueryResult,
22
23
  } from '@naturalcycles/db-lib'
23
24
  import {
24
- pMap,
25
+ _assert,
25
26
  _chunk,
26
- _omit,
27
27
  _filterUndefinedValues,
28
- ObjectWithId,
29
- _assert,
30
28
  _isTruthy,
29
+ _omit,
30
+ _stringMapEntries,
31
+ ObjectWithId,
32
+ pMap,
33
+ StringMap,
31
34
  } from '@naturalcycles/js-lib'
32
35
  import { ReadableTyped } from '@naturalcycles/nodejs-lib'
33
36
  import { escapeDocId, unescapeDocId } from './firestore.util'
@@ -62,7 +65,7 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
62
65
 
63
66
  override support: CommonDBSupport = {
64
67
  ...commonDBFullSupport,
65
- updateByQuery: false,
68
+ patchByQuery: false, // todo: can be implemented
66
69
  tableSchemas: false,
67
70
  }
68
71
 
@@ -286,6 +289,33 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
286
289
  }
287
290
  }
288
291
 
292
+ /**
293
+ * Caveat: it always returns an empty object, not the actual incrementMap.
294
+ */
295
+ override async incrementBatch(
296
+ table: string,
297
+ prop: string,
298
+ incrementMap: StringMap<number>,
299
+ _opt?: CommonDBOptions,
300
+ ): Promise<StringMap<number>> {
301
+ const { firestore } = this.cfg
302
+ const col = firestore.collection(table)
303
+ const batch = firestore.batch()
304
+
305
+ for (const [id, increment] of _stringMapEntries(incrementMap)) {
306
+ batch.set(
307
+ col.doc(escapeDocId(id)),
308
+ {
309
+ [prop]: FieldValue.increment(increment),
310
+ },
311
+ { merge: true },
312
+ )
313
+ }
314
+
315
+ await batch.commit()
316
+ return {}
317
+ }
318
+
289
319
  override async ping(): Promise<void> {
290
320
  // no-op now
291
321
  }