From 5aa7f32a61861f429f8e4445eccd3eb7bbb78649 Mon Sep 17 00:00:00 2001 From: Gustav Bylund Date: Wed, 7 Mar 2018 14:00:27 +0100 Subject: [PATCH] feat(firestore): allow collection and doc from ref (#1487) Allows firestore collection and doc to be constructed from both references and string paths. Fixes #1337 --- src/firestore/firestore.spec.ts | 16 +++++++++++-- src/firestore/firestore.ts | 40 +++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/firestore/firestore.spec.ts b/src/firestore/firestore.spec.ts index 0d811cdb1..270293593 100644 --- a/src/firestore/firestore.spec.ts +++ b/src/firestore/firestore.spec.ts @@ -47,16 +47,28 @@ describe('AngularFirestore', () => { expect(afs.app).toBeDefined(); }); - it('should create an AngularFirestoreDocument', () => { + it('should create an AngularFirestoreDocument from a string path', () => { const doc = afs.doc('a/doc'); expect(doc instanceof AngularFirestoreDocument).toBe(true); }); - it('should create an AngularFirestoreCollection', () => { + it('should create an AngularFirestoreDocument from a string path', () => { + const ref = afs.doc('a/doc').ref; + const doc = afs.doc(ref); + expect(doc instanceof AngularFirestoreDocument).toBe(true); + }); + + it('should create an AngularFirestoreCollection from a string path', () => { const collection = afs.collection('stuffs'); expect(collection instanceof AngularFirestoreCollection).toBe(true); }); + it('should create an AngularFirestoreCollection from a reference', () => { + const ref = afs.collection('stuffs').ref; + const collection = afs.collection(ref); + expect(collection instanceof AngularFirestoreCollection).toBe(true); + }); + it('should throw on an invalid document path', () => { const singleWrapper = () => afs.doc('collection'); const tripleWrapper = () => afs.doc('collection/doc/subcollection'); diff --git a/src/firestore/firestore.ts b/src/firestore/firestore.ts index d9c6da377..8c4915ead 100644 --- a/src/firestore/firestore.ts +++ b/src/firestore/firestore.ts @@ -1,4 +1,4 @@ -import { FirebaseFirestore, CollectionReference } from '@firebase/firestore-types'; +import { FirebaseFirestore, CollectionReference, DocumentReference } from '@firebase/firestore-types'; import { Observable } from 'rxjs/Observable'; import { Subscriber } from 'rxjs/Subscriber'; import { from } from 'rxjs/observable/from'; @@ -106,25 +106,41 @@ export class AngularFirestore { } /** - * Create a reference to a Firestore Collection based on a path and an optional - * query function to narrow the result set. - * @param path + * Create a reference to a Firestore Collection based on a path or + * CollectionReference and an optional query function to narrow the result + * set. + * @param pathOrRef * @param queryFn */ - collection(path: string, queryFn?: QueryFn): AngularFirestoreCollection { - const collectionRef = this.firestore.collection(path); + collection(path: string, queryFn?: QueryFn): AngularFirestoreCollection + collection(ref: CollectionReference, queryFn?: QueryFn): AngularFirestoreCollection + collection(pathOrRef: string | CollectionReference, queryFn?: QueryFn): AngularFirestoreCollection { + let collectionRef: CollectionReference; + if (typeof pathOrRef === 'string') { + collectionRef = this.firestore.collection(pathOrRef); + } else { + collectionRef = pathOrRef; + } const { ref, query } = associateQuery(collectionRef, queryFn); return new AngularFirestoreCollection(ref, query); } /** - * Create a reference to a Firestore Document based on a path. Note that documents - * are not queryable because they are simply objects. However, documents have - * sub-collections that return a Collection reference and can be queried. - * @param path + * Create a reference to a Firestore Document based on a path or + * DocumentReference. Note that documents are not queryable because they are + * simply objects. However, documents have sub-collections that return a + * Collection reference and can be queried. + * @param pathOrRef */ - doc(path: string): AngularFirestoreDocument { - const ref = this.firestore.doc(path); + doc(path: string): AngularFirestoreDocument + doc(ref: DocumentReference): AngularFirestoreDocument + doc(pathOrRef: string | DocumentReference): AngularFirestoreDocument { + let ref: DocumentReference; + if (typeof pathOrRef === 'string') { + ref = this.firestore.doc(pathOrRef); + } else { + ref = pathOrRef; + } return new AngularFirestoreDocument(ref); }