-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathGeoFirestore.ts
More file actions
125 lines (117 loc) · 4.64 KB
/
Copy pathGeoFirestore.ts
File metadata and controls
125 lines (117 loc) · 4.64 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
import {GeoFirestoreTypes} from 'geofirestore-core';
import {GeoCollectionReference} from './GeoCollectionReference';
import {GeoDocumentReference} from './GeoDocumentReference';
import {GeoQuery} from './GeoQuery';
import {GeoWriteBatch} from './GeoWriteBatch';
/**
* `GeoFirestore` represents a Firestore Database and is the entry point for all GeoFirestore operations.
*/
export class GeoFirestore {
/**
* @param _firestore Firestore represents a Firestore Database and is the entry point for all Firestore operations.
*/
constructor(
private _firestore:
| GeoFirestoreTypes.web.Firestore
| GeoFirestoreTypes.cloud.Firestore
) {
if (Object.prototype.toString.call(_firestore) !== '[object Object]') {
throw new Error('Firestore must be an instance of Firestore');
}
}
/** The native `Firestore` instance. */
get native():
| GeoFirestoreTypes.cloud.Firestore
| GeoFirestoreTypes.web.Firestore {
return this._firestore;
}
/**
* Creates a write batch, used for performing multiple writes as a single atomic operation.
*
* @param customKey Key to use for GeoPoints in a write batch.
* @return A new `GeoWriteBatch` instance.
*/
batch(customKey?: string): GeoWriteBatch {
return new GeoWriteBatch(this._firestore.batch(), customKey);
}
/**
* Gets a `GeoCollectionReference` instance that refers to the collection at the specified path.
*
* @param collectionPath A slash-separated path to a collection.
* @param customKey Key to use for GeoPoints in a collection.
* @return A new `GeoCollectionReference` instance.
*/
collection(
collectionPath: string,
customKey?: string
): GeoCollectionReference {
return new GeoCollectionReference(
this._firestore.collection(collectionPath),
customKey
);
}
/**
* Creates and returns a new GeoQuery that includes all documents in the
* database that are contained in a collection or subcollection with the
* given collectionId.
*
* @param collectionId Identifies the collections to query over. Every
* collection or subcollection with this ID as the last segment of its path
* will be included. Cannot contain a slash.
* @return The created GeoQuery.
*/
collectionGroup(collectionId: string): GeoQuery {
return new GeoQuery(this._firestore.collectionGroup(collectionId));
}
/**
* Gets a `GeoDocumentReference` instance that refers to the document at the
* specified path.
*
* @param documentPath A slash-separated path to a document.
* @return The `GeoDocumentReference` instance.
*/
doc(documentPath: string): GeoDocumentReference {
return new GeoDocumentReference(this._firestore.doc(documentPath));
}
/**
* Executes the given updateFunction and then attempts to commit the changes applied within the transaction. If any document read within
* the transaction has changed, the updateFunction will be retried. If it fails to commit after 5 attempts, the transaction will fail.
*
* Note: The `updateFunction` passed into `runTransaction` is a standard Firestore transaction. You should then immediateley create a
* `GeoTransaction` to then make your calls to. Below is a small example on how to do that.
*
* @example
* ```typescript
* const geofirestore = new GeoFirestore(firebase.firestore());
* const sfDocRef = geofirestore.collection('cities').doc('SF');
*
* geofirestore.runTransaction((transaction) => {
* // Immediateley create a `GeoTransaction` from the `transaction`
* const geotransaction = new GeoTransaction(transaction);
* // This code may get re-run multiple times if there are conflicts.
* return geotransaction.get(sfDocRef).then((sfDoc) => {
* if (!sfDoc.exists) {
* throw Error('Document does not exist!');
* }
* const newPopulation = sfDoc.data().population + 1;
* geotransaction.update(sfDocRef, { population: newPopulation });
* });
* });
* ```
*
* @param updateFunction The function to execute within the transaction context.
* @return If the transaction completed successfully or was explicitly aborted (by the updateFunction returning a failed Promise), the
* Promise returned by the updateFunction will be returned here. Else if the transaction failed, a rejected Promise with the
* corresponding failure error will be returned.
*/
runTransaction(
updateFunction: (
transaction:
| GeoFirestoreTypes.cloud.Transaction
| GeoFirestoreTypes.web.Transaction
) => Promise<any>
): Promise<any> {
const firestore = this._firestore as GeoFirestoreTypes.cloud.Firestore;
return firestore.runTransaction(updateFunction);
}
}