Skip to content

feat(firestore): allow collection and doc from ref #1487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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