PhotosAPI/modules/database.py

34 lines
951 B
Python
Raw Normal View History

2022-12-20 02:22:32 +02:00
from modules.utils import configGet
from pymongo import MongoClient
db_config = configGet("database")
if db_config["user"] is not None and db_config["password"] is not None:
con_string = 'mongodb://{0}:{1}@{2}:{3}/{4}'.format(
db_config["user"],
db_config["password"],
db_config["host"],
db_config["port"],
db_config["name"]
)
else:
con_string = 'mongodb://{0}:{1}/{2}'.format(
db_config["host"],
db_config["port"],
db_config["name"]
)
db_client = MongoClient(con_string)
db = db_client.get_database(name=db_config["name"])
collections = db.list_collection_names()
2022-12-20 12:37:42 +02:00
for collection in ["users", "albums", "photos", "tokens"]:
2022-12-20 02:22:32 +02:00
if not collection in collections:
db.create_collection(collection)
2022-12-20 12:37:42 +02:00
col_users = db.get_collection("users")
2022-12-20 02:22:32 +02:00
col_albums = db.get_collection("albums")
col_photos = db.get_collection("photos")
col_tokens = db.get_collection("tokens")