firecmsco / firecms

Awesome Firebase/Firestore-based CMS. The missing admin panel for your Firebase project!
https://firecms.co
Other
1.14k stars 185 forks source link

Delete field like how firestore allows it using deleteField #548

Closed faizanabidnaqvi closed 12 months ago

faizanabidnaqvi commented 1 year ago

Hi there!

I have this requirement to completely delete some fields from a doc, rather than setting them to null. Firestore provides a 'deleteField()' sentinel that we set on a field which then gets deleted.

In v1, I used this frequently across custom fields and in callbacks where I would mutate the particular field in values using: value["field"] = deleteField()

This way worked to delete the field in v1. In v2, however, this way doesn't work anymore and it instead results in adding another field under that field like: field._methodName: "deleteField"

I understand this could be due to how Firecms handles the values before updating. Is there a way to completely delete a field in firecms or any workaround? If not, is there a plan to add such a functionality?

For now, I am thinking of directly calling Firestore in the callbacks to delete the fields I don't want.

faizanabidnaqvi commented 12 months ago

Looking into the code for firestore data source, seems like setting a field to 'undefined' deletes it so I will do that!

muhamed-didovic commented 10 months ago

@faizanabidnaqvi , thanks for the heads up, I have an array of objects, and deleting property in objects gives me this error: FirebaseError: Function setDoc() called with invalid data. deleteField() is not currently supported inside arrays

Any workaround for this also does it still work in your case when deleting fields?

faizanabidnaqvi commented 10 months ago

Hi @muhamed-didovic Firebase doesn't allow calling deleteField() in arrays and firecms replaces 'undefined' fields with deleteField(); so I guess the only possible solution is to replace the entire array which can be done through firecms anyway (using the array field).

Even if we were directly using firebase sdk directly, array fields are supposed to be replaced entirely if we wish to change any element in the array.