FirebaseExtended / firestoreodm-flutter

BSD 3-Clause "New" or "Revised" License
33 stars 9 forks source link

Feature request - support FieldValues in set() #5

Open lirantzairi opened 5 months ago

lirantzairi commented 5 months ago

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,
    )
  2. Add a parameter to method update(), e.g bool forceCreate, which will create the doc if it doesn't exist instead of throwing.