UNPKG

6.92 kBJavaScriptView Raw
1"use strict";
2// The MIT License (MIT)
3//
4// Copyright (c) 2017 Firebase
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.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;
25const path_1 = require("path");
26const change_1 = require("../../common/change");
27const firestore_1 = require("../../common/providers/firestore");
28const cloud_functions_1 = require("../cloud-functions");
29/** @internal */
30exports.provider = "google.firestore";
31/** @internal */
32exports.service = "firestore.googleapis.com";
33/** @internal */
34exports.defaultDatabase = "(default)";
35/**
36 * Select the Firestore document to listen to for events.
37 * @param path Full database path to listen to. This includes the name of
38 * the collection that the document is a part of. For example, if the
39 * collection is named "users" and the document is named "Ada", then the
40 * path is "/users/Ada".
41 */
42function document(path) {
43 return _documentWithOptions(path, {});
44}
45exports.document = document;
46// Multiple namespaces are not yet supported by Firestore.
47function namespace(namespace) {
48 return _namespaceWithOptions(namespace, {});
49}
50exports.namespace = namespace;
51// Multiple databases are not yet supported by Firestore.
52function database(database) {
53 return _databaseWithOptions(database, {});
54}
55exports.database = database;
56/** @internal */
57function _databaseWithOptions(database = exports.defaultDatabase, options) {
58 return new DatabaseBuilder(database, options);
59}
60exports._databaseWithOptions = _databaseWithOptions;
61/** @internal */
62function _namespaceWithOptions(namespace, options) {
63 return _databaseWithOptions(exports.defaultDatabase, options).namespace(namespace);
64}
65exports._namespaceWithOptions = _namespaceWithOptions;
66/** @internal */
67function _documentWithOptions(path, options) {
68 return _databaseWithOptions(exports.defaultDatabase, options).document(path);
69}
70exports._documentWithOptions = _documentWithOptions;
71class 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}
83exports.DatabaseBuilder = DatabaseBuilder;
84class 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}
100exports.NamespaceBuilder = NamespaceBuilder;
101function 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}
105exports.snapshotConstructor = snapshotConstructor;
106// TODO remove this function when wire format changes to new format
107function 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}
111exports.beforeSnapshotConstructor = beforeSnapshotConstructor;
112function changeConstructor(raw) {
113 return change_1.Change.fromObjects(beforeSnapshotConstructor(raw), snapshotConstructor(raw));
114}
115class DocumentBuilder {
116 constructor(triggerResource, options) {
117 this.triggerResource = triggerResource;
118 this.options = options;
119 // TODO what validation do we want to do here?
120 }
121 /** Respond to all document writes (creates, updates, or deletes). */
122 onWrite(handler) {
123 return this.onOperation(handler, "document.write", changeConstructor);
124 }
125 /** Respond only to document updates. */
126 onUpdate(handler) {
127 return this.onOperation(handler, "document.update", changeConstructor);
128 }
129 /** Respond only to document creations. */
130 onCreate(handler) {
131 return this.onOperation(handler, "document.create", snapshotConstructor);
132 }
133 /** Respond only to document deletions. */
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}
150exports.DocumentBuilder = DocumentBuilder;