@naturalcycles/firestore-lib 1.7.0 → 1.8.0
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.
- package/dist/firestore.db.d.ts +8 -4
- package/dist/firestore.db.js +23 -12
- package/dist/firestore.util.js +2 -3
- package/dist/query.util.js +1 -2
- package/package.json +9 -4
- package/src/firestore.db.ts +45 -27
package/dist/firestore.db.d.ts
CHANGED
|
@@ -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,
|
|
3
|
+
import { ObjectWithId, StringMap } 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
|
|
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,15 @@ 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
|
|
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
28
|
runInTransaction(fn: DBTransactionFn, opt?: CommonDBTransactionOptions): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Caveat: it always returns an empty object, not the actual incrementMap.
|
|
31
|
+
*/
|
|
32
|
+
incrementBatch(table: string, prop: string, incrementMap: StringMap<number>, _opt?: CommonDBOptions): Promise<StringMap<number>>;
|
|
29
33
|
ping(): Promise<void>;
|
|
30
34
|
getTables(): Promise<string[]>;
|
|
31
35
|
}
|
|
@@ -38,6 +42,6 @@ export declare class FirestoreDBTransaction implements DBTransaction {
|
|
|
38
42
|
constructor(db: FirestoreDB, tx: Transaction);
|
|
39
43
|
rollback(): Promise<void>;
|
|
40
44
|
getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>;
|
|
41
|
-
saveBatch<ROW extends
|
|
45
|
+
saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
|
|
42
46
|
deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number>;
|
|
43
47
|
}
|
package/dist/firestore.db.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.FirestoreDBTransaction = exports.FirestoreDB = exports.RollbackError = void 0;
|
|
4
|
+
const firestore_1 = require("@google-cloud/firestore");
|
|
4
5
|
const db_lib_1 = require("@naturalcycles/db-lib");
|
|
5
6
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
|
-
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
7
7
|
const firestore_util_1 = require("./firestore.util");
|
|
8
8
|
const query_util_1 = require("./query.util");
|
|
9
9
|
const methodMap = {
|
|
@@ -23,7 +23,7 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
|
|
|
23
23
|
this.cfg = cfg;
|
|
24
24
|
this.support = {
|
|
25
25
|
...db_lib_1.commonDBFullSupport,
|
|
26
|
-
|
|
26
|
+
patchByQuery: false, // todo: can be implemented
|
|
27
27
|
tableSchemas: false,
|
|
28
28
|
};
|
|
29
29
|
}
|
|
@@ -72,16 +72,12 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
|
|
|
72
72
|
}
|
|
73
73
|
streamQuery(q, _opt) {
|
|
74
74
|
const firestoreQuery = (0, query_util_1.dbQueryToFirestoreQuery)(q, this.cfg.firestore.collection(q.table));
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}), {
|
|
82
|
-
errorMode: js_lib_1.ErrorMode.SUPPRESS, // because .pipe cannot propagate errors
|
|
83
|
-
}));
|
|
84
|
-
return stream;
|
|
75
|
+
return firestoreQuery.stream().map(doc => {
|
|
76
|
+
return {
|
|
77
|
+
id: (0, firestore_util_1.unescapeDocId)(doc.id),
|
|
78
|
+
...doc.data(),
|
|
79
|
+
};
|
|
80
|
+
});
|
|
85
81
|
}
|
|
86
82
|
// SAVE
|
|
87
83
|
async saveBatch(table, rows, opt = {}) {
|
|
@@ -167,6 +163,21 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
|
|
|
167
163
|
throw err;
|
|
168
164
|
}
|
|
169
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Caveat: it always returns an empty object, not the actual incrementMap.
|
|
168
|
+
*/
|
|
169
|
+
async incrementBatch(table, prop, incrementMap, _opt) {
|
|
170
|
+
const { firestore } = this.cfg;
|
|
171
|
+
const col = firestore.collection(table);
|
|
172
|
+
const batch = firestore.batch();
|
|
173
|
+
for (const [id, increment] of (0, js_lib_1._stringMapEntries)(incrementMap)) {
|
|
174
|
+
batch.set(col.doc((0, firestore_util_1.escapeDocId)(id)), {
|
|
175
|
+
[prop]: firestore_1.FieldValue.increment(increment),
|
|
176
|
+
}, { merge: true });
|
|
177
|
+
}
|
|
178
|
+
await batch.commit();
|
|
179
|
+
return {};
|
|
180
|
+
}
|
|
170
181
|
async ping() {
|
|
171
182
|
// no-op now
|
|
172
183
|
}
|
package/dist/firestore.util.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.escapeDocId = escapeDocId;
|
|
4
|
+
exports.unescapeDocId = unescapeDocId;
|
|
4
5
|
const SLASH = '_SLASH_';
|
|
5
6
|
function escapeDocId(docId) {
|
|
6
7
|
if (typeof docId === 'number')
|
|
7
8
|
return String(docId);
|
|
8
9
|
return docId.replaceAll('/', SLASH);
|
|
9
10
|
}
|
|
10
|
-
exports.escapeDocId = escapeDocId;
|
|
11
11
|
function unescapeDocId(docId) {
|
|
12
12
|
// if (typeof docId === 'number') return docId
|
|
13
13
|
return docId.replaceAll(SLASH, '/');
|
|
14
14
|
}
|
|
15
|
-
exports.unescapeDocId = unescapeDocId;
|
package/dist/query.util.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.dbQueryToFirestoreQuery =
|
|
3
|
+
exports.dbQueryToFirestoreQuery = dbQueryToFirestoreQuery;
|
|
4
4
|
// Map DBQueryFilterOp to WhereFilterOp
|
|
5
5
|
// Currently it's fully aligned!
|
|
6
6
|
const OP_MAP = {
|
|
@@ -27,4 +27,3 @@ function dbQueryToFirestoreQuery(dbQuery, emptyQuery) {
|
|
|
27
27
|
}
|
|
28
28
|
return q;
|
|
29
29
|
}
|
|
30
|
-
exports.dbQueryToFirestoreQuery = dbQueryToFirestoreQuery;
|
package/package.json
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/firestore-lib",
|
|
3
3
|
"scripts": {
|
|
4
|
-
"prepare": "husky
|
|
4
|
+
"prepare": "husky",
|
|
5
|
+
"build": "dev-lib build",
|
|
6
|
+
"test": "dev-lib test",
|
|
7
|
+
"lint": "dev-lib lint",
|
|
8
|
+
"bt": "dev-lib bt",
|
|
9
|
+
"lbt": "dev-lib lbt"
|
|
5
10
|
},
|
|
6
11
|
"dependencies": {
|
|
7
12
|
"@google-cloud/firestore": "^7.0.0",
|
|
@@ -10,9 +15,9 @@
|
|
|
10
15
|
"@naturalcycles/nodejs-lib": "^13.1.0"
|
|
11
16
|
},
|
|
12
17
|
"devDependencies": {
|
|
13
|
-
"@naturalcycles/dev-lib": "^
|
|
18
|
+
"@naturalcycles/dev-lib": "^15.22.0",
|
|
14
19
|
"@naturalcycles/test-lib": "^1.0.3",
|
|
15
|
-
"@types/node": "^
|
|
20
|
+
"@types/node": "^22.7.5",
|
|
16
21
|
"dotenv": "^16.0.0",
|
|
17
22
|
"firebase-admin": "^12.0.0",
|
|
18
23
|
"jest": "^29.1.2"
|
|
@@ -37,7 +42,7 @@
|
|
|
37
42
|
"engines": {
|
|
38
43
|
"node": ">=18.12.0"
|
|
39
44
|
},
|
|
40
|
-
"version": "1.
|
|
45
|
+
"version": "1.8.0",
|
|
41
46
|
"description": "Firestore implementation of CommonDB interface",
|
|
42
47
|
"author": "Natural Cycles Team",
|
|
43
48
|
"license": "MIT"
|
package/src/firestore.db.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
FieldValue,
|
|
2
3
|
Firestore,
|
|
3
4
|
Query,
|
|
4
5
|
QueryDocumentSnapshot,
|
|
@@ -21,17 +22,17 @@ import {
|
|
|
21
22
|
RunQueryResult,
|
|
22
23
|
} from '@naturalcycles/db-lib'
|
|
23
24
|
import {
|
|
24
|
-
|
|
25
|
-
pMap,
|
|
25
|
+
_assert,
|
|
26
26
|
_chunk,
|
|
27
|
-
_omit,
|
|
28
27
|
_filterUndefinedValues,
|
|
29
|
-
ObjectWithId,
|
|
30
|
-
AnyObjectWithId,
|
|
31
|
-
_assert,
|
|
32
28
|
_isTruthy,
|
|
29
|
+
_omit,
|
|
30
|
+
_stringMapEntries,
|
|
31
|
+
ObjectWithId,
|
|
32
|
+
pMap,
|
|
33
|
+
StringMap,
|
|
33
34
|
} from '@naturalcycles/js-lib'
|
|
34
|
-
import { ReadableTyped
|
|
35
|
+
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
35
36
|
import { escapeDocId, unescapeDocId } from './firestore.util'
|
|
36
37
|
import { dbQueryToFirestoreQuery } from './query.util'
|
|
37
38
|
|
|
@@ -40,7 +41,7 @@ export interface FirestoreDBCfg {
|
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
export interface FirestoreDBOptions extends CommonDBOptions {}
|
|
43
|
-
export interface FirestoreDBSaveOptions<ROW extends
|
|
44
|
+
export interface FirestoreDBSaveOptions<ROW extends ObjectWithId>
|
|
44
45
|
extends CommonDBSaveOptions<ROW> {}
|
|
45
46
|
|
|
46
47
|
type SaveOp = 'create' | 'update' | 'set'
|
|
@@ -64,7 +65,7 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
|
|
|
64
65
|
|
|
65
66
|
override support: CommonDBSupport = {
|
|
66
67
|
...commonDBFullSupport,
|
|
67
|
-
|
|
68
|
+
patchByQuery: false, // todo: can be implemented
|
|
68
69
|
tableSchemas: false,
|
|
69
70
|
}
|
|
70
71
|
|
|
@@ -142,26 +143,16 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
|
|
|
142
143
|
): ReadableTyped<ROW> {
|
|
143
144
|
const firestoreQuery = dbQueryToFirestoreQuery(q, this.cfg.firestore.collection(q.table))
|
|
144
145
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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
|
|
146
|
+
return (firestoreQuery.stream() as ReadableTyped<QueryDocumentSnapshot<any>>).map(doc => {
|
|
147
|
+
return {
|
|
148
|
+
id: unescapeDocId(doc.id),
|
|
149
|
+
...doc.data(),
|
|
150
|
+
} as ROW
|
|
151
|
+
})
|
|
161
152
|
}
|
|
162
153
|
|
|
163
154
|
// SAVE
|
|
164
|
-
override async saveBatch<ROW extends
|
|
155
|
+
override async saveBatch<ROW extends ObjectWithId>(
|
|
165
156
|
table: string,
|
|
166
157
|
rows: ROW[],
|
|
167
158
|
opt: FirestoreDBSaveOptions<ROW> = {},
|
|
@@ -298,6 +289,33 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
|
|
|
298
289
|
}
|
|
299
290
|
}
|
|
300
291
|
|
|
292
|
+
/**
|
|
293
|
+
* Caveat: it always returns an empty object, not the actual incrementMap.
|
|
294
|
+
*/
|
|
295
|
+
override async incrementBatch(
|
|
296
|
+
table: string,
|
|
297
|
+
prop: string,
|
|
298
|
+
incrementMap: StringMap<number>,
|
|
299
|
+
_opt?: CommonDBOptions,
|
|
300
|
+
): Promise<StringMap<number>> {
|
|
301
|
+
const { firestore } = this.cfg
|
|
302
|
+
const col = firestore.collection(table)
|
|
303
|
+
const batch = firestore.batch()
|
|
304
|
+
|
|
305
|
+
for (const [id, increment] of _stringMapEntries(incrementMap)) {
|
|
306
|
+
batch.set(
|
|
307
|
+
col.doc(escapeDocId(id)),
|
|
308
|
+
{
|
|
309
|
+
[prop]: FieldValue.increment(increment),
|
|
310
|
+
},
|
|
311
|
+
{ merge: true },
|
|
312
|
+
)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
await batch.commit()
|
|
316
|
+
return {}
|
|
317
|
+
}
|
|
318
|
+
|
|
301
319
|
override async ping(): Promise<void> {
|
|
302
320
|
// no-op now
|
|
303
321
|
}
|
|
@@ -328,7 +346,7 @@ export class FirestoreDBTransaction implements DBTransaction {
|
|
|
328
346
|
return await this.db.getByIds(table, ids, { ...opt, tx: this })
|
|
329
347
|
}
|
|
330
348
|
|
|
331
|
-
async saveBatch<ROW extends
|
|
349
|
+
async saveBatch<ROW extends ObjectWithId>(
|
|
332
350
|
table: string,
|
|
333
351
|
rows: ROW[],
|
|
334
352
|
opt?: CommonDBSaveOptions<ROW>,
|