119 lines
4.0 KiB
Python
119 lines
4.0 KiB
Python
from dataclasses import dataclass
|
|
from uuid import UUID
|
|
|
|
from core.exceptions import ModifiedCountException, RecordNotFoundException
|
|
from core.models.base import BaseModel, DELETED_AT_QUERY
|
|
from core.service.mongo_db import use_collection
|
|
from core.utils import get_time, get_v_id
|
|
|
|
|
|
@dataclass
|
|
class ConfigMongoWriter:
|
|
created_at: bool = True
|
|
updated_at: bool = True
|
|
has_owner: bool = True
|
|
init_id: bool = True
|
|
init_v_id: bool = False
|
|
|
|
|
|
async def mongo_inserter(model: BaseModel, collection: str, options: ConfigMongoWriter = ConfigMongoWriter(),
|
|
getter=None, db=None, v_id_prefix=None, client=None) -> dict | BaseModel:
|
|
"""
|
|
Inserts data into collection
|
|
"""
|
|
try:
|
|
if options.init_id:
|
|
model.init_id()
|
|
if options.has_owner:
|
|
model.init_owner()
|
|
if options.created_at and options.updated_at:
|
|
model.cu_at()
|
|
if options.created_at:
|
|
model.c_at()
|
|
if options.updated_at:
|
|
model.u_at()
|
|
if options.init_v_id:
|
|
model.v_id = get_v_id(v_id_prefix or 'uk', source_id=model.id)
|
|
|
|
document = model.mongo_dict()
|
|
if client:
|
|
result = await client(collection).insert_one(document)
|
|
else:
|
|
result = await use_collection(collection, db).insert_one(document)
|
|
assert result.acknowledged
|
|
if getter:
|
|
return await getter(result.inserted_id)
|
|
return result
|
|
except AssertionError as e:
|
|
raise AssertionError(f'{collection} insert error')
|
|
except Exception as e:
|
|
raise e
|
|
|
|
|
|
async def mongo_soft_deleter(query: dict | UUID, collection: str, db=None, client=None):
|
|
"""
|
|
Updates data in collection
|
|
"""
|
|
# doc = {"$set": {'deleted_at': get_time()}, }
|
|
_query = query
|
|
if isinstance(query, UUID):
|
|
_query = {'_id': query}
|
|
__trash_collection = f'{collection}--trash'
|
|
__driver = client or use_collection
|
|
__current_record = await __driver(collection, db=db).find_one(_query)
|
|
if not __current_record:
|
|
raise RecordNotFoundException(identifier=str(query))
|
|
__current_record['deleted_at'] = get_time()
|
|
__driver(__trash_collection, db=db).insert_one(__current_record)
|
|
__driver(collection, db=db).delete_one(_query)
|
|
return True
|
|
|
|
|
|
async def mongo_updater(model: BaseModel,
|
|
query: dict | UUID,
|
|
collection: str,
|
|
options: ConfigMongoWriter = ConfigMongoWriter(created_at=False, has_owner=False),
|
|
getter=None,
|
|
db=None,
|
|
extra_query: dict = None,
|
|
client=None,
|
|
):
|
|
"""
|
|
Updates data in collection
|
|
"""
|
|
_query = query
|
|
if isinstance(query, UUID):
|
|
_query = {'_id': query}
|
|
if options.has_owner:
|
|
model.init_owner()
|
|
if options.updated_at:
|
|
model.u_at()
|
|
if client:
|
|
result = await client(collection).update_one(_query, {"$set": model.dict(),
|
|
**(extra_query if extra_query else {})})
|
|
else:
|
|
result = await use_collection(collection, db).update_one(_query, {"$set": model.dict(),
|
|
**(extra_query if extra_query else {})})
|
|
if not result.modified_count:
|
|
raise ModifiedCountException(query)
|
|
if getter:
|
|
return await getter(_query)
|
|
return result
|
|
|
|
|
|
async def mongo_find_one(query: dict | UUID, collection: str, db=None, client=None, project=None) -> dict:
|
|
"""
|
|
Ge
|
|
"""
|
|
_query = query
|
|
if isinstance(query, UUID):
|
|
_query = {'_id': query}
|
|
_query = dict(DELETED_AT_QUERY, **_query)
|
|
if client:
|
|
result = await client(collection).find_one(_query, project)
|
|
else:
|
|
result = await use_collection(collection, db).find_one(_query, project)
|
|
if not result:
|
|
raise RecordNotFoundException(identifier=query.__str__())
|
|
return result
|