Skip to content

Commit 1a9edce

Browse files
authored
Rtdb delete (#264)
* test-code * update to use delete queue * Update comments * add delete queue variable * Update to allow a single reference string
1 parent 2757938 commit 1a9edce

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

addons/godot-firebase/database/database_store.gd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ func put(path : String, payload) -> void:
2727
func patch(path : String, payload) -> void:
2828
_update_data(path, payload, true)
2929

30+
## @args path, payload
31+
## Deletes data at the reference point provided
32+
## NOTE: This will delete without warning, so make sure the reference is pointed to the level you want and not the root or you will lose everything
33+
func delete(path : String, payload) -> void:
34+
_update_data(path, payload, true)
35+
3036
## Returns a deep copy of this data store's payload.
3137
func get_data() -> Dictionary:
3238
return _data[_ROOT].duplicate(true)

addons/godot-firebase/database/reference.gd

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ var _db_path : String
2929
var _cached_filter : String
3030
var _push_queue : Array = []
3131
var _update_queue : Array = []
32+
var _delete_queue : Array = []
3233
var _can_connect_to_host : bool = false
3334

3435
const _put_tag : String = "put"
3536
const _patch_tag : String = "patch"
37+
const _delete_tag : String = "delete"
3638
const _separator : String = "/"
3739
const _json_list_tag : String = ".json"
3840
const _query_tag : String = "?"
@@ -83,6 +85,8 @@ func on_new_sse_event(headers : Dictionary, event : String, data : Dictionary) -
8385
emit_signal("new_data_update", FirebaseResource.new(data.path, data.data))
8486
elif command == _patch_tag:
8587
emit_signal("patch_data_update", FirebaseResource.new(data.path, data.data))
88+
elif command == _delete_tag:
89+
emit_signal("delete_data_update", FirebaseResource.new(data.path, data.data))
8690
pass
8791

8892
func set_store(store_ref : FirebaseDatabaseStore) -> void:
@@ -111,6 +115,12 @@ func push(data : Dictionary) -> void:
111115
else:
112116
_push_queue.append(data)
113117

118+
func delete(reference : String) -> void:
119+
if _pusher.get_http_client_status() == HTTPClient.STATUS_DISCONNECTED:
120+
_pusher.request(_get_list_url() + _db_path + _separator + reference + _get_remaining_path(), _headers, true, HTTPClient.METHOD_DELETE, "")
121+
else:
122+
_delete_queue.append(reference)
123+
114124
#
115125
# Returns a deep copy of the current local copy of the data stored at this reference in the Firebase
116126
# Realtime Database.
@@ -156,6 +166,8 @@ func _route_data(command : String, path : String, data) -> void:
156166
_store.put(path, data)
157167
elif command == _patch_tag:
158168
_store.patch(path, data)
169+
elif command == _delete_tag:
170+
_store.delete(path, data)
159171

160172
func on_push_request_complete(result : int, response_code : int, headers : PoolStringArray, body : PoolByteArray) -> void:
161173
if response_code == HTTPClient.RESPONSE_OK:
@@ -168,4 +180,7 @@ func on_push_request_complete(result : int, response_code : int, headers : PoolS
168180
return
169181
if _update_queue.size() > 0:
170182
var e = _update_queue.pop_front()
171-
update(e['path'], e['data'])
183+
update(e['path'], e['data'])
184+
return
185+
if _delete_queue.size() > 0:
186+
delete(_delete_queue.pop_front())

0 commit comments

Comments
 (0)