firestoreodm-flutter icon indicating copy to clipboard operation
firestoreodm-flutter copied to clipboard

Feature request - support FieldValues in set()

Open liranzairi opened this issue 1 year ago • 0 comments

Scenario

Let's say you have a reference to a document and you want to either create the doc if it doesn't exist or update it (including deleting a field in it) if it does exist. With the original cloud_firestore sdk you could do:

FirebaseFirestore.instance.doc('path_to_doc').set(
  {
    'fieldToUpdate': 'New value',
    'fieldToRemove': FieldValue.delete(),
  },
  SetOptions(merge: true),
);

However cloud_firestore_odm doesn't give you an option to do it. If you use set() you can't use FieldValue. If you use update() and the doc doesn't exist it will fail. I also don't want to use the transaction API because it doesn't have the offline capabilities and immediate responses which are important for user experience.

Possible solution ideas

  1. Create another method, e.g
setFieldValues(
  FieldValue? fieldToUpdateFieldValue,
  FieldValue? fieldToRemoveFieldValue,
)
  1. Add a parameter to method update(), e.g bool forceCreate, which will create the doc if it doesn't exist instead of throwing.

liranzairi avatar Feb 07 '24 21:02 liranzairi