@ddd-ts/store-firestore 0.0.3-5 → 0.0.3-50
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.store.d.ts +17 -13
- package/dist/firestore.store.d.ts.map +1 -1
- package/dist/firestore.store.js +60 -13
- package/dist/firestore.store.js.map +1 -1
- package/dist/firestore.store.spec.d.ts +2 -0
- package/dist/firestore.store.spec.d.ts.map +1 -0
- package/dist/firestore.store.spec.js +62 -0
- package/dist/firestore.store.spec.js.map +1 -0
- package/dist/firestore.transaction.d.ts +14 -0
- package/dist/firestore.transaction.d.ts.map +1 -0
- package/dist/firestore.transaction.js +25 -0
- package/dist/firestore.transaction.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/package.json +17 -18
- package/dist/firebase.transaction.d.ts +0 -6
- package/dist/firebase.transaction.d.ts.map +0 -1
- package/dist/firebase.transaction.js +0 -11
- package/dist/firebase.transaction.js.map +0 -1
|
@@ -1,19 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CollectionReference, Firestore, FirestoreDataConverter, DocumentData
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { Store, Model } from "@ddd-ts/model";
|
|
2
|
+
import { CollectionReference, Firestore, FirestoreDataConverter, DocumentData } from "firebase-admin/firestore";
|
|
3
|
+
import { FirestoreTransaction } from "./firestore.transaction";
|
|
4
|
+
import { ISerializer } from "@ddd-ts/serialization";
|
|
5
|
+
export declare class FirestoreStore<M extends Model> implements Store<M> {
|
|
6
6
|
readonly collectionName: string;
|
|
7
7
|
readonly firestore: Firestore;
|
|
8
|
-
readonly serializer:
|
|
8
|
+
readonly serializer: ISerializer<M>;
|
|
9
9
|
readonly converter?: FirestoreDataConverter<DocumentData> | undefined;
|
|
10
10
|
collection: CollectionReference;
|
|
11
|
-
constructor(collectionName: string, firestore: Firestore, serializer:
|
|
12
|
-
|
|
13
|
-
protected
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
constructor(collectionName: string, firestore: Firestore, serializer: ISerializer<M>, converter?: FirestoreDataConverter<DocumentData> | undefined);
|
|
12
|
+
private getIdFromModel;
|
|
13
|
+
protected executeQuery(query: FirebaseFirestore.Query<any>, trx?: FirestoreTransaction): Promise<M[]>;
|
|
14
|
+
private streamPages;
|
|
15
|
+
protected streamQuery(query: FirebaseFirestore.Query<any>, pageSize?: number): AsyncIterable<M>;
|
|
16
|
+
save(model: M, trx?: FirestoreTransaction): Promise<void>;
|
|
17
|
+
load(id: M["id"], trx?: FirestoreTransaction): Promise<M | undefined>;
|
|
18
|
+
loadAll(transaction?: FirestoreTransaction): Promise<M[]>;
|
|
19
|
+
delete(id: M["id"], trx?: FirestoreTransaction): Promise<void>;
|
|
20
|
+
loadMany(ids: M["id"][], trx?: FirestoreTransaction): Promise<M[]>;
|
|
21
|
+
streamAll(pageSize?: number): AsyncIterable<M>;
|
|
18
22
|
}
|
|
19
23
|
//# sourceMappingURL=firestore.store.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.store.d.ts","sourceRoot":"","sources":["../src/firestore.store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"firestore.store.d.ts","sourceRoot":"","sources":["../src/firestore.store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EACL,mBAAmB,EACnB,SAAS,EACT,sBAAsB,EACtB,YAAY,EAGb,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEpD,qBAAa,cAAc,CAAC,CAAC,SAAS,KAAK,CAAE,YAAW,KAAK,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
package/dist/firestore.store.js
CHANGED
|
@@ -21,40 +21,87 @@ class FirestoreStore {
|
|
|
21
21
|
this.collection = this.firestore.collection(collectionName);
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
getIdFromModel(m) {
|
|
25
|
+
if (Object.getOwnPropertyNames(m.id).includes("serialize")) {
|
|
26
|
+
if ("serialize" in m.id) {
|
|
27
|
+
return m.id.serialize();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return m.id.toString();
|
|
31
|
+
}
|
|
32
|
+
async executeQuery(query, trx) {
|
|
33
|
+
const result = trx ? await trx.transaction.get(query) : await query.get();
|
|
34
|
+
return Promise.all(result.docs.map((doc) => this.serializer.deserialize({ id: doc.id, ...doc.data() })));
|
|
26
35
|
}
|
|
27
|
-
async *
|
|
28
|
-
|
|
36
|
+
async *streamPages(query, pageSize) {
|
|
37
|
+
let last;
|
|
38
|
+
let nextPagePromise;
|
|
39
|
+
function getNextPagePromise(cursor) {
|
|
40
|
+
return cursor
|
|
41
|
+
? query.limit(pageSize).startAfter(cursor).get()
|
|
42
|
+
: query.limit(pageSize).get();
|
|
43
|
+
}
|
|
44
|
+
do {
|
|
45
|
+
const paginatedQuery = nextPagePromise ?? getNextPagePromise(last);
|
|
46
|
+
const { docs } = await paginatedQuery;
|
|
47
|
+
last = docs[pageSize - 1];
|
|
48
|
+
if (last) {
|
|
49
|
+
nextPagePromise = getNextPagePromise(last);
|
|
50
|
+
}
|
|
51
|
+
for (const doc of docs) {
|
|
52
|
+
yield doc;
|
|
53
|
+
}
|
|
54
|
+
} while (last);
|
|
55
|
+
}
|
|
56
|
+
async *streamQuery(query, pageSize) {
|
|
57
|
+
const stream = pageSize === 1
|
|
58
|
+
? query.stream()
|
|
59
|
+
: this.streamPages(query, pageSize ?? 50);
|
|
29
60
|
for await (const doc of stream) {
|
|
30
|
-
yield this.serializer.deserialize(doc.data());
|
|
61
|
+
yield this.serializer.deserialize({ id: doc.id, ...doc.data() });
|
|
31
62
|
}
|
|
32
63
|
}
|
|
33
64
|
async save(model, trx) {
|
|
34
65
|
const serialized = await this.serializer.serialize(model);
|
|
35
|
-
const ref = this.collection.doc(this.
|
|
36
|
-
trx ? trx.set(ref, serialized) : await ref.set(serialized);
|
|
66
|
+
const ref = this.collection.doc(this.getIdFromModel(model));
|
|
67
|
+
trx ? trx.transaction.set(ref, serialized) : await ref.set(serialized);
|
|
37
68
|
}
|
|
38
69
|
async load(id, trx) {
|
|
39
70
|
const ref = this.collection.doc(id.toString());
|
|
40
|
-
const snapshot = trx ? await trx.get(ref) : await ref.get();
|
|
71
|
+
const snapshot = trx ? await trx.transaction.get(ref) : await ref.get();
|
|
41
72
|
if (!snapshot.exists) {
|
|
42
73
|
return undefined;
|
|
43
74
|
}
|
|
44
|
-
return this.serializer.deserialize(
|
|
75
|
+
return this.serializer.deserialize({
|
|
76
|
+
id: id.toString(),
|
|
77
|
+
...snapshot.data(),
|
|
78
|
+
});
|
|
45
79
|
}
|
|
46
|
-
async loadAll() {
|
|
47
|
-
|
|
48
|
-
|
|
80
|
+
async loadAll(transaction) {
|
|
81
|
+
let docs;
|
|
82
|
+
if (transaction) {
|
|
83
|
+
docs = await transaction.transaction.getAll();
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
({ docs } = await this.collection.get());
|
|
87
|
+
}
|
|
88
|
+
return Promise.all(docs.map((doc) => this.serializer.deserialize({ id: doc.id, ...doc.data() })));
|
|
49
89
|
}
|
|
50
90
|
async delete(id, trx) {
|
|
51
91
|
if (trx) {
|
|
52
|
-
trx.delete(this.collection.doc(id.toString()));
|
|
92
|
+
trx.transaction.delete(this.collection.doc(id.toString()));
|
|
53
93
|
}
|
|
54
94
|
else {
|
|
55
95
|
await this.collection.doc(id.toString()).delete();
|
|
56
96
|
}
|
|
57
97
|
}
|
|
98
|
+
async loadMany(ids, trx) {
|
|
99
|
+
const result = await Promise.all(ids.map((id) => this.load(id, trx)));
|
|
100
|
+
return result.filter((m) => m !== undefined);
|
|
101
|
+
}
|
|
102
|
+
streamAll(pageSize) {
|
|
103
|
+
return this.streamQuery(this.collection, pageSize);
|
|
104
|
+
}
|
|
58
105
|
}
|
|
59
106
|
exports.FirestoreStore = FirestoreStore;
|
|
60
107
|
//# sourceMappingURL=firestore.store.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.store.js","sourceRoot":"","sources":["../src/firestore.store.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"firestore.store.js","sourceRoot":"","sources":["../src/firestore.store.ts"],"names":[],"mappings":";;;AAYA,MAAa,cAAc;IAIP;IACA;IACA;IACA;IANlB,UAAU,CAAsB;IAEhC,YACkB,cAAsB,EACtB,SAAoB,EACpB,UAA0B,EAC1B,SAAgD;QAHhD,mBAAc,GAAd,cAAc,CAAQ;QACtB,cAAS,GAAT,SAAS,CAAW;QACpB,eAAU,GAAV,UAAU,CAAgB;QAC1B,cAAS,GAAT,SAAS,CAAuC;QAEhE,IAAI,IAAI,CAAC,SAAS,EAAE;YAClB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,SAAS;iBAC7B,UAAU,CAAC,cAAc,CAAC;iBAC1B,aAAa,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;SAClC;aAAM;YACL,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,cAAc,CAAC,CAAC;SAC7D;IACH,CAAC;IAEO,cAAc,CAAC,CAAI;QACzB,IAAI,MAAM,CAAC,mBAAmB,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE;YAC1D,IAAI,WAAW,IAAI,CAAC,CAAC,EAAE,EAAE;gBACvB,OAAO,CAAC,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC;aACzB;SACF;QACD,OAAO,CAAC,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC;IACzB,CAAC;IAES,KAAK,CA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AC,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;CACF;AAtID,wCAsIC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"firestore.store.spec.d.ts","sourceRoot":"","sources":["../src/firestore.store.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,62 @@
|
|
|
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 (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
process.env.FIRESTORE_EMULATOR_HOST = "localhost:8080";
|
|
27
|
+
const fb = __importStar(require("firebase-admin"));
|
|
28
|
+
const tests_1 = require("@ddd-ts/tests");
|
|
29
|
+
const _1 = require(".");
|
|
30
|
+
class MyElementStore extends _1.FirestoreStore {
|
|
31
|
+
constructor(database) {
|
|
32
|
+
super("my_collection", database, new tests_1.MyElementSerializer());
|
|
33
|
+
}
|
|
34
|
+
loadEven() {
|
|
35
|
+
return this.executeQuery(this.collection.where("even", "==", true));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
describe("FirestoreStore", () => {
|
|
39
|
+
const app = fb.initializeApp({ projectId: "demo-es" });
|
|
40
|
+
const firestore = app.firestore();
|
|
41
|
+
function getStore() {
|
|
42
|
+
return {
|
|
43
|
+
store: new MyElementStore(firestore),
|
|
44
|
+
transactionPerformer: new _1.FirestoreTransactionPerformer(firestore),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
(0, tests_1.StoreSuite)(getStore);
|
|
48
|
+
it("streams all the database 10 by 10", async () => {
|
|
49
|
+
const { store } = getStore();
|
|
50
|
+
const elements = [...Array.from({ length: 100 }).keys()].map((_, index) => new tests_1.MyElement(index.toString(), `name-${index.toString()}`, index % 2 === 0));
|
|
51
|
+
await Promise.all(elements.map((e) => store.save(e)));
|
|
52
|
+
const streamed = [];
|
|
53
|
+
for await (const element of store.streamAll(10)) {
|
|
54
|
+
streamed.push(element.name);
|
|
55
|
+
}
|
|
56
|
+
expect(streamed.length).toBe(100);
|
|
57
|
+
expect(streamed.sort()).toEqual([...Array.from({ length: 100 }).keys()]
|
|
58
|
+
.map((_, index) => `name-${index.toString()}`)
|
|
59
|
+
.sort());
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
//# sourceMappingURL=firestore.store.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"firestore.store.spec.js","sourceRoot":"","sources":["../src/firestore.store.spec.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO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,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACtD,MAAM,QAAQ,GAAa,EAAE,CAAC;QAC9B,IAAI,KAAK,EAAE,MAAM,OAAO,IAAI,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC,EAAE;YAC/C,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;SAC7B;QACD,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,OAAO,CAC7B,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;aACpC,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,QAAQ,KAAK,CAAC,QAAQ,EAAE,EAAE,CAAC;aAC7C,IAAI,EAAE,CACV,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Firestore } from "firebase-admin/firestore";
|
|
2
|
+
import { TransactionPerformer } from "@ddd-ts/model";
|
|
3
|
+
import { CommitListener } from "@ddd-ts/model/dist/model/transaction";
|
|
4
|
+
export declare class FirestoreTransaction {
|
|
5
|
+
readonly transaction: FirebaseFirestore.Transaction;
|
|
6
|
+
commitListeners: CommitListener[];
|
|
7
|
+
constructor(transaction: FirebaseFirestore.Transaction);
|
|
8
|
+
onCommit(callback: CommitListener): void;
|
|
9
|
+
executeCommitListeners(): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export declare class FirestoreTransactionPerformer extends TransactionPerformer<FirestoreTransaction> {
|
|
12
|
+
constructor(db: Firestore);
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=firestore.transaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"firestore.transaction.d.ts","sourceRoot":"","sources":["../src/firestore.transaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,sCAAsC,CAAC;AAGtE,qBAAa,oBAAoB;aAEH,WAAW,EAAE,iBAAiB,CAAC,WAAW;IADtE,eAAe,EAAE,cAAc,EAAE,CAAM;gBACX,WAAW,EAAE,iBAAiB,CAAC,WAAW;IAEtE,QAAQ,CAAC,QAAQ,EAAE,cAAc;IAI3B,sBAAsB;CAG7B;AAED,qBAAa,6BAA8B,SAAQ,oBAAoB,CAAC,oBAAoB,CAAC;gBAC/E,EAAE,EAAE,SAAS;CAG1B"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FirestoreTransactionPerformer = exports.FirestoreTransaction = void 0;
|
|
4
|
+
const model_1 = require("@ddd-ts/model");
|
|
5
|
+
class FirestoreTransaction {
|
|
6
|
+
transaction;
|
|
7
|
+
commitListeners = [];
|
|
8
|
+
constructor(transaction) {
|
|
9
|
+
this.transaction = transaction;
|
|
10
|
+
}
|
|
11
|
+
onCommit(callback) {
|
|
12
|
+
this.commitListeners.push(callback);
|
|
13
|
+
}
|
|
14
|
+
async executeCommitListeners() {
|
|
15
|
+
await Promise.all(this.commitListeners.map((cb) => cb()));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.FirestoreTransaction = FirestoreTransaction;
|
|
19
|
+
class FirestoreTransactionPerformer extends model_1.TransactionPerformer {
|
|
20
|
+
constructor(db) {
|
|
21
|
+
super((effect) => db.runTransaction((trx) => effect(new FirestoreTransaction(trx))));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.FirestoreTransactionPerformer = FirestoreTransactionPerformer;
|
|
25
|
+
//# sourceMappingURL=firestore.transaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"firestore.transaction.js","sourceRoot":"","sources":["../src/firestore.transaction.ts"],"names":[],"mappings":";;;AACA,yCAAqD;AAIrD,MAAa,oBAAoB;IAEH;IAD5B,eAAe,GAAqB,EAAE,CAAC;IACvC,YAA4B,WAA0C;QAA1C,gBAAW,GAAX,WAAW,CAA+B;IAAG,CAAC;IAE1E,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;AAXD,oDAWC;AAED,MAAa,6BAA8B,SAAQ,4BAA0C;IAC3F,YAAY,EAAa;QACvB,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC,cAAc,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,oBAAoB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACvF,CAAC;CACF;AAJD,sEAIC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,6BAA6B,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.FirestoreTransaction = exports.FirestoreTransactionPerformer = exports.FirestoreStore = void 0;
|
|
4
4
|
var firestore_store_1 = require("./firestore.store");
|
|
5
5
|
Object.defineProperty(exports, "FirestoreStore", { enumerable: true, get: function () { return firestore_store_1.FirestoreStore; } });
|
|
6
|
-
var
|
|
7
|
-
Object.defineProperty(exports, "
|
|
6
|
+
var firestore_transaction_1 = require("./firestore.transaction");
|
|
7
|
+
Object.defineProperty(exports, "FirestoreTransactionPerformer", { enumerable: true, get: function () { return firestore_transaction_1.FirestoreTransactionPerformer; } });
|
|
8
|
+
Object.defineProperty(exports, "FirestoreTransaction", { enumerable: true, get: function () { return firestore_transaction_1.FirestoreTransaction; } });
|
|
8
9
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qDAAmD;AAA1C,iHAAA,cAAc,OAAA;AACvB
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qDAAmD;AAA1C,iHAAA,cAAc,OAAA;AACvB,iEAA8F;AAArF,sIAAA,6BAA6B,OAAA;AAAE,6HAAA,oBAAoB,OAAA"}
|
package/package.json
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
2
|
+
"name": "@ddd-ts/store-firestore",
|
|
3
|
+
"version": "0.0.3-50",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "builder"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@ddd-ts/model": "*",
|
|
12
|
+
"@ddd-ts/serialization": "*",
|
|
13
|
+
"firebase-admin": "^11.5.0"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@ddd-ts/dev": "*",
|
|
17
|
+
"@ddd-ts/types": "*"
|
|
18
|
+
}
|
|
20
19
|
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { Firestore } from "firebase-admin/firestore";
|
|
2
|
-
import { TransactionPerformer } from "@ddd-ts/model";
|
|
3
|
-
export declare class FirebaseTransactionPerformer extends TransactionPerformer<FirebaseFirestore.Transaction> {
|
|
4
|
-
constructor(db: Firestore);
|
|
5
|
-
}
|
|
6
|
-
//# sourceMappingURL=firebase.transaction.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"firebase.transaction.d.ts","sourceRoot":"","sources":["../src/firebase.transaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAErD,qBAAa,4BAA6B,SAAQ,oBAAoB,CAAC,iBAAiB,CAAC,WAAW,CAAC;gBACvF,EAAE,EAAE,SAAS;CAG1B"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FirebaseTransactionPerformer = void 0;
|
|
4
|
-
const model_1 = require("@ddd-ts/model");
|
|
5
|
-
class FirebaseTransactionPerformer extends model_1.TransactionPerformer {
|
|
6
|
-
constructor(db) {
|
|
7
|
-
super((effect) => db.runTransaction(effect));
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
exports.FirebaseTransactionPerformer = FirebaseTransactionPerformer;
|
|
11
|
-
//# sourceMappingURL=firebase.transaction.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"firebase.transaction.js","sourceRoot":"","sources":["../src/firebase.transaction.ts"],"names":[],"mappings":";;;AACA,yCAAqD;AAErD,MAAa,4BAA6B,SAAQ,4BAAmD;IACnG,YAAY,EAAa;QACvB,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC;IAC/C,CAAC;CACF;AAJD,oEAIC"}
|