-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathindex.js
More file actions
411 lines (368 loc) · 12.9 KB
/
Copy pathindex.js
File metadata and controls
411 lines (368 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
* @typedef {import('..').FirebaseFirestoreTypes} FirebaseFirestoreTypes
* @typedef {import('..').FirebaseFirestoreTypes.CollectionReference} CollectionReference
* @typedef {import('..').FirebaseFirestoreTypes.DocumentData} DocumentData
* @typedef {import('..').FirebaseFirestoreTypes.DocumentReference} DocumentReference
* @typedef {import('..').FirebaseFirestoreTypes.FieldPath} FieldPath
* @typedef {import('..').FirebaseFirestoreTypes.Module} Firestore
* @typedef {import('..').FirebaseFirestoreTypes.Query} Query
* @typedef {import('..').FirebaseFirestoreTypes.SetOptions} SetOptions
* @typedef {import('..').FirebaseFirestoreTypes.Settings} FirestoreSettings
* @typedef {import('..').FirebaseFirestoreTypes.PersistentCacheIndexManager} PersistentCacheIndexManager
* @typedef {import('@firebase/app').FirebaseApp} FirebaseApp
*/
import { getApp, setLogLevel as appSetLogLevel } from '@react-native-firebase/app';
import { isObject } from '@react-native-firebase/app/lib/common';
import {
FirestoreAggregateQuerySnapshot,
AggregateField,
AggregateType,
fieldPathFromArgument,
} from '../FirestoreAggregate';
import FirestoreQuery from '../FirestoreQuery';
import { MODULAR_DEPRECATION_ARG } from '../../../app/lib/common';
/**
* @param {FirebaseApp?} app
* @param {String?} databaseId
* @returns {Firestore}
*/
export function getFirestore(app, databaseId) {
if (app) {
if (databaseId) {
return getApp(app.name).firestore(databaseId);
} else {
return getApp(app.name).firestore();
}
}
if (databaseId) {
return getApp().firestore(databaseId);
}
return getApp().firestore();
}
/**
* @param {Firestore | CollectionReference | DocumentReference<unknown>} parent
* @param {string?} path
* @param {string?} pathSegments
* @returns {DocumentReference}
*/
export function doc(parent, path, ...pathSegments) {
if (pathSegments && pathSegments.length) {
path = path + '/' + pathSegments.map(e => e.replace(/^\/|\/$/g, '')).join('/');
}
return parent.doc.call(parent, path, MODULAR_DEPRECATION_ARG);
}
/**
* @param {Firestore | DocumentReference<unknown> | CollectionReference<unknown>} parent
* @param {string} path
* @param {string?} pathSegments
* @returns {CollectionReference<DocumentData>}
*/
export function collection(parent, path, ...pathSegments) {
if (pathSegments && pathSegments.length) {
path = path + '/' + pathSegments.map(e => e.replace(/^\/|\/$/g, '')).join('/');
}
return parent.collection.call(parent, path, MODULAR_DEPRECATION_ARG);
}
/**
* @param {DocumentReference<AppModelType, DbModelType> | CollectionReference<AppModelType, DbModelType>} left
* @param {DocumentReference<AppModelType, DbModelType> | CollectionReference<AppModelType, DbModelType>} right
* @return boolean true if the two references are equal
*/
export function refEqual(left, right) {
return left.isEqual.call(left, right, MODULAR_DEPRECATION_ARG);
}
/**
* @param {Firestore} firestore
* @param {string} collectionId
* @returns {Query<DocumentData>}
*/
export function collectionGroup(firestore, collectionId) {
return firestore.collectionGroup.call(firestore, collectionId, MODULAR_DEPRECATION_ARG);
}
let _id_SnapshotInSync = 0;
export function onSnapshotsInSync(firestore, callback) {
const listenerId = _id_SnapshotInSync++;
firestore.native.addSnapshotsInSync(listenerId);
const onSnapshotsInSyncSubscription = firestore.emitter.addListener(
firestore.eventNameForApp(`firestore_snapshots_in_sync_event:${listenerId}`),
() => {
callback();
},
);
return () => {
onSnapshotsInSyncSubscription.remove();
firestore.native.removeSnapshotsInSync(listenerId);
};
}
/**
* @param {DocumentReference} reference
* @param {import('.').PartialWithFieldValue} data
* @param {SetOptions?} options
* @returns {Promise<void>}
*/
export function setDoc(reference, data, options) {
return reference.set.call(reference, data, options, MODULAR_DEPRECATION_ARG);
}
/**
* @param {DocumentReference} reference
* @param {string | FieldPath | import('.').UpdateData} fieldOrUpdateData
* @param {unknown?} value
* @param {unknown} moreFieldsAndValues
* @returns {Promise<void>}
*/
export function updateDoc(reference, fieldOrUpdateData, value, ...moreFieldsAndValues) {
if (!fieldOrUpdateData) {
// @ts-ignore
return reference.update.call(reference, MODULAR_DEPRECATION_ARG);
}
if (!value) {
return reference.update.call(reference, fieldOrUpdateData, MODULAR_DEPRECATION_ARG);
}
if (!moreFieldsAndValues || !Array.isArray(moreFieldsAndValues)) {
return reference.update.call(reference, fieldOrUpdateData, value, MODULAR_DEPRECATION_ARG);
}
return reference.update.call(
reference,
fieldOrUpdateData,
value,
...moreFieldsAndValues,
MODULAR_DEPRECATION_ARG,
);
}
/**
* @param {CollectionReference} reference
* @param {WithFieldValue} data
* @returns {Promise<DocumentReference>}
*/
export function addDoc(reference, data) {
return reference.add.call(reference, data, MODULAR_DEPRECATION_ARG);
}
/**
* @param {Firestore} firestore
* @returns {Promise<void>}
*/
export function enableNetwork(firestore) {
return firestore.enableNetwork.call(firestore, MODULAR_DEPRECATION_ARG);
}
/**
* @param {Firestore} firestore
* @returns {Promise<void>}
*/
export function disableNetwork(firestore) {
return firestore.disableNetwork.call(firestore, MODULAR_DEPRECATION_ARG);
}
/**
* @param {Firestore} firestore
* @returns {Promise<void>}
*/
export function clearPersistence(firestore) {
// this will call deprecation warning as it isn't part of firebase-js-sdk API
return firestore.clearPersistence();
}
/**
* @param {Firestore} firestore
* @returns {Promise<void>}
*/
export function clearIndexedDbPersistence(firestore) {
return firestore.clearPersistence.call(firestore, MODULAR_DEPRECATION_ARG);
}
/**
* @param {Firestore} firestore
* @returns {Promise<void>}
*/
export function terminate(firestore) {
return firestore.terminate.call(firestore, MODULAR_DEPRECATION_ARG);
}
/**
* @param {Firestore} firestore
* @returns {Promise<void>}
*/
export function waitForPendingWrites(firestore) {
return firestore.waitForPendingWrites.call(firestore, MODULAR_DEPRECATION_ARG);
}
/**
* @param {FirebaseApp} app
* @param {FirestoreSettings} settings
* @param {string?} databaseId
* @returns {Promise<Firestore>}
*/
export async function initializeFirestore(app, settings /* databaseId */) {
// TODO(exaby73): implement 2nd database once it's supported
const firebase = getApp(app.name);
const firestore = firebase.firestore();
await firestore.settings.call(firestore, settings, MODULAR_DEPRECATION_ARG);
return firestore;
}
export function connectFirestoreEmulator(firestore, host, port, options) {
return firestore.useEmulator.call(firestore, host, port, options, MODULAR_DEPRECATION_ARG);
}
/**
* @param {import('./').LogLevel} logLevel
* @returns {void}
*/
export function setLogLevel(logLevel) {
return appSetLogLevel(logLevel);
}
/**
* @param {Firestore} firestore
* @param {(transaction: FirebaseFirestoreTypes.Transaction) => Promise} updateFunction
* @returns {Promise}
*/
export function runTransaction(firestore, updateFunction) {
return firestore.runTransaction.call(firestore, updateFunction, MODULAR_DEPRECATION_ARG);
}
/**
* @param {Query} query
* @returns {Promise<FirebaseFirestoreTypes.AggregateQuerySnapshot>}
*/
export function getCountFromServer(query) {
return query.count.call(query, MODULAR_DEPRECATION_ARG).get();
}
export function getAggregateFromServer(query, aggregateSpec) {
if (!(query instanceof FirestoreQuery)) {
throw new Error(
'`getAggregateFromServer(*, aggregateSpec)` `query` must be an instance of `FirestoreQuery`',
);
}
if (!isObject(aggregateSpec)) {
throw new Error('`getAggregateFromServer(query, *)` `aggregateSpec` must be an object');
} else {
const containsOneAggregateField = Object.values(aggregateSpec).find(
value => value instanceof AggregateField,
);
if (!containsOneAggregateField) {
throw new Error(
'`getAggregateFromServer(query, *)` `aggregateSpec` must contain at least one `AggregateField`',
);
}
}
const aggregateQueries = [];
for (const key in aggregateSpec) {
if (aggregateSpec.hasOwnProperty(key)) {
const aggregateField = aggregateSpec[key];
// we ignore any fields that are not `AggregateField`
if (aggregateField instanceof AggregateField) {
switch (aggregateField.aggregateType) {
case AggregateType.AVG:
case AggregateType.SUM:
case AggregateType.COUNT:
const aggregateQuery = {
aggregateType: aggregateField.aggregateType,
field:
aggregateField._fieldPath === null ? null : aggregateField._fieldPath._toPath(),
key,
};
aggregateQueries.push(aggregateQuery);
break;
default:
throw new Error(
`'AggregateField' has an an unknown 'AggregateType' : ${aggregateField.aggregateType}`,
);
}
}
}
}
return query._firestore.native
.aggregateQuery(
query._collectionPath.relativeName,
query._modifiers.type,
query._modifiers.filters,
query._modifiers.orders,
query._modifiers.options,
aggregateQueries,
)
.then(data => new FirestoreAggregateQuerySnapshot(query, data, false));
}
/**
* Create an AggregateField object that can be used to compute the sum of
* a specified field over a range of documents in the result set of a query.
* @param field Specifies the field to sum across the result set.
*/
export function sum(field) {
return new AggregateField(AggregateType.SUM, fieldPathFromArgument(field));
}
/**
* Create an AggregateField object that can be used to compute the average of
* a specified field over a range of documents in the result set of a query.
* @param field Specifies the field to average across the result set.
*/
export function average(field) {
return new AggregateField(AggregateType.AVG, fieldPathFromArgument(field));
}
/**
* Create an AggregateField object that can be used to compute the count of
* documents in the result set of a query.
*/
export function count() {
return new AggregateField(AggregateType.COUNT, null);
}
/**
* @param {Firestore} firestore
* @param {ReadableStream<Uint8Array> | ArrayBuffer | string} bundleData
* @returns {import('.').LoadBundleTask}
*/
export function loadBundle(firestore, bundleData) {
return firestore.loadBundle.call(firestore, bundleData, MODULAR_DEPRECATION_ARG);
}
/**
* @param {Firestore} firestore
* @param {string} name
* @returns {Query<DocumentData>}
*/
export function namedQuery(firestore, name) {
return firestore.namedQuery.call(firestore, name, MODULAR_DEPRECATION_ARG);
}
/**
* @param {Firestore} firestore
* @returns {FirebaseFirestoreTypes.WriteBatch}
*/
export function writeBatch(firestore) {
return firestore.batch.call(firestore, MODULAR_DEPRECATION_ARG);
}
/**
* Gets the `PersistentCacheIndexManager` instance used by this Cloud Firestore instance.
* This is not the same as Cloud Firestore Indexes.
* Persistent cache indexes are optional indexes that only exist within the SDK to assist in local query execution.
* Returns `null` if local persistent storage is not in use.
* @param {Firestore} firestore
* @returns {PersistentCacheIndexManager | null}
*/
export function getPersistentCacheIndexManager(firestore) {
return firestore.persistentCacheIndexManager.call(firestore, MODULAR_DEPRECATION_ARG);
}
/**
* Enables the SDK to create persistent cache indexes automatically for local query
* execution when the SDK believes cache indexes can help improves performance.
* This feature is disabled by default.
* @param {PersistentCacheIndexManager} indexManager
* @returns {Promise<void}
*/
export function enablePersistentCacheIndexAutoCreation(indexManager) {
return indexManager.enableIndexAutoCreation.call(indexManager, MODULAR_DEPRECATION_ARG);
}
/**
* Stops creating persistent cache indexes automatically for local query execution.
* The indexes which have been created by calling `enableIndexAutoCreation()` still take effect.
* @param {PersistentCacheIndexManager} indexManager
* @returns {Promise<void}
*/
export function disablePersistentCacheIndexAutoCreation(indexManager) {
return indexManager.disableIndexAutoCreation.call(indexManager, MODULAR_DEPRECATION_ARG);
}
/**
* Removes all persistent cache indexes. Note this function also deletes indexes
* generated by `setIndexConfiguration()`, which is deprecated.
* @param {PersistentCacheIndexManager} indexManager
* @returns {Promise<void}
*/
export function deleteAllPersistentCacheIndexes(indexManager) {
return indexManager.deleteAllIndexes.call(indexManager, MODULAR_DEPRECATION_ARG);
}
export * from './query';
export * from './snapshot';
export * from './Bytes';
export * from './FieldPath';
export * from './FieldValue';
export * from './GeoPoint';
export * from './Timestamp';
export { Filter } from '../FirestoreFilter';