From c14ff547d4f6b6569bab3293ee574b03a8770ae3 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Tue, 23 Jun 2020 18:10:05 +0200 Subject: Add deletion methods --- opendc/util/database.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/opendc/util/database.py b/opendc/util/database.py index 67632bd0..0e424aa4 100644 --- a/opendc/util/database.py +++ b/opendc/util/database.py @@ -58,6 +58,26 @@ def update(_id, obj, collection): return convert_bson_to_json(bson) +def delete_one(query, collection): + """Deletes one object matching the given query. + + The query needs to be in json format, i.e.: `{'name': prefab_name}`. + """ + bson = getattr(opendcdb, collection).delete_one(query) + + return convert_bson_to_json(bson) + + +def delete_all(query, collection): + """Deletes all objects matching the given query. + + The query needs to be in json format, i.e.: `{'name': prefab_name}`. + """ + bson = getattr(opendcdb, collection).delete_many(query) + + return convert_bson_to_json(bson) + + def convert_bson_to_json(bson): # Convert BSON representation to JSON json_string = dumps(bson) -- cgit v1.2.3