Skip to content

Commit

Permalink
feat(firestore): allow collection and doc from ref
Browse files Browse the repository at this point in the history
Allows firestore collection and doc to be constructed from both
references and string paths.

Fixes angular#1337
  • Loading branch information
Maistho committed Feb 27, 2018
1 parent 3d21a6a commit 3aca768
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
16 changes: 14 additions & 2 deletions src/firestore/firestore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
40 changes: 28 additions & 12 deletions src/firestore/firestore.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<T>(path: string, queryFn?: QueryFn): AngularFirestoreCollection<T> {
const collectionRef = this.firestore.collection(path);
collection<T>(path: string, queryFn?: QueryFn): AngularFirestoreCollection<T>
collection<T>(ref: CollectionReference, queryFn?: QueryFn): AngularFirestoreCollection<T>
collection<T>(pathOrRef: string | CollectionReference, queryFn?: QueryFn): AngularFirestoreCollection<T> {
let collectionRef: CollectionReference;
if (typeof pathOrRef === 'string') {
collectionRef = this.firestore.collection(pathOrRef);
} else {
collectionRef = pathOrRef;
}
const { ref, query } = associateQuery(collectionRef, queryFn);
return new AngularFirestoreCollection<T>(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<T>(path: string): AngularFirestoreDocument<T> {
const ref = this.firestore.doc(path);
doc<T>(path: string): AngularFirestoreDocument<T>
doc<T>(ref: DocumentReference): AngularFirestoreDocument<T>
doc<T>(pathOrRef: string | DocumentReference): AngularFirestoreDocument<T> {
let ref: DocumentReference;
if (typeof pathOrRef === 'string') {
ref = this.firestore.doc(pathOrRef);
} else {
ref = pathOrRef;
}
return new AngularFirestoreDocument<T>(ref);
}

Expand Down

0 comments on commit 3aca768

Please sign in to comment.