Skip to content

Commit

Permalink
moved to SQLModel with autodetection of metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
samj committed Sep 12, 2024
1 parent 7074d7d commit 2aed502
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
8 changes: 5 additions & 3 deletions backend/db.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# database helper functions
import os
import logging
from sqlalchemy.orm import sessionmaker, declarative_base
from sqlmodel import SQLModel
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
from alembic import command
from alembic.config import Config as AlembicConfig
from common.paths import base_dir, db_path, db_url
from contextlib import asynccontextmanager

logger = logging.getLogger(__name__)

# Define the SQLAlchemy Base
Base = declarative_base()
# Define SQLModelBase class
class SQLModelBase(SQLModel):
pass

# Create async engine
engine = create_async_engine(db_url, echo=False)
Expand Down
57 changes: 26 additions & 31 deletions backend/models.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
from sqlalchemy import Column, String
from backend.db import Base
from sqlmodel import Field
from backend.db import SQLModelBase

class Config(Base):
__tablename__ = "config"
key = Column(String, primary_key=True)
value = Column(String, nullable=True)
class Config(SQLModelBase, table=True):
key: str = Field(primary_key=True)
value: str | None = Field(default=None)

class Resource(Base):
__tablename__ = "resource"
id = Column(String, primary_key=True)
name = Column(String, nullable=False)
uri = Column(String, nullable=False)
class Resource(SQLModelBase, table=True):
id: str = Field(primary_key=True)
name: str = Field()
uri: str = Field()

class User(Base):
__tablename__ = "user"
id = Column(String, primary_key=True)
name = Column(String, nullable=False)
email = Column(String, nullable=False)
class User(SQLModelBase, table=True):
id: str = Field(primary_key=True)
name: str = Field()
email: str = Field()

class Asset(Base):
__tablename__ = "asset"
id = Column(String, primary_key=True)
user_id = Column(String, nullable=True)
title = Column(String, nullable=False)
creator = Column(String, nullable=True)
subject = Column(String, nullable=True)
description = Column(String, nullable=True)
class Asset(SQLModelBase, table=True):
id: str = Field(primary_key=True)
user_id: str | None = Field(default=None, foreign_key="user.id")
title: str = Field()
creator: str | None = Field(default=None)
subject: str | None = Field(default=None)
description: str | None = Field(default=None)

class Persona(Base):
__tablename__ = "persona"
id = Column(String, primary_key=True)
name = Column(String, nullable=False)
description = Column(String, nullable=True)
voice_id = Column(String, nullable=True)
face_id = Column(String, nullable=True)
class Persona(SQLModelBase, table=True):
id: str = Field(primary_key=True)
name: str = Field()
description: str | None = Field(default=None)
voice_id: str | None = Field(default=None)
face_id: str | None = Field(default=None)

0 comments on commit 2aed502

Please sign in to comment.