Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support for firestore CollectionReference#withConverter in AngularFirestoreCollection #2291

jadengis started this conversation in Ideas
Discussion options

Since v7.6.0, the CollectionReference for Firestore in firebase-js-sdk has supported a withConverter method that allows the specification of a converter object that will automatically map DocumentData from firestore into application types. I would like to use this feature as I would allow me to write some more generic APIs for data access with firebase. Unfortunately, the current version of angularfire does not support it for AngularFirestoreCollection

Is there any plan to support this feature in angularfire? I was thinking about submitting a simple PR but didn't know if this would be possible due to the current firebase js sdk dependencies.

You must be logged in to vote

Replies: 4 comments 1 reply

Comment options

There's a comment on that on #2267

You must be logged in to vote
0 replies
Comment options

I have a bit of a workaround for this that can be used until the withConverter as added as an extension to the normal API.

The AngularFirestore.collection method accepts a CollectionReference as well as a string. So, you can first create a CollectionReference complete with the converter applied using the plain old firestore library and then provide that reference to the collection method.

getCollectionWithConverter(): CollectionReference {
 return this.db.firestore.collection("Courses").withConverter(this.converter)
}
createCourse(course: Course) {
 return this.db.collection(this.getCollectionWithConverter()).add(course);
}

My converter (injected as "converter") then implements the FirestoreDataConverter interface.

Works without any issues.

You must be logged in to vote
1 reply
Comment options

should this also work for reading data?

Comment options

I have a bit of a workaround for this that can be used until the withConverter as added as an extension to the normal API.

The AngularFirestore.collection method accepts a CollectionReference as well as a string. So, you can first create a CollectionReference complete with the converter applied using the plain old firestore library and then provide that reference to the collection method.

getCollectionWithConverter(): CollectionReference {
 return this.db.firestore.collection("Courses").withConverter(this.converter)
}
createCourse(course: Course) {
 return this.db.collection(this.getCollectionWithConverter()).add(course);
}

My converter (injected as "converter") then implements the FirestoreDataConverter interface.

Works without any issues.

worked for me, thank you !

You must be logged in to vote
0 replies
Comment options

While @IainAdamsLabs is correct I'd like to point out that using .valueChanges({idField:'id'}) or something like that will kill the class you created with the transformer as it is converted to an object here https://github.com/angular/angularfire/blob/master/src/firestore/collection/collection.ts#L113-L117
A better way to achieve this would be, to use snapshot.id in the converter (fromFirestore) which will hold the id regardless.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
Converted from issue

This discussion was converted from issue #2291 on December 10, 2020 02:47.

AltStyle によって変換されたページ (->オリジナル) /