@naturalcycles/firestore-lib 1.4.1 → 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'
@@ -85,8 +92,15 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
85
92
  }
86
93
  // DELETE
87
94
  async deleteByQuery(q, opt) {
88
- const firestoreQuery = (0, query_util_1.dbQueryToFirestoreQuery)(q.select([]), this.cfg.firestore.collection(q.table));
89
- 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
+ }
90
104
  await this.deleteByIds(q.table, ids, opt);
91
105
  return ids.length;
92
106
  }
@@ -133,5 +147,8 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
133
147
  async ping() {
134
148
  // no-op now
135
149
  }
150
+ async getTables() {
151
+ return [];
152
+ }
136
153
  }
137
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.1",
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)
@@ -165,18 +173,25 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
165
173
  q: DBQuery<ROW>,
166
174
  opt?: FirestoreDBOptions,
167
175
  ): Promise<number> {
168
- const firestoreQuery = dbQueryToFirestoreQuery(
169
- q.select([]),
170
- this.cfg.firestore.collection(q.table),
171
- )
172
- 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
+ }
173
188
 
174
189
  await this.deleteByIds(q.table, ids, opt)
175
190
 
176
191
  return ids.length
177
192
  }
178
193
 
179
- override async deleteByIds<ROW extends ObjectWithId>(
194
+ async deleteByIds<ROW extends ObjectWithId>(
180
195
  table: string,
181
196
  ids: ROW['id'][],
182
197
  _opt?: FirestoreDBOptions,
@@ -233,4 +248,8 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
233
248
  override async ping(): Promise<void> {
234
249
  // no-op now
235
250
  }
251
+
252
+ override async getTables(): Promise<string[]> {
253
+ return []
254
+ }
236
255
  }