@naturalcycles/firestore-lib 1.6.1 → 1.7.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,13 +1,13 @@
1
1
  import { Firestore, Query, Transaction } from '@google-cloud/firestore';
2
- import { BaseCommonDB, CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, CommonDBSupport, DBQuery, DBTransaction, DBTransactionFn, RunQueryResult } from '@naturalcycles/db-lib';
3
- import { ObjectWithId, AnyObjectWithId } from '@naturalcycles/js-lib';
2
+ import { BaseCommonDB, CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, CommonDBSupport, CommonDBTransactionOptions, DBQuery, DBTransaction, DBTransactionFn, RunQueryResult } from '@naturalcycles/db-lib';
3
+ import { ObjectWithId } from '@naturalcycles/js-lib';
4
4
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
5
5
  export interface FirestoreDBCfg {
6
6
  firestore: Firestore;
7
7
  }
8
8
  export interface FirestoreDBOptions extends CommonDBOptions {
9
9
  }
10
- export interface FirestoreDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObjectWithId> extends CommonDBSaveOptions<ROW> {
10
+ export interface FirestoreDBSaveOptions<ROW extends ObjectWithId> extends CommonDBSaveOptions<ROW> {
11
11
  }
12
12
  export declare class RollbackError extends Error {
13
13
  constructor();
@@ -21,11 +21,11 @@ export declare class FirestoreDB extends BaseCommonDB implements CommonDB {
21
21
  runFirestoreQuery<ROW extends ObjectWithId>(q: Query, _opt?: FirestoreDBOptions): Promise<ROW[]>;
22
22
  runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: FirestoreDBOptions): Promise<number>;
23
23
  streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBStreamOptions): ReadableTyped<ROW>;
24
- saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: FirestoreDBSaveOptions<ROW>): Promise<void>;
24
+ saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: FirestoreDBSaveOptions<ROW>): Promise<void>;
25
25
  deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBOptions): Promise<number>;
26
26
  deleteByIds(table: string, ids: string[], opt?: FirestoreDBOptions): Promise<number>;
27
27
  private querySnapshotToArray;
28
- runInTransaction(fn: DBTransactionFn): Promise<void>;
28
+ runInTransaction(fn: DBTransactionFn, opt?: CommonDBTransactionOptions): Promise<void>;
29
29
  ping(): Promise<void>;
30
30
  getTables(): Promise<string[]>;
31
31
  }
@@ -38,6 +38,6 @@ export declare class FirestoreDBTransaction implements DBTransaction {
38
38
  constructor(db: FirestoreDB, tx: Transaction);
39
39
  rollback(): Promise<void>;
40
40
  getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>;
41
- saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
41
+ saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
42
42
  deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number>;
43
43
  }
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FirestoreDBTransaction = exports.FirestoreDB = exports.RollbackError = void 0;
4
4
  const db_lib_1 = require("@naturalcycles/db-lib");
5
5
  const js_lib_1 = require("@naturalcycles/js-lib");
6
- const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
7
6
  const firestore_util_1 = require("./firestore.util");
8
7
  const query_util_1 = require("./query.util");
9
8
  const methodMap = {
@@ -72,16 +71,12 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
72
71
  }
73
72
  streamQuery(q, _opt) {
74
73
  const firestoreQuery = (0, query_util_1.dbQueryToFirestoreQuery)(q, this.cfg.firestore.collection(q.table));
75
- const stream = firestoreQuery
76
- .stream()
77
- .on('error', err => stream.emit('error', err))
78
- .pipe((0, nodejs_lib_1.transformMapSimple)(doc => ({
79
- id: (0, firestore_util_1.unescapeDocId)(doc.id),
80
- ...doc.data(),
81
- }), {
82
- errorMode: js_lib_1.ErrorMode.SUPPRESS, // because .pipe cannot propagate errors
83
- }));
84
- return stream;
74
+ return firestoreQuery.stream().map(doc => {
75
+ return {
76
+ id: (0, firestore_util_1.unescapeDocId)(doc.id),
77
+ ...doc.data(),
78
+ };
79
+ });
85
80
  }
86
81
  // SAVE
87
82
  async saveBatch(table, rows, opt = {}) {
@@ -149,11 +144,14 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
149
144
  });
150
145
  return rows;
151
146
  }
152
- async runInTransaction(fn) {
147
+ async runInTransaction(fn, opt = {}) {
148
+ const { readOnly } = opt;
153
149
  try {
154
150
  await this.cfg.firestore.runTransaction(async (firestoreTx) => {
155
151
  const tx = new FirestoreDBTransaction(this, firestoreTx);
156
152
  await fn(tx);
153
+ }, {
154
+ readOnly,
157
155
  });
158
156
  }
159
157
  catch (err) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/firestore-lib",
3
3
  "scripts": {
4
- "prepare": "husky install"
4
+ "prepare": "husky"
5
5
  },
6
6
  "dependencies": {
7
7
  "@google-cloud/firestore": "^7.0.0",
@@ -37,7 +37,7 @@
37
37
  "engines": {
38
38
  "node": ">=18.12.0"
39
39
  },
40
- "version": "1.6.1",
40
+ "version": "1.7.1",
41
41
  "description": "Firestore implementation of CommonDB interface",
42
42
  "author": "Natural Cycles Team",
43
43
  "license": "MIT"
@@ -14,23 +14,22 @@ import {
14
14
  CommonDBSaveOptions,
15
15
  CommonDBStreamOptions,
16
16
  CommonDBSupport,
17
+ CommonDBTransactionOptions,
17
18
  DBQuery,
18
19
  DBTransaction,
19
20
  DBTransactionFn,
20
21
  RunQueryResult,
21
22
  } from '@naturalcycles/db-lib'
22
23
  import {
23
- ErrorMode,
24
24
  pMap,
25
25
  _chunk,
26
26
  _omit,
27
27
  _filterUndefinedValues,
28
28
  ObjectWithId,
29
- AnyObjectWithId,
30
29
  _assert,
31
30
  _isTruthy,
32
31
  } from '@naturalcycles/js-lib'
33
- import { ReadableTyped, transformMapSimple } from '@naturalcycles/nodejs-lib'
32
+ import { ReadableTyped } from '@naturalcycles/nodejs-lib'
34
33
  import { escapeDocId, unescapeDocId } from './firestore.util'
35
34
  import { dbQueryToFirestoreQuery } from './query.util'
36
35
 
@@ -39,7 +38,7 @@ export interface FirestoreDBCfg {
39
38
  }
40
39
 
41
40
  export interface FirestoreDBOptions extends CommonDBOptions {}
42
- export interface FirestoreDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObjectWithId>
41
+ export interface FirestoreDBSaveOptions<ROW extends ObjectWithId>
43
42
  extends CommonDBSaveOptions<ROW> {}
44
43
 
45
44
  type SaveOp = 'create' | 'update' | 'set'
@@ -141,26 +140,16 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
141
140
  ): ReadableTyped<ROW> {
142
141
  const firestoreQuery = dbQueryToFirestoreQuery(q, this.cfg.firestore.collection(q.table))
143
142
 
144
- const stream: ReadableTyped<ROW> = firestoreQuery
145
- .stream()
146
- .on('error', err => stream.emit('error', err))
147
- .pipe(
148
- transformMapSimple<QueryDocumentSnapshot<any>, ROW>(
149
- doc => ({
150
- id: unescapeDocId(doc.id),
151
- ...doc.data(),
152
- }),
153
- {
154
- errorMode: ErrorMode.SUPPRESS, // because .pipe cannot propagate errors
155
- },
156
- ),
157
- )
158
-
159
- return stream
143
+ return (firestoreQuery.stream() as ReadableTyped<QueryDocumentSnapshot<any>>).map(doc => {
144
+ return {
145
+ id: unescapeDocId(doc.id),
146
+ ...doc.data(),
147
+ } as ROW
148
+ })
160
149
  }
161
150
 
162
151
  // SAVE
163
- override async saveBatch<ROW extends Partial<ObjectWithId>>(
152
+ override async saveBatch<ROW extends ObjectWithId>(
164
153
  table: string,
165
154
  rows: ROW[],
166
155
  opt: FirestoreDBSaveOptions<ROW> = {},
@@ -272,12 +261,22 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
272
261
  return rows
273
262
  }
274
263
 
275
- override async runInTransaction(fn: DBTransactionFn): Promise<void> {
264
+ override async runInTransaction(
265
+ fn: DBTransactionFn,
266
+ opt: CommonDBTransactionOptions = {},
267
+ ): Promise<void> {
268
+ const { readOnly } = opt
269
+
276
270
  try {
277
- await this.cfg.firestore.runTransaction(async firestoreTx => {
278
- const tx = new FirestoreDBTransaction(this, firestoreTx)
279
- await fn(tx)
280
- })
271
+ await this.cfg.firestore.runTransaction(
272
+ async firestoreTx => {
273
+ const tx = new FirestoreDBTransaction(this, firestoreTx)
274
+ await fn(tx)
275
+ },
276
+ {
277
+ readOnly,
278
+ },
279
+ )
281
280
  } catch (err) {
282
281
  if (err instanceof RollbackError) {
283
282
  // RollbackError should be handled gracefully (not re-throw)
@@ -317,7 +316,7 @@ export class FirestoreDBTransaction implements DBTransaction {
317
316
  return await this.db.getByIds(table, ids, { ...opt, tx: this })
318
317
  }
319
318
 
320
- async saveBatch<ROW extends Partial<ObjectWithId>>(
319
+ async saveBatch<ROW extends ObjectWithId>(
321
320
  table: string,
322
321
  rows: ROW[],
323
322
  opt?: CommonDBSaveOptions<ROW>,