Skip to content
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

Allow Firestore collection and doc to accept a Reference as input #1337

Closed
Maistho opened this issue Nov 11, 2017 · 3 comments
Closed

Allow Firestore collection and doc to accept a Reference as input #1337

Maistho opened this issue Nov 11, 2017 · 3 comments

Comments

@Maistho
Copy link
Contributor

Maistho commented Nov 11, 2017

angularfire2/database allows initialising a object/list using either a path string or a reference. It would be nice to allow the same for the firestore.

The new interface would look something like this:

collection<T>(pathOrRef: string | firebase.firestore.CollectionReference, queryFn?: QueryFn): AngularFirestoreCollection<T>
doc<T>(pathOrRef: string | firebase.firestore.DocumentReference): AngularFirestoreDocument<T>

It should be a simple change, I can make a PR for it if it sounds interesting.

@JamieWritesCode
Copy link

JamieWritesCode commented Nov 15, 2017

We are actually using something very similar in our app already. Inspired by this post I created a service to wrap and extend angularfire with this functionality. It looks like this:

// types

type CollectionPredicate<T> = string | AngularFirestoreCollection<T>;
type DocPredicate<T> = string | AngularFirestoreDocument<T>;

// return a reference

col<T>(ref: CollectionPredicate<T>, queryFn?): AngularFirestoreCollection<T> {
    return typeof ref === 'string' ? this.afs.collection<T>(ref, queryFn) : ref;
}

doc<T>(ref: DocPredicate<T>): AngularFirestoreDocument<T> {
    return typeof ref === 'string' ? this.afs.doc<T>(ref) : ref;
}

// return an observable

doc$<T>(ref: DocPredicate<T>): Observable<T> {
    return this.doc(ref).snapshotChanges().map(doc => {
        return doc.payload.data() as T;
    });
}

col$<T>(ref: CollectionPredicate<T>, queryFn?): Observable<T[]> {
    return this.col(ref, queryFn).snapshotChanges().map(docs => {
        return docs.map(a => a.payload.doc.data()) as T[];
    });
}

@houfu
Copy link
Contributor

houfu commented Feb 27, 2018

I am interested in this as well.

Maistho added a commit to Maistho/angularfire2 that referenced this issue Feb 27, 2018
Allows firestore collection and doc to be constructed from both
references and string paths.

Fixes angular#1337
Maistho added a commit to Maistho/angularfire2 that referenced this issue Feb 27, 2018
Allows firestore collection and doc to be constructed from both
references and string paths.

Fixes angular#1337
@Maistho
Copy link
Contributor Author

Maistho commented Feb 27, 2018

Just opened a PR #1487

myspivey pushed a commit to myspivey/angularfire2 that referenced this issue Apr 10, 2018
Allows firestore collection and doc to be constructed from both
references and string paths.

Fixes angular#1337
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants