@react-native-firebase/firestore 18.4.0 → 18.5.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,218 @@
1
+ /**
2
+ * @typedef {import('..').FirebaseFirestoreTypes} FirebaseFirestoreTypes
3
+ * @typedef {import('..').FirebaseFirestoreTypes.CollectionReference} CollectionReference
4
+ * @typedef {import('..').FirebaseFirestoreTypes.DocumentData} DocumentData
5
+ * @typedef {import('..').FirebaseFirestoreTypes.DocumentReference} DocumentReference
6
+ * @typedef {import('..').FirebaseFirestoreTypes.FieldPath} FieldPath
7
+ * @typedef {import('..').FirebaseFirestoreTypes.Module} Firestore
8
+ * @typedef {import('..').FirebaseFirestoreTypes.Query} Query
9
+ * @typedef {import('..').FirebaseFirestoreTypes.SetOptions} SetOptions
10
+ * @typedef {import('..').FirebaseFirestoreTypes.Settings} FirestoreSettings
11
+ * @typedef {import('@firebase/app').FirebaseApp} FirebaseApp
12
+ */
13
+
14
+ import { firebase } from '../index';
15
+
16
+ /**
17
+ * @param {FirebaseApp?} app
18
+ * @returns {Firestore}
19
+ */
20
+ export function getFirestore(app) {
21
+ if (app) {
22
+ return firebase.firestore(app);
23
+ }
24
+
25
+ return firebase.firestore();
26
+ }
27
+
28
+ /**
29
+ * @param {Firestore | CollectionReference | DocumentReference<unknown>} parent
30
+ * @param {string?} path
31
+ * @param {string?} pathSegments
32
+ * @returns {DocumentReference}
33
+ */
34
+ export function doc(parent, path, ...pathSegments) {
35
+ if (pathSegments && pathSegments.length) {
36
+ path = path + '/' + pathSegments.map(e => e.replace(/^\/|\/$/g, '')).join('/');
37
+ }
38
+
39
+ return parent.doc(path);
40
+ }
41
+
42
+ /**
43
+ * @param {Firestore | DocumentReference<unknown> | CollectionReference<unknown>} parent
44
+ * @param {string} path
45
+ * @param {string?} pathSegments
46
+ * @returns {CollectionReference<DocumentData>}
47
+ */
48
+ export function collection(parent, path, ...pathSegments) {
49
+ if (pathSegments && pathSegments.length) {
50
+ path = path + '/' + pathSegments.map(e => e.replace(/^\/|\/$/g, '')).join('/');
51
+ }
52
+
53
+ return parent.collection(path);
54
+ }
55
+
56
+ /**
57
+ * @param {Firestore} firestore
58
+ * @param {string} collectionId
59
+ * @returns {Query<DocumentData>}
60
+ */
61
+ export function collectionGroup(firestore, collectionId) {
62
+ return firestore.collectionGroup(collectionId);
63
+ }
64
+
65
+ /**
66
+ * @param {DocumentReference} reference
67
+ * @param {import('.').PartialWithFieldValue} data
68
+ * @param {SetOptions?} options
69
+ * @returns {Promise<void>}
70
+ */
71
+ export function setDoc(reference, data, options) {
72
+ return reference.set(data, options);
73
+ }
74
+
75
+ /**
76
+ * @param {DocumentReference} reference
77
+ * @param {string | FieldPath | import('.').UpdateData} fieldOrUpdateData
78
+ * @param {unknown?} value
79
+ * @param {unknown} moreFieldsAndValues
80
+ * @returns {Promise<void>}
81
+ */
82
+ export function updateDoc(reference, fieldOrUpdateData, value, ...moreFieldsAndValues) {
83
+ if (!fieldOrUpdateData) {
84
+ // @ts-ignore
85
+ return reference.update();
86
+ }
87
+
88
+ if (!value) {
89
+ return reference.update(fieldOrUpdateData);
90
+ }
91
+
92
+ if (!moreFieldsAndValues || !Array.isArray(moreFieldsAndValues)) {
93
+ return reference.update(fieldOrUpdateData, value);
94
+ }
95
+
96
+ return reference.update(fieldOrUpdateData, value, ...moreFieldsAndValues);
97
+ }
98
+
99
+ /**
100
+ * @param {CollectionReference} reference
101
+ * @param {WithFieldValue} data
102
+ * @returns {Promise<DocumentReference>}
103
+ */
104
+ export function addDoc(reference, data) {
105
+ return reference.add(data);
106
+ }
107
+
108
+ /**
109
+ * @param {Firestore} firestore
110
+ * @returns {Promise<void>}
111
+ */
112
+ export function enableNetwork(firestore) {
113
+ return firestore.enableNetwork();
114
+ }
115
+
116
+ /**
117
+ * @param {Firestore} firestore
118
+ * @returns {Promise<void>}
119
+ */
120
+ export function disableNetwork(firestore) {
121
+ return firestore.disableNetwork();
122
+ }
123
+
124
+ /**
125
+ * @param {Firestore} firestore
126
+ * @returns {Promise<void>}
127
+ */
128
+ export function clearPersistence(firestore) {
129
+ return firestore.clearPersistence();
130
+ }
131
+
132
+ /**
133
+ * @param {Firestore} firestore
134
+ * @returns {Promise<void>}
135
+ */
136
+ export function terminate(firestore) {
137
+ return firestore.terminate();
138
+ }
139
+
140
+ /**
141
+ * @param {Firestore} firestore
142
+ * @returns {Promise<void>}
143
+ */
144
+ export function waitForPendingWrites(firestore) {
145
+ return firestore.waitForPendingWrites();
146
+ }
147
+
148
+ /**
149
+ * @param {FirebaseApp} app
150
+ * @param {FirestoreSettings} settings
151
+ * @param {string?} databaseId
152
+ * @returns {Promise<Firestore>}
153
+ */
154
+ export async function initializeFirestore(app, settings /* databaseId */) {
155
+ // TODO(exaby73): implement 2nd database once it's supported
156
+ const firestore = firebase.firestore(app);
157
+ await firestore.settings(settings);
158
+ return firestore;
159
+ }
160
+
161
+ /**
162
+ * @param {import('./').LogLevel} logLevel
163
+ * @returns {void}
164
+ */
165
+ export function setLogLevel(logLevel) {
166
+ return firebase.firestore.setLogLevel(logLevel);
167
+ }
168
+
169
+ /**
170
+ * @param {Firestore} firestore
171
+ * @param {(transaction: FirebaseFirestoreTypes.Transaction) => Promise} updateFunction
172
+ * @returns {Promise}
173
+ */
174
+ export function runTransaction(firestore, updateFunction) {
175
+ return firestore.runTransaction(updateFunction);
176
+ }
177
+
178
+ /**
179
+ * @param {Query} query
180
+ * @returns {Promise<FirebaseFirestoreTypes.AggregateQuerySnapshot>}
181
+ */
182
+ export function getCountFromServer(query) {
183
+ return query.count().get();
184
+ }
185
+
186
+ /**
187
+ * @param {Firestore} firestore
188
+ * @param {ReadableStream<Uint8Array> | ArrayBuffer | string} bundleData
189
+ * @returns {import('.').LoadBundleTask}
190
+ */
191
+ export function loadBundle(firestore, bundleData) {
192
+ return firestore.loadBundle(bundleData);
193
+ }
194
+
195
+ /**
196
+ * @param {Firestore} firestore
197
+ * @param {string} name
198
+ * @returns {Query<DocumentData>}
199
+ */
200
+ export function namedQuery(firestore, name) {
201
+ return firestore.namedQuery(name);
202
+ }
203
+
204
+ /**
205
+ * @param {Firestore} firestore
206
+ * @returns {FirebaseFirestoreTypes.WriteBatch}
207
+ */
208
+ export function writeBatch(firestore) {
209
+ return firestore.batch();
210
+ }
211
+
212
+ export * from './query';
213
+ export * from './snapshot';
214
+ export * from './Bytes';
215
+ export * from './FieldPath';
216
+ export * from './FieldValue';
217
+ export * from './GeoPoint';
218
+ export * from './Timestamp';
@@ -0,0 +1,344 @@
1
+ import { FirebaseFirestoreTypes } from '../..';
2
+
3
+ import Query = FirebaseFirestoreTypes.Query;
4
+ import QueryCompositeFilterConstraint = FirebaseFirestoreTypes.QueryCompositeFilterConstraint;
5
+ import WhereFilterOp = FirebaseFirestoreTypes.WhereFilterOp;
6
+ import FieldPath = FirebaseFirestoreTypes.FieldPath;
7
+ import QuerySnapshot = FirebaseFirestoreTypes.QuerySnapshot;
8
+ import DocumentReference = FirebaseFirestoreTypes.DocumentReference;
9
+ import DocumentSnapshot = FirebaseFirestoreTypes.DocumentSnapshot;
10
+ import DocumentData = FirebaseFirestoreTypes.DocumentData;
11
+
12
+ /** Describes the different query constraints available in this SDK. */
13
+ export type QueryConstraintType =
14
+ | 'where'
15
+ | 'orderBy'
16
+ | 'limit'
17
+ | 'limitToLast'
18
+ | 'startAt'
19
+ | 'startAfter'
20
+ | 'endAt'
21
+ | 'endBefore';
22
+
23
+ /**
24
+ * An `AppliableConstraint` is an abstraction of a constraint that can be applied
25
+ * to a Firestore query.
26
+ */
27
+ export interface AppliableConstraint {
28
+ /**
29
+ * Takes the provided {@link Query} and returns a copy of the {@link Query} with this
30
+ * {@link AppliableConstraint} applied.
31
+ */
32
+ _apply<T>(query: Query<T>): Query<T>;
33
+ }
34
+
35
+ /**
36
+ * A `QueryConstraint` is used to narrow the set of documents returned by a
37
+ * Firestore query. `QueryConstraint`s are created by invoking {@link where},
38
+ * {@link orderBy}, {@link (startAt:1)}, {@link (startAfter:1)}, {@link
39
+ * (endBefore:1)}, {@link (endAt:1)}, {@link limit}, {@link limitToLast} and
40
+ * can then be passed to {@link (query:1)} to create a new query instance that
41
+ * also contains this `QueryConstraint`.
42
+ */
43
+ export interface IQueryConstraint extends AppliableConstraint {
44
+ /** The type of this query constraint */
45
+ readonly type: QueryConstraintType;
46
+
47
+ /**
48
+ * Takes the provided {@link Query} and returns a copy of the {@link Query} with this
49
+ * {@link AppliableConstraint} applied.
50
+ */
51
+ _apply<T>(query: Query<T>): Query<T>;
52
+ }
53
+
54
+ export class QueryOrderByConstraint extends QueryConstraint {
55
+ readonly type: QueryConstraintType = 'orderBy';
56
+ }
57
+
58
+ export class QueryLimitConstraint extends QueryConstraint {
59
+ readonly type: QueryConstraintType = 'limit';
60
+ }
61
+
62
+ export class QueryStartAtConstraint extends QueryConstraint {
63
+ readonly type: QueryConstraintType = 'startAt';
64
+ }
65
+
66
+ export class QueryEndAtConstraint extends QueryConstraint {
67
+ readonly type: QueryConstraintType = 'endAt';
68
+ }
69
+
70
+ export class QueryFieldFilterConstraint extends QueryConstraint {
71
+ readonly type: QueryConstraintType = 'where';
72
+ }
73
+
74
+ /**
75
+ * `QueryNonFilterConstraint` is a helper union type that represents
76
+ * QueryConstraints which are used to narrow or order the set of documents,
77
+ * but that do not explicitly filter on a document field.
78
+ * `QueryNonFilterConstraint`s are created by invoking {@link orderBy},
79
+ * {@link (startAt:1)}, {@link (startAfter:1)}, {@link (endBefore:1)}, {@link (endAt:1)},
80
+ * {@link limit} or {@link limitToLast} and can then be passed to {@link (query:1)}
81
+ * to create a new query instance that also contains the `QueryConstraint`.
82
+ */
83
+ export type QueryNonFilterConstraint =
84
+ | QueryOrderByConstraint
85
+ | QueryLimitConstraint
86
+ | QueryStartAtConstraint
87
+ | QueryEndAtConstraint;
88
+
89
+ /**
90
+ * Creates a new immutable instance of {@link Query} that is extended to also
91
+ * include additional query constraints.
92
+ *
93
+ * @param query - The {@link Query} instance to use as a base for the new
94
+ * constraints.
95
+ * @param compositeFilter - The {@link QueryCompositeFilterConstraint} to
96
+ * apply. Create {@link QueryCompositeFilterConstraint} using {@link and} or
97
+ * {@link or}.
98
+ * @param queryConstraints - Additional {@link QueryNonFilterConstraint}s to
99
+ * apply (e.g. {@link orderBy}, {@link limit}).
100
+ * @throws if any of the provided query constraints cannot be combined with the
101
+ * existing or new constraints.
102
+ */
103
+ export function query<T>(
104
+ query: Query<T>,
105
+ compositeFilter: QueryCompositeFilterConstraint,
106
+ ...queryConstraints: QueryNonFilterConstraint[]
107
+ ): Query<T>;
108
+
109
+ /**
110
+ * Creates a new immutable instance of {@link Query} that is extended to also
111
+ * include additional query constraints.
112
+ *
113
+ * @param query - The {@link Query} instance to use as a base for the new
114
+ * constraints.
115
+ * @param queryConstraints - The list of {@link IQueryConstraint}s to apply.
116
+ * @throws if any of the provided query constraints cannot be combined with the
117
+ * existing or new constraints.
118
+ */
119
+ export function query<T>(query: Query<T>, ...queryConstraints: IQueryConstraint[]): Query<T>;
120
+
121
+ export function query<T>(
122
+ query: Query<T>,
123
+ queryConstraint: QueryCompositeFilterConstraint | IQueryConstraint | undefined,
124
+ ...additionalQueryConstraints: Array<IQueryConstraint | QueryNonFilterConstraint>
125
+ ): Query<T>;
126
+
127
+ /**
128
+ * Creates a {@link QueryFieldFilterConstraint} that enforces that documents
129
+ * must contain the specified field and that the value should satisfy the
130
+ * relation constraint provided.
131
+ *
132
+ * @param fieldPath - The path to compare
133
+ * @param opStr - The operation string (e.g "&lt;", "&lt;=", "==", "&lt;",
134
+ * "&lt;=", "!=").
135
+ * @param value - The value for comparison
136
+ * @returns The created {@link QueryFieldFilterConstraint}.
137
+ */
138
+ export function where(
139
+ fieldPath: string | FieldPath,
140
+ opStr: WhereFilterOp,
141
+ value: unknown,
142
+ ): QueryFieldFilterConstraint;
143
+
144
+ /**
145
+ * The or() function used to generate a logical OR query.
146
+ * e.g. or(where('name', '==', 'Ada'), where('name', '==', 'Bob'))
147
+ */
148
+ export function or(...queries: QueryFilterConstraint[]): QueryCompositeFilterConstraint;
149
+
150
+ /**
151
+ * The and() function used to generate a logical AND query.
152
+ * e.g. and(where('name', '==', 'Ada'), where('name', '==', 'Bob'))
153
+ */
154
+ export function and(...queries: QueryFilterConstraint[]): QueryCompositeFilterConstraint;
155
+
156
+ /**
157
+ * The direction of a {@link orderBy} clause is specified as 'desc' or 'asc'
158
+ * (descending or ascending).
159
+ */
160
+ export type OrderByDirection = 'desc' | 'asc';
161
+
162
+ /**
163
+ * Creates a {@link QueryOrderByConstraint} that sorts the query result by the
164
+ * specified field, optionally in descending order instead of ascending.
165
+ *
166
+ * Note: Documents that do not contain the specified field will not be present
167
+ * in the query result.
168
+ *
169
+ * @param fieldPath - The field to sort by.
170
+ * @param directionStr - Optional direction to sort by ('asc' or 'desc'). If
171
+ * not specified, order will be ascending.
172
+ * @returns The created {@link QueryOrderByConstraint}.
173
+ */
174
+ export function orderBy(
175
+ fieldPath: string | FieldPath,
176
+ directionStr: OrderByDirection = 'asc',
177
+ ): QueryOrderByConstraint;
178
+
179
+ /**
180
+ * Creates a {@link QueryStartAtConstraint} that modifies the result set to
181
+ * start at the provided document (inclusive). The starting position is relative
182
+ * to the order of the query. The document must contain all of the fields
183
+ * provided in the `orderBy` of this query.
184
+ *
185
+ * @param snapshot - The snapshot of the document to start at.
186
+ * @returns A {@link QueryStartAtConstraint} to pass to `query()`.
187
+ */
188
+ export function startAt(snapshot: DocumentSnapshot<unknown>): QueryStartAtConstraint;
189
+ /**
190
+ *
191
+ * Creates a {@link QueryStartAtConstraint} that modifies the result set to
192
+ * start at the provided fields relative to the order of the query. The order of
193
+ * the field values must match the order of the order by clauses of the query.
194
+ *
195
+ * @param fieldValues - The field values to start this query at, in order
196
+ * of the query's order by.
197
+ * @returns A {@link QueryStartAtConstraint} to pass to `query()`.
198
+ */
199
+ export function startAt(...fieldValues: unknown[]): QueryStartAtConstraint;
200
+
201
+ export function startAt(
202
+ ...docOrFields: Array<unknown | DocumentSnapshot<unknown>>
203
+ ): QueryStartAtConstraint;
204
+
205
+ /**
206
+ * Creates a {@link QueryStartAtConstraint} that modifies the result set to
207
+ * start after the provided document (exclusive). The starting position is
208
+ * relative to the order of the query. The document must contain all of the
209
+ * fields provided in the orderBy of the query.
210
+ *
211
+ * @param snapshot - The snapshot of the document to start after.
212
+ * @returns A {@link QueryStartAtConstraint} to pass to `query()`
213
+ */
214
+ export function startAfter<AppModelType, DbModelType extends DocumentData>(
215
+ snapshot: DocumentSnapshot<AppModelType, DbModelType>,
216
+ ): QueryStartAtConstraint;
217
+
218
+ /**
219
+ * Creates a {@link QueryStartAtConstraint} that modifies the result set to
220
+ * start after the provided fields relative to the order of the query. The order
221
+ * of the field values must match the order of the order by clauses of the query.
222
+ *
223
+ * @param fieldValues - The field values to start this query after, in order
224
+ * of the query's order by.
225
+ * @returns A {@link QueryStartAtConstraint} to pass to `query()`
226
+ */
227
+ export function startAfter(...fieldValues: unknown[]): QueryStartAtConstraint;
228
+
229
+ export function startAfter<AppModelType, DbModelType extends DocumentData>(
230
+ ...docOrFields: Array<unknown | DocumentSnapshot<AppModelType, DbModelType>>
231
+ ): QueryStartAtConstraint;
232
+
233
+ /**
234
+ * Creates a {@link QueryLimitConstraint} that only returns the first matching
235
+ * documents.
236
+ *
237
+ * @param limit - The maximum number of items to return.
238
+ * @returns The created {@link QueryLimitConstraint}.
239
+ */
240
+ export function limit(limit: number): QueryLimitConstraint;
241
+
242
+ /**
243
+ * Executes the query and returns the results as a `QuerySnapshot`.
244
+ *
245
+ * Note: `getDocs()` attempts to provide up-to-date data when possible by
246
+ * waiting for data from the server, but it may return cached data or fail if
247
+ * you are offline and the server cannot be reached. To specify this behavior,
248
+ * invoke {@link getDocsFromCache} or {@link getDocsFromServer}.
249
+ *
250
+ * @returns A `Promise` that will be resolved with the results of the query.
251
+ */
252
+ export function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>>;
253
+
254
+ /**
255
+ * Executes the query and returns the results as a `QuerySnapshot` from cache.
256
+ * Returns an empty result set if no documents matching the query are currently
257
+ * cached.
258
+ *
259
+ * @returns A `Promise` that will be resolved with the results of the query.
260
+ */
261
+ export function getDocsFromCache<T>(query: Query<T>): Promise<QuerySnapshot<T>>;
262
+
263
+ /**
264
+ * Executes the query and returns the results as a `QuerySnapshot` from the
265
+ * server. Returns an error if the network is not available.
266
+ *
267
+ * @returns A `Promise` that will be resolved with the results of the query.
268
+ */
269
+ export function getDocsFromServer<T>(query: Query<T>): Promise<QuerySnapshot<T>>;
270
+
271
+ /**
272
+ * Deletes the document referred to by the specified `DocumentReference`.
273
+ *
274
+ * @param reference - A reference to the document to delete.
275
+ * @returns A Promise resolved once the document has been successfully
276
+ * deleted from the backend (note that it won't resolve while you're offline).
277
+ */
278
+ export function deleteDoc(reference: DocumentReference<unknown>): Promise<void>;
279
+
280
+ /**
281
+ * Creates a `QueryConstraint` with the specified ending point.
282
+ *
283
+ * Using `startAt()`, `startAfter()`, `endBefore()`, `endAt()` and `equalTo()`
284
+ * allows you to choose arbitrary starting and ending points for your queries.
285
+ *
286
+ * The ending point is inclusive, so children with exactly the specified value
287
+ * will be included in the query. The optional key argument can be used to
288
+ * further limit the range of the query. If it is specified, then children that
289
+ * have exactly the specified value must also have a key name less than or equal
290
+ * to the specified key.
291
+ *
292
+ * You can read more about `endAt()` in
293
+ * {@link https://firebase.google.com/docs/database/web/lists-of-data#filtering_data | Filtering data}.
294
+ *
295
+ * @param value - The value to end at. The argument type depends on which
296
+ * `orderBy*()` function was used in this query. Specify a value that matches
297
+ * the `orderBy*()` type. When used in combination with `orderByKey()`, the
298
+ * value must be a string.
299
+ * @param key - The child key to end at, among the children with the previously
300
+ * specified priority. This argument is only allowed if ordering by child,
301
+ * value, or priority.
302
+ */
303
+ export function endAt(value: number | string | boolean | null, key?: string): QueryConstraint;
304
+
305
+ /**
306
+ * Creates a `QueryConstraint` with the specified ending point (exclusive).
307
+ *
308
+ * Using `startAt()`, `startAfter()`, `endBefore()`, `endAt()` and `equalTo()`
309
+ * allows you to choose arbitrary starting and ending points for your queries.
310
+ *
311
+ * The ending point is exclusive. If only a value is provided, children
312
+ * with a value less than the specified value will be included in the query.
313
+ * If a key is specified, then children must have a value less than or equal
314
+ * to the specified value and a key name less than the specified key.
315
+ *
316
+ * @param value - The value to end before. The argument type depends on which
317
+ * `orderBy*()` function was used in this query. Specify a value that matches
318
+ * the `orderBy*()` type. When used in combination with `orderByKey()`, the
319
+ * value must be a string.
320
+ * @param key - The child key to end before, among the children with the
321
+ * previously specified priority. This argument is only allowed if ordering by
322
+ * child, value, or priority.
323
+ */
324
+ export function endBefore(value: number | string | boolean | null, key?: string): QueryConstraint;
325
+
326
+ /**
327
+ * Creates a new `QueryConstraint` that is limited to return only the last
328
+ * specified number of children.
329
+ *
330
+ * The `limitToLast()` method is used to set a maximum number of children to be
331
+ * synced for a given callback. If we set a limit of 100, we will initially only
332
+ * receive up to 100 `child_added` events. If we have fewer than 100 messages
333
+ * stored in our Database, a `child_added` event will fire for each message.
334
+ * However, if we have over 100 messages, we will only receive a `child_added`
335
+ * event for the last 100 ordered messages. As items change, we will receive
336
+ * `child_removed` events for each item that drops out of the active list so
337
+ * that the total number stays at 100.
338
+ *
339
+ * You can read more about `limitToLast()` in
340
+ * {@link https://firebase.google.com/docs/database/web/lists-of-data#filtering_data | Filtering data}.
341
+ *
342
+ * @param limit - The maximum number of nodes to include in this query.
343
+ */
344
+ export function limitToLast(limit: number): QueryConstraint;