Skip to main content

fastapi-sqlmodel-crud is a program which is based on fastapi+sqlmodel and used to quickly build the Create, Read, Update, Delete generic API interface.

Project description

项目介绍

fastapi-sqlmodel-crud是一个基于FastAPI+SQLModel, 用于快速构建Create,Read,Update,Delete通用API接口的项目.

安装

pip install fastapi-sqlmodel-crud 

简单示例

main.py:

from typing import Optional, Generator, Any
from fastapi import FastAPI
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlmodel import SQLModel, Field
from sqlmodel.ext.asyncio.session import AsyncSession
from fastapi_amis_admin.crud import SQLModelCrud


#  1.创建SQLModel模型
class Article(SQLModel, table=True):
    id: int = Field(default=None, primary_key=True, nullable=False)
    title: str = Field(title='ArticleTitle', max_length=200)
    description: Optional[str] = Field(default='', title='ArticleDescription', max_length=400)
    status: bool = Field(None, title='status')
    content: str = Field(title='ArticleContent')


# 2.创建 AsyncSession
database_url = 'sqlite+aiosqlite:///admisadmin.db'
engine: AsyncEngine = create_async_engine(database_url, future=True, pool_recycle=1200)
session_maker: sessionmaker = sessionmaker(engine, class_=AsyncSession,
                                           expire_on_commit=False, autocommit=False, autoflush=False)


async def session_factory() -> Generator[AsyncSession, Any, None]:
    async with session_maker() as session:
        yield session


# 3. 注册crud路由
article_crud = SQLModelCrud(model=Article, session_factory=session_factory).register_crud()

app = FastAPI(debug=True)

# 4. 包含路由器
app.include_router(article_crud.router)


# 5. 创建模型数据库表(首次运行)
@app.on_event("startup")
async def startup():
    async with engine.begin() as conn:
        await conn.run_sync(SQLModel.metadata.create_all)

开发文档

依赖项目

许可协议

该项目遵循 Apache2.0 许可协议。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_sqlmodel_crud-0.0.18.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

fastapi_sqlmodel_crud-0.0.18-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_sqlmodel_crud-0.0.18.tar.gz.

File metadata

File hashes

Hashes for fastapi_sqlmodel_crud-0.0.18.tar.gz
Algorithm Hash digest
SHA256 a78c5a18fb30730ab770e77d945d1736d9aa793dd9bf759b6b234bb8624bb9f7
MD5 e2b90dfb30bbb9cc3cf46acf79266514
BLAKE2b-256 0b029c52910a7f3409e34c8782292cec6a1e3167b7c6d00cdfc613da5442270f

See more details on using hashes here.

File details

Details for the file fastapi_sqlmodel_crud-0.0.18-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_sqlmodel_crud-0.0.18-py3-none-any.whl
Algorithm Hash digest
SHA256 c121f160728ad1a7ff06c8715f0c48960d3e3082b3523429c27c3a6725b1ba25
MD5 98cbd374718473a919ae056d1ec36085
BLAKE2b-256 dcfc884f6a65142c6e46ab521fdfbf12c6cbce303d6911e1ae06831a11f6d34a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page