@naturalcycles/firestore-lib 1.4.0 → 1.4.2

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.
@@ -23,4 +23,5 @@ export declare class FirestoreDB extends BaseCommonDB implements CommonDB {
23
23
  private querySnapshotToArray;
24
24
  commitTransaction(tx: DBTransaction, _opt?: CommonDBSaveOptions): Promise<void>;
25
25
  ping(): Promise<void>;
26
+ getTables(): Promise<string[]>;
26
27
  }
@@ -41,6 +41,13 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
41
41
  }
42
42
  // QUERY
43
43
  async runQuery(q, opt) {
44
+ const idFilter = q._filters.find(f => f.name === 'id');
45
+ if (idFilter) {
46
+ const ids = Array.isArray(idFilter.val) ? idFilter.val : [idFilter.val];
47
+ return {
48
+ rows: await this.getByIds(q.table, ids, opt),
49
+ };
50
+ }
44
51
  const firestoreQuery = (0, query_util_1.dbQueryToFirestoreQuery)(q, this.cfg.firestore.collection(q.table));
45
52
  let rows = await this.runFirestoreQuery(firestoreQuery, opt);
46
53
  // Special case when projection query didn't specify 'id'
@@ -59,12 +66,16 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
59
66
  }
60
67
  streamQuery(q, _opt) {
61
68
  const firestoreQuery = (0, query_util_1.dbQueryToFirestoreQuery)(q, this.cfg.firestore.collection(q.table));
62
- return firestoreQuery.stream().pipe((0, nodejs_lib_1.transformMapSimple)(doc => ({
69
+ const stream = firestoreQuery
70
+ .stream()
71
+ .on('error', err => stream.emit('error', err))
72
+ .pipe((0, nodejs_lib_1.transformMapSimple)(doc => ({
63
73
  id: (0, firestore_util_1.unescapeDocId)(doc.id),
64
74
  ...doc.data(),
65
75
  }), {
66
76
  errorMode: js_lib_1.ErrorMode.SUPPRESS, // because .pipe cannot propagate errors
67
77
  }));
78
+ return stream;
68
79
  }
69
80
  // SAVE
70
81
  async saveBatch(table, rows, opt = {}) {
@@ -81,8 +92,15 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
81
92
  }
82
93
  // DELETE
83
94
  async deleteByQuery(q, opt) {
84
- const firestoreQuery = (0, query_util_1.dbQueryToFirestoreQuery)(q.select([]), this.cfg.firestore.collection(q.table));
85
- const ids = (await this.runFirestoreQuery(firestoreQuery)).map(obj => obj.id);
95
+ let ids;
96
+ const idFilter = q._filters.find(f => f.name === 'id');
97
+ if (idFilter) {
98
+ ids = Array.isArray(idFilter.val) ? idFilter.val : [idFilter.val];
99
+ }
100
+ else {
101
+ const firestoreQuery = (0, query_util_1.dbQueryToFirestoreQuery)(q.select([]), this.cfg.firestore.collection(q.table));
102
+ ids = (await this.runFirestoreQuery(firestoreQuery)).map(obj => obj.id);
103
+ }
86
104
  await this.deleteByIds(q.table, ids, opt);
87
105
  return ids.length;
88
106
  }
@@ -129,5 +147,8 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
129
147
  async ping() {
130
148
  // no-op now
131
149
  }
150
+ async getTables() {
151
+ return [];
152
+ }
132
153
  }
133
154
  exports.FirestoreDB = FirestoreDB;
package/package.json CHANGED
@@ -37,7 +37,7 @@
37
37
  "engines": {
38
38
  "node": ">=14.15.0"
39
39
  },
40
- "version": "1.4.0",
40
+ "version": "1.4.2",
41
41
  "description": "Firestore implementation of CommonDB interface",
42
42
  "author": "Natural Cycles Team",
43
43
  "license": "MIT"
@@ -45,7 +45,7 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
45
45
  }
46
46
 
47
47
  // GET
48
- override async getByIds<ROW extends ObjectWithId>(
48
+ async getByIds<ROW extends ObjectWithId>(
49
49
  table: string,
50
50
  ids: ROW['id'][],
51
51
  _opt?: FirestoreDBOptions,
@@ -77,6 +77,14 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
77
77
  q: DBQuery<ROW>,
78
78
  opt?: FirestoreDBOptions,
79
79
  ): Promise<RunQueryResult<ROW>> {
80
+ const idFilter = q._filters.find(f => f.name === 'id')
81
+ if (idFilter) {
82
+ const ids: string[] = Array.isArray(idFilter.val) ? idFilter.val : [idFilter.val]
83
+ return {
84
+ rows: await this.getByIds(q.table, ids, opt),
85
+ }
86
+ }
87
+
80
88
  const firestoreQuery = dbQueryToFirestoreQuery(q, this.cfg.firestore.collection(q.table))
81
89
 
82
90
  let rows = await this.runFirestoreQuery<ROW>(firestoreQuery, opt)
@@ -111,17 +119,22 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
111
119
  ): ReadableTyped<ROW> {
112
120
  const firestoreQuery = dbQueryToFirestoreQuery(q, this.cfg.firestore.collection(q.table))
113
121
 
114
- return firestoreQuery.stream().pipe(
115
- transformMapSimple<QueryDocumentSnapshot<any>, ROW>(
116
- doc => ({
117
- id: unescapeDocId(doc.id),
118
- ...doc.data(),
119
- }),
120
- {
121
- errorMode: ErrorMode.SUPPRESS, // because .pipe cannot propagate errors
122
- },
123
- ),
124
- )
122
+ const stream: ReadableTyped<ROW> = firestoreQuery
123
+ .stream()
124
+ .on('error', err => stream.emit('error', err))
125
+ .pipe(
126
+ transformMapSimple<QueryDocumentSnapshot<any>, ROW>(
127
+ doc => ({
128
+ id: unescapeDocId(doc.id),
129
+ ...doc.data(),
130
+ }),
131
+ {
132
+ errorMode: ErrorMode.SUPPRESS, // because .pipe cannot propagate errors
133
+ },
134
+ ),
135
+ )
136
+
137
+ return stream
125
138
  }
126
139
 
127
140
  // SAVE
@@ -160,18 +173,25 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
160
173
  q: DBQuery<ROW>,
161
174
  opt?: FirestoreDBOptions,
162
175
  ): Promise<number> {
163
- const firestoreQuery = dbQueryToFirestoreQuery(
164
- q.select([]),
165
- this.cfg.firestore.collection(q.table),
166
- )
167
- const ids = (await this.runFirestoreQuery<ROW>(firestoreQuery)).map(obj => obj.id)
176
+ let ids: ROW['id'][]
177
+
178
+ const idFilter = q._filters.find(f => f.name === 'id')
179
+ if (idFilter) {
180
+ ids = Array.isArray(idFilter.val) ? idFilter.val : [idFilter.val]
181
+ } else {
182
+ const firestoreQuery = dbQueryToFirestoreQuery(
183
+ q.select([]),
184
+ this.cfg.firestore.collection(q.table),
185
+ )
186
+ ids = (await this.runFirestoreQuery<ROW>(firestoreQuery)).map(obj => obj.id)
187
+ }
168
188
 
169
189
  await this.deleteByIds(q.table, ids, opt)
170
190
 
171
191
  return ids.length
172
192
  }
173
193
 
174
- override async deleteByIds<ROW extends ObjectWithId>(
194
+ async deleteByIds<ROW extends ObjectWithId>(
175
195
  table: string,
176
196
  ids: ROW['id'][],
177
197
  _opt?: FirestoreDBOptions,
@@ -228,4 +248,8 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
228
248
  override async ping(): Promise<void> {
229
249
  // no-op now
230
250
  }
251
+
252
+ override async getTables(): Promise<string[]> {
253
+ return []
254
+ }
231
255
  }