-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
draft: SQL model for collections (#5)
Starting with this since it is outside the main API and let's me figure out the ORM and interaction with the API methods, etc.
- Loading branch information
1 parent
093be1f
commit e54d7b1
Showing
9 changed files
with
96 additions
and
14 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import Annotated, List | ||
from fastapi import APIRouter, Path | ||
from sqlmodel import Session, select | ||
|
||
from app.common.schema import Collection, DbDep | ||
|
||
router = APIRouter(tags=["collections"], prefix="/collections") | ||
|
||
@router.put("/") | ||
async def add(db: DbDep, collection: Collection) -> Collection: | ||
"""Create a collection.""" | ||
with Session(db) as session: | ||
session.add(collection) | ||
session.commit() | ||
session.refresh(collection) | ||
return collection | ||
|
||
@router.get("/") | ||
async def list(db: DbDep) -> List[Collection]: | ||
"""List collections.""" | ||
with Session(db) as session: | ||
collections = session.exec(select(Collection)).all() | ||
return collections | ||
|
||
PathCollectionId = Annotated[int, Path(..., description="The collection ID.")] | ||
|
||
@router.get("/{id}") | ||
async def get(id: PathCollectionId, db: DbDep) -> Collection: | ||
"""Get a specific collection.""" | ||
with Session(db) as session: | ||
return session.get(Collection, id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from typing import Annotated, Optional | ||
from fastapi import Depends, Request | ||
from sqlalchemy import Engine, UniqueConstraint | ||
from sqlmodel import Field, SQLModel | ||
|
||
class Collection(SQLModel, table=True): | ||
id: Optional[int] = Field(default=None, primary_key=True) | ||
|
||
# TODO: We may want this to be unique per-tenant rather than globally unique names. | ||
name: str = Field(index=True, unique=True) | ||
|
||
class Document(SQLModel, table=True): | ||
"""Schema for documents in the SQL DB. | ||
""" | ||
__table_args__ = (UniqueConstraint("collection_id", "url"), | ||
UniqueConstraint("collection_id", "doc_id")) | ||
|
||
id: Optional[int] = Field(default=None, primary_key=True) | ||
collection_id: int = Field(foreign_key="collection.id") | ||
|
||
url: str = Field(index=True) | ||
doc_id: Optional[str] = Field(default = None) | ||
|
||
def _db(request: Request) -> Engine: | ||
return request.state.engine | ||
|
||
DbDep = Annotated[Engine, Depends(_db)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
from fastapi import APIRouter | ||
|
||
from app.chunks.router import router as chunks_router | ||
from app.collections.router import router as collections_router | ||
from app.documents.router import router as documents_router | ||
|
||
api_router = APIRouter(prefix="/api") | ||
|
||
api_router.include_router(collections_router) | ||
api_router.include_router(documents_router) | ||
api_router.include_router(chunks_router) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters