summaryrefslogtreecommitdiff
path: root/opendc/util/database.py
diff options
context:
space:
mode:
authorjc0b <j@jc0b.computer>2020-06-17 22:08:23 +0200
committerjc0b <j@jc0b.computer>2020-06-17 22:08:23 +0200
commite7d9a09f6d1c4eb894b2fd82ceadda56b2b4797e (patch)
tree2dc9f1469d39752f8822a5acbb255aea52ea9c2b /opendc/util/database.py
parent82c12c8a19be4ad16e046356d7ce8ecd3adde820 (diff)
Added some databases methods
Diffstat (limited to 'opendc/util/database.py')
-rw-r--r--opendc/util/database.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/opendc/util/database.py b/opendc/util/database.py
index 7ae7b82b..9b570d0d 100644
--- a/opendc/util/database.py
+++ b/opendc/util/database.py
@@ -104,6 +104,32 @@ def fetchall(statement, t=None):
return values
+def fetchone(query, collection):
+ """Uses existing mongo connection to return a single (the first) document in a collection matching the given query as a JSON object"""
+ # query needs to be in json format, i.e.: {'name': prefab_name}
+ #TODO: determine which collection to pull from
+ bson = prefabs_collection.find_one(query)
+ json_string = dumps(bson) #convert BSON representation to JSON
+ json_obj = json.loads(json_string) #load as a JSON object
+ #leave the id field in for now, we can use it later
+ #json_obj.pop("_id")
+ return json_obj
+
+
+
+def fetchall(query, collection):
+ """Uses existing mongo connection to return all documents matching a given query, as a list of JSON objects"""
+ results = []
+ cursor = prefabs_collection.find(query)
+ for doc in cursor:
+ json_string = dumps(bson) #convert BSON representation to JSON
+ json_obj = json.loads(json_string) #load as a JSON object
+ #leave the id field in for now, we can use it later
+ #json_obj.pop("_id")
+ results.append(json_obj)
+ return results
+
+
def datetime_to_string(datetime_to_convert):
"""Return a database-compatible string representation of the given datetime object."""