cordova-plugin-firebasex-firestore 1.0.0

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.
@@ -0,0 +1,219 @@
1
+ /**
2
+ * @fileoverview Cordova JavaScript interface for the FirebaseX Firestore plugin.
3
+ *
4
+ * Provides CRUD operations on Firestore documents and collections, query filtering,
5
+ * and real-time snapshot listeners with change tracking.
6
+ *
7
+ * @module firebasex-firestore
8
+ * @see https://firebase.google.com/docs/firestore
9
+ */
10
+
11
+ var exec = require('cordova/exec');
12
+
13
+ /** @private Cordova service name registered in plugin.xml. */
14
+ var SERVICE = 'FirebasexFirestorePlugin';
15
+
16
+ /**
17
+ * Wraps a callback so its result is coerced to a strict boolean.
18
+ *
19
+ * @private
20
+ * @param {function} callback - The original callback.
21
+ * @returns {function} A wrapper that calls callback with a boolean.
22
+ */
23
+ var ensureBooleanFn = function (callback) {
24
+ return function (result) {
25
+ callback(result === true || result === 1);
26
+ };
27
+ };
28
+
29
+ /**
30
+ * Adds a new document with an auto-generated ID to a Firestore collection.
31
+ *
32
+ * @param {Object} document - The document data to add (must be a plain object, not an array).
33
+ * @param {string} collection - The Firestore collection path.
34
+ * @param {boolean} [timestamp=false] - If {@code true}, adds {@code created} and
35
+ * {@code lastUpdate} Firestore Timestamp fields automatically.
36
+ * @param {function} success - Called with the auto-generated document ID string.
37
+ * @param {function} error - Called with an error message on failure.
38
+ */
39
+ exports.addDocumentToFirestoreCollection = function (document, collection, timestamp, success, error) {
40
+ if (typeof collection !== 'string') return error("'collection' must be a string specifying the Firestore collection name");
41
+ if (typeof document !== 'object' || typeof document.length === 'number') return error("'document' must be an object specifying record data");
42
+
43
+ if (typeof timestamp !== "boolean" && typeof error === "undefined") {
44
+ error = success;
45
+ success = timestamp;
46
+ timestamp = false;
47
+ }
48
+
49
+ exec(success, error, SERVICE, "addDocumentToFirestoreCollection", [document, collection, timestamp || false]);
50
+ };
51
+
52
+ /**
53
+ * Creates or overwrites a document with a specific ID in a Firestore collection.
54
+ *
55
+ * @param {string|number} documentId - The document identifier.
56
+ * @param {Object} document - The document data.
57
+ * @param {string} collection - The Firestore collection path.
58
+ * @param {boolean} [timestamp=false] - If {@code true}, adds a {@code lastUpdate} Timestamp field.
59
+ * @param {function} success - Called on success.
60
+ * @param {function} error - Called with an error message on failure.
61
+ */
62
+ exports.setDocumentInFirestoreCollection = function (documentId, document, collection, timestamp, success, error) {
63
+ if (typeof documentId !== 'string' && typeof documentId !== 'number') return error("'documentId' must be a string or number specifying the Firestore document identifier");
64
+ if (typeof collection !== 'string') return error("'collection' must be a string specifying the Firestore collection name");
65
+ if (typeof document !== 'object' || typeof document.length === 'number') return error("'document' must be an object specifying record data");
66
+
67
+ if (typeof timestamp !== "boolean" && typeof error === "undefined") {
68
+ error = success;
69
+ success = timestamp;
70
+ timestamp = false;
71
+ }
72
+
73
+ exec(success, error, SERVICE, "setDocumentInFirestoreCollection", [documentId.toString(), document, collection, timestamp || false]);
74
+ };
75
+
76
+ /**
77
+ * Updates specific fields of an existing document in a Firestore collection.
78
+ * Fails if the document does not exist.
79
+ *
80
+ * @param {string|number} documentId - The document identifier.
81
+ * @param {Object} document - The fields to update.
82
+ * @param {string} collection - The Firestore collection path.
83
+ * @param {boolean} [timestamp=false] - If {@code true}, updates the {@code lastUpdate} Timestamp field.
84
+ * @param {function} success - Called on success.
85
+ * @param {function} error - Called with an error message on failure.
86
+ */
87
+ exports.updateDocumentInFirestoreCollection = function (documentId, document, collection, timestamp, success, error) {
88
+ if (typeof documentId !== 'string' && typeof documentId !== 'number') return error("'documentId' must be a string or number specifying the Firestore document identifier");
89
+ if (typeof collection !== 'string') return error("'collection' must be a string specifying the Firestore collection name");
90
+ if (typeof document !== 'object' || typeof document.length === 'number') return error("'document' must be an object specifying record data");
91
+
92
+ if (typeof timestamp !== "boolean" && typeof error === "undefined") {
93
+ error = success;
94
+ success = timestamp;
95
+ timestamp = false;
96
+ }
97
+
98
+ exec(success, error, SERVICE, "updateDocumentInFirestoreCollection", [documentId.toString(), document, collection, timestamp || false]);
99
+ };
100
+
101
+ /**
102
+ * Deletes a document from a Firestore collection.
103
+ *
104
+ * @param {string|number} documentId - The document identifier.
105
+ * @param {string} collection - The Firestore collection path.
106
+ * @param {function} success - Called on success.
107
+ * @param {function} error - Called with an error message on failure.
108
+ */
109
+ exports.deleteDocumentFromFirestoreCollection = function (documentId, collection, success, error) {
110
+ if (typeof documentId !== 'string' && typeof documentId !== 'number') return error("'documentId' must be a string or number specifying the Firestore document identifier");
111
+ if (typeof collection !== 'string') return error("'collection' must be a string specifying the Firestore collection name");
112
+
113
+ exec(success, error, SERVICE, "deleteDocumentFromFirestoreCollection", [documentId.toString(), collection]);
114
+ };
115
+
116
+ /**
117
+ * Checks whether a document exists in a Firestore collection.
118
+ *
119
+ * @param {string|number} documentId - The document identifier.
120
+ * @param {string} collection - The Firestore collection path.
121
+ * @param {function} success - Called with a boolean: {@code true} if the document exists.
122
+ * @param {function} error - Called with an error message on failure.
123
+ */
124
+ exports.documentExistsInFirestoreCollection = function (documentId, collection, success, error) {
125
+ if (typeof documentId !== 'string' && typeof documentId !== 'number') return error("'documentId' must be a string or number specifying the Firestore document identifier");
126
+ if (typeof collection !== 'string') return error("'collection' must be a string specifying the Firestore collection name");
127
+
128
+ exec(ensureBooleanFn(success), error, SERVICE, "documentExistsInFirestoreCollection", [documentId.toString(), collection]);
129
+ };
130
+
131
+ /**
132
+ * Fetches a single document from a Firestore collection.
133
+ *
134
+ * @param {string|number} documentId - The document identifier.
135
+ * @param {string} collection - The Firestore collection path.
136
+ * @param {function} success - Called with the document data as a JSON object.
137
+ * @param {function} error - Called with an error message on failure or if not found.
138
+ */
139
+ exports.fetchDocumentInFirestoreCollection = function (documentId, collection, success, error) {
140
+ if (typeof documentId !== 'string' && typeof documentId !== 'number') return error("'documentId' must be a string or number specifying the Firestore document identifier");
141
+ if (typeof collection !== 'string') return error("'collection' must be a string specifying the Firestore collection name");
142
+
143
+ exec(success, error, SERVICE, "fetchDocumentInFirestoreCollection", [documentId.toString(), collection]);
144
+ };
145
+
146
+ /**
147
+ * Fetches all documents from a Firestore collection, optionally filtered.
148
+ *
149
+ * @param {string} collection - The Firestore collection path.
150
+ * @param {Array.<Array>} [filters] - An array of filter arrays. Each filter is an array
151
+ * describing a query operation. Supported filter types:
152
+ * - {@code ["where", fieldName, operator, value, type]} — operators: ==, <, >, <=, >=, array-contains
153
+ * - {@code ["orderBy", fieldName, direction]} — direction: "asc" or "desc"
154
+ * - {@code ["startAt", value, type]}
155
+ * - {@code ["endAt", value, type]}
156
+ * - {@code ["limit", count]}
157
+ * @param {function} success - Called with a JSON object mapping document IDs to document data.
158
+ * @param {function} error - Called with an error message on failure.
159
+ */
160
+ exports.fetchFirestoreCollection = function (collection, filters, success, error) {
161
+ if (typeof collection !== 'string') return error("'collection' must be a string specifying the Firestore collection name");
162
+ if (filters && (typeof filters !== 'object' || typeof filters.length === 'undefined' || (filters.length && typeof filters[0] !== 'object'))) return error("'filters' must be a array specifying a list of filters (as arrays) to apply to documents in the Firestore collection");
163
+
164
+ exec(success, error, SERVICE, "fetchFirestoreCollection", [collection, filters || []]);
165
+ };
166
+
167
+ /**
168
+ * Registers a real-time listener on a single document in a Firestore collection.
169
+ *
170
+ * The success callback is called multiple times: first with {@code {eventType: "id", id: listenerId}},
171
+ * then with {@code {eventType: "change", snapshot: ..., source: "local"|"remote", fromCache: boolean}}
172
+ * on each change.
173
+ *
174
+ * @param {function} success - Called with listener events.
175
+ * @param {function} error - Called with an error message on failure.
176
+ * @param {string|number} documentId - The document identifier.
177
+ * @param {string} collection - The Firestore collection path.
178
+ * @param {boolean} includeMetadata - Whether to include metadata-only changes.
179
+ */
180
+ exports.listenToDocumentInFirestoreCollection = function (success, error, documentId, collection, includeMetadata) {
181
+ if (typeof documentId !== 'string' && typeof documentId !== 'number') return error("'documentId' must be a string or number specifying the Firestore document identifier");
182
+ if (typeof collection !== 'string') return error("'collection' must be a string specifying the Firestore collection name");
183
+
184
+ exec(success, error, SERVICE, "listenToDocumentInFirestoreCollection", [documentId.toString(), collection, includeMetadata]);
185
+ };
186
+
187
+ /**
188
+ * Registers a real-time listener on an entire Firestore collection, optionally filtered.
189
+ *
190
+ * The success callback is called multiple times: first with {@code {eventType: "id", id: listenerId}},
191
+ * then with {@code {eventType: "change", documents: {...}}} on each change.
192
+ * Each document entry includes {@code type} ("new", "modified", "removed"),
193
+ * {@code snapshot}, {@code source}, and {@code fromCache}.
194
+ *
195
+ * @param {function} success - Called with listener events.
196
+ * @param {function} error - Called with an error message on failure.
197
+ * @param {string} collection - The Firestore collection path.
198
+ * @param {Array.<Array>} [filters] - Query filters (same format as {@link fetchFirestoreCollection}).
199
+ * @param {boolean} includeMetadata - Whether to include metadata-only changes.
200
+ */
201
+ exports.listenToFirestoreCollection = function (success, error, collection, filters, includeMetadata) {
202
+ if (typeof collection !== 'string') return error("'collection' must be a string specifying the Firestore collection name");
203
+ if (filters && (typeof filters !== 'object' || typeof filters.length === 'undefined')) return error("'filters' must be a array specifying a list of filters to apply to documents in the Firestore collection");
204
+
205
+ exec(success, error, SERVICE, "listenToFirestoreCollection", [collection, filters, includeMetadata]);
206
+ };
207
+
208
+ /**
209
+ * Removes a previously registered Firestore snapshot listener.
210
+ *
211
+ * @param {function} success - Called on success.
212
+ * @param {function} error - Called with an error message if the listener ID is not found.
213
+ * @param {string|number} listenerId - The listener ID returned in the initial listener response.
214
+ */
215
+ exports.removeFirestoreListener = function (success, error, listenerId) {
216
+ if (typeof listenerId === 'undefined') return error("'listenerId' must be specified");
217
+
218
+ exec(success, error, SERVICE, "removeFirestoreListener", [listenerId.toString()]);
219
+ };