Skip to content

Commit 136f1e5

Browse files
Maisthodavideast
authored andcommitted
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
1 parent 3d21a6a commit 136f1e5

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

src/firestore/firestore.spec.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,28 @@ describe('AngularFirestore', () => {
4747
expect(afs.app).toBeDefined();
4848
});
4949

50-
it('should create an AngularFirestoreDocument', () => {
50+
it('should create an AngularFirestoreDocument from a string path', () => {
5151
const doc = afs.doc('a/doc');
5252
expect(doc instanceof AngularFirestoreDocument).toBe(true);
5353
});
5454

55-
it('should create an AngularFirestoreCollection', () => {
55+
it('should create an AngularFirestoreDocument from a string path', () => {
56+
const ref = afs.doc('a/doc').ref;
57+
const doc = afs.doc(ref);
58+
expect(doc instanceof AngularFirestoreDocument).toBe(true);
59+
});
60+
61+
it('should create an AngularFirestoreCollection from a string path', () => {
5662
const collection = afs.collection('stuffs');
5763
expect(collection instanceof AngularFirestoreCollection).toBe(true);
5864
});
5965

66+
it('should create an AngularFirestoreCollection from a reference', () => {
67+
const ref = afs.collection('stuffs').ref;
68+
const collection = afs.collection(ref);
69+
expect(collection instanceof AngularFirestoreCollection).toBe(true);
70+
});
71+
6072
it('should throw on an invalid document path', () => {
6173
const singleWrapper = () => afs.doc('collection');
6274
const tripleWrapper = () => afs.doc('collection/doc/subcollection');

src/firestore/firestore.ts

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FirebaseFirestore, CollectionReference } from '@firebase/firestore-types';
1+
import { FirebaseFirestore, CollectionReference, DocumentReference } from '@firebase/firestore-types';
22
import { Observable } from 'rxjs/Observable';
33
import { Subscriber } from 'rxjs/Subscriber';
44
import { from } from 'rxjs/observable/from';
@@ -106,25 +106,41 @@ export class AngularFirestore {
106106
}
107107

108108
/**
109-
* Create a reference to a Firestore Collection based on a path and an optional
110-
* query function to narrow the result set.
111-
* @param path
109+
* Create a reference to a Firestore Collection based on a path or
110+
* CollectionReference and an optional query function to narrow the result
111+
* set.
112+
* @param pathOrRef
112113
* @param queryFn
113114
*/
114-
collection<T>(path: string, queryFn?: QueryFn): AngularFirestoreCollection<T> {
115-
const collectionRef = this.firestore.collection(path);
115+
collection<T>(path: string, queryFn?: QueryFn): AngularFirestoreCollection<T>
116+
collection<T>(ref: CollectionReference, queryFn?: QueryFn): AngularFirestoreCollection<T>
117+
collection<T>(pathOrRef: string | CollectionReference, queryFn?: QueryFn): AngularFirestoreCollection<T> {
118+
let collectionRef: CollectionReference;
119+
if (typeof pathOrRef === 'string') {
120+
collectionRef = this.firestore.collection(pathOrRef);
121+
} else {
122+
collectionRef = pathOrRef;
123+
}
116124
const { ref, query } = associateQuery(collectionRef, queryFn);
117125
return new AngularFirestoreCollection<T>(ref, query);
118126
}
119127

120128
/**
121-
* Create a reference to a Firestore Document based on a path. Note that documents
122-
* are not queryable because they are simply objects. However, documents have
123-
* sub-collections that return a Collection reference and can be queried.
124-
* @param path
129+
* Create a reference to a Firestore Document based on a path or
130+
* DocumentReference. Note that documents are not queryable because they are
131+
* simply objects. However, documents have sub-collections that return a
132+
* Collection reference and can be queried.
133+
* @param pathOrRef
125134
*/
126-
doc<T>(path: string): AngularFirestoreDocument<T> {
127-
const ref = this.firestore.doc(path);
135+
doc<T>(path: string): AngularFirestoreDocument<T>
136+
doc<T>(ref: DocumentReference): AngularFirestoreDocument<T>
137+
doc<T>(pathOrRef: string | DocumentReference): AngularFirestoreDocument<T> {
138+
let ref: DocumentReference;
139+
if (typeof pathOrRef === 'string') {
140+
ref = this.firestore.doc(pathOrRef);
141+
} else {
142+
ref = pathOrRef;
143+
}
128144
return new AngularFirestoreDocument<T>(ref);
129145
}
130146

0 commit comments

Comments
 (0)