browsertrix/backend/db.py
Ilya Kreymer 61a608bfbe update models:
- replace storages with archives, which have a single storage (for now)
- crawls associated with archives
- users below to archive, with one admin user (if archive created by default)
- update crawlconfig for latest browsertrix-crawler (0.4.4)
- k8s: fix permissions for crawler role
- k8s: fix minio service (now requiring two ports)
2021-08-18 16:53:49 -07:00

53 lines
1.2 KiB
Python

"""
Browsertrix API Mongo DB initialization
"""
import os
from typing import Optional
import motor.motor_asyncio
from pydantic import BaseModel
DATABASE_URL = (
f"mongodb://root:example@{os.environ.get('MONGO_HOST', 'localhost')}:27017"
)
# ============================================================================
def init_db():
"""initializde the mongodb connector"""
client = motor.motor_asyncio.AsyncIOMotorClient(
DATABASE_URL, uuidRepresentation="standard"
)
mdb = client["browsertrixcloud"]
return mdb
# ============================================================================
class BaseMongoModel(BaseModel):
"""Base pydantic model that is also a mongo doc"""
id: Optional[str]
@classmethod
def from_dict(cls, data):
"""convert dict from mongo to an Archive"""
if not data:
return None
data["id"] = str(data.pop("_id"))
return cls(**data)
def serialize(self):
"""convert Archive to dict"""
return self.dict(exclude_unset=True, exclude_defaults=True, exclude_none=True)
def to_dict(self):
"""convert to dict for mongo"""
res = self.dict()
res["_id"] = res.pop("id", "")
return res