diff options
author | Kristofer Hallin <kristofer@sunet.se> | 2021-10-29 09:29:16 +0200 |
---|---|---|
committer | Kristofer Hallin <kristofer@sunet.se> | 2021-10-29 09:29:16 +0200 |
commit | 2bfbe7568a8c6477de60a676d9027dcb9714af42 (patch) | |
tree | f4da7aee25d4728659182a2cc19197f341d165d3 /src/db.py | |
parent | 34a353a539f71b6a87413b58ea483b36f94e3516 (diff) |
Use FastAPI + JWT instead of Falcon.
Diffstat (limited to 'src/db.py')
-rwxr-xr-x | src/db.py | 31 |
1 files changed, 22 insertions, 9 deletions
@@ -7,28 +7,41 @@ # value if you're too quick with generating the timestamps, ie # invoking time.time() several times quickly enough. +import os +import sys import time import couch -import index + +from index import CouchIindex class DictDB(): - def __init__(self, database, hostname, username, password): + def __init__(self): """ Check if the database exists, otherwise we will create it together with the indexes specified in index.py. """ + try: + self.database = os.environ['COUCHDB_NAME'] + self.hostname = os.environ['COUCHDB_HOSTNAME'] + self.username = os.environ['COUCHDB_USER'] + self.password = os.environ['COUCHDB_PASSWORD'] + except KeyError: + print('The environment variables COUCHDB_NAME, COUCHDB_HOSTNAME,' + + ' COUCHDB_USER and COUCHDB_PASSWORD must be set.') + sys.exit(-1) + self.server = couch.client.Server( - f"http://{username}:{password}@{hostname}:5984/") + f"http://{self.username}:{self.password}@{self.hostname}:5984/") try: - self.couchdb = self.server.database(database) + self.couchdb = self.server.database(self.database) except couch.exceptions.NotFound: print("Creating database and indexes.") - self.couchdb = self.server.create(database) + self.couchdb = self.server.create(self.database) - for i in index.indexes: + for i in CouchIindex(): self.couchdb.index(i) self._ts = time.time() @@ -54,12 +67,12 @@ class DictDB(): if type(data) is list: for item in data: item['_id'] = str(self.unique_key()) - self.couchdb.save_bulk(data) + ret = self.couchdb.save_bulk(data) else: data['_id'] = str(self.unique_key()) - self.couchdb.save(data) + ret = self.couchdb.save(data) - return True + return ret def get(self, key): """ |