/
GeoCollectionReference.ts
71 lines (65 loc) · 2.85 KB
/
GeoCollectionReference.ts
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
import { GeoFirestoreTypes } from './GeoFirestoreTypes';
import { GeoDocumentReference } from './GeoDocumentReference';
import { GeoQuery } from './GeoQuery';
import { findCoordinatesKey, encodeGeohash, encodeGeoDocument } from './utils';
/**
* A `GeoCollectionReference` object can be used for adding documents, getting document references, and querying for documents (using the
* methods inherited from `GeoQuery`).
*/
export class GeoCollectionReference extends GeoQuery {
/**
* @param _collection The `CollectionReference` instance.
*/
constructor(private _collection: GeoFirestoreTypes.cloud.CollectionReference | GeoFirestoreTypes.web.CollectionReference) {
super(_collection);
}
/** The identifier of the collection. */
get id(): string {
return this._collection.id;
}
/**
* A reference to the containing Document if this is a subcollection, else null.
*/
get parent(): GeoDocumentReference | null {
return this._collection.parent ? new GeoDocumentReference(this._collection.parent) : null;
}
/**
* A string representing the path of the referenced collection (relative
* to the root of the database).
*/
get path(): string {
return this._collection.path;
}
/**
* Add a new document to this collection with the specified data, assigning it a document ID automatically.
*
* @param data An Object containing the data for the new document.
* @param customKey The key of the document to use as the location. Otherwise we default to `coordinates`.
* @return A Promise resolved with a `GeoDocumentReference` pointing to the newly created document after it has been written to the
* backend.
*/
public add(
data: GeoFirestoreTypes.DocumentData,
customKey?: string
): Promise<GeoDocumentReference> {
if (Object.prototype.toString.call(data) === '[object Object]') {
const locationKey: string = findCoordinatesKey(data, customKey);
const location: GeoFirestoreTypes.cloud.GeoPoint | GeoFirestoreTypes.web.GeoPoint = data[locationKey];
const geohash: string = encodeGeohash(location);
return (this._collection as GeoFirestoreTypes.cloud.CollectionReference)
.add(encodeGeoDocument(location, geohash, data)).then(doc => new GeoDocumentReference(doc));
} else {
throw new Error('document must be an object');
}
}
/**
* Get a `GeoDocumentReference` for the document within the collection at the specified path. If no path is specified, an
* automatically-generated unique ID will be used for the returned GeoDocumentReference.
*
* @param documentPath A slash-separated path to a document.
* @return The `GeoDocumentReference` instance.
*/
public doc(documentPath?: string): GeoDocumentReference {
return (documentPath) ? new GeoDocumentReference(this._collection.doc(documentPath)) : new GeoDocumentReference(this._collection.doc());
}
}