@naturalcycles/firestore-lib 1.3.0 → 1.4.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.
@@ -15,7 +15,7 @@ export declare class FirestoreDB extends BaseCommonDB implements CommonDB {
15
15
  getByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], _opt?: FirestoreDBOptions): Promise<ROW[]>;
16
16
  runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBOptions): Promise<RunQueryResult<ROW>>;
17
17
  runFirestoreQuery<ROW extends ObjectWithId>(q: Query, _opt?: FirestoreDBOptions): Promise<ROW[]>;
18
- runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBOptions): Promise<number>;
18
+ runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: FirestoreDBOptions): Promise<number>;
19
19
  streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBStreamOptions): ReadableTyped<ROW>;
20
20
  saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: FirestoreDBSaveOptions<ROW>): Promise<void>;
21
21
  deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBOptions): Promise<number>;
@@ -52,18 +52,23 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
52
52
  async runFirestoreQuery(q, _opt) {
53
53
  return this.querySnapshotToArray(await q.get());
54
54
  }
55
- async runQueryCount(q, opt) {
56
- const { rows } = await this.runQuery(q.select([]), opt);
57
- return rows.length;
55
+ async runQueryCount(q, _opt) {
56
+ const firestoreQuery = (0, query_util_1.dbQueryToFirestoreQuery)(q, this.cfg.firestore.collection(q.table));
57
+ const r = await firestoreQuery.count().get();
58
+ return r.data().count;
58
59
  }
59
60
  streamQuery(q, _opt) {
60
61
  const firestoreQuery = (0, query_util_1.dbQueryToFirestoreQuery)(q, this.cfg.firestore.collection(q.table));
61
- return firestoreQuery.stream().pipe((0, nodejs_lib_1.transformMapSimple)(doc => ({
62
+ const stream = firestoreQuery
63
+ .stream()
64
+ .on('error', err => stream.emit('error', err))
65
+ .pipe((0, nodejs_lib_1.transformMapSimple)(doc => ({
62
66
  id: (0, firestore_util_1.unescapeDocId)(doc.id),
63
67
  ...doc.data(),
64
68
  }), {
65
69
  errorMode: js_lib_1.ErrorMode.SUPPRESS, // because .pipe cannot propagate errors
66
70
  }));
71
+ return stream;
67
72
  }
68
73
  // SAVE
69
74
  async saveBatch(table, rows, opt = {}) {
package/package.json CHANGED
@@ -37,7 +37,7 @@
37
37
  "engines": {
38
38
  "node": ">=14.15.0"
39
39
  },
40
- "version": "1.3.0",
40
+ "version": "1.4.1",
41
41
  "description": "Firestore implementation of CommonDB interface",
42
42
  "author": "Natural Cycles Team",
43
43
  "license": "MIT"
@@ -98,10 +98,11 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
98
98
 
99
99
  override async runQueryCount<ROW extends ObjectWithId>(
100
100
  q: DBQuery<ROW>,
101
- opt?: FirestoreDBOptions,
101
+ _opt?: FirestoreDBOptions,
102
102
  ): Promise<number> {
103
- const { rows } = await this.runQuery(q.select([]), opt)
104
- return rows.length
103
+ const firestoreQuery = dbQueryToFirestoreQuery(q, this.cfg.firestore.collection(q.table))
104
+ const r = await firestoreQuery.count().get()
105
+ return r.data().count
105
106
  }
106
107
 
107
108
  override streamQuery<ROW extends ObjectWithId>(
@@ -110,17 +111,22 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
110
111
  ): ReadableTyped<ROW> {
111
112
  const firestoreQuery = dbQueryToFirestoreQuery(q, this.cfg.firestore.collection(q.table))
112
113
 
113
- return firestoreQuery.stream().pipe(
114
- transformMapSimple<QueryDocumentSnapshot<any>, ROW>(
115
- doc => ({
116
- id: unescapeDocId(doc.id),
117
- ...doc.data(),
118
- }),
119
- {
120
- errorMode: ErrorMode.SUPPRESS, // because .pipe cannot propagate errors
121
- },
122
- ),
123
- )
114
+ const stream: ReadableTyped<ROW> = firestoreQuery
115
+ .stream()
116
+ .on('error', err => stream.emit('error', err))
117
+ .pipe(
118
+ transformMapSimple<QueryDocumentSnapshot<any>, ROW>(
119
+ doc => ({
120
+ id: unescapeDocId(doc.id),
121
+ ...doc.data(),
122
+ }),
123
+ {
124
+ errorMode: ErrorMode.SUPPRESS, // because .pipe cannot propagate errors
125
+ },
126
+ ),
127
+ )
128
+
129
+ return stream
124
130
  }
125
131
 
126
132
  // SAVE