firebase - How do I prevent updates to nonexistent nodes? -
i using rest api patch nodes in firebase store. prevent updates nodes not exist (because deleted). right now, doing patch nonexistent reference recreates it.
i looked setting security rules, newdata.exists() not discriminate between setting new value , patching, couldn't figure out how allow want without restricting new creations.
i can snapshot of reference , check before patching, hoping there more elegant way without using 2 rest calls.
edit: code!
my firebase schema looks like:
requests: rq123: id: '123' sender: '1' recipient: '2' expiration: '1234567', filled: false, filleddate: '',
new requests written mobile client. server can make updates request entries using rest api. using python-firebase library, looks like:
request_ref = firebase_root + '/requests/' + request.id patch_data = { 'filled':'true', 'filled_date':'7654321' } firebase_conn.patch(request_ref, patch_data)
given design of app, i'd perform patch if request entry still exists. it's clear can snapshot , perform check way before patching, seemed awkward me.
as remarked in comments, there no difference between these cases:
- writing database location doesn't exist yet
- writing database location doesn't exist anymore
so have create distinction in application.
you have few options. not of them apply rest api, i'll mention them completeness anyway.
transactional update
the firebase sdks (for javascript, java/android , ios/osx) have method called transaction
, allows run compare-and-set operation.
with operation could
ref.transaction(function(current) { if (current) { current.filled: true, current.filled_date:'7654321' } return current; });
but since method not available on rest api, doesn't apply scenario.
mark deleted records
alternatively can mark deleted records, instead of deleting them:
requests: rq123: id: '123' sender: '1' recipient: '2' expiration: '1234567' filled: false filleddate: '' deleted: true
you delete other properties when pseudo-delete request, i.e.
requests: rq123: deleted: true
then in security rules, can reject write operation when flag present:
".write": "data.child('deleted').val() != true"
there many ways flag record. example seems in case, record-node have id
property. leave record-node marker, remove all properties:
requests: rq123: true
since firebase deleted nodes don't have value, put true
in here values.
with above structure, can allow writes either have id
property (which case when create request) or when id
property present (the path request rest api):
".write": "newdata.child('id').exists() || data.child('id').exists()"
keep list of deleted nodes
my final approach keep list of deleted request keys:
requests: rq123: id: '123' sender: '1' recipient: '2' expiration: '1234567' filled: false filleddate: '' deleted: rq456: true rq789: true
once again, set dummy value of true
deleted nodes prevent firebase deleting them.
with structure, can reject write operations when key you're writing exists in list of deleted
requests:
".write": "!root.child('deleted').child(newdata.key()).exists()"
each approach has own advantages , disadvantages, you'll have decide 1 best scenario.
Comments
Post a Comment