-
Notifications
You must be signed in to change notification settings - Fork 2.2k
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 10
Replies: 4 comments 1 reply
-
There's a comment on that on #2267
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 6
-
should this also work for reading data?
Beta Was this translation helpful? Give feedback.
All reactions
-
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 !
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1