mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-07-19 13:19:41 +02:00
* add basic pre-commit file * add flake8 * add isort * add pep585-upgrade (typing upgrades) * use namespace for import * add mypy * update ci for backend * flake8 scope * fix version format * update makefile * disable strict option (temporary) * fix mypy issues * upgrade type hints (pre-commit) * add vscode typing check * add types to dev deps * remote container draft * update setup script * update compose version * run setup on create * dev containers update * remove unused pages * update setup tips * expose ports * Update pre-commit to include flask8-print (#1053) * Add in flake8-print to pre-commit * pin version of flake8-print * formatting * update getting strated docs * add mypy to pre-commit * purge .mypy_cache on clean * drop mypy Co-authored-by: zackbcom <zackbcom@users.noreply.github.com>
63 lines
1.7 KiB
Python
63 lines
1.7 KiB
Python
from abc import ABC, abstractproperty
|
|
from pathlib import Path
|
|
|
|
from pydantic import BaseModel, BaseSettings, PostgresDsn
|
|
|
|
|
|
class AbstractDBProvider(ABC):
|
|
@abstractproperty
|
|
def db_url(self) -> str:
|
|
pass
|
|
|
|
@property
|
|
def db_url_public(self) -> str:
|
|
pass
|
|
|
|
|
|
class SQLiteProvider(AbstractDBProvider, BaseModel):
|
|
data_dir: Path
|
|
prefix: str = ""
|
|
|
|
@property
|
|
def db_path(self):
|
|
return self.data_dir / f"{self.prefix}mealie.db"
|
|
|
|
@property
|
|
def db_url(self) -> str:
|
|
return f"sqlite:///{str(self.db_path.absolute())}"
|
|
|
|
@property
|
|
def db_url_public(self) -> str:
|
|
return self.db_url
|
|
|
|
|
|
class PostgresProvider(AbstractDBProvider, BaseSettings):
|
|
POSTGRES_USER: str = "mealie"
|
|
POSTGRES_PASSWORD: str = "mealie"
|
|
POSTGRES_SERVER: str = "postgres"
|
|
POSTGRES_PORT: str = "5432"
|
|
POSTGRES_DB: str = "mealie"
|
|
|
|
@property
|
|
def db_url(self) -> str:
|
|
host = f"{self.POSTGRES_SERVER}:{self.POSTGRES_PORT}"
|
|
return PostgresDsn.build(
|
|
scheme="postgresql",
|
|
user=self.POSTGRES_USER,
|
|
password=self.POSTGRES_PASSWORD,
|
|
host=host,
|
|
path=f"/{self.POSTGRES_DB or ''}",
|
|
)
|
|
|
|
@property
|
|
def db_url_public(self) -> str:
|
|
user = self.POSTGRES_USER
|
|
password = self.POSTGRES_PASSWORD
|
|
return self.db_url.replace(user, "*****", 1).replace(password, "*****", 1)
|
|
|
|
|
|
def db_provider_factory(provider_name: str, data_dir: Path, env_file: Path, env_encoding="utf-8") -> AbstractDBProvider:
|
|
if provider_name == "postgres":
|
|
return PostgresProvider(_env_file=env_file, _env_file_encoding=env_encoding)
|
|
else:
|
|
return SQLiteProvider(data_dir=data_dir)
|