Skip to content

Commit

Permalink
Update base model and configs
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhouyang committed Mar 29, 2024
1 parent 3b0854e commit aeea49b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
2 changes: 2 additions & 0 deletions melody/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
class DatabaseSettings(BaseSettings):
model_config = SettingsConfigDict(env_file=".env", extra="ignore")
database_uri: str = "sqlite://melody.db"
conn_pool_min_size: int = 5
conn_pool_max_size: int = 20


database_settings = DatabaseSettings()
40 changes: 23 additions & 17 deletions melody/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,45 @@
from sqlmodel import Field, SQLModel


class BaseModel(SQLModel, table=False):
class BaseModel(SQLModel):

tenant_id: str | None = Field(
nullable=True,
default=None,
comment="The tenant id of the record",
sa_column=sa.Column(sa.String(64), default=None),
class Config:
orm_mode = True

tenant_id: str = Field(
default="",
nullable=False,
title="tenant_id",
description="The tenant id of the record",
min_length=0,
max_length=64,
)

created_at: datetime = Field(
nullable=False,
default=datetime.now(timezone.utc),
comment="Timestamp of record creation",
sa_column=sa.Column(sa.TIMESTAMP, default=datetime.now(timezone.utc)),
title="created_at",
description="Timestamp of record creation",
)

updated_at: datetime = Field(
nullable=False,
default=datetime.now(timezone.utc),
comment="Timestamp of record update",
sa_column=sa.Column(sa.TIMESTAMP, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc)),
title="updated_at",
description="Timestamp of record update",
)

deleted_at: datetime | None = Field(
nullable=True,
default=None,
comment="Timestamp of record deletion",
sa_column=sa.Column(sa.TIMESTAMP, default=None),
title="deleted_at",
description="Timestamp of record deletion",
)

props: dict | None = Field(
nullable=True,
default=None,
comment="Additional properties of the record",
sa_column=sa.Column(sa.JSON, default={}),
props: dict = Field(
nullable=False,
default={},
title="props",
description="Additional properties of the record",
sa_type=sa.JSON,
)
8 changes: 8 additions & 0 deletions melody/deps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Annotated, TypeAlias

from databases import Database
from fastapi import Depends
from sqlalchemy.ext.asyncio import create_async_engine
from sqlmodel.ext.asyncio.session import AsyncSession
Expand All @@ -15,3 +16,10 @@ async def database_session():


DatabaseSession: TypeAlias = Annotated[AsyncSession, Depends(database_session)]


database = Database(
database_settings.database_uri,
min_size=database_settings.conn_pool_min_size,
max_size=database_settings.conn_pool_max_size,
)

0 comments on commit aeea49b

Please sign in to comment.