1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | Object.defineProperty(exports, "__esModule", { value: true });
|
24 | exports.DocumentBuilder = exports.beforeSnapshotConstructor = exports.snapshotConstructor = exports.NamespaceBuilder = exports.DatabaseBuilder = exports._documentWithOptions = exports._namespaceWithOptions = exports._databaseWithOptions = exports.database = exports.namespace = exports.document = exports.defaultDatabase = exports.service = exports.provider = void 0;
|
25 | const path_1 = require("path");
|
26 | const change_1 = require("../../common/change");
|
27 | const firestore_1 = require("../../common/providers/firestore");
|
28 | const cloud_functions_1 = require("../cloud-functions");
|
29 |
|
30 | exports.provider = "google.firestore";
|
31 |
|
32 | exports.service = "firestore.googleapis.com";
|
33 |
|
34 | exports.defaultDatabase = "(default)";
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | function document(path) {
|
43 | return _documentWithOptions(path, {});
|
44 | }
|
45 | exports.document = document;
|
46 |
|
47 | function namespace(namespace) {
|
48 | return _namespaceWithOptions(namespace, {});
|
49 | }
|
50 | exports.namespace = namespace;
|
51 |
|
52 | function database(database) {
|
53 | return _databaseWithOptions(database, {});
|
54 | }
|
55 | exports.database = database;
|
56 |
|
57 | function _databaseWithOptions(database = exports.defaultDatabase, options) {
|
58 | return new DatabaseBuilder(database, options);
|
59 | }
|
60 | exports._databaseWithOptions = _databaseWithOptions;
|
61 |
|
62 | function _namespaceWithOptions(namespace, options) {
|
63 | return _databaseWithOptions(exports.defaultDatabase, options).namespace(namespace);
|
64 | }
|
65 | exports._namespaceWithOptions = _namespaceWithOptions;
|
66 |
|
67 | function _documentWithOptions(path, options) {
|
68 | return _databaseWithOptions(exports.defaultDatabase, options).document(path);
|
69 | }
|
70 | exports._documentWithOptions = _documentWithOptions;
|
71 | class DatabaseBuilder {
|
72 | constructor(database, options) {
|
73 | this.database = database;
|
74 | this.options = options;
|
75 | }
|
76 | namespace(namespace) {
|
77 | return new NamespaceBuilder(this.database, this.options, namespace);
|
78 | }
|
79 | document(path) {
|
80 | return new NamespaceBuilder(this.database, this.options).document(path);
|
81 | }
|
82 | }
|
83 | exports.DatabaseBuilder = DatabaseBuilder;
|
84 | class NamespaceBuilder {
|
85 | constructor(database, options, namespace) {
|
86 | this.database = database;
|
87 | this.options = options;
|
88 | this.namespace = namespace;
|
89 | }
|
90 | document(path) {
|
91 | return new DocumentBuilder(() => {
|
92 | if (!process.env.GCLOUD_PROJECT) {
|
93 | throw new Error("process.env.GCLOUD_PROJECT is not set.");
|
94 | }
|
95 | const database = path_1.posix.join("projects", process.env.GCLOUD_PROJECT, "databases", this.database);
|
96 | return path_1.posix.join(database, this.namespace ? `documents@${this.namespace}` : "documents", path);
|
97 | }, this.options);
|
98 | }
|
99 | }
|
100 | exports.NamespaceBuilder = NamespaceBuilder;
|
101 | function snapshotConstructor(event) {
|
102 | var _a, _b, _c, _d;
|
103 | return (0, firestore_1.createSnapshotFromJson)(event.data, event.context.resource.name, (_b = (_a = event === null || event === void 0 ? void 0 : event.data) === null || _a === void 0 ? void 0 : _a.value) === null || _b === void 0 ? void 0 : _b.readTime, (_d = (_c = event === null || event === void 0 ? void 0 : event.data) === null || _c === void 0 ? void 0 : _c.value) === null || _d === void 0 ? void 0 : _d.updateTime);
|
104 | }
|
105 | exports.snapshotConstructor = snapshotConstructor;
|
106 |
|
107 | function beforeSnapshotConstructor(event) {
|
108 | var _a, _b;
|
109 | return (0, firestore_1.createBeforeSnapshotFromJson)(event.data, event.context.resource.name, (_b = (_a = event === null || event === void 0 ? void 0 : event.data) === null || _a === void 0 ? void 0 : _a.oldValue) === null || _b === void 0 ? void 0 : _b.readTime, undefined);
|
110 | }
|
111 | exports.beforeSnapshotConstructor = beforeSnapshotConstructor;
|
112 | function changeConstructor(raw) {
|
113 | return change_1.Change.fromObjects(beforeSnapshotConstructor(raw), snapshotConstructor(raw));
|
114 | }
|
115 | class DocumentBuilder {
|
116 | constructor(triggerResource, options) {
|
117 | this.triggerResource = triggerResource;
|
118 | this.options = options;
|
119 |
|
120 | }
|
121 |
|
122 | onWrite(handler) {
|
123 | return this.onOperation(handler, "document.write", changeConstructor);
|
124 | }
|
125 |
|
126 | onUpdate(handler) {
|
127 | return this.onOperation(handler, "document.update", changeConstructor);
|
128 | }
|
129 |
|
130 | onCreate(handler) {
|
131 | return this.onOperation(handler, "document.create", snapshotConstructor);
|
132 | }
|
133 |
|
134 | onDelete(handler) {
|
135 | return this.onOperation(handler, "document.delete", beforeSnapshotConstructor);
|
136 | }
|
137 | onOperation(handler, eventType, dataConstructor) {
|
138 | return (0, cloud_functions_1.makeCloudFunction)({
|
139 | handler,
|
140 | provider: exports.provider,
|
141 | eventType,
|
142 | service: exports.service,
|
143 | triggerResource: this.triggerResource,
|
144 | legacyEventType: `providers/cloud.firestore/eventTypes/${eventType}`,
|
145 | dataConstructor,
|
146 | options: this.options,
|
147 | });
|
148 | }
|
149 | }
|
150 | exports.DocumentBuilder = DocumentBuilder;
|