Skip to content

Commit

Permalink
feat(GeoFirestore): add ability to use with persistence options, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSolati committed Sep 17, 2018
1 parent 63a39c6 commit 9e2a2e4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/geofirestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import { firestore, GeoFirestoreObj, QueryCriteria } from './interfaces';
* Creates a GeoFirestore instance.
*/
export class GeoFirestore {
private _isWeb: boolean;
/**
* @param collectionRef A Firestore Collection reference where the GeoFirestore data will be stored.
*/
constructor(private _collectionRef: firestore.CollectionReference | firestore.cloud.CollectionReference) {
if (Object.prototype.toString.call(this._collectionRef) !== '[object Object]') {
throw new Error('collectionRef must be an instance of a Firestore Collection');
}

this._isWeb = Object.prototype.toString.call((this._collectionRef as firestore.CollectionReference).firestore.enablePersistence) === '[object Function]';
}

/********************/
Expand Down Expand Up @@ -43,11 +46,14 @@ export class GeoFirestore {
* If the provided key does not exist, the returned promise is fulfilled with null.
*
* @param $key The key of the location to retrieve.
* @param options Describes whether we should get from server or cache.
* @returns A promise that is fulfilled with the document of the given key.
*/
public get($key: string): Promise<any> {
public get($key: string, options: firestore.GetOptions = { source: 'default' }): Promise<any> {
validateKey($key);
const promise = this._collectionRef.doc($key).get() as Promise<firestore.DocumentSnapshot>;
const documentReference: firestore.DocumentReference = this._collectionRef.doc($key) as firestore.DocumentReference;
const promise: Promise<firestore.DocumentSnapshot> = this._isWeb ? documentReference.get(options) : documentReference.get();

return promise.then((documentSnapshot: firestore.DocumentSnapshot) => {
if (!documentSnapshot.exists) {
return null;
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ export namespace firestore {
export interface DocumentChange extends FirestoreWeb.DocumentChange { }
export interface DocumentReference extends FirestoreWeb.DocumentReference { }
export interface DocumentSnapshot extends FirestoreWeb.DocumentSnapshot { }
export interface GetOptions extends FirestoreWeb.GetOptions { }
export interface GeoPoint extends FirestoreWeb.GeoPoint { }
export interface Query extends FirestoreWeb.Query { }
export interface QuerySnapshot extends FirestoreWeb.QuerySnapshot { }
export interface WriteBatch extends FirestoreWeb.WriteBatch { }
export namespace cloud {
export interface CollectionReference extends FirebaseFirestore.CollectionReference {}
export interface CollectionReference extends FirebaseFirestore.CollectionReference { }
export interface DocumentChange extends FirebaseFirestore.DocumentChange { }
export interface DocumentReference extends FirebaseFirestore.DocumentReference { }
export interface DocumentSnapshot extends FirebaseFirestore.DocumentSnapshot { }
Expand Down

0 comments on commit 9e2a2e4

Please sign in to comment.