Trigger support for withConverter
Not sure if you accept Feature Requests, but I'll make it brief:
Would be nice if the onCreate, onWrite, onUpdate and onDelete triggers had support for withConverter:
Example:
firestore
.document(`businesses/{businessId}`)
.withConverter(new BusinessConverter())
.onWrite(async (change, context) => {
const business: Business = change.after.data()
})
Thanks
ref: https://github.com/googleapis/nodejs-firestore/issues/1449
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
Great idea! I don't have a timeline for this, but I'll keep it in mind for our roadmap.
Second this! I ended up here wondering if this was already a thing. We have some model classes whose methods are called immediately inside an onUpdate. It would save a few lines in every trigger to be able to have those models converted on the way out of Firestore like we do in our database classes.
Thanks for all the great work, my team and I love using Firebase :slightly_smiling_face:
Work around at this moment.
async (snap, context) => {
const _ref = snap.ref.withConverter(Model.converter)
const _snap = await _ref.get()
const model = _snap.data()
}
Work around at this moment.
async (snap, context) => { const _ref = snap.ref.withConverter(Model.converter) const _snap = await _ref.get() const action = _snap.data() }
That would cause an additional read, I'm afraid.
I'll admit I'm not used to using converters. It seems like a reasonable workaround would be
async (snap, context) => {
const action = Model.converter.fromFirestore(snap.data())
}
But a converter expects a QueryDocumentSnapshot and we return a DocumentSnapshot. CC @schmidt-sebastian in case he has ideas what to do about the type mismatch.
QueryDocumentSnapshots are just DoumentSnapshots for documents that are known to exists. Code like data = snapshot.exists ? fromFirestore(snapshot as QueryDocumentSnapshot) : null is perfectly safe.
Anything I can do to help get this moved along? This would be a great feature.