@naturalcycles/firestore-lib 1.7.0 → 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
2
  import { BaseCommonDB, CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, CommonDBSupport, CommonDBTransactionOptions, DBQuery, DBTransaction, DBTransactionFn, RunQueryResult } from '@naturalcycles/db-lib';
3
- import { ObjectWithId, AnyObjectWithId } from '@naturalcycles/js-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,7 +21,7 @@ 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;
@@ -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 = {}) {
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.7.0",
40
+ "version": "1.7.1",
41
41
  "description": "Firestore implementation of CommonDB interface",
42
42
  "author": "Natural Cycles Team",
43
43
  "license": "MIT"
@@ -21,17 +21,15 @@ import {
21
21
  RunQueryResult,
22
22
  } from '@naturalcycles/db-lib'
23
23
  import {
24
- ErrorMode,
25
24
  pMap,
26
25
  _chunk,
27
26
  _omit,
28
27
  _filterUndefinedValues,
29
28
  ObjectWithId,
30
- AnyObjectWithId,
31
29
  _assert,
32
30
  _isTruthy,
33
31
  } from '@naturalcycles/js-lib'
34
- import { ReadableTyped, transformMapSimple } from '@naturalcycles/nodejs-lib'
32
+ import { ReadableTyped } from '@naturalcycles/nodejs-lib'
35
33
  import { escapeDocId, unescapeDocId } from './firestore.util'
36
34
  import { dbQueryToFirestoreQuery } from './query.util'
37
35
 
@@ -40,7 +38,7 @@ export interface FirestoreDBCfg {
40
38
  }
41
39
 
42
40
  export interface FirestoreDBOptions extends CommonDBOptions {}
43
- export interface FirestoreDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObjectWithId>
41
+ export interface FirestoreDBSaveOptions<ROW extends ObjectWithId>
44
42
  extends CommonDBSaveOptions<ROW> {}
45
43
 
46
44
  type SaveOp = 'create' | 'update' | 'set'
@@ -142,26 +140,16 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
142
140
  ): ReadableTyped<ROW> {
143
141
  const firestoreQuery = dbQueryToFirestoreQuery(q, this.cfg.firestore.collection(q.table))
144
142
 
145
- const stream: ReadableTyped<ROW> = firestoreQuery
146
- .stream()
147
- .on('error', err => stream.emit('error', err))
148
- .pipe(
149
- transformMapSimple<QueryDocumentSnapshot<any>, ROW>(
150
- doc => ({
151
- id: unescapeDocId(doc.id),
152
- ...doc.data(),
153
- }),
154
- {
155
- errorMode: ErrorMode.SUPPRESS, // because .pipe cannot propagate errors
156
- },
157
- ),
158
- )
159
-
160
- 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
+ })
161
149
  }
162
150
 
163
151
  // SAVE
164
- override async saveBatch<ROW extends Partial<ObjectWithId>>(
152
+ override async saveBatch<ROW extends ObjectWithId>(
165
153
  table: string,
166
154
  rows: ROW[],
167
155
  opt: FirestoreDBSaveOptions<ROW> = {},
@@ -328,7 +316,7 @@ export class FirestoreDBTransaction implements DBTransaction {
328
316
  return await this.db.getByIds(table, ids, { ...opt, tx: this })
329
317
  }
330
318
 
331
- async saveBatch<ROW extends Partial<ObjectWithId>>(
319
+ async saveBatch<ROW extends ObjectWithId>(
332
320
  table: string,
333
321
  rows: ROW[],
334
322
  opt?: CommonDBSaveOptions<ROW>,