@ddd-ts/store-firestore 0.0.36 → 0.0.38
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/LICENSE +21 -0
- package/dist/asyncTools.js +14 -41
- package/dist/asyncTools.mjs +15 -0
- package/dist/converter.d.ts +1 -1
- package/dist/converter.d.ts.map +1 -1
- package/dist/converter.js +57 -80
- package/dist/converter.mjs +62 -0
- package/dist/firestore.store.d.ts +2 -2
- package/dist/firestore.store.d.ts.map +1 -1
- package/dist/firestore.store.js +100 -125
- package/dist/firestore.store.mjs +100 -0
- package/dist/firestore.transaction.js +28 -29
- package/dist/firestore.transaction.mjs +28 -0
- package/dist/index.js +9 -11
- package/dist/index.mjs +5 -0
- package/dist/shape/src/addons/microsecond-timestamp.js +62 -0
- package/dist/shape/src/addons/microsecond-timestamp.mjs +61 -0
- package/dist/shape/src/index.js +1 -0
- package/dist/shape/src/index.mjs +3 -0
- package/package.json +33 -22
- package/dist/asyncTools.js.map +0 -1
- package/dist/asyncTools.spec.js +0 -27
- package/dist/asyncTools.spec.js.map +0 -1
- package/dist/converter.js.map +0 -1
- package/dist/firestore.store.js.map +0 -1
- package/dist/firestore.store.spec.js +0 -87
- package/dist/firestore.store.spec.js.map +0 -1
- package/dist/firestore.transaction.js.map +0 -1
- package/dist/index.js.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Aetherall
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/asyncTools.js
CHANGED
|
@@ -1,43 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.combine = combine;
|
|
4
|
-
exports.batch = batch;
|
|
5
|
-
async function* combine(iterables) {
|
|
6
|
-
function getNext(asyncIterator) {
|
|
7
|
-
return asyncIterator.next().then((result) => ({
|
|
8
|
-
asyncIterator,
|
|
9
|
-
result,
|
|
10
|
-
}));
|
|
11
|
-
}
|
|
12
|
-
const asyncIterators = new Map(iterables.map((iterable) => {
|
|
13
|
-
const iterator = iterable[Symbol.asyncIterator]();
|
|
14
|
-
return [iterator, getNext(iterator)];
|
|
15
|
-
}));
|
|
16
|
-
while (asyncIterators.size > 0) {
|
|
17
|
-
const { asyncIterator, result } = await Promise.race(asyncIterators.values());
|
|
18
|
-
if (result.done) {
|
|
19
|
-
if (asyncIterator.return) {
|
|
20
|
-
asyncIterator.return().catch(console.error);
|
|
21
|
-
}
|
|
22
|
-
asyncIterators.delete(asyncIterator);
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
asyncIterators.set(asyncIterator, getNext(asyncIterator));
|
|
26
|
-
yield result.value;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
1
|
+
|
|
2
|
+
//#region src/asyncTools.ts
|
|
30
3
|
async function* batch(iterator, size) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
yield batch;
|
|
41
|
-
}
|
|
4
|
+
let batch = [];
|
|
5
|
+
for await (const item of iterator) {
|
|
6
|
+
batch.push(item);
|
|
7
|
+
if (batch.length === size) {
|
|
8
|
+
yield batch;
|
|
9
|
+
batch = [];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
if (batch.length > 0) yield batch;
|
|
42
13
|
}
|
|
43
|
-
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
exports.batch = batch;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/asyncTools.ts
|
|
2
|
+
async function* batch(iterator, size) {
|
|
3
|
+
let batch = [];
|
|
4
|
+
for await (const item of iterator) {
|
|
5
|
+
batch.push(item);
|
|
6
|
+
if (batch.length === size) {
|
|
7
|
+
yield batch;
|
|
8
|
+
batch = [];
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
if (batch.length > 0) yield batch;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { batch };
|
package/dist/converter.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FirestoreDataConverter } from "firebase-admin/firestore";
|
|
1
|
+
import { type FirestoreDataConverter } from "firebase-admin/firestore";
|
|
2
2
|
export declare class DefaultConverter<T extends FirebaseFirestore.DocumentData> implements FirestoreDataConverter<T> {
|
|
3
3
|
toFirestore(modelObject: T): FirebaseFirestore.DocumentData;
|
|
4
4
|
fromFirestore(snapshot: FirebaseFirestore.QueryDocumentSnapshot): T;
|
package/dist/converter.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"converter.d.ts","sourceRoot":"","sources":["../src/converter.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,sBAAsB,
|
|
1
|
+
{"version":3,"file":"converter.d.ts","sourceRoot":"","sources":["../src/converter.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,sBAAsB,EAE5B,MAAM,0BAA0B,CAAC;AAElC,qBAAa,gBAAgB,CAAC,CAAC,SAAS,iBAAiB,CAAC,YAAY,CACpE,YAAW,sBAAsB,CAAC,CAAC,CAAC;IAE7B,WAAW,CAAC,WAAW,EAAE,CAAC,GAAG,iBAAiB,CAAC,YAAY;IAI3D,aAAa,CAAC,QAAQ,EAAE,iBAAiB,CAAC,qBAAqB,GAAG,CAAC;IAInE,qBAAqB,CAC1B,QAAQ,EAAE,iBAAiB,CAAC,gBAAgB,GAC3C,CAAC,GAAG,SAAS;IAQT,kBAAkB,CACvB,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,iBAAiB,CAAC,UAAU,CAAC,CAAC,CAAC,GACxD,iBAAiB,CAAC,UAAU,CAAC,CAAC,CAAC;CAGnC;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAmCxC;AAED;;GAEG;AACH,wBAAgB,wBAAwB,CACtC,GAAG,EAAE,iBAAiB,CAAC,YAAY,GAClC,iBAAiB,CAAC,YAAY,CAgChC"}
|
package/dist/converter.js
CHANGED
|
@@ -1,85 +1,62 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
toFirestorePartial(modelObject) {
|
|
22
|
-
return toFirestore(modelObject);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
exports.DefaultConverter = DefaultConverter;
|
|
1
|
+
require('./shape/src/index.js');
|
|
2
|
+
let firebase_admin_firestore = require("firebase-admin/firestore");
|
|
3
|
+
|
|
4
|
+
//#region src/converter.ts
|
|
5
|
+
var DefaultConverter = class {
|
|
6
|
+
toFirestore(modelObject) {
|
|
7
|
+
return toFirestore(modelObject);
|
|
8
|
+
}
|
|
9
|
+
fromFirestore(snapshot) {
|
|
10
|
+
return decodeFirebaseTimestamps(snapshot.data());
|
|
11
|
+
}
|
|
12
|
+
fromFirestoreSnapshot(snapshot) {
|
|
13
|
+
const data = snapshot.data();
|
|
14
|
+
if (!data) return;
|
|
15
|
+
return decodeFirebaseTimestamps(data);
|
|
16
|
+
}
|
|
17
|
+
toFirestorePartial(modelObject) {
|
|
18
|
+
return toFirestore(modelObject);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
26
21
|
function toFirestore(obj) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
if (obj instanceof Object) {
|
|
45
|
-
return Object.entries(obj).reduce((map, [key, value]) => {
|
|
46
|
-
if (value !== undefined) {
|
|
47
|
-
map[key] = toFirestore(value);
|
|
48
|
-
}
|
|
49
|
-
return map;
|
|
50
|
-
}, {});
|
|
51
|
-
}
|
|
52
|
-
return obj;
|
|
22
|
+
if (obj instanceof Date) {
|
|
23
|
+
if ("microseconds" in obj && typeof obj.microseconds === "bigint") {
|
|
24
|
+
const microseconds = obj.microseconds;
|
|
25
|
+
const seconds = microseconds / 1000000n;
|
|
26
|
+
const nanoseconds = microseconds % 1000000n * 1000n;
|
|
27
|
+
return new firebase_admin_firestore.Timestamp(Number(seconds), Number(nanoseconds));
|
|
28
|
+
}
|
|
29
|
+
return obj;
|
|
30
|
+
}
|
|
31
|
+
if (obj instanceof firebase_admin_firestore.FieldValue || obj?.constructor?.name === "VectorValue") return obj;
|
|
32
|
+
if (Array.isArray(obj)) return obj.map(toFirestore);
|
|
33
|
+
if (obj instanceof Object) return Object.entries(obj).reduce((map, [key, value]) => {
|
|
34
|
+
if (value !== void 0) map[key] = toFirestore(value);
|
|
35
|
+
return map;
|
|
36
|
+
}, {});
|
|
37
|
+
return obj;
|
|
53
38
|
}
|
|
54
39
|
/**
|
|
55
|
-
|
|
56
|
-
|
|
40
|
+
* decode Firebase timestamps into Dates
|
|
41
|
+
*/
|
|
57
42
|
function decodeFirebaseTimestamps(obj) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (Array.isArray(obj)) {
|
|
75
|
-
return obj.map(decodeFirebaseTimestamps);
|
|
76
|
-
}
|
|
77
|
-
if (obj instanceof Object) {
|
|
78
|
-
return Object.entries(obj).reduce((map, [key, value]) => {
|
|
79
|
-
map[key] = decodeFirebaseTimestamps(value);
|
|
80
|
-
return map;
|
|
81
|
-
}, {});
|
|
82
|
-
}
|
|
83
|
-
return obj;
|
|
43
|
+
if (obj instanceof Date) return obj;
|
|
44
|
+
if (obj?.toDate) {
|
|
45
|
+
const result = obj.toDate();
|
|
46
|
+
if (obj?.seconds !== void 0 && obj?.nanoseconds !== void 0) {
|
|
47
|
+
result.microseconds = BigInt(obj.seconds) * BigInt(1e6) + BigInt(obj.nanoseconds) / BigInt(1e3);
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
if (obj?.constructor?.name === "VectorValue") return obj;
|
|
53
|
+
if (Array.isArray(obj)) return obj.map(decodeFirebaseTimestamps);
|
|
54
|
+
if (obj instanceof Object) return Object.entries(obj).reduce((map, [key, value]) => {
|
|
55
|
+
map[key] = decodeFirebaseTimestamps(value);
|
|
56
|
+
return map;
|
|
57
|
+
}, {});
|
|
58
|
+
return obj;
|
|
84
59
|
}
|
|
85
|
-
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
exports.DefaultConverter = DefaultConverter;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import "./shape/src/index.mjs";
|
|
2
|
+
import { FieldValue, Timestamp } from "firebase-admin/firestore";
|
|
3
|
+
|
|
4
|
+
//#region src/converter.ts
|
|
5
|
+
var DefaultConverter = class {
|
|
6
|
+
toFirestore(modelObject) {
|
|
7
|
+
return toFirestore(modelObject);
|
|
8
|
+
}
|
|
9
|
+
fromFirestore(snapshot) {
|
|
10
|
+
return decodeFirebaseTimestamps(snapshot.data());
|
|
11
|
+
}
|
|
12
|
+
fromFirestoreSnapshot(snapshot) {
|
|
13
|
+
const data = snapshot.data();
|
|
14
|
+
if (!data) return;
|
|
15
|
+
return decodeFirebaseTimestamps(data);
|
|
16
|
+
}
|
|
17
|
+
toFirestorePartial(modelObject) {
|
|
18
|
+
return toFirestore(modelObject);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
function toFirestore(obj) {
|
|
22
|
+
if (obj instanceof Date) {
|
|
23
|
+
if ("microseconds" in obj && typeof obj.microseconds === "bigint") {
|
|
24
|
+
const microseconds = obj.microseconds;
|
|
25
|
+
const seconds = microseconds / 1000000n;
|
|
26
|
+
const nanoseconds = microseconds % 1000000n * 1000n;
|
|
27
|
+
return new Timestamp(Number(seconds), Number(nanoseconds));
|
|
28
|
+
}
|
|
29
|
+
return obj;
|
|
30
|
+
}
|
|
31
|
+
if (obj instanceof FieldValue || obj?.constructor?.name === "VectorValue") return obj;
|
|
32
|
+
if (Array.isArray(obj)) return obj.map(toFirestore);
|
|
33
|
+
if (obj instanceof Object) return Object.entries(obj).reduce((map, [key, value]) => {
|
|
34
|
+
if (value !== void 0) map[key] = toFirestore(value);
|
|
35
|
+
return map;
|
|
36
|
+
}, {});
|
|
37
|
+
return obj;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* decode Firebase timestamps into Dates
|
|
41
|
+
*/
|
|
42
|
+
function decodeFirebaseTimestamps(obj) {
|
|
43
|
+
if (obj instanceof Date) return obj;
|
|
44
|
+
if (obj?.toDate) {
|
|
45
|
+
const result = obj.toDate();
|
|
46
|
+
if (obj?.seconds !== void 0 && obj?.nanoseconds !== void 0) {
|
|
47
|
+
result.microseconds = BigInt(obj.seconds) * BigInt(1e6) + BigInt(obj.nanoseconds) / BigInt(1e3);
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
if (obj?.constructor?.name === "VectorValue") return obj;
|
|
53
|
+
if (Array.isArray(obj)) return obj.map(decodeFirebaseTimestamps);
|
|
54
|
+
if (obj instanceof Object) return Object.entries(obj).reduce((map, [key, value]) => {
|
|
55
|
+
map[key] = decodeFirebaseTimestamps(value);
|
|
56
|
+
return map;
|
|
57
|
+
}, {});
|
|
58
|
+
return obj;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
export { DefaultConverter };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { CollectionReference, DocumentData, QueryDocumentSnapshot } from "firebase-admin/firestore";
|
|
2
|
-
import { Store, ISerializer,
|
|
1
|
+
import { CollectionReference, type DocumentData, QueryDocumentSnapshot } from "firebase-admin/firestore";
|
|
2
|
+
import type { Store, ISerializer, IIdentifiable, Serialized } from "@ddd-ts/core";
|
|
3
3
|
import { FirestoreTransaction } from "./firestore.transaction";
|
|
4
4
|
import { DefaultConverter } from "./converter";
|
|
5
5
|
export declare class FirestoreStore<M extends IIdentifiable, S extends ISerializer<M> = ISerializer<M>> implements Store<M> {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.store.d.ts","sourceRoot":"","sources":["../src/firestore.store.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,YAAY,
|
|
1
|
+
{"version":3,"file":"firestore.store.d.ts","sourceRoot":"","sources":["../src/firestore.store.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,KAAK,YAAY,EACjB,qBAAqB,EAEtB,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,EACV,KAAK,EACL,WAAW,EACX,aAAa,EACb,UAAU,EACX,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAE/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C,qBAAa,cAAc,CACzB,CAAC,SAAS,aAAa,EACvB,CAAC,SAAS,WAAW,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CACzC,YAAW,KAAK,CAAC,CAAC,CAAC;aAID,WAAW,EAAE,mBAAmB;aAChC,UAAU,EAAE,CAAC;aACb,KAAK,CAAC,EAAE,MAAM;IAJzB,gBAAgB,iCAA0B;gBAE/B,WAAW,EAAE,mBAAmB,EAChC,UAAU,EAAE,CAAC,EACb,KAAK,CAAC,EAAE,MAAM,YAAA;IAGhC,IAAI,SAAS,gCAEZ;IAED,IAAI,UAAU,IAGP,mBAAmB,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CACvD;IAEK,YAAY,CAChB,KAAK,EAAE,iBAAiB,CAAC,KAAK,CAAC,GAAG,CAAC,EACnC,GAAG,CAAC,EAAE,oBAAoB,GACzB,OAAO,CAAC,CAAC,EAAE,CAAC;IAcR,WAAW,CAAC,KAAK,EAAE,iBAAiB,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,MAAM;IAyBjE,WAAW,CAChB,KAAK,EAAE,iBAAiB,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
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
async delete(id, trx) {
|
|
103
|
-
if (trx) {
|
|
104
|
-
trx.transaction.delete(this.collection.doc(id.serialize()));
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
await this.collection.doc(id.serialize()).delete();
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
async loadMany(ids, trx) {
|
|
111
|
-
const result = await Promise.all(ids.map((id) => this.load(id, trx)));
|
|
112
|
-
return result.filter((m) => m !== undefined);
|
|
113
|
-
}
|
|
114
|
-
streamAll(pageSize) {
|
|
115
|
-
return this.streamQuery(this.collection, pageSize);
|
|
116
|
-
}
|
|
117
|
-
async countAll() {
|
|
118
|
-
return (await this.collection.count().get()).data().count;
|
|
119
|
-
}
|
|
120
|
-
async count(query) {
|
|
121
|
-
return (await query.count().get()).data().count;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
exports.FirestoreStore = FirestoreStore;
|
|
125
|
-
//# sourceMappingURL=firestore.store.js.map
|
|
1
|
+
require('./firestore.transaction.js');
|
|
2
|
+
const require_asyncTools = require('./asyncTools.js');
|
|
3
|
+
const require_converter = require('./converter.js');
|
|
4
|
+
let firebase_admin_firestore = require("firebase-admin/firestore");
|
|
5
|
+
|
|
6
|
+
//#region src/firestore.store.ts
|
|
7
|
+
var FirestoreStore = class {
|
|
8
|
+
defaultConverter = new require_converter.DefaultConverter();
|
|
9
|
+
constructor(_collection, serializer, $name) {
|
|
10
|
+
this._collection = _collection;
|
|
11
|
+
this.serializer = serializer;
|
|
12
|
+
this.$name = $name;
|
|
13
|
+
}
|
|
14
|
+
get firestore() {
|
|
15
|
+
return this._collection.firestore;
|
|
16
|
+
}
|
|
17
|
+
get collection() {
|
|
18
|
+
return this._collection.withConverter(this.defaultConverter);
|
|
19
|
+
}
|
|
20
|
+
async executeQuery(query, trx) {
|
|
21
|
+
const result = trx ? await trx.transaction.get(query) : await query.get();
|
|
22
|
+
return Promise.all(result.docs.map((doc) => this.serializer.deserialize({
|
|
23
|
+
...this.$name ? { $name: this.$name } : {},
|
|
24
|
+
id: doc.id,
|
|
25
|
+
...doc.data()
|
|
26
|
+
})));
|
|
27
|
+
}
|
|
28
|
+
async *streamPages(query, pageSize) {
|
|
29
|
+
let last;
|
|
30
|
+
let nextPagePromise;
|
|
31
|
+
function getNextPagePromise(cursor) {
|
|
32
|
+
return cursor ? query.limit(pageSize).startAfter(cursor).get() : query.limit(pageSize).get();
|
|
33
|
+
}
|
|
34
|
+
do {
|
|
35
|
+
const { docs } = await (nextPagePromise ?? getNextPagePromise(last));
|
|
36
|
+
last = docs[pageSize - 1];
|
|
37
|
+
if (last) nextPagePromise = getNextPagePromise(last);
|
|
38
|
+
for (const doc of docs) yield doc;
|
|
39
|
+
} while (last);
|
|
40
|
+
}
|
|
41
|
+
async *streamQuery(query, pageSize) {
|
|
42
|
+
const finalPageSize = pageSize ?? 50;
|
|
43
|
+
const stream = finalPageSize === 1 ? query.stream() : this.streamPages(query, finalPageSize);
|
|
44
|
+
for await (const docs of require_asyncTools.batch(stream, finalPageSize)) {
|
|
45
|
+
const deserializedDocs = await Promise.all(docs.map((doc) => this.serializer.deserialize({
|
|
46
|
+
...this.$name ? { $name: this.$name } : {},
|
|
47
|
+
id: doc.id,
|
|
48
|
+
...doc.data()
|
|
49
|
+
})));
|
|
50
|
+
for (const deserializedDoc of deserializedDocs) yield deserializedDoc;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async save(model, trx) {
|
|
54
|
+
const serialized = await this.serializer.serialize(model);
|
|
55
|
+
const ref = this.collection.doc(model.id.serialize());
|
|
56
|
+
trx ? trx.transaction.set(ref, serialized) : await ref.set(serialized);
|
|
57
|
+
}
|
|
58
|
+
async saveAll(models, trx) {
|
|
59
|
+
await Promise.all(models.map((m) => this.save(m, trx)));
|
|
60
|
+
}
|
|
61
|
+
async load(id, trx) {
|
|
62
|
+
const ref = this.collection.doc(id.serialize());
|
|
63
|
+
const snapshot = trx ? await trx.transaction.get(ref) : await ref.get();
|
|
64
|
+
if (!snapshot.exists) return;
|
|
65
|
+
return this.serializer.deserialize({
|
|
66
|
+
...this.$name ? { $name: this.$name } : {},
|
|
67
|
+
id: id.serialize(),
|
|
68
|
+
...snapshot.data()
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
async loadAll(transaction) {
|
|
72
|
+
let docs;
|
|
73
|
+
if (transaction) docs = await transaction.transaction.getAll();
|
|
74
|
+
else ({docs} = await this.collection.get());
|
|
75
|
+
return Promise.all(docs.map((doc) => this.serializer.deserialize({
|
|
76
|
+
...this.$name ? { $name: this.$name } : {},
|
|
77
|
+
id: doc.id,
|
|
78
|
+
...doc.data()
|
|
79
|
+
})));
|
|
80
|
+
}
|
|
81
|
+
async delete(id, trx) {
|
|
82
|
+
if (trx) trx.transaction.delete(this.collection.doc(id.serialize()));
|
|
83
|
+
else await this.collection.doc(id.serialize()).delete();
|
|
84
|
+
}
|
|
85
|
+
async loadMany(ids, trx) {
|
|
86
|
+
return (await Promise.all(ids.map((id) => this.load(id, trx)))).filter((m) => m !== void 0);
|
|
87
|
+
}
|
|
88
|
+
streamAll(pageSize) {
|
|
89
|
+
return this.streamQuery(this.collection, pageSize);
|
|
90
|
+
}
|
|
91
|
+
async countAll() {
|
|
92
|
+
return (await this.collection.count().get()).data().count;
|
|
93
|
+
}
|
|
94
|
+
async count(query) {
|
|
95
|
+
return (await query.count().get()).data().count;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
//#endregion
|
|
100
|
+
exports.FirestoreStore = FirestoreStore;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import "./firestore.transaction.mjs";
|
|
2
|
+
import { batch } from "./asyncTools.mjs";
|
|
3
|
+
import { DefaultConverter } from "./converter.mjs";
|
|
4
|
+
import "firebase-admin/firestore";
|
|
5
|
+
|
|
6
|
+
//#region src/firestore.store.ts
|
|
7
|
+
var FirestoreStore = class {
|
|
8
|
+
defaultConverter = new DefaultConverter();
|
|
9
|
+
constructor(_collection, serializer, $name) {
|
|
10
|
+
this._collection = _collection;
|
|
11
|
+
this.serializer = serializer;
|
|
12
|
+
this.$name = $name;
|
|
13
|
+
}
|
|
14
|
+
get firestore() {
|
|
15
|
+
return this._collection.firestore;
|
|
16
|
+
}
|
|
17
|
+
get collection() {
|
|
18
|
+
return this._collection.withConverter(this.defaultConverter);
|
|
19
|
+
}
|
|
20
|
+
async executeQuery(query, trx) {
|
|
21
|
+
const result = trx ? await trx.transaction.get(query) : await query.get();
|
|
22
|
+
return Promise.all(result.docs.map((doc) => this.serializer.deserialize({
|
|
23
|
+
...this.$name ? { $name: this.$name } : {},
|
|
24
|
+
id: doc.id,
|
|
25
|
+
...doc.data()
|
|
26
|
+
})));
|
|
27
|
+
}
|
|
28
|
+
async *streamPages(query, pageSize) {
|
|
29
|
+
let last;
|
|
30
|
+
let nextPagePromise;
|
|
31
|
+
function getNextPagePromise(cursor) {
|
|
32
|
+
return cursor ? query.limit(pageSize).startAfter(cursor).get() : query.limit(pageSize).get();
|
|
33
|
+
}
|
|
34
|
+
do {
|
|
35
|
+
const { docs } = await (nextPagePromise ?? getNextPagePromise(last));
|
|
36
|
+
last = docs[pageSize - 1];
|
|
37
|
+
if (last) nextPagePromise = getNextPagePromise(last);
|
|
38
|
+
for (const doc of docs) yield doc;
|
|
39
|
+
} while (last);
|
|
40
|
+
}
|
|
41
|
+
async *streamQuery(query, pageSize) {
|
|
42
|
+
const finalPageSize = pageSize ?? 50;
|
|
43
|
+
const stream = finalPageSize === 1 ? query.stream() : this.streamPages(query, finalPageSize);
|
|
44
|
+
for await (const docs of batch(stream, finalPageSize)) {
|
|
45
|
+
const deserializedDocs = await Promise.all(docs.map((doc) => this.serializer.deserialize({
|
|
46
|
+
...this.$name ? { $name: this.$name } : {},
|
|
47
|
+
id: doc.id,
|
|
48
|
+
...doc.data()
|
|
49
|
+
})));
|
|
50
|
+
for (const deserializedDoc of deserializedDocs) yield deserializedDoc;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async save(model, trx) {
|
|
54
|
+
const serialized = await this.serializer.serialize(model);
|
|
55
|
+
const ref = this.collection.doc(model.id.serialize());
|
|
56
|
+
trx ? trx.transaction.set(ref, serialized) : await ref.set(serialized);
|
|
57
|
+
}
|
|
58
|
+
async saveAll(models, trx) {
|
|
59
|
+
await Promise.all(models.map((m) => this.save(m, trx)));
|
|
60
|
+
}
|
|
61
|
+
async load(id, trx) {
|
|
62
|
+
const ref = this.collection.doc(id.serialize());
|
|
63
|
+
const snapshot = trx ? await trx.transaction.get(ref) : await ref.get();
|
|
64
|
+
if (!snapshot.exists) return;
|
|
65
|
+
return this.serializer.deserialize({
|
|
66
|
+
...this.$name ? { $name: this.$name } : {},
|
|
67
|
+
id: id.serialize(),
|
|
68
|
+
...snapshot.data()
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
async loadAll(transaction) {
|
|
72
|
+
let docs;
|
|
73
|
+
if (transaction) docs = await transaction.transaction.getAll();
|
|
74
|
+
else ({docs} = await this.collection.get());
|
|
75
|
+
return Promise.all(docs.map((doc) => this.serializer.deserialize({
|
|
76
|
+
...this.$name ? { $name: this.$name } : {},
|
|
77
|
+
id: doc.id,
|
|
78
|
+
...doc.data()
|
|
79
|
+
})));
|
|
80
|
+
}
|
|
81
|
+
async delete(id, trx) {
|
|
82
|
+
if (trx) trx.transaction.delete(this.collection.doc(id.serialize()));
|
|
83
|
+
else await this.collection.doc(id.serialize()).delete();
|
|
84
|
+
}
|
|
85
|
+
async loadMany(ids, trx) {
|
|
86
|
+
return (await Promise.all(ids.map((id) => this.load(id, trx)))).filter((m) => m !== void 0);
|
|
87
|
+
}
|
|
88
|
+
streamAll(pageSize) {
|
|
89
|
+
return this.streamQuery(this.collection, pageSize);
|
|
90
|
+
}
|
|
91
|
+
async countAll() {
|
|
92
|
+
return (await this.collection.count().get()).data().count;
|
|
93
|
+
}
|
|
94
|
+
async count(query) {
|
|
95
|
+
return (await query.count().get()).data().count;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
//#endregion
|
|
100
|
+
export { FirestoreStore };
|
|
@@ -1,30 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
let _ddd_ts_core = require("@ddd-ts/core");
|
|
2
|
+
|
|
3
|
+
//#region src/firestore.transaction.ts
|
|
4
|
+
var FirestoreTransaction = class {
|
|
5
|
+
commitListeners = [];
|
|
6
|
+
constructor(transaction) {
|
|
7
|
+
this.transaction = transaction;
|
|
8
|
+
}
|
|
9
|
+
counter = -1;
|
|
10
|
+
increment() {
|
|
11
|
+
this.counter++;
|
|
12
|
+
return this.counter;
|
|
13
|
+
}
|
|
14
|
+
onCommit(callback) {
|
|
15
|
+
this.commitListeners.push(callback);
|
|
16
|
+
}
|
|
17
|
+
async executeCommitListeners() {
|
|
18
|
+
await Promise.all(this.commitListeners.map((cb) => cb()));
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var FirestoreTransactionPerformer = class extends _ddd_ts_core.TransactionPerformer {
|
|
22
|
+
constructor(db) {
|
|
23
|
+
super((effect) => db.runTransaction((trx) => effect(new FirestoreTransaction(trx))));
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
23
28
|
exports.FirestoreTransaction = FirestoreTransaction;
|
|
24
|
-
|
|
25
|
-
constructor(db) {
|
|
26
|
-
super((effect) => db.runTransaction((trx) => effect(new FirestoreTransaction(trx))));
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
exports.FirestoreTransactionPerformer = FirestoreTransactionPerformer;
|
|
30
|
-
//# sourceMappingURL=firestore.transaction.js.map
|
|
29
|
+
exports.FirestoreTransactionPerformer = FirestoreTransactionPerformer;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { TransactionPerformer } from "@ddd-ts/core";
|
|
2
|
+
|
|
3
|
+
//#region src/firestore.transaction.ts
|
|
4
|
+
var FirestoreTransaction = class {
|
|
5
|
+
commitListeners = [];
|
|
6
|
+
constructor(transaction) {
|
|
7
|
+
this.transaction = transaction;
|
|
8
|
+
}
|
|
9
|
+
counter = -1;
|
|
10
|
+
increment() {
|
|
11
|
+
this.counter++;
|
|
12
|
+
return this.counter;
|
|
13
|
+
}
|
|
14
|
+
onCommit(callback) {
|
|
15
|
+
this.commitListeners.push(callback);
|
|
16
|
+
}
|
|
17
|
+
async executeCommitListeners() {
|
|
18
|
+
await Promise.all(this.commitListeners.map((cb) => cb()));
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var FirestoreTransactionPerformer = class extends TransactionPerformer {
|
|
22
|
+
constructor(db) {
|
|
23
|
+
super((effect) => db.runTransaction((trx) => effect(new FirestoreTransaction(trx))));
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
export { FirestoreTransaction, FirestoreTransactionPerformer };
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
Object.defineProperty(exports, "DefaultConverter", { enumerable: true, get: function () { return converter_1.DefaultConverter; } });
|
|
11
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_firestore_transaction = require('./firestore.transaction.js');
|
|
3
|
+
const require_converter = require('./converter.js');
|
|
4
|
+
const require_firestore_store = require('./firestore.store.js');
|
|
5
|
+
|
|
6
|
+
exports.DefaultConverter = require_converter.DefaultConverter;
|
|
7
|
+
exports.FirestoreStore = require_firestore_store.FirestoreStore;
|
|
8
|
+
exports.FirestoreTransaction = require_firestore_transaction.FirestoreTransaction;
|
|
9
|
+
exports.FirestoreTransactionPerformer = require_firestore_transaction.FirestoreTransactionPerformer;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { FirestoreTransaction, FirestoreTransactionPerformer } from "./firestore.transaction.mjs";
|
|
2
|
+
import { DefaultConverter } from "./converter.mjs";
|
|
3
|
+
import { FirestoreStore } from "./firestore.store.mjs";
|
|
4
|
+
|
|
5
|
+
export { DefaultConverter, FirestoreStore, FirestoreTransaction, FirestoreTransactionPerformer };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
|
|
2
|
+
//#region ../shape/src/addons/microsecond-timestamp.ts
|
|
3
|
+
var MicrosecondTimestamp = class MicrosecondTimestamp {
|
|
4
|
+
static MILLISECOND = new MicrosecondTimestamp(BigInt(1e3));
|
|
5
|
+
static SECOND = this.MILLISECOND.mult(1e3);
|
|
6
|
+
static MINUTE = this.SECOND.mult(60);
|
|
7
|
+
static HOUR = this.MINUTE.mult(60);
|
|
8
|
+
static DAY = this.HOUR.mult(24);
|
|
9
|
+
static WEEK = this.DAY.mult(7);
|
|
10
|
+
static MONTH = this.DAY.mult(30);
|
|
11
|
+
constructor(micros) {
|
|
12
|
+
this.micros = micros;
|
|
13
|
+
}
|
|
14
|
+
isAfter(other) {
|
|
15
|
+
return this.micros > other.micros;
|
|
16
|
+
}
|
|
17
|
+
equals(other) {
|
|
18
|
+
return this.micros === other.micros;
|
|
19
|
+
}
|
|
20
|
+
isBefore(other) {
|
|
21
|
+
return this.micros < other.micros;
|
|
22
|
+
}
|
|
23
|
+
add(micros) {
|
|
24
|
+
const value = micros instanceof MicrosecondTimestamp ? micros.micros : micros;
|
|
25
|
+
return new MicrosecondTimestamp(this.micros + value);
|
|
26
|
+
}
|
|
27
|
+
sub(micros) {
|
|
28
|
+
const value = micros instanceof MicrosecondTimestamp ? micros.micros : micros;
|
|
29
|
+
return new MicrosecondTimestamp(this.micros - value);
|
|
30
|
+
}
|
|
31
|
+
mult(factor) {
|
|
32
|
+
return new MicrosecondTimestamp(this.micros * BigInt(factor));
|
|
33
|
+
}
|
|
34
|
+
static now() {
|
|
35
|
+
return new MicrosecondTimestamp(BigInt(Date.now()) * BigInt(1e3));
|
|
36
|
+
}
|
|
37
|
+
static fromNanoseconds(nanoseconds) {
|
|
38
|
+
return new MicrosecondTimestamp(nanoseconds / BigInt(1e3));
|
|
39
|
+
}
|
|
40
|
+
static fromMicroseconds(microseconds) {
|
|
41
|
+
return new MicrosecondTimestamp(microseconds);
|
|
42
|
+
}
|
|
43
|
+
static deserialize(serialized) {
|
|
44
|
+
if (serialized instanceof MicrosecondTimestamp) return serialized;
|
|
45
|
+
if (typeof serialized === "bigint") return new MicrosecondTimestamp(serialized);
|
|
46
|
+
if ("microseconds" in serialized && typeof serialized.microseconds === "bigint") return new MicrosecondTimestamp(serialized.microseconds);
|
|
47
|
+
return new MicrosecondTimestamp(BigInt(serialized.getTime()) * BigInt(1e3));
|
|
48
|
+
}
|
|
49
|
+
serialize() {
|
|
50
|
+
const date = new Date(Number(this.micros / BigInt(1e3)));
|
|
51
|
+
date.microseconds = this.micros;
|
|
52
|
+
return date;
|
|
53
|
+
}
|
|
54
|
+
static sort(left, right) {
|
|
55
|
+
if (left.isAfter(right)) return 1;
|
|
56
|
+
if (left.isBefore(right)) return -1;
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
exports.MicrosecondTimestamp = MicrosecondTimestamp;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
//#region ../shape/src/addons/microsecond-timestamp.ts
|
|
2
|
+
var MicrosecondTimestamp = class MicrosecondTimestamp {
|
|
3
|
+
static MILLISECOND = new MicrosecondTimestamp(BigInt(1e3));
|
|
4
|
+
static SECOND = this.MILLISECOND.mult(1e3);
|
|
5
|
+
static MINUTE = this.SECOND.mult(60);
|
|
6
|
+
static HOUR = this.MINUTE.mult(60);
|
|
7
|
+
static DAY = this.HOUR.mult(24);
|
|
8
|
+
static WEEK = this.DAY.mult(7);
|
|
9
|
+
static MONTH = this.DAY.mult(30);
|
|
10
|
+
constructor(micros) {
|
|
11
|
+
this.micros = micros;
|
|
12
|
+
}
|
|
13
|
+
isAfter(other) {
|
|
14
|
+
return this.micros > other.micros;
|
|
15
|
+
}
|
|
16
|
+
equals(other) {
|
|
17
|
+
return this.micros === other.micros;
|
|
18
|
+
}
|
|
19
|
+
isBefore(other) {
|
|
20
|
+
return this.micros < other.micros;
|
|
21
|
+
}
|
|
22
|
+
add(micros) {
|
|
23
|
+
const value = micros instanceof MicrosecondTimestamp ? micros.micros : micros;
|
|
24
|
+
return new MicrosecondTimestamp(this.micros + value);
|
|
25
|
+
}
|
|
26
|
+
sub(micros) {
|
|
27
|
+
const value = micros instanceof MicrosecondTimestamp ? micros.micros : micros;
|
|
28
|
+
return new MicrosecondTimestamp(this.micros - value);
|
|
29
|
+
}
|
|
30
|
+
mult(factor) {
|
|
31
|
+
return new MicrosecondTimestamp(this.micros * BigInt(factor));
|
|
32
|
+
}
|
|
33
|
+
static now() {
|
|
34
|
+
return new MicrosecondTimestamp(BigInt(Date.now()) * BigInt(1e3));
|
|
35
|
+
}
|
|
36
|
+
static fromNanoseconds(nanoseconds) {
|
|
37
|
+
return new MicrosecondTimestamp(nanoseconds / BigInt(1e3));
|
|
38
|
+
}
|
|
39
|
+
static fromMicroseconds(microseconds) {
|
|
40
|
+
return new MicrosecondTimestamp(microseconds);
|
|
41
|
+
}
|
|
42
|
+
static deserialize(serialized) {
|
|
43
|
+
if (serialized instanceof MicrosecondTimestamp) return serialized;
|
|
44
|
+
if (typeof serialized === "bigint") return new MicrosecondTimestamp(serialized);
|
|
45
|
+
if ("microseconds" in serialized && typeof serialized.microseconds === "bigint") return new MicrosecondTimestamp(serialized.microseconds);
|
|
46
|
+
return new MicrosecondTimestamp(BigInt(serialized.getTime()) * BigInt(1e3));
|
|
47
|
+
}
|
|
48
|
+
serialize() {
|
|
49
|
+
const date = new Date(Number(this.micros / BigInt(1e3)));
|
|
50
|
+
date.microseconds = this.micros;
|
|
51
|
+
return date;
|
|
52
|
+
}
|
|
53
|
+
static sort(left, right) {
|
|
54
|
+
if (left.isAfter(right)) return 1;
|
|
55
|
+
if (left.isBefore(right)) return -1;
|
|
56
|
+
return 0;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
export { MicrosecondTimestamp };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const require_microsecond_timestamp = require('./addons/microsecond-timestamp.js');
|
package/package.json
CHANGED
|
@@ -1,23 +1,34 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
2
|
+
"name": "@ddd-ts/store-firestore",
|
|
3
|
+
"version": "0.0.38",
|
|
4
|
+
"types": "dist/index.d.ts",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"url": "git+https://github.com/ddd-ts/monorepo"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@ddd-ts/core": "0.0.38",
|
|
14
|
+
"firebase-admin": "^13.2.0"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@ddd-ts/shape": "0.0.38",
|
|
18
|
+
"@ddd-ts/tools": "0.0.38",
|
|
19
|
+
"@ddd-ts/types": "0.0.38",
|
|
20
|
+
"@types/jest": "^29.5.1"
|
|
21
|
+
},
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"import": "./dist/index.mjs",
|
|
25
|
+
"require": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"main": "./dist/index.js",
|
|
30
|
+
"module": "./dist/index.mjs",
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsdown --config node_modules/@ddd-ts/tools/tsdown.config.js"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/dist/asyncTools.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"asyncTools.js","sourceRoot":"","sources":["../src/asyncTools.ts"],"names":[],"mappings":";;AAAA,0BA0BC;AAED,sBAaC;AAzCM,KAAK,SAAS,CAAC,CAAC,OAAO,CAAI,SAA6B;IAC7D,SAAS,OAAO,CAAC,aAA+B;QAC9C,OAAO,aAAa,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;YAC5C,aAAa;YACb,MAAM;SACP,CAAC,CAAC,CAAC;IACN,CAAC;IAED,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE;QACxD,MAAM,QAAQ,GAAG,QAAQ,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC;QAClD,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAA;IACtC,CAAC,CAAC,CAAC,CAAC;IAEJ,OAAO,cAAc,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC;QAC/B,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,cAAc,CAAC,MAAM,EAAE,CAAC,CAAC;QAE9E,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC;YAChB,IAAI,aAAa,CAAC,MAAM,EAAE,CAAC;gBACzB,aAAa,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YAC9C,CAAC;YACD,cAAc,CAAC,MAAM,CAAC,aAAa,CAAC,CAAA;QACtC,CAAC;aAAM,CAAC;YACN,cAAc,CAAC,GAAG,CAAC,aAAa,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC;YAC1D,MAAM,MAAM,CAAC,KAAK,CAAC;QACrB,CAAC;IACH,CAAC;AACH,CAAC;AAEM,KAAK,SAAS,CAAC,CAAC,KAAK,CAAI,QAA0B,EAAE,IAAY;IACtE,IAAI,KAAK,GAAG,EAAE,CAAC;IAEf,IAAI,KAAK,EAAE,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;QAClC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjB,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,EAAE,CAAC;YAC1B,MAAM,KAAK,CAAC;YACZ,KAAK,GAAG,EAAE,CAAC;QACb,CAAC;IACH,CAAC;IACD,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACrB,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["export async function* combine<T>(iterables: AsyncIterable<T>[]) {\n function getNext(asyncIterator: AsyncIterator<T>) {\n return asyncIterator.next().then((result) => ({\n asyncIterator,\n result,\n }));\n }\n\n const asyncIterators = new Map(iterables.map((iterable) => {\n const iterator = iterable[Symbol.asyncIterator]();\n return [iterator, getNext(iterator)]\n }));\n\n while (asyncIterators.size > 0) {\n const { asyncIterator, result } = await Promise.race(asyncIterators.values());\n\n if (result.done) {\n if (asyncIterator.return) {\n asyncIterator.return().catch(console.error);\n }\n asyncIterators.delete(asyncIterator)\n } else {\n asyncIterators.set(asyncIterator, getNext(asyncIterator));\n yield result.value;\n }\n }\n}\n\nexport async function* batch<T>(iterator: AsyncIterable<T>, size: number) {\n let batch = [];\n\n for await (const item of iterator) {\n batch.push(item);\n if (batch.length === size) {\n yield batch;\n batch = [];\n }\n }\n if (batch.length > 0) {\n yield batch;\n }\n}\n"]}
|
package/dist/asyncTools.spec.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const asyncTools_1 = require("./asyncTools");
|
|
4
|
-
describe("async tools", () => {
|
|
5
|
-
it("combines async iterators", async () => {
|
|
6
|
-
const ended = jest.fn();
|
|
7
|
-
async function* createAsyncIterator(count) {
|
|
8
|
-
for (let i = 0; i < count; i += 1) {
|
|
9
|
-
yield i;
|
|
10
|
-
}
|
|
11
|
-
ended();
|
|
12
|
-
}
|
|
13
|
-
const allNumbers = [];
|
|
14
|
-
for await (const nb of (0, asyncTools_1.combine)([
|
|
15
|
-
createAsyncIterator(1),
|
|
16
|
-
createAsyncIterator(2),
|
|
17
|
-
createAsyncIterator(3),
|
|
18
|
-
createAsyncIterator(4),
|
|
19
|
-
createAsyncIterator(5),
|
|
20
|
-
])) {
|
|
21
|
-
allNumbers.push(nb);
|
|
22
|
-
}
|
|
23
|
-
expect(allNumbers.sort()).toEqual([0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4]);
|
|
24
|
-
expect(ended).toHaveBeenCalledTimes(5);
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
//# sourceMappingURL=asyncTools.spec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"asyncTools.spec.js","sourceRoot":"","sources":["../src/asyncTools.spec.ts"],"names":[],"mappings":";;AAAA,6CAAuC;AAEvC,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IAC3B,EAAE,CAAC,0BAA0B,EAAE,KAAK,IAAI,EAAE;QACxC,MAAM,KAAK,GAAG,IAAI,CAAC,EAAE,EAAE,CAAA;QAEvB,KAAK,SAAS,CAAC,CAAC,mBAAmB,CAAC,KAAa;YAC/C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;gBAClC,MAAM,CAAC,CAAC;YACV,CAAC;YACD,KAAK,EAAE,CAAC;QACV,CAAC;QAED,MAAM,UAAU,GAAa,EAAE,CAAC;QAEhC,IAAI,KAAK,EAAE,MAAM,EAAE,IAAI,IAAA,oBAAO,EAAC;YAC7B,mBAAmB,CAAC,CAAC,CAAC;YACtB,mBAAmB,CAAC,CAAC,CAAC;YACtB,mBAAmB,CAAC,CAAC,CAAC;YACtB,mBAAmB,CAAC,CAAC,CAAC;YACtB,mBAAmB,CAAC,CAAC,CAAC;SACvB,CAAC,EAAE,CAAC;YACH,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACtB,CAAC;QAED,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QACjF,MAAM,CAAC,KAAK,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { combine } from \"./asyncTools\";\n\ndescribe(\"async tools\", () => {\n it(\"combines async iterators\", async () => {\n const ended = jest.fn()\n\n async function* createAsyncIterator(count: number) {\n for (let i = 0; i < count; i += 1) {\n yield i;\n }\n ended();\n }\n\n const allNumbers: number[] = [];\n\n for await (const nb of combine([\n createAsyncIterator(1),\n createAsyncIterator(2),\n createAsyncIterator(3),\n createAsyncIterator(4),\n createAsyncIterator(5),\n ])) {\n allNumbers.push(nb);\n }\n\n expect(allNumbers.sort()).toEqual([0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4]);\n expect(ended).toHaveBeenCalledTimes(5);\n });\n});\n"]}
|
package/dist/converter.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"converter.js","sourceRoot":"","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AC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QACvB,OAAO,GAAG,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC;IAC3C,CAAC;IACD,IAAI,GAAG,YAAY,MAAM,EAAE,CAAC;QAC1B,OAAO,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAC/B,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;YACpB,GAAG,CAAC,GAAG,CAAC,GAAG,wBAAwB,CAAC,KAAK,CAAC,CAAC;YAC3C,OAAO,GAAG,CAAC;QACb,CAAC,EACD,EAA4B,CAC7B,CAAC;IACJ,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["import { MicrosecondTimestamp } from \"@ddd-ts/shape\";\nimport {\n FieldValue,\n FirestoreDataConverter,\n Timestamp,\n} from \"firebase-admin/firestore\";\n\nexport class DefaultConverter<T extends FirebaseFirestore.DocumentData>\n implements FirestoreDataConverter<T>\n{\n public toFirestore(modelObject: T): FirebaseFirestore.DocumentData {\n return toFirestore(modelObject) as FirebaseFirestore.DocumentData;\n }\n\n public fromFirestore(snapshot: FirebaseFirestore.QueryDocumentSnapshot): T {\n return decodeFirebaseTimestamps(snapshot.data()) as T;\n }\n\n public fromFirestoreSnapshot(\n snapshot: FirebaseFirestore.DocumentSnapshot,\n ): T | undefined {\n const data = snapshot.data();\n if (!data) {\n return undefined;\n }\n return decodeFirebaseTimestamps(data) as T;\n }\n\n public toFirestorePartial(\n modelObject: Partial<T> | FirebaseFirestore.UpdateData<T>,\n ): FirebaseFirestore.UpdateData<T> {\n return toFirestore(modelObject) as any;\n }\n}\n\nexport function toFirestore<T>(obj: T): T {\n if (obj instanceof Date) {\n if (\n \"microseconds\" in obj &&\n typeof (obj as any).microseconds === \"bigint\"\n ) {\n const microseconds = (obj as any).microseconds as bigint;\n const seconds = microseconds / 1_000_000n;\n const micros = microseconds % 1_000_000n;\n const nanoseconds = micros * 1000n; // Convert to nanoseconds\n return new Timestamp(\n Number(seconds),\n Number(nanoseconds),\n ) as unknown as T;\n }\n return obj;\n }\n if (obj instanceof FieldValue || obj?.constructor?.name === \"VectorValue\") {\n return obj;\n }\n if (Array.isArray(obj)) {\n return obj.map(toFirestore) as unknown as T;\n }\n if (obj instanceof Object) {\n return Object.entries(obj).reduce(\n (map, [key, value]) => {\n if (value !== undefined) {\n map[key] = toFirestore(value);\n }\n return map;\n },\n {} as { [key: string]: any },\n ) as T;\n }\n return obj;\n}\n\n/**\n * decode Firebase timestamps into Dates\n */\nexport function decodeFirebaseTimestamps(\n obj: FirebaseFirestore.DocumentData,\n): FirebaseFirestore.DocumentData {\n if (obj instanceof Date) {\n return obj;\n }\n if (obj?.toDate) {\n const result = obj.toDate();\n\n if (obj?.seconds !== undefined && obj?.nanoseconds !== undefined) {\n const microseconds =\n BigInt(obj.seconds) * BigInt(1_000_000) +\n BigInt(obj.nanoseconds) / BigInt(1_000);\n (result as any).microseconds = microseconds; // Attach microseconds to the date\n return result;\n }\n return result;\n }\n if (obj?.constructor?.name === \"VectorValue\") {\n return obj;\n }\n if (Array.isArray(obj)) {\n return obj.map(decodeFirebaseTimestamps);\n }\n if (obj instanceof Object) {\n return Object.entries(obj).reduce(\n (map, [key, value]) => {\n map[key] = decodeFirebaseTimestamps(value);\n return map;\n },\n {} as { [key: string]: any },\n );\n }\n return obj;\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.store.js","sourceRoot":"","sources":["../src/firestore.store.ts"],"names":[],"mappings":";;;AAeA,6CAAqC;AACrC,2CAA+C;AAE/C,MAAa,cAAc;IAOP;IACA;IACA;IAJX,gBAAgB,GAAG,IAAI,4BAAgB,EAAE,CAAC;IACjD,YACkB,WAAgC,EAChC,UAAa,EACb,KAAc;QAFd,gBAAW,GAAX,WAAW,CAAqB;QAChC,eAAU,GAAV,UAAU,CAAG;QACb,UAAK,GAA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AAC,CAAC,CAAC;QACtE,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,SAAS,CAAQ,CAAC;IACtD,CAAC;IAED,SAAS,CAAC,QAAiB;QACzB,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;IACrD,CAAC;IAED,KAAK,CAAC,QAAQ;QACZ,OAAO,CAAC,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;IAC5D,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,KAA4C;QACtD,OAAO,CAAC,MAAM,KAAK,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;IAClD,CAAC;CACF;AAlKD,wCAkKC","sourcesContent":["import {\n CollectionReference,\n DocumentData,\n QueryDocumentSnapshot,\n DocumentSnapshot,\n} from \"firebase-admin/firestore\";\n\nimport {\n Store,\n ISerializer,\n type IIdentifiable,\n Serialized,\n} from \"@ddd-ts/core\";\n\nimport { FirestoreTransaction } from \"./firestore.transaction\";\nimport { batch } from \"./asyncTools\";\nimport { DefaultConverter } from \"./converter\";\n\nexport class FirestoreStore<\n M extends IIdentifiable,\n S extends ISerializer<M> = ISerializer<M>,\n> implements Store<M>\n{\n public defaultConverter = new DefaultConverter();\n constructor(\n public readonly _collection: CollectionReference,\n public readonly serializer: S,\n public readonly $name?: string,\n ) {}\n\n get firestore() {\n return this._collection.firestore;\n }\n\n get collection() {\n return this._collection.withConverter(\n this.defaultConverter,\n ) as CollectionReference<Serialized<S>, Serialized<S>>;\n }\n\n async executeQuery(\n query: FirebaseFirestore.Query<any>,\n trx?: FirestoreTransaction,\n ): Promise<M[]> {\n const result = trx ? await trx.transaction.get(query) : await query.get();\n\n return Promise.all(\n result.docs.map((doc) =>\n this.serializer.deserialize({\n ...(this.$name ? { $name: this.$name } : {}),\n id: doc.id,\n ...doc.data(),\n }),\n ),\n );\n }\n\n async *streamPages(query: FirebaseFirestore.Query<any>, pageSize: number) {\n let last: DocumentSnapshot | undefined;\n let nextPagePromise:\n | Promise<FirebaseFirestore.QuerySnapshot<any>>\n | undefined;\n\n function getNextPagePromise(cursor: DocumentSnapshot | undefined) {\n return cursor\n ? query.limit(pageSize).startAfter(cursor).get()\n : query.limit(pageSize).get();\n }\n\n do {\n const paginatedQuery = nextPagePromise ?? getNextPagePromise(last);\n const { docs } = await paginatedQuery;\n last = docs[pageSize - 1];\n if (last) {\n nextPagePromise = getNextPagePromise(last);\n }\n for (const doc of docs) {\n yield doc;\n }\n } while (last);\n }\n\n async *streamQuery(\n query: FirebaseFirestore.Query<any>,\n pageSize?: number,\n ): AsyncIterable<M> {\n const finalPageSize = pageSize ?? 50;\n const stream =\n finalPageSize === 1\n ? (query.stream() as AsyncIterable<QueryDocumentSnapshot>)\n : this.streamPages(query, finalPageSize);\n for await (const docs of batch(stream, finalPageSize)) {\n const deserializedDocs = await Promise.all(\n docs.map((doc) =>\n this.serializer.deserialize({\n ...(this.$name ? { $name: this.$name } : {}),\n id: doc.id,\n ...(doc.data() as any),\n }),\n ),\n );\n for (const deserializedDoc of deserializedDocs) {\n yield deserializedDoc;\n }\n }\n }\n\n async save(model: M, trx?: FirestoreTransaction): Promise<void> {\n const serialized = await this.serializer.serialize(model);\n\n const ref = this.collection.doc(model.id.serialize());\n\n trx\n ? trx.transaction.set(ref, serialized)\n : await ref.set(serialized as any);\n }\n\n async saveAll(models: M[], trx?: FirestoreTransaction): Promise<void> {\n await Promise.all(models.map((m) => this.save(m, trx)));\n }\n\n async load(id: M[\"id\"], trx?: FirestoreTransaction): Promise<M | undefined> {\n const ref = this.collection.doc(id.serialize());\n\n const snapshot = trx ? await trx.transaction.get(ref) : await ref.get();\n\n if (!snapshot.exists) {\n return undefined;\n }\n\n return this.serializer.deserialize({\n ...(this.$name ? { $name: this.$name } : {}),\n id: id.serialize(),\n ...snapshot.data(),\n });\n }\n\n async loadAll(transaction?: FirestoreTransaction): Promise<M[]> {\n let docs: DocumentSnapshot[];\n\n if (transaction) {\n docs = await transaction.transaction.getAll();\n } else {\n ({ docs } = await this.collection.get());\n }\n return Promise.all(\n docs.map((doc) =>\n this.serializer.deserialize({\n ...(this.$name ? { $name: this.$name } : {}),\n id: doc.id,\n ...doc.data(),\n }),\n ),\n );\n }\n\n async delete(id: M[\"id\"], trx?: FirestoreTransaction): Promise<void> {\n if (trx) {\n trx.transaction.delete(this.collection.doc(id.serialize()));\n } else {\n await this.collection.doc(id.serialize()).delete();\n }\n }\n\n async loadMany(ids: M[\"id\"][], trx?: FirestoreTransaction): Promise<M[]> {\n const result = await Promise.all(ids.map((id) => this.load(id, trx)));\n return result.filter((m) => m !== undefined) as M[];\n }\n\n streamAll(pageSize?: number): AsyncIterable<M> {\n return this.streamQuery(this.collection, pageSize);\n }\n\n async countAll() {\n return (await this.collection.count().get()).data().count;\n }\n\n async count(query: FirebaseFirestore.Query<DocumentData>) {\n return (await query.count().get()).data().count;\n }\n}\n"]}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
process.env.FIRESTORE_EMULATOR_HOST = "localhost:8080";
|
|
37
|
-
const fb = __importStar(require("firebase-admin"));
|
|
38
|
-
const shape_1 = require("@ddd-ts/shape");
|
|
39
|
-
const firestore_store_1 = require("./firestore.store");
|
|
40
|
-
const core_1 = require("@ddd-ts/core");
|
|
41
|
-
describe("FirestoreStore", () => {
|
|
42
|
-
const app = fb.initializeApp({ projectId: "demo-es" });
|
|
43
|
-
const database = app.firestore();
|
|
44
|
-
class AccountId extends (0, shape_1.Primitive)(String) {
|
|
45
|
-
}
|
|
46
|
-
class Account extends (0, shape_1.Shape)({
|
|
47
|
-
id: AccountId,
|
|
48
|
-
balance: Number,
|
|
49
|
-
timestamp: (0, shape_1.Optional)(shape_1.MicrosecondTimestamp),
|
|
50
|
-
}) {
|
|
51
|
-
}
|
|
52
|
-
class AccountSerializer extends core_1.AutoSerializer.First(Account) {
|
|
53
|
-
}
|
|
54
|
-
class FirestoreAccountStore extends firestore_store_1.FirestoreStore {
|
|
55
|
-
constructor(database) {
|
|
56
|
-
super(database.collection("accounts"), new AccountSerializer());
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
const firestoreAccountStore = new FirestoreAccountStore(database);
|
|
60
|
-
it("saves and retrieves an account", async () => {
|
|
61
|
-
const account = new Account({
|
|
62
|
-
id: new AccountId("123"),
|
|
63
|
-
balance: 100,
|
|
64
|
-
timestamp: undefined,
|
|
65
|
-
});
|
|
66
|
-
await firestoreAccountStore.save(account);
|
|
67
|
-
const retrievedAccount = await firestoreAccountStore.load(account.id);
|
|
68
|
-
expect(retrievedAccount).toEqual(account);
|
|
69
|
-
});
|
|
70
|
-
it("loads all accounts", async () => {
|
|
71
|
-
const accounts = await firestoreAccountStore.loadAll();
|
|
72
|
-
expect(accounts).toBeInstanceOf(Array);
|
|
73
|
-
expect(accounts.length).toBeGreaterThan(0);
|
|
74
|
-
});
|
|
75
|
-
it("works with microsecond timestamps", async () => {
|
|
76
|
-
const timestamp = shape_1.MicrosecondTimestamp.now().add(123n);
|
|
77
|
-
const account = new Account({
|
|
78
|
-
id: new AccountId("456"),
|
|
79
|
-
balance: 200,
|
|
80
|
-
timestamp: timestamp,
|
|
81
|
-
});
|
|
82
|
-
await firestoreAccountStore.save(account);
|
|
83
|
-
const retrievedAccount = await firestoreAccountStore.load(account.id);
|
|
84
|
-
expect(retrievedAccount).toEqual(account);
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
//# sourceMappingURL=firestore.store.spec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.store.spec.js","sourceRoot":"","sources":["../src/firestore.store.spec.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,CAAC,GAAG,CAAC,uBAAuB,GAAG,gBAAgB,CAAC;AAEvD,mDAAqC;AACrC,yCAKuB;AACvB,uDAAmD;AACnD,uCAA8C;AAE9C,QAAQ,CAAC,gB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M,qBAAqB,CAAC,OAAO,EAAE,CAAC;QACvD,MAAM,CAAC,QAAQ,CAAC,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QACvC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;IAC7C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,mCAAmC,EAAE,KAAK,IAAI,EAAE;QACjD,MAAM,SAAS,GAAG,4BAAoB,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAEvD,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC;YAC1B,EAAE,EAAE,IAAI,SAAS,CAAC,KAAK,CAAC;YACxB,OAAO,EAAE,GAAG;YACZ,SAAS,EAAE,SAAS;SACrB,CAAC,CAAC;QAEH,MAAM,qBAAqB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAE1C,MAAM,gBAAgB,GAAG,MAAM,qBAAqB,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACtE,MAAM,CAAC,gBAAgB,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAC5C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["process.env.FIRESTORE_EMULATOR_HOST = \"localhost:8080\";\n\nimport * as fb from \"firebase-admin\";\nimport {\n Primitive,\n Shape,\n MicrosecondTimestamp,\n Optional,\n} from \"@ddd-ts/shape\";\nimport { FirestoreStore } from \"./firestore.store\";\nimport { AutoSerializer } from \"@ddd-ts/core\";\n\ndescribe(\"FirestoreStore\", () => {\n const app = fb.initializeApp({ projectId: \"demo-es\" });\n const database = app.firestore();\n\n class AccountId extends Primitive(String) {}\n class Account extends Shape({\n id: AccountId,\n balance: Number,\n timestamp: Optional(MicrosecondTimestamp),\n }) {}\n\n class AccountSerializer extends AutoSerializer.First(Account) {}\n\n class FirestoreAccountStore extends FirestoreStore<Account> {\n constructor(database: fb.firestore.Firestore) {\n super(database.collection(\"accounts\"), new AccountSerializer());\n }\n }\n\n const firestoreAccountStore = new FirestoreAccountStore(database);\n\n it(\"saves and retrieves an account\", async () => {\n const account = new Account({\n id: new AccountId(\"123\"),\n balance: 100,\n timestamp: undefined,\n });\n await firestoreAccountStore.save(account);\n\n const retrievedAccount = await firestoreAccountStore.load(account.id);\n expect(retrievedAccount).toEqual(account);\n });\n\n it(\"loads all accounts\", async () => {\n const accounts = await firestoreAccountStore.loadAll();\n expect(accounts).toBeInstanceOf(Array);\n expect(accounts.length).toBeGreaterThan(0);\n });\n\n it(\"works with microsecond timestamps\", async () => {\n const timestamp = MicrosecondTimestamp.now().add(123n);\n\n const account = new Account({\n id: new AccountId(\"456\"),\n balance: 200,\n timestamp: timestamp,\n });\n\n await firestoreAccountStore.save(account);\n\n const retrievedAccount = await firestoreAccountStore.load(account.id);\n expect(retrievedAccount).toEqual(account);\n });\n});\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.transaction.js","sourceRoot":"","sources":["../src/firestore.transaction.ts"],"names":[],"mappings":";;;AAIA,uCAIsB;AAEtB,MAAa,oBAAoB;IAEH;IAD5B,eAAe,GAAqB,EAAE,CAAC;IACvC,YAA4B,WAAgC;QAAhC,gBAAW,GAAX,WAAW,CAAqB;IAAG,CAAC;IAEhE,OAAO,GAAG,CAAC,CAAC,CAAC;IAEb,SAAS;QACP,IAAI,CAAC,OAAO,EAAE,CAAC;QACf,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,QAAQ,CAAC,QAAwB;QAC/B,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACtC,CAAC;IAED,KAAK,CAAC,sBAAsB;QAC1B,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IAC5D,CAAC;CACF;AAlBD,oDAkBC;AAED,MAAa,6BAA8B,SAAQ,2BAA0C;IAC3F,YAAY,EAAa;QACvB,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CACf,EAAE,CAAC,cAAc,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,oBAAoB,CAAC,GAAG,CAAC,CAAC,CAAC,CAClE,CAAC;IACJ,CAAC;CACF;AAND,sEAMC","sourcesContent":["import type {\n Firestore,\n Transaction as FirebaseTransaction,\n} from \"firebase-admin/firestore\";\nimport {\n TransactionPerformer,\n type Transaction,\n type CommitListener,\n} from \"@ddd-ts/core\";\n\nexport class FirestoreTransaction implements Transaction {\n commitListeners: CommitListener[] = [];\n constructor(public readonly transaction: FirebaseTransaction) {}\n\n counter = -1;\n\n increment() {\n this.counter++;\n return this.counter;\n }\n\n onCommit(callback: CommitListener) {\n this.commitListeners.push(callback);\n }\n\n async executeCommitListeners() {\n await Promise.all(this.commitListeners.map((cb) => cb()));\n }\n}\n\nexport class FirestoreTransactionPerformer extends TransactionPerformer<FirestoreTransaction> {\n constructor(db: Firestore) {\n super((effect) =>\n db.runTransaction((trx) => effect(new FirestoreTransaction(trx))),\n );\n }\n}\n"]}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qDAAmD;AAA1C,iHAAA,cAAc,OAAA;AACvB,iEAGiC;AAF/B,sIAAA,6BAA6B,OAAA;AAC7B,6HAAA,oBAAoB,OAAA;AAEtB,yCAA+C;AAAtC,6GAAA,gBAAgB,OAAA","sourcesContent":["export { FirestoreStore } from \"./firestore.store\";\nexport {\n FirestoreTransactionPerformer,\n FirestoreTransaction,\n} from \"./firestore.transaction\";\nexport { DefaultConverter } from \"./converter\";\n"]}
|