From b86b49deb4e9e023a231137499bf956369cb58e3 Mon Sep 17 00:00:00 2001 From: RemyBENALOUANE <66668680+RemyBENALOUANE@users.noreply.github.com> Date: Thu, 31 Oct 2024 16:38:34 +0100 Subject: [PATCH] Add set(document_id: String, data: Dictionary) function in firestore_collection.gd --- .../firestore/firestore_collection.gd | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/addons/godot-firebase/firestore/firestore_collection.gd b/addons/godot-firebase/firestore/firestore_collection.gd index 4883f87..d9c0e4b 100644 --- a/addons/godot-firebase/firestore/firestore_collection.gd +++ b/addons/godot-firebase/firestore/firestore_collection.gd @@ -176,3 +176,24 @@ func _process_request(task : FirestoreTask, document_id : String, url : String, func get_database_url(append) -> String: return _base_url + _extended_url.rstrip("/") + ":" + append + + +## @args document_id: StringName, data: Variant +## @return void +# used to SET a document, specify the document ID and new data +func set(document_id: StringName, data: Variant) -> void: + var task: FirestoreTask = FirestoreTask.new() + task.action = FirestoreTask.Task.TASK_PATCH + task.data = collection_name + "/" + document_id + var url = _get_request_url() + _separator + document_id.replace(" ", "%20") + + _process_request(task, document_id, url, JSON.stringify(Utilities.dict2fields(data))) + var result = await Firebase.Firestore._handle_task_finished(task) + + if result != null: + for child in get_children(): + if child.doc_name == document_id: + child.replace(result, true) + break + else: + print("set_document returned null for %s %s" % [collection_name, document_id])