firebase-functions icon indicating copy to clipboard operation
firebase-functions copied to clipboard

Trigger support for withConverter

Open larssn opened this issue 4 years ago • 8 comments

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

larssn avatar Mar 15 '21 15:03 larssn

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Mar 15 '21 15:03 google-oss-bot

Great idea! I don't have a timeline for this, but I'll keep it in mind for our roadmap.

inlined avatar Mar 15 '21 18:03 inlined

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:

rconjoe avatar Sep 16 '21 15:09 rconjoe

Work around at this moment.

async (snap, context) => {
  const _ref = snap.ref.withConverter(Model.converter)
  const _snap = await _ref.get()
  const model = _snap.data()
}

dfdgsdfg avatar Dec 07 '21 10:12 dfdgsdfg

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.

larssn avatar Feb 08 '22 16:02 larssn

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.

inlined avatar Feb 09 '22 17:02 inlined

QueryDocumentSnapshots are just DoumentSnapshots for documents that are known to exists. Code like data = snapshot.exists ? fromFirestore(snapshot as QueryDocumentSnapshot) : null is perfectly safe.

schmidt-sebastian avatar Feb 09 '22 18:02 schmidt-sebastian

Anything I can do to help get this moved along? This would be a great feature.

charlesfries avatar Sep 22 '22 21:09 charlesfries