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.
- package/.github/FUNDING.yml +6 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +125 -0
- package/.github/ISSUE_TEMPLATE/documentation-issue.md +36 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +43 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +42 -0
- package/.github/stale.yml +17 -0
- package/CHANGELOG.md +3 -0
- package/LICENSE +21 -0
- package/README.md +644 -0
- package/package.json +18 -0
- package/plugin.xml +72 -0
- package/scripts/ios/after_plugin_install.js +137 -0
- package/scripts/post_install.js +157 -0
- package/src/android/FirebasexFirestorePlugin.java +754 -0
- package/src/ios/FirebasexFirestorePlugin.h +39 -0
- package/src/ios/FirebasexFirestorePlugin.m +575 -0
- package/types/index.d.ts +68 -0
- package/www/firebasex-firestore.js +219 -0
package/README.md
ADDED
|
@@ -0,0 +1,644 @@
|
|
|
1
|
+
# cordova-plugin-firebasex-firestore [](https://www.npmjs.com/package/cordova-plugin-firebasex-firestore)
|
|
2
|
+
|
|
3
|
+
Firebase Firestore module for the [modular FirebaseX Cordova plugin suite](https://github.com/dpa99c/cordova-plugin-firebasex#modular-plugins).
|
|
4
|
+
|
|
5
|
+
This plugin wraps the [Firebase Firestore SDK](https://firebase.google.com/docs/reference/js/firebase.firestore) and provides methods to read and write data, listen to real-time updates, and control Firestore data collection in your Cordova app.
|
|
6
|
+
|
|
7
|
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
8
|
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
9
|
+
**Table of Contents**
|
|
10
|
+
|
|
11
|
+
- [Installation](#installation)
|
|
12
|
+
- [Plugin variables](#plugin-variables)
|
|
13
|
+
- [API](#api)
|
|
14
|
+
- [addDocumentToFirestoreCollection](#adddocumenttofirestorecollection)
|
|
15
|
+
- [setDocumentInFirestoreCollection](#setdocumentinfirestorecollection)
|
|
16
|
+
- [updateDocumentInFirestoreCollection](#updatedocumentinfirestorecollection)
|
|
17
|
+
- [deleteDocumentFromFirestoreCollection](#deletedocumentfromfirestorecollection)
|
|
18
|
+
- [documentExistsInFirestoreCollection](#documentexistsinfirestorecollection)
|
|
19
|
+
- [fetchDocumentInFirestoreCollection](#fetchdocumentinfirestorecollection)
|
|
20
|
+
- [fetchFirestoreCollection](#fetchfirestorecollection)
|
|
21
|
+
- [listenToDocumentInFirestoreCollection](#listentodocumentinfirestorecollection)
|
|
22
|
+
- [listenToFirestoreCollection](#listentofirestorecollection)
|
|
23
|
+
- [removeFirestoreListener](#removefirestorelistener)
|
|
24
|
+
- [Reporting issues](#reporting-issues)
|
|
25
|
+
|
|
26
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
27
|
+
|
|
28
|
+
# Installation
|
|
29
|
+
|
|
30
|
+
Install the plugin by adding it to your project's config.xml:
|
|
31
|
+
|
|
32
|
+
cordova plugin add cordova-plugin-firebasex-firestore
|
|
33
|
+
|
|
34
|
+
or by running:
|
|
35
|
+
|
|
36
|
+
cordova plugin add cordova-plugin-firebasex-firestore
|
|
37
|
+
|
|
38
|
+
**This module depends on `cordova-plugin-firebasex-core` which will be installed automatically as a dependency.**
|
|
39
|
+
|
|
40
|
+
## Plugin variables
|
|
41
|
+
|
|
42
|
+
The following plugin variables are used to configure the firestore module at install time.
|
|
43
|
+
They can be set on the command line at plugin installation time:
|
|
44
|
+
|
|
45
|
+
cordova plugin add cordova-plugin-firebasex-firestore --variable VARIABLE_NAME=value
|
|
46
|
+
|
|
47
|
+
Or in your `config.xml`:
|
|
48
|
+
|
|
49
|
+
<plugin name="cordova-plugin-firebasex-firestore">
|
|
50
|
+
<variable name="VARIABLE_NAME" value="value" />
|
|
51
|
+
</plugin>
|
|
52
|
+
|
|
53
|
+
| Variable | Default | Description |
|
|
54
|
+
|---|---|---|
|
|
55
|
+
| `ANDROID_FIREBASE_FIRESTORE_VERSION` | `26.1.0` | Android Firebase Firestore SDK version. |
|
|
56
|
+
| `ANDROID_GRPC_OKHTTP` | `1.75.0` | Android gRPC OkHttp version (Firestore dependency). |
|
|
57
|
+
| `ANDROID_GSON_VERSION` | `2.13.2` | Google Gson library version (Firestore dependency). |
|
|
58
|
+
| `IOS_FIREBASE_SDK_VERSION` | `12.9.0` | iOS Firebase SDK version (for firestore pod). |
|
|
59
|
+
| `IOS_USE_PRECOMPILED_FIRESTORE_POD` | `false` | Use precompiled Firestore pod for faster iOS builds. See [Precompiled Firestore Pod](#precompiled-firestore-pod-ios) below. |
|
|
60
|
+
|
|
61
|
+
### Precompiled Firestore Pod (iOS)
|
|
62
|
+
|
|
63
|
+
For faster iOS builds, enable the precompiled Firestore pod:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
cordova plugin add cordova-plugin-firebasex-firestore --variable IOS_USE_PRECOMPILED_FIRESTORE_POD=true
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
# API
|
|
70
|
+
|
|
71
|
+
The following methods are available via the `FirebasexFirestore` global object.
|
|
72
|
+
|
|
73
|
+
These API functions provide CRUD operations for working with documents in Firestore collections.
|
|
74
|
+
|
|
75
|
+
Notes:
|
|
76
|
+
|
|
77
|
+
- Only top-level Firestore collections are currently supported - [subcollections](https://firebase.google.com/docs/firestore/manage-data/structure-data#subcollections) (nested collections within documents) are currently not supported due to the complexity of mapping the native objects into the plugin's JS API layer.
|
|
78
|
+
- A document object may contain values of primitive Javascript types `string`, `number`, `boolean`, `array` or `object`.
|
|
79
|
+
Arrays and objects may contain nested structures of these types.
|
|
80
|
+
- If a collection name referenced in a document write operation does not already exist, it will be created by the first write operation referencing it.
|
|
81
|
+
|
|
82
|
+
## addDocumentToFirestoreCollection
|
|
83
|
+
|
|
84
|
+
Adds a new document to a Firestore collection, which will be allocated an auto-generated document ID.
|
|
85
|
+
|
|
86
|
+
**Parameters**:
|
|
87
|
+
|
|
88
|
+
- {object} document - document object to add to collection
|
|
89
|
+
- {string} collection - name of top-level collection to add document to.
|
|
90
|
+
- {boolean} timestamp (optional) - Add 'created' and 'lastUpdate' variables in the document. Default `false`.
|
|
91
|
+
- {function} success (optional) - callback function to call on successfully adding the document.
|
|
92
|
+
Will be passed a {string} argument containing the auto-generated document ID that the document was stored against.
|
|
93
|
+
- {function} error (optional) - callback function which will be passed a {string} error message as an argument.
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
var document = {
|
|
97
|
+
a_string: "foo",
|
|
98
|
+
a_list: [1, 2, 3],
|
|
99
|
+
an_object: {
|
|
100
|
+
an_integer: 1,
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
var collection = "my_collection";
|
|
104
|
+
|
|
105
|
+
// with timestamp
|
|
106
|
+
FirebasexFirestore.addDocumentToFirestoreCollection(
|
|
107
|
+
document,
|
|
108
|
+
collection,
|
|
109
|
+
true,
|
|
110
|
+
function (documentId) {
|
|
111
|
+
console.log("Successfully added document with id=" + documentId);
|
|
112
|
+
},
|
|
113
|
+
function (error) {
|
|
114
|
+
console.error("Error adding document: " + error);
|
|
115
|
+
}
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
// without timestamp
|
|
119
|
+
FirebasexFirestore.addDocumentToFirestoreCollection(
|
|
120
|
+
document,
|
|
121
|
+
collection,
|
|
122
|
+
function (documentId) {
|
|
123
|
+
console.log("Successfully added document with id=" + documentId);
|
|
124
|
+
},
|
|
125
|
+
function (error) {
|
|
126
|
+
console.error("Error adding document: " + error);
|
|
127
|
+
}
|
|
128
|
+
);
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## setDocumentInFirestoreCollection
|
|
132
|
+
|
|
133
|
+
Sets (adds/replaces) a document with the given ID in a Firestore collection.
|
|
134
|
+
|
|
135
|
+
**Parameters**:
|
|
136
|
+
|
|
137
|
+
- {string} documentId - document ID to use when setting document in the collection.
|
|
138
|
+
- {object} document - document object to set in collection.
|
|
139
|
+
- {string} collection - name of top-level collection to set document in.
|
|
140
|
+
- {boolean} timestamp (optional) - Add 'lastUpdate' variable in the document. Default `false`.
|
|
141
|
+
- {function} success (optional) - callback function to call on successfully setting the document.
|
|
142
|
+
- {function} error (optional) - callback function which will be passed a {string} error message as an argument.
|
|
143
|
+
|
|
144
|
+
```javascript
|
|
145
|
+
var documentId = "my_doc";
|
|
146
|
+
var document = {
|
|
147
|
+
a_string: "foo",
|
|
148
|
+
a_list: [1, 2, 3],
|
|
149
|
+
an_object: {
|
|
150
|
+
an_integer: 1,
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
var collection = "my_collection";
|
|
154
|
+
|
|
155
|
+
// with timestamp
|
|
156
|
+
FirebasexFirestore.setDocumentInFirestoreCollection(
|
|
157
|
+
documentId,
|
|
158
|
+
document,
|
|
159
|
+
collection,
|
|
160
|
+
true,
|
|
161
|
+
function () {
|
|
162
|
+
console.log("Successfully set document with id=" + documentId);
|
|
163
|
+
},
|
|
164
|
+
function (error) {
|
|
165
|
+
console.error("Error setting document: " + error);
|
|
166
|
+
}
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
// without timestamp
|
|
170
|
+
FirebasexFirestore.setDocumentInFirestoreCollection(
|
|
171
|
+
documentId,
|
|
172
|
+
document,
|
|
173
|
+
collection,
|
|
174
|
+
function () {
|
|
175
|
+
console.log("Successfully set document with id=" + documentId);
|
|
176
|
+
},
|
|
177
|
+
function (error) {
|
|
178
|
+
console.error("Error setting document: " + error);
|
|
179
|
+
}
|
|
180
|
+
);
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## updateDocumentInFirestoreCollection
|
|
184
|
+
|
|
185
|
+
Updates an existing document with the given ID in a Firestore collection.
|
|
186
|
+
This is a non-destructive update that will only overwrite existing keys in the existing document or add new ones if they don't already exist.
|
|
187
|
+
If the no document with the specified ID exists in the collection, an error will be raised.
|
|
188
|
+
|
|
189
|
+
**Parameters**:
|
|
190
|
+
|
|
191
|
+
- {string} documentId - document ID of the document to update.
|
|
192
|
+
- {object} document - entire document or document fragment to update existing document with.
|
|
193
|
+
- {string} collection - name of top-level collection to update document in.
|
|
194
|
+
- {boolean} timestamp (optional) - Add 'lastUpdate' variable in the document. Default `false`.
|
|
195
|
+
- {function} success (optional) - callback function to call on successfully updating the document.
|
|
196
|
+
- {function} error (optional) - callback function which will be passed a {string} error message as an argument.
|
|
197
|
+
|
|
198
|
+
```javascript
|
|
199
|
+
var documentId = "my_doc";
|
|
200
|
+
var documentFragment = {
|
|
201
|
+
a_string: "new value",
|
|
202
|
+
a_new_string: "bar",
|
|
203
|
+
};
|
|
204
|
+
var collection = "my_collection";
|
|
205
|
+
|
|
206
|
+
// with timestamp
|
|
207
|
+
FirebasexFirestore.updateDocumentInFirestoreCollection(
|
|
208
|
+
documentId,
|
|
209
|
+
documentFragment,
|
|
210
|
+
collection,
|
|
211
|
+
true,
|
|
212
|
+
function () {
|
|
213
|
+
console.log("Successfully updated document with id=" + documentId);
|
|
214
|
+
},
|
|
215
|
+
function (error) {
|
|
216
|
+
console.error("Error updating document: " + error);
|
|
217
|
+
}
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
// without timestamp
|
|
221
|
+
FirebasexFirestore.updateDocumentInFirestoreCollection(
|
|
222
|
+
documentId,
|
|
223
|
+
documentFragment,
|
|
224
|
+
collection,
|
|
225
|
+
function () {
|
|
226
|
+
console.log("Successfully updated document with id=" + documentId);
|
|
227
|
+
},
|
|
228
|
+
function (error) {
|
|
229
|
+
console.error("Error updating document: " + error);
|
|
230
|
+
}
|
|
231
|
+
);
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
## deleteDocumentFromFirestoreCollection
|
|
235
|
+
|
|
236
|
+
Deletes an existing document with the given ID in a Firestore collection.
|
|
237
|
+
|
|
238
|
+
Note: If the no document with the specified ID exists in the collection, the Firebase SDK will still return a successful outcome.
|
|
239
|
+
|
|
240
|
+
**Parameters**:
|
|
241
|
+
|
|
242
|
+
- {string} documentId - document ID of the document to delete.
|
|
243
|
+
- {string} collection - name of top-level collection to delete document in.
|
|
244
|
+
- {function} success - callback function to call on successfully deleting the document.
|
|
245
|
+
- {function} error - callback function which will be passed a {string} error message as an argument.
|
|
246
|
+
|
|
247
|
+
```javascript
|
|
248
|
+
var documentId = "my_doc";
|
|
249
|
+
var collection = "my_collection";
|
|
250
|
+
FirebasexFirestore.deleteDocumentFromFirestoreCollection(
|
|
251
|
+
documentId,
|
|
252
|
+
collection,
|
|
253
|
+
function () {
|
|
254
|
+
console.log("Successfully deleted document with id=" + documentId);
|
|
255
|
+
},
|
|
256
|
+
function (error) {
|
|
257
|
+
console.error("Error deleting document: " + error);
|
|
258
|
+
}
|
|
259
|
+
);
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## documentExistsInFirestoreCollection
|
|
263
|
+
|
|
264
|
+
Indicates if a document with the given ID exists in a Firestore collection.
|
|
265
|
+
|
|
266
|
+
**Parameters**:
|
|
267
|
+
|
|
268
|
+
- {string} documentId - document ID of the document.
|
|
269
|
+
- {string} collection - name of top-level collection to check for document.
|
|
270
|
+
- {function} success - callback function to call pass result.
|
|
271
|
+
Will be passed an {boolean} which is `true` if a document exists.
|
|
272
|
+
- {function} error - callback function which will be passed a {string} error message as an argument.
|
|
273
|
+
|
|
274
|
+
```javascript
|
|
275
|
+
var documentId = "my_doc";
|
|
276
|
+
var collection = "my_collection";
|
|
277
|
+
FirebasexFirestore.documentExistsInFirestoreCollection(
|
|
278
|
+
documentId,
|
|
279
|
+
collection,
|
|
280
|
+
function (exists) {
|
|
281
|
+
console.log("Document " + (exists ? "exists" : "doesn't exist"));
|
|
282
|
+
},
|
|
283
|
+
function (error) {
|
|
284
|
+
console.error("Error fetching document: " + error);
|
|
285
|
+
}
|
|
286
|
+
);
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
## fetchDocumentInFirestoreCollection
|
|
290
|
+
|
|
291
|
+
Fetches an existing document with the given ID from a Firestore collection.
|
|
292
|
+
|
|
293
|
+
Notes:
|
|
294
|
+
|
|
295
|
+
- If no document with the specified ID exists in the collection, the error callback will be invoked.
|
|
296
|
+
- If the document contains references to another document, they will be converted to the document path string to avoid circular reference issues.
|
|
297
|
+
|
|
298
|
+
**Parameters**:
|
|
299
|
+
|
|
300
|
+
- {string} documentId - document ID of the document to fetch.
|
|
301
|
+
- {string} collection - name of top-level collection to fetch document from.
|
|
302
|
+
- {function} success - callback function to call on successfully fetching the document.
|
|
303
|
+
Will be passed an {object} contain the document contents.
|
|
304
|
+
- {function} error - callback function which will be passed a {string} error message as an argument.
|
|
305
|
+
|
|
306
|
+
```javascript
|
|
307
|
+
var documentId = "my_doc";
|
|
308
|
+
var collection = "my_collection";
|
|
309
|
+
FirebasexFirestore.fetchDocumentInFirestoreCollection(
|
|
310
|
+
documentId,
|
|
311
|
+
collection,
|
|
312
|
+
function (document) {
|
|
313
|
+
console.log(
|
|
314
|
+
"Successfully fetched document: " + JSON.stringify(document)
|
|
315
|
+
);
|
|
316
|
+
},
|
|
317
|
+
function (error) {
|
|
318
|
+
console.error("Error fetching document: " + error);
|
|
319
|
+
}
|
|
320
|
+
);
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
## fetchFirestoreCollection
|
|
324
|
+
|
|
325
|
+
Fetches all the documents in the specific collection.
|
|
326
|
+
|
|
327
|
+
Notes:
|
|
328
|
+
|
|
329
|
+
- If no collection with the specified name exists, the error callback will be invoked.
|
|
330
|
+
- If the documents in the collection contain references to another document, they will be converted to the document path string to avoid circular reference issues.
|
|
331
|
+
|
|
332
|
+
**Parameters**:
|
|
333
|
+
|
|
334
|
+
- {string} collection - name of top-level collection to fetch.
|
|
335
|
+
- {array} filters (optional) - a list of filters to sort/filter the documents returned from your collection.
|
|
336
|
+
|
|
337
|
+
- Supports `where`, `orderBy`, `startAt`, `endAt` and `limit` filters.
|
|
338
|
+
- See the [Firestore documentation](https://firebase.google.com/docs/firestore/query-data/queries) for more details.
|
|
339
|
+
- Each filter is defined as an array of filter components:
|
|
340
|
+
- `where`: [`where`, `fieldName`, `operator`, `value`, `valueType`]
|
|
341
|
+
- `fieldName` - name of field to match
|
|
342
|
+
- `operator` - operator to apply to match
|
|
343
|
+
- supported operators: `==`, `<`, `>`, `<=`, `>=`, `array-contains`
|
|
344
|
+
- `value` - field value to match
|
|
345
|
+
- `valueType` (optional) - type of variable to fetch value as
|
|
346
|
+
- supported types: `string`, `boolean`, `integer`, `double`, `long`
|
|
347
|
+
- if not specified, defaults to `string`
|
|
348
|
+
- `startAt`: [`startAt`, `value`, `valueType`]
|
|
349
|
+
- `value` - field value to start at
|
|
350
|
+
- `valueType` (optional) - type of variable to fetch value as (as above)
|
|
351
|
+
- `endAt`: [`endAt`, `value`, `valueType`]
|
|
352
|
+
- `value` - field value to end at
|
|
353
|
+
- `valueType` (optional) - type of variable to fetch value as (as above)
|
|
354
|
+
- `orderBy`: [`orderBy`, `fieldName`, `sortDirection`]
|
|
355
|
+
- `fieldName` - name of field to order by
|
|
356
|
+
- `sortDirection` - direction to order in: `asc` or `desc`
|
|
357
|
+
- `limit`: [`limit`, `value`]
|
|
358
|
+
- `value` - `integer` defining maximum number of results to return.
|
|
359
|
+
|
|
360
|
+
- {function} success - callback function to call on successfully fetching the collection.
|
|
361
|
+
Will be passed an {object} containing all the documents in the collection, indexed by document ID.
|
|
362
|
+
If a Firebase collection with that name does not exist or it contains no documents, the object will be empty.
|
|
363
|
+
- {function} error - callback function which will be passed a {string} error message as an argument.
|
|
364
|
+
|
|
365
|
+
```javascript
|
|
366
|
+
var collection = "my_collection";
|
|
367
|
+
var filters = [
|
|
368
|
+
["where", "my_string", "==", "foo"],
|
|
369
|
+
["where", "my_integer", ">=", 0, "integer"],
|
|
370
|
+
["where", "my_boolean", "==", true, "boolean"],
|
|
371
|
+
["orderBy", "an_integer", "desc"],
|
|
372
|
+
["startAt", "an_integer", 10, "integer"],
|
|
373
|
+
["endAt", "an_integer", 100, "integer"],
|
|
374
|
+
["limit", 100000],
|
|
375
|
+
];
|
|
376
|
+
|
|
377
|
+
FirebasexFirestore.fetchFirestoreCollection(
|
|
378
|
+
collection,
|
|
379
|
+
filters,
|
|
380
|
+
function (documents) {
|
|
381
|
+
console.log(
|
|
382
|
+
"Successfully fetched collection: " + JSON.stringify(documents)
|
|
383
|
+
);
|
|
384
|
+
},
|
|
385
|
+
function (error) {
|
|
386
|
+
console.error("Error fetching collection: " + error);
|
|
387
|
+
}
|
|
388
|
+
);
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
## listenToDocumentInFirestoreCollection
|
|
392
|
+
|
|
393
|
+
Adds a listener to detect real-time changes to the specified document.
|
|
394
|
+
|
|
395
|
+
Note: If the document contains references to another document, they will be converted to the document path string to avoid circular reference issues.
|
|
396
|
+
|
|
397
|
+
Upon adding a listener using this function, the success callback function will be invoked with an `id` event which specifies the native ID of the added listener.
|
|
398
|
+
This can be used to subsequently remove the listener using [`removeFirestoreListener()`](#removefirestorelistener).
|
|
399
|
+
For example:
|
|
400
|
+
|
|
401
|
+
```json
|
|
402
|
+
{
|
|
403
|
+
"eventType": "id",
|
|
404
|
+
"id": 12345
|
|
405
|
+
}
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
The callback will also be immediately invoked again with a `change` event which contains a snapshot of the document at the time of adding the listener.
|
|
409
|
+
Then each time the document is changed, either locally or remotely, the callback will be invoked with another `change` event detailing the change.
|
|
410
|
+
|
|
411
|
+
Event fields:
|
|
412
|
+
|
|
413
|
+
- `source` - specifies if the change was `local` (made locally on the app) or `remote` (made via the server).
|
|
414
|
+
- `fromCache` - specifies whether the snapshot was read from local cache
|
|
415
|
+
- `snapshot` - a snapshot of document at the time of the change.
|
|
416
|
+
- May not be present if change event is due to a metadata change.
|
|
417
|
+
|
|
418
|
+
For example:
|
|
419
|
+
|
|
420
|
+
```json
|
|
421
|
+
{
|
|
422
|
+
"eventType": "change",
|
|
423
|
+
"source": "remote",
|
|
424
|
+
"fromCache": true,
|
|
425
|
+
"snapshot": {
|
|
426
|
+
"a_field": "a_value"
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
See the [Firestore documentation](https://firebase.google.com/docs/firestore/query-data/listen) for more info on real-time listeners.
|
|
432
|
+
|
|
433
|
+
**Parameters**:
|
|
434
|
+
|
|
435
|
+
- {function} success - callback function to call on successfully adding the listener AND on subsequently detecting changes to that document.
|
|
436
|
+
Will be passed an {object} representing the `id` or `change` event.
|
|
437
|
+
- {function} error - callback function which will be passed a {string} error message as an argument.
|
|
438
|
+
- {string} documentId - document ID of the document to listen to.
|
|
439
|
+
- {string} collection - name of top-level collection to listen to the document in.
|
|
440
|
+
- {boolean} includeMetadata - whether to listen for changes to document metadata.
|
|
441
|
+
- Defaults to `false`.
|
|
442
|
+
- See [Events for metadata changes](https://firebase.google.com/docs/firestore/query-data/listen#events-metadata-changes) for more info.
|
|
443
|
+
|
|
444
|
+
```javascript
|
|
445
|
+
var documentId = "my_doc";
|
|
446
|
+
var collection = "my_collection";
|
|
447
|
+
var includeMetadata = true;
|
|
448
|
+
var listenerId;
|
|
449
|
+
|
|
450
|
+
FirebasexFirestore.listenToDocumentInFirestoreCollection(
|
|
451
|
+
function (event) {
|
|
452
|
+
switch (event.eventType) {
|
|
453
|
+
case "id":
|
|
454
|
+
listenerId = event.id;
|
|
455
|
+
console.log(
|
|
456
|
+
"Successfully added document listener with id=" + listenerId
|
|
457
|
+
);
|
|
458
|
+
break;
|
|
459
|
+
case "change":
|
|
460
|
+
console.log("Detected document change");
|
|
461
|
+
console.log("Source of change: " + event.source);
|
|
462
|
+
console.log("Read from local cache: " + event.fromCache);
|
|
463
|
+
if (event.snapshot) {
|
|
464
|
+
console.log(
|
|
465
|
+
"Document snapshot: " + JSON.stringify(event.snapshot)
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
break;
|
|
469
|
+
}
|
|
470
|
+
},
|
|
471
|
+
function (error) {
|
|
472
|
+
console.error("Error adding listener: " + error);
|
|
473
|
+
},
|
|
474
|
+
documentId,
|
|
475
|
+
collection,
|
|
476
|
+
includeMetadata
|
|
477
|
+
);
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
## listenToFirestoreCollection
|
|
481
|
+
|
|
482
|
+
Adds a listener to detect real-time changes to documents in a Firestore collection.
|
|
483
|
+
|
|
484
|
+
Note: If the documents in the collection contain references to another document, they will be converted to the document path string to avoid circular reference issues.
|
|
485
|
+
|
|
486
|
+
Upon adding a listener using this function, the success callback function will be invoked with an `id` event which specifies the native ID of the added listener.
|
|
487
|
+
This can be used to subsequently remove the listener using [`removeFirestoreListener()`](#removefirestorelistener).
|
|
488
|
+
For example:
|
|
489
|
+
|
|
490
|
+
```json
|
|
491
|
+
{
|
|
492
|
+
"eventType": "id",
|
|
493
|
+
"id": 12345
|
|
494
|
+
}
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
The callback will also be immediately invoked again with a `change` event which contains a snapshot of all documents in the collection at the time of adding the listener.
|
|
498
|
+
Then each time document(s) in the collection change, either locally or remotely, the callback will be invoked with another `change` event detailing the change.
|
|
499
|
+
|
|
500
|
+
Event fields:
|
|
501
|
+
|
|
502
|
+
- `documents` - key/value list of document changes indexed by document ID. For each document change:
|
|
503
|
+
- `source` - specifies if the change was `local` (made locally on the app) or `remote` (made via the server).
|
|
504
|
+
- `fromCache` - specifies whether the snapshot was read from local cache
|
|
505
|
+
- `type` - specifies the change type:
|
|
506
|
+
- `added` - document was added to collection
|
|
507
|
+
- `modified` - document was modified in collection
|
|
508
|
+
- `removed` - document was removed from collection
|
|
509
|
+
- `metadata` - document metadata changed
|
|
510
|
+
- `snapshot` - a snapshot of document at the time of the change.
|
|
511
|
+
- May not be present if change event is due to a metadata change.
|
|
512
|
+
|
|
513
|
+
For example:
|
|
514
|
+
|
|
515
|
+
```json
|
|
516
|
+
{
|
|
517
|
+
"eventType": "change",
|
|
518
|
+
"documents": {
|
|
519
|
+
"a_doc": {
|
|
520
|
+
"source": "remote",
|
|
521
|
+
"fromCache": false,
|
|
522
|
+
"type": "added",
|
|
523
|
+
"snapshot": {
|
|
524
|
+
"a_field": "a_value"
|
|
525
|
+
}
|
|
526
|
+
},
|
|
527
|
+
"another_doc": {
|
|
528
|
+
"source": "remote",
|
|
529
|
+
"fromCache": false,
|
|
530
|
+
"type": "removed",
|
|
531
|
+
"snapshot": {
|
|
532
|
+
"foo": "bar"
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
See the [Firestore documentation](https://firebase.google.com/docs/firestore/query-data/listen) for more info on real-time listeners.
|
|
540
|
+
|
|
541
|
+
**Parameters**:
|
|
542
|
+
|
|
543
|
+
- {function} success - callback function to call on successfully adding the listener AND on subsequently detecting changes to that collection.
|
|
544
|
+
Will be passed an {object} representing the `id` or `change` event.
|
|
545
|
+
- {function} error - callback function which will be passed a {string} error message as an argument.
|
|
546
|
+
- {string} collection - name of top-level collection to listen to the document in.
|
|
547
|
+
- {array} filters (optional) - a list of filters to sort/filter the documents returned from your collection.
|
|
548
|
+
- See [fetchFirestoreCollection](#fetchfirestorecollection)
|
|
549
|
+
- {boolean} includeMetadata (optional) - whether to listen for changes to document metadata.
|
|
550
|
+
- Defaults to `false`.
|
|
551
|
+
- See [Events for metadata changes](https://firebase.google.com/docs/firestore/query-data/listen#events-metadata-changes) for more info.
|
|
552
|
+
|
|
553
|
+
```javascript
|
|
554
|
+
var collection = "my_collection";
|
|
555
|
+
var filters = [
|
|
556
|
+
["where", "field", "==", "value"],
|
|
557
|
+
["orderBy", "field", "desc"],
|
|
558
|
+
];
|
|
559
|
+
var includeMetadata = true;
|
|
560
|
+
var listenerId;
|
|
561
|
+
|
|
562
|
+
FirebasexFirestore.listenToFirestoreCollection(
|
|
563
|
+
function (event) {
|
|
564
|
+
switch (event.eventType) {
|
|
565
|
+
case "id":
|
|
566
|
+
listenerId = event.id;
|
|
567
|
+
console.log(
|
|
568
|
+
"Successfully added collection listener with id=" +
|
|
569
|
+
listenerId
|
|
570
|
+
);
|
|
571
|
+
break;
|
|
572
|
+
case "change":
|
|
573
|
+
console.log("Detected collection change");
|
|
574
|
+
if (event.documents) {
|
|
575
|
+
for (var documentId in event.documents) {
|
|
576
|
+
console.log("Document ID: " + documentId);
|
|
577
|
+
|
|
578
|
+
var docChange = event.documents[documentId];
|
|
579
|
+
console.log("Source of change: " + docChange.source);
|
|
580
|
+
console.log("Change type: " + docChange.type);
|
|
581
|
+
console.log(
|
|
582
|
+
"Read from local cache: " + docChange.fromCache
|
|
583
|
+
);
|
|
584
|
+
if (docChange.snapshot) {
|
|
585
|
+
console.log(
|
|
586
|
+
"Document snapshot: " +
|
|
587
|
+
JSON.stringify(docChange.snapshot)
|
|
588
|
+
);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
},
|
|
595
|
+
function (error) {
|
|
596
|
+
console.error("Error adding listener: " + error);
|
|
597
|
+
},
|
|
598
|
+
collection,
|
|
599
|
+
filters,
|
|
600
|
+
includeMetadata
|
|
601
|
+
);
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
## removeFirestoreListener
|
|
605
|
+
|
|
606
|
+
Removes an existing native Firestore listener (see [detaching listeners](https://firebase.google.com/docs/firestore/query-data/listen#detach_a_listener)) added with [`listenToDocumentInFirestoreCollection()`](#listentodocumentinfirestorecollection) or [`listenToFirestoreCollection()`](#listentofirestorecollection).
|
|
607
|
+
|
|
608
|
+
Upon adding a listener using either of the above functions, the success callback function will be invoked with an `id` event which specifies the native ID of the added listener.
|
|
609
|
+
For example:
|
|
610
|
+
|
|
611
|
+
```json
|
|
612
|
+
{
|
|
613
|
+
"eventType": "id",
|
|
614
|
+
"id": 12345
|
|
615
|
+
}
|
|
616
|
+
```
|
|
617
|
+
|
|
618
|
+
This can be used to subsequently remove the listener using this function.
|
|
619
|
+
You should remove listeners when you're not using them as while active they maintain a continual HTTP connection to the Firebase servers costing memory, bandwidth and money: see [best practices for realtime updates](https://firebase.google.com/docs/firestore/best-practices#realtime_updates) and [billing for realtime updates](https://firebase.google.com/docs/firestore/pricing#listens).
|
|
620
|
+
|
|
621
|
+
**Parameters**:
|
|
622
|
+
|
|
623
|
+
- {function} success - callback function to call on successfully removing the listener.
|
|
624
|
+
- {function} error - callback function which will be passed a {string} error message as an argument.
|
|
625
|
+
- {string|number} listenerId - ID of the listener to remove
|
|
626
|
+
|
|
627
|
+
```javascript
|
|
628
|
+
FirebasexFirestore.removeFirestoreListener(
|
|
629
|
+
function () {
|
|
630
|
+
console.log("Successfully removed listener");
|
|
631
|
+
},
|
|
632
|
+
function (error) {
|
|
633
|
+
console.error("Error removing listener: " + error);
|
|
634
|
+
},
|
|
635
|
+
listenerId
|
|
636
|
+
);
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
# Reporting issues
|
|
640
|
+
|
|
641
|
+
Before reporting an issue with this plugin, please do the following:
|
|
642
|
+
- Check the existing [issues](https://github.com/dpa99c/cordova-plugin-firebasex-firestore/issues) to see if the issue has already been reported.
|
|
643
|
+
- Check the [issue template](https://github.com/dpa99c/cordova-plugin-firebasex-firestore/issues/new/choose) and provide all requested information.
|
|
644
|
+
- The more information and context you provide, the easier it is for the maintainers to understand the issue and provide a resolution.
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cordova-plugin-firebasex-firestore",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Firestore module for cordova-plugin-firebasex",
|
|
5
|
+
"cordova": {
|
|
6
|
+
"id": "cordova-plugin-firebasex-firestore",
|
|
7
|
+
"platforms": ["android", "ios"]
|
|
8
|
+
},
|
|
9
|
+
"keywords": ["cordova", "firebase", "firestore"],
|
|
10
|
+
"author": "Dave Alden",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"cordova-plugin-firebasex-core": "^1.0.0"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"xml-js": "^1.6.11"
|
|
17
|
+
}
|
|
18
|
+
}
|