from os import getenv from bson import ObjectId from pymongo import MongoClient import backend.elastic as elastic def get_database(): client = MongoClient(getenv("MONGO_URL")) return client["documents"] def create_document(document): dbname = get_database() docs_coll = dbname["docs"] doc = document.copy() docs_coll.insert_one(document) elastic.add_document("test-index", document["_id"], doc) def get_document_by_id(id): dbname = get_database() docs_coll = dbname["docs"] doc = docs_coll.find({"_id": ObjectId(id)}, {"_id": 0}).next().items() return doc def get_document_by_name(name): dbname = get_database() docs_coll = dbname["docs"] return docs_coll.find({"name": name}) def edit_data(id, data): dbname = get_database() docs_coll = dbname["docs"] docs_coll.update_one({"_id": ObjectId(id)}, {"$set": {"data": data}}) doc = docs_coll.find_one({"_id": ObjectId(id)}, {"_id": 0}) elastic.add_document("test-index", id, doc) def edit_access(id, access): dbname = get_database() docs_coll = dbname["docs"] docs_coll.update_one({"_id": id}, {"$set": {"access": access}}) def edit_name(id, name): dbname = get_database() docs_coll = dbname["docs"] docs_coll.update_one({"_id": id}, {"$set": {"name": name}})