@naturalcycles/firestore-lib 1.1.2 → 1.1.6
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/CHANGELOG.md +28 -0
- package/dist/firestore.db.d.ts +9 -12
- package/dist/firestore.db.js +25 -39
- package/dist/firestore.util.js +2 -3
- package/dist/index.js +0 -1
- package/dist/query.util.js +2 -1
- package/dist/string.util.js +0 -1
- package/package.json +12 -10
- package/src/firestore.db.ts +35 -44
- package/src/query.util.ts +5 -3
- package/dist/firestore.db.js.map +0 -1
- package/dist/firestore.util.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/query.util.js.map +0 -1
- package/dist/string.util.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
## [1.1.6](https://github.com/NaturalCycles/firestore-lib/compare/v1.1.5...v1.1.6) (2021-10-17)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* db-lib compat ([ad28580](https://github.com/NaturalCycles/firestore-lib/commit/ad28580cf772b9ff05d07cc89e6ee19be52981e3))
|
|
7
|
+
|
|
8
|
+
## [1.1.5](https://github.com/NaturalCycles/firestore-lib/compare/v1.1.4...v1.1.5) (2021-10-17)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* adapt to latest db-lib ([e64d4ca](https://github.com/NaturalCycles/firestore-lib/commit/e64d4cad792a2f24d3e592dbbff59492017a9d60))
|
|
14
|
+
|
|
15
|
+
## [1.1.4](https://github.com/NaturalCycles/firestore-lib/compare/v1.1.3...v1.1.4) (2021-10-01)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* adapt to db-lib ([05cbc49](https://github.com/NaturalCycles/firestore-lib/commit/05cbc4996efe5a3f1091835b016e2534b59befde))
|
|
21
|
+
|
|
22
|
+
## [1.1.3](https://github.com/NaturalCycles/firestore-lib/compare/v1.1.2...v1.1.3) (2021-08-25)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Bug Fixes
|
|
26
|
+
|
|
27
|
+
* deps ([541438a](https://github.com/NaturalCycles/firestore-lib/commit/541438aab99613d5b0c69b48b68008d6e0fbe06f))
|
|
28
|
+
|
|
1
29
|
## [1.1.2](https://github.com/NaturalCycles/firestore-lib/compare/v1.1.1...v1.1.2) (2021-02-27)
|
|
2
30
|
|
|
3
31
|
|
package/dist/firestore.db.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Query } from '@google-cloud/firestore';
|
|
2
|
-
import {
|
|
2
|
+
import { BaseCommonDB, CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, DBQuery, DBTransaction, ObjectWithId, RunQueryResult, AnyObjectWithId } from '@naturalcycles/db-lib';
|
|
3
3
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
4
4
|
import * as firebaseAdmin from 'firebase-admin';
|
|
5
5
|
export interface FirestoreDBCfg {
|
|
@@ -7,24 +7,21 @@ export interface FirestoreDBCfg {
|
|
|
7
7
|
}
|
|
8
8
|
export interface FirestoreDBOptions extends CommonDBOptions {
|
|
9
9
|
}
|
|
10
|
-
export interface FirestoreDBSaveOptions extends CommonDBSaveOptions {
|
|
10
|
+
export interface FirestoreDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId> extends CommonDBSaveOptions<ROW> {
|
|
11
11
|
}
|
|
12
|
-
export declare class FirestoreDB implements CommonDB {
|
|
12
|
+
export declare class FirestoreDB extends BaseCommonDB implements CommonDB {
|
|
13
13
|
cfg: FirestoreDBCfg;
|
|
14
14
|
constructor(cfg: FirestoreDBCfg);
|
|
15
15
|
getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: FirestoreDBOptions): Promise<ROW[]>;
|
|
16
|
-
getById<ROW extends ObjectWithId>(table: string, id: string,
|
|
16
|
+
getById<ROW extends ObjectWithId>(table: string, id: string, _opt?: FirestoreDBOptions): Promise<ROW | undefined>;
|
|
17
17
|
runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBOptions): Promise<RunQueryResult<ROW>>;
|
|
18
|
-
runFirestoreQuery<ROW extends ObjectWithId>(q: Query,
|
|
18
|
+
runFirestoreQuery<ROW extends ObjectWithId>(q: Query, _opt?: FirestoreDBOptions): Promise<ROW[]>;
|
|
19
19
|
runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBOptions): Promise<number>;
|
|
20
|
-
streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>,
|
|
21
|
-
saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[],
|
|
20
|
+
streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBStreamOptions): ReadableTyped<ROW>;
|
|
21
|
+
saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], _opt?: FirestoreDBSaveOptions<ROW>): Promise<void>;
|
|
22
22
|
deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBOptions): Promise<number>;
|
|
23
|
-
deleteByIds(table: string, ids: string[],
|
|
23
|
+
deleteByIds(table: string, ids: string[], _opt?: FirestoreDBOptions): Promise<number>;
|
|
24
24
|
private querySnapshotToArray;
|
|
25
|
-
|
|
26
|
-
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<CommonSchema<ROW>>;
|
|
27
|
-
createTable(schema: CommonSchema, opt?: CommonDBCreateOptions): Promise<void>;
|
|
28
|
-
commitTransaction(tx: DBTransaction, opt?: CommonDBSaveOptions): Promise<void>;
|
|
25
|
+
commitTransaction(_tx: DBTransaction, _opt?: CommonDBSaveOptions): Promise<void>;
|
|
29
26
|
ping(): Promise<void>;
|
|
30
27
|
}
|
package/dist/firestore.db.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.FirestoreDB = void 0;
|
|
4
|
+
const db_lib_1 = require("@naturalcycles/db-lib");
|
|
4
5
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
|
-
const
|
|
6
|
+
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
6
7
|
const firestore_util_1 = require("./firestore.util");
|
|
7
8
|
const query_util_1 = require("./query.util");
|
|
8
|
-
class FirestoreDB {
|
|
9
|
+
class FirestoreDB extends db_lib_1.BaseCommonDB {
|
|
9
10
|
constructor(cfg) {
|
|
11
|
+
super();
|
|
10
12
|
this.cfg = cfg;
|
|
11
13
|
}
|
|
12
14
|
// GET
|
|
@@ -15,8 +17,8 @@ class FirestoreDB {
|
|
|
15
17
|
// TODO: check if we can query by keys or smth
|
|
16
18
|
return (await Promise.all(ids.map(id => this.getById(table, id, opt)))).filter(Boolean);
|
|
17
19
|
}
|
|
18
|
-
async getById(table, id,
|
|
19
|
-
const doc = await this.cfg.firestore.collection(table).doc(firestore_util_1.escapeDocId(id)).get();
|
|
20
|
+
async getById(table, id, _opt) {
|
|
21
|
+
const doc = await this.cfg.firestore.collection(table).doc((0, firestore_util_1.escapeDocId)(id)).get();
|
|
20
22
|
const data = doc.data();
|
|
21
23
|
if (data === undefined)
|
|
22
24
|
return;
|
|
@@ -27,56 +29,53 @@ class FirestoreDB {
|
|
|
27
29
|
}
|
|
28
30
|
// QUERY
|
|
29
31
|
async runQuery(q, opt) {
|
|
30
|
-
const firestoreQuery = query_util_1.dbQueryToFirestoreQuery(q, this.cfg.firestore.collection(q.table));
|
|
32
|
+
const firestoreQuery = (0, query_util_1.dbQueryToFirestoreQuery)(q, this.cfg.firestore.collection(q.table));
|
|
31
33
|
let rows = await this.runFirestoreQuery(firestoreQuery, opt);
|
|
32
34
|
// Special case when projection query didn't specify 'id'
|
|
33
35
|
if (q._selectedFieldNames && !q._selectedFieldNames.includes('id')) {
|
|
34
|
-
rows = rows.map(r => js_lib_1._omit(r, ['id']));
|
|
36
|
+
rows = rows.map(r => (0, js_lib_1._omit)(r, ['id']));
|
|
35
37
|
}
|
|
36
38
|
return { rows };
|
|
37
39
|
}
|
|
38
|
-
async runFirestoreQuery(q,
|
|
40
|
+
async runFirestoreQuery(q, _opt) {
|
|
39
41
|
return this.querySnapshotToArray(await q.get());
|
|
40
42
|
}
|
|
41
43
|
async runQueryCount(q, opt) {
|
|
42
44
|
const { rows } = await this.runQuery(q.select([]), opt);
|
|
43
45
|
return rows.length;
|
|
44
46
|
}
|
|
45
|
-
streamQuery(q,
|
|
46
|
-
const firestoreQuery = query_util_1.dbQueryToFirestoreQuery(q, this.cfg.firestore.collection(q.table));
|
|
47
|
-
return firestoreQuery.stream().pipe(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
...doc.data(),
|
|
53
|
-
});
|
|
54
|
-
},
|
|
47
|
+
streamQuery(q, _opt) {
|
|
48
|
+
const firestoreQuery = (0, query_util_1.dbQueryToFirestoreQuery)(q, this.cfg.firestore.collection(q.table));
|
|
49
|
+
return firestoreQuery.stream().pipe((0, nodejs_lib_1.transformMapSimple)(doc => ({
|
|
50
|
+
id: (0, firestore_util_1.unescapeDocId)(doc.id),
|
|
51
|
+
...doc.data(),
|
|
52
|
+
}), {
|
|
53
|
+
errorMode: js_lib_1.ErrorMode.SUPPRESS, // because .pipe cannot propagate errors
|
|
55
54
|
}));
|
|
56
55
|
}
|
|
57
56
|
// SAVE
|
|
58
|
-
async saveBatch(table, rows,
|
|
57
|
+
async saveBatch(table, rows, _opt) {
|
|
59
58
|
// Firestore allows max 500 items in one batch
|
|
60
|
-
await js_lib_1.pMap(js_lib_1._chunk(rows, 500), async (chunk) => {
|
|
59
|
+
await (0, js_lib_1.pMap)((0, js_lib_1._chunk)(rows, 500), async (chunk) => {
|
|
61
60
|
const batch = this.cfg.firestore.batch();
|
|
62
61
|
chunk.forEach(row => {
|
|
63
|
-
batch.set(this.cfg.firestore.collection(table).doc(firestore_util_1.escapeDocId(row.id)), js_lib_1._filterNullishValues(row));
|
|
62
|
+
batch.set(this.cfg.firestore.collection(table).doc((0, firestore_util_1.escapeDocId)(row.id)), (0, js_lib_1._filterNullishValues)(row));
|
|
64
63
|
});
|
|
65
64
|
await batch.commit();
|
|
66
65
|
}, { concurrency: 1 });
|
|
67
66
|
}
|
|
68
67
|
// DELETE
|
|
69
68
|
async deleteByQuery(q, opt) {
|
|
70
|
-
const firestoreQuery = query_util_1.dbQueryToFirestoreQuery(q.select([]), this.cfg.firestore.collection(q.table));
|
|
69
|
+
const firestoreQuery = (0, query_util_1.dbQueryToFirestoreQuery)(q.select([]), this.cfg.firestore.collection(q.table));
|
|
71
70
|
const ids = (await this.runFirestoreQuery(firestoreQuery)).map(obj => obj.id);
|
|
72
71
|
await this.deleteByIds(q.table, ids, opt);
|
|
73
72
|
return ids.length;
|
|
74
73
|
}
|
|
75
|
-
async deleteByIds(table, ids,
|
|
76
|
-
await js_lib_1.pMap(js_lib_1._chunk(ids, 500), async (chunk) => {
|
|
74
|
+
async deleteByIds(table, ids, _opt) {
|
|
75
|
+
await (0, js_lib_1.pMap)((0, js_lib_1._chunk)(ids, 500), async (chunk) => {
|
|
77
76
|
const batch = this.cfg.firestore.batch();
|
|
78
77
|
chunk.forEach(id => {
|
|
79
|
-
batch.delete(this.cfg.firestore.collection(table).doc(firestore_util_1.escapeDocId(id)));
|
|
78
|
+
batch.delete(this.cfg.firestore.collection(table).doc((0, firestore_util_1.escapeDocId)(id)));
|
|
80
79
|
});
|
|
81
80
|
await batch.commit();
|
|
82
81
|
});
|
|
@@ -86,25 +85,13 @@ class FirestoreDB {
|
|
|
86
85
|
const rows = [];
|
|
87
86
|
qs.forEach(doc => {
|
|
88
87
|
rows.push({
|
|
89
|
-
id: firestore_util_1.unescapeDocId(doc.id),
|
|
88
|
+
id: (0, firestore_util_1.unescapeDocId)(doc.id),
|
|
90
89
|
...doc.data(),
|
|
91
90
|
});
|
|
92
91
|
});
|
|
93
92
|
return rows;
|
|
94
93
|
}
|
|
95
|
-
async
|
|
96
|
-
return []; // todo
|
|
97
|
-
}
|
|
98
|
-
async getTableSchema(table) {
|
|
99
|
-
return {
|
|
100
|
-
table,
|
|
101
|
-
fields: [],
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
async createTable(schema, opt) {
|
|
105
|
-
// todo
|
|
106
|
-
}
|
|
107
|
-
async commitTransaction(tx, opt) {
|
|
94
|
+
async commitTransaction(_tx, _opt) {
|
|
108
95
|
throw new Error('commitTransaction is not supported yet');
|
|
109
96
|
}
|
|
110
97
|
async ping() {
|
|
@@ -112,4 +99,3 @@ class FirestoreDB {
|
|
|
112
99
|
}
|
|
113
100
|
}
|
|
114
101
|
exports.FirestoreDB = FirestoreDB;
|
|
115
|
-
//# sourceMappingURL=firestore.db.js.map
|
package/dist/firestore.util.js
CHANGED
|
@@ -4,11 +4,10 @@ exports.unescapeDocId = exports.escapeDocId = void 0;
|
|
|
4
4
|
const string_util_1 = require("./string.util");
|
|
5
5
|
const SLASH = '_SLASH_';
|
|
6
6
|
function escapeDocId(docId) {
|
|
7
|
-
return string_util_1.replaceAll(docId, '/', SLASH);
|
|
7
|
+
return (0, string_util_1.replaceAll)(docId, '/', SLASH);
|
|
8
8
|
}
|
|
9
9
|
exports.escapeDocId = escapeDocId;
|
|
10
10
|
function unescapeDocId(docId) {
|
|
11
|
-
return string_util_1.replaceAll(docId, SLASH, '/');
|
|
11
|
+
return (0, string_util_1.replaceAll)(docId, SLASH, '/');
|
|
12
12
|
}
|
|
13
13
|
exports.unescapeDocId = unescapeDocId;
|
|
14
|
-
//# sourceMappingURL=firestore.util.js.map
|
package/dist/index.js
CHANGED
|
@@ -5,4 +5,3 @@ const firestore_db_1 = require("./firestore.db");
|
|
|
5
5
|
Object.defineProperty(exports, "FirestoreDB", { enumerable: true, get: function () { return firestore_db_1.FirestoreDB; } });
|
|
6
6
|
const query_util_1 = require("./query.util");
|
|
7
7
|
Object.defineProperty(exports, "dbQueryToFirestoreQuery", { enumerable: true, get: function () { return query_util_1.dbQueryToFirestoreQuery; } });
|
|
8
|
-
//# sourceMappingURL=index.js.map
|
package/dist/query.util.js
CHANGED
|
@@ -9,10 +9,12 @@ const OP_MAP = {
|
|
|
9
9
|
};
|
|
10
10
|
function dbQueryToFirestoreQuery(dbQuery, emptyQuery) {
|
|
11
11
|
// filter
|
|
12
|
+
// eslint-disable-next-line unicorn/no-array-reduce, unicorn/prefer-object-from-entries
|
|
12
13
|
let q = dbQuery._filters.reduce((q, f) => {
|
|
13
14
|
return q.where(f.name, OP_MAP[f.op] || f.op, f.val);
|
|
14
15
|
}, emptyQuery);
|
|
15
16
|
// order
|
|
17
|
+
// eslint-disable-next-line unicorn/no-array-reduce, unicorn/prefer-object-from-entries
|
|
16
18
|
q = dbQuery._orders.reduce((q, ord) => {
|
|
17
19
|
return q.orderBy(ord.name, ord.descending ? 'desc' : 'asc');
|
|
18
20
|
}, q);
|
|
@@ -26,4 +28,3 @@ function dbQueryToFirestoreQuery(dbQuery, emptyQuery) {
|
|
|
26
28
|
return q;
|
|
27
29
|
}
|
|
28
30
|
exports.dbQueryToFirestoreQuery = dbQueryToFirestoreQuery;
|
|
29
|
-
//# sourceMappingURL=query.util.js.map
|
package/dist/string.util.js
CHANGED
package/package.json
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/firestore-lib",
|
|
3
|
-
"scripts": {
|
|
3
|
+
"scripts": {
|
|
4
|
+
"prepare": "husky install"
|
|
5
|
+
},
|
|
4
6
|
"dependencies": {
|
|
5
7
|
"@naturalcycles/db-lib": "^8.2.0",
|
|
6
|
-
"@naturalcycles/js-lib": "^14.6.0"
|
|
8
|
+
"@naturalcycles/js-lib": "^14.6.0",
|
|
9
|
+
"@naturalcycles/nodejs-lib": "^12.4.0"
|
|
7
10
|
},
|
|
8
11
|
"peerDependencies": {
|
|
9
12
|
"firebase-admin": ">=9.3.0"
|
|
10
13
|
},
|
|
11
14
|
"devDependencies": {
|
|
12
|
-
"@naturalcycles/dev-lib": "^
|
|
13
|
-
"@naturalcycles/nodejs-lib": "^12.4.0",
|
|
15
|
+
"@naturalcycles/dev-lib": "^12.1.2",
|
|
14
16
|
"@naturalcycles/test-lib": "^1.0.3",
|
|
15
|
-
"@types/node": "^
|
|
16
|
-
"dotenv": "^
|
|
17
|
-
"firebase-admin": "^
|
|
18
|
-
"jest": "^
|
|
17
|
+
"@types/node": "^16.7.1",
|
|
18
|
+
"dotenv": "^10.0.0",
|
|
19
|
+
"firebase-admin": "^10.0.0",
|
|
20
|
+
"jest": "^27.0.6"
|
|
19
21
|
},
|
|
20
22
|
"files": [
|
|
21
23
|
"dist",
|
|
@@ -35,9 +37,9 @@
|
|
|
35
37
|
"url": "https://github.com/NaturalCycles/firestore-lib"
|
|
36
38
|
},
|
|
37
39
|
"engines": {
|
|
38
|
-
"node": ">=
|
|
40
|
+
"node": ">=12.13"
|
|
39
41
|
},
|
|
40
|
-
"version": "1.1.
|
|
42
|
+
"version": "1.1.6",
|
|
41
43
|
"description": "Firestore implementation of CommonDB interface",
|
|
42
44
|
"author": "Natural Cycles Team",
|
|
43
45
|
"license": "MIT"
|
package/src/firestore.db.ts
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
import { Query, QueryDocumentSnapshot, QuerySnapshot } from '@google-cloud/firestore'
|
|
2
2
|
import {
|
|
3
|
+
BaseCommonDB,
|
|
3
4
|
CommonDB,
|
|
4
|
-
CommonDBCreateOptions,
|
|
5
5
|
CommonDBOptions,
|
|
6
6
|
CommonDBSaveOptions,
|
|
7
7
|
CommonDBStreamOptions,
|
|
8
|
-
CommonSchema,
|
|
9
8
|
DBQuery,
|
|
10
9
|
DBTransaction,
|
|
11
10
|
ObjectWithId,
|
|
12
11
|
RunQueryResult,
|
|
12
|
+
AnyObjectWithId,
|
|
13
13
|
} from '@naturalcycles/db-lib'
|
|
14
|
-
import { pMap, _chunk, _filterNullishValues, _omit } from '@naturalcycles/js-lib'
|
|
15
|
-
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
14
|
+
import { ErrorMode, pMap, _chunk, _filterNullishValues, _omit } from '@naturalcycles/js-lib'
|
|
15
|
+
import { ReadableTyped, transformMapSimple } from '@naturalcycles/nodejs-lib'
|
|
16
16
|
import * as firebaseAdmin from 'firebase-admin'
|
|
17
|
-
import { Transform } from 'stream'
|
|
18
17
|
import { escapeDocId, unescapeDocId } from './firestore.util'
|
|
19
18
|
import { dbQueryToFirestoreQuery } from './query.util'
|
|
20
19
|
|
|
@@ -23,13 +22,16 @@ export interface FirestoreDBCfg {
|
|
|
23
22
|
}
|
|
24
23
|
|
|
25
24
|
export interface FirestoreDBOptions extends CommonDBOptions {}
|
|
26
|
-
export interface FirestoreDBSaveOptions extends
|
|
25
|
+
export interface FirestoreDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId>
|
|
26
|
+
extends CommonDBSaveOptions<ROW> {}
|
|
27
27
|
|
|
28
|
-
export class FirestoreDB implements CommonDB {
|
|
29
|
-
constructor(public cfg: FirestoreDBCfg) {
|
|
28
|
+
export class FirestoreDB extends BaseCommonDB implements CommonDB {
|
|
29
|
+
constructor(public cfg: FirestoreDBCfg) {
|
|
30
|
+
super()
|
|
31
|
+
}
|
|
30
32
|
|
|
31
33
|
// GET
|
|
32
|
-
async getByIds<ROW extends ObjectWithId>(
|
|
34
|
+
override async getByIds<ROW extends ObjectWithId>(
|
|
33
35
|
table: string,
|
|
34
36
|
ids: string[],
|
|
35
37
|
opt?: FirestoreDBOptions,
|
|
@@ -44,7 +46,7 @@ export class FirestoreDB implements CommonDB {
|
|
|
44
46
|
async getById<ROW extends ObjectWithId>(
|
|
45
47
|
table: string,
|
|
46
48
|
id: string,
|
|
47
|
-
|
|
49
|
+
_opt?: FirestoreDBOptions,
|
|
48
50
|
): Promise<ROW | undefined> {
|
|
49
51
|
const doc = await this.cfg.firestore.collection(table).doc(escapeDocId(id)).get()
|
|
50
52
|
|
|
@@ -58,7 +60,7 @@ export class FirestoreDB implements CommonDB {
|
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
// QUERY
|
|
61
|
-
async runQuery<ROW extends ObjectWithId>(
|
|
63
|
+
override async runQuery<ROW extends ObjectWithId>(
|
|
62
64
|
q: DBQuery<ROW>,
|
|
63
65
|
opt?: FirestoreDBOptions,
|
|
64
66
|
): Promise<RunQueryResult<ROW>> {
|
|
@@ -76,12 +78,12 @@ export class FirestoreDB implements CommonDB {
|
|
|
76
78
|
|
|
77
79
|
async runFirestoreQuery<ROW extends ObjectWithId>(
|
|
78
80
|
q: Query,
|
|
79
|
-
|
|
81
|
+
_opt?: FirestoreDBOptions,
|
|
80
82
|
): Promise<ROW[]> {
|
|
81
83
|
return this.querySnapshotToArray(await q.get())
|
|
82
84
|
}
|
|
83
85
|
|
|
84
|
-
async runQueryCount<ROW extends ObjectWithId>(
|
|
86
|
+
override async runQueryCount<ROW extends ObjectWithId>(
|
|
85
87
|
q: DBQuery<ROW>,
|
|
86
88
|
opt?: FirestoreDBOptions,
|
|
87
89
|
): Promise<number> {
|
|
@@ -89,30 +91,30 @@ export class FirestoreDB implements CommonDB {
|
|
|
89
91
|
return rows.length
|
|
90
92
|
}
|
|
91
93
|
|
|
92
|
-
streamQuery<ROW extends ObjectWithId>(
|
|
94
|
+
override streamQuery<ROW extends ObjectWithId>(
|
|
93
95
|
q: DBQuery<ROW>,
|
|
94
|
-
|
|
96
|
+
_opt?: CommonDBStreamOptions,
|
|
95
97
|
): ReadableTyped<ROW> {
|
|
96
98
|
const firestoreQuery = dbQueryToFirestoreQuery(q, this.cfg.firestore.collection(q.table))
|
|
97
99
|
|
|
98
100
|
return firestoreQuery.stream().pipe(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
101
|
+
transformMapSimple<QueryDocumentSnapshot<any>, ROW>(
|
|
102
|
+
doc => ({
|
|
103
|
+
id: unescapeDocId(doc.id),
|
|
104
|
+
...doc.data(),
|
|
105
|
+
}),
|
|
106
|
+
{
|
|
107
|
+
errorMode: ErrorMode.SUPPRESS, // because .pipe cannot propagate errors
|
|
106
108
|
},
|
|
107
|
-
|
|
109
|
+
),
|
|
108
110
|
)
|
|
109
111
|
}
|
|
110
112
|
|
|
111
113
|
// SAVE
|
|
112
|
-
async saveBatch<ROW extends ObjectWithId>(
|
|
114
|
+
override async saveBatch<ROW extends ObjectWithId>(
|
|
113
115
|
table: string,
|
|
114
116
|
rows: ROW[],
|
|
115
|
-
|
|
117
|
+
_opt?: FirestoreDBSaveOptions<ROW>,
|
|
116
118
|
): Promise<void> {
|
|
117
119
|
// Firestore allows max 500 items in one batch
|
|
118
120
|
await pMap(
|
|
@@ -134,7 +136,7 @@ export class FirestoreDB implements CommonDB {
|
|
|
134
136
|
}
|
|
135
137
|
|
|
136
138
|
// DELETE
|
|
137
|
-
async deleteByQuery<ROW extends ObjectWithId>(
|
|
139
|
+
override async deleteByQuery<ROW extends ObjectWithId>(
|
|
138
140
|
q: DBQuery<ROW>,
|
|
139
141
|
opt?: FirestoreDBOptions,
|
|
140
142
|
): Promise<number> {
|
|
@@ -149,7 +151,11 @@ export class FirestoreDB implements CommonDB {
|
|
|
149
151
|
return ids.length
|
|
150
152
|
}
|
|
151
153
|
|
|
152
|
-
async deleteByIds(
|
|
154
|
+
override async deleteByIds(
|
|
155
|
+
table: string,
|
|
156
|
+
ids: string[],
|
|
157
|
+
_opt?: FirestoreDBOptions,
|
|
158
|
+
): Promise<number> {
|
|
153
159
|
await pMap(_chunk(ids, 500), async chunk => {
|
|
154
160
|
const batch = this.cfg.firestore.batch()
|
|
155
161
|
|
|
@@ -176,26 +182,11 @@ export class FirestoreDB implements CommonDB {
|
|
|
176
182
|
return rows
|
|
177
183
|
}
|
|
178
184
|
|
|
179
|
-
async
|
|
180
|
-
return [] // todo
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
async getTableSchema<ROW extends ObjectWithId>(table: string): Promise<CommonSchema<ROW>> {
|
|
184
|
-
return {
|
|
185
|
-
table,
|
|
186
|
-
fields: [],
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
async createTable(schema: CommonSchema, opt?: CommonDBCreateOptions): Promise<void> {
|
|
191
|
-
// todo
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
async commitTransaction(tx: DBTransaction, opt?: CommonDBSaveOptions): Promise<void> {
|
|
185
|
+
override async commitTransaction(_tx: DBTransaction, _opt?: CommonDBSaveOptions): Promise<void> {
|
|
195
186
|
throw new Error('commitTransaction is not supported yet')
|
|
196
187
|
}
|
|
197
188
|
|
|
198
|
-
async ping(): Promise<void> {
|
|
189
|
+
override async ping(): Promise<void> {
|
|
199
190
|
// no-op now
|
|
200
191
|
}
|
|
201
192
|
}
|
package/src/query.util.ts
CHANGED
|
@@ -13,13 +13,15 @@ export function dbQueryToFirestoreQuery<ROW extends ObjectWithId>(
|
|
|
13
13
|
emptyQuery: Query,
|
|
14
14
|
): Query {
|
|
15
15
|
// filter
|
|
16
|
+
// eslint-disable-next-line unicorn/no-array-reduce, unicorn/prefer-object-from-entries
|
|
16
17
|
let q = dbQuery._filters.reduce((q, f) => {
|
|
17
|
-
return q.where(f.name, OP_MAP[f.op] || (f.op as WhereFilterOp), f.val)
|
|
18
|
+
return q.where(f.name as string, OP_MAP[f.op] || (f.op as WhereFilterOp), f.val)
|
|
18
19
|
}, emptyQuery)
|
|
19
20
|
|
|
20
21
|
// order
|
|
22
|
+
// eslint-disable-next-line unicorn/no-array-reduce, unicorn/prefer-object-from-entries
|
|
21
23
|
q = dbQuery._orders.reduce((q, ord) => {
|
|
22
|
-
return q.orderBy(ord.name, ord.descending ? 'desc' : 'asc')
|
|
24
|
+
return q.orderBy(ord.name as string, ord.descending ? 'desc' : 'asc')
|
|
23
25
|
}, q)
|
|
24
26
|
|
|
25
27
|
// limit
|
|
@@ -28,7 +30,7 @@ export function dbQueryToFirestoreQuery<ROW extends ObjectWithId>(
|
|
|
28
30
|
// selectedFields
|
|
29
31
|
if (dbQuery._selectedFieldNames) {
|
|
30
32
|
// todo: check if at least id / __key__ is required to be set
|
|
31
|
-
q = q.select(...dbQuery._selectedFieldNames)
|
|
33
|
+
q = q.select(...(dbQuery._selectedFieldNames as string[]))
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
return q
|
package/dist/firestore.db.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.db.js","sourceRoot":"","sources":["../src/firestore.db.ts"],"names":[],"mappings":";;;AAaA,kDAAiF;AAGjF,mCAAkC;AAClC,qDAA6D;AAC7D,6CAAsD;AAStD,MAAa,WAAW;IACtB,YAAmB,GAAmB;QAAnB,QAAG,GAAH,GAAG,CAAgB;IAAG,CAAC;IAE1C,MAAM;IACN,KAAK,CAAC,QAAQ,CACZ,KAAa,EACb,GAAa,EACb,GAAwB;QAExB,uDAAuD;QACvD,8CAA8C;QAC9C,OAAO,CAAC,MAAM,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,OAAO,CAAM,KAAK,EAAE,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CACjF,OAAO,CACC,CAAA;IACZ,CAAC;IAED,KAAK,CAAC,OAAO,CACX,KAAa,EACb,EAAU,EACV,GAAwB;QAExB,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,4BAAW,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAA;QAEjF,MAAM,IAAI,GAAG,GAAG,CAAC,IAAI,EAAE,CAAA;QACvB,IAAI,IAAI,KAAK,SAAS;YAAE,OAAM;QAE9B,OAAO;YACL,EAAE;YACF,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SACX,CAAA;IACH,CAAC;IAED,KAAK,CAAC,WAAW,CAAC,MAAoB,EAAE,GAA2B;QACjE,OAAO;IACT,CAAC;IAED,KAAK,CAAC,iBAAiB,CAAC,EAAiB,EAAE,GAAyB;QAClE,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAA;IAC3D,CAAC;IAED,KAAK,CAAC,IAAI;QACR,YAAY;IACd,CAAC;CACF;AA7KD,kCA6KC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.util.js","sourceRoot":"","sources":["../src/firestore.util.ts"],"names":[],"mappings":";;;AAAA,+CAA0C;AAE1C,MAAM,KAAK,GAAG,SAAS,CAAA;AAEvB,SAAgB,WAAW,CAAC,KAAa;IACvC,OAAO,wBAAU,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;AACtC,CAAC;AAFD,kCAEC;AAED,SAAgB,aAAa,CAAC,KAAa;IACzC,OAAO,wBAAU,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,CAAA;AACtC,CAAC;AAFD,sCAEC"}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iDAKuB;AAKd,4FATP,0BAAW,OASO;AAJpB,6CAAsD;AAIhC,wGAJb,oCAAuB,OAIa"}
|
package/dist/query.util.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"query.util.js","sourceRoot":"","sources":["../src/query.util.ts"],"names":[],"mappings":";;;AAGA,uCAAuC;AACvC,gCAAgC;AAChC,MAAM,MAAM,GAA0D;AACpE,aAAa;AACb,wBAAwB;CACzB,CAAA;AAED,SAAgB,uBAAuB,CACrC,OAAqB,EACrB,UAAiB;IAEjB,SAAS;IACT,IAAI,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAA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