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_sqlmodel_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.11.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

fastapi_sqlmodel_crud-0.0.11-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-sqlmodel-crud-0.0.11.tar.gz.

File metadata

  • Download URL: fastapi-sqlmodel-crud-0.0.11.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5

File hashes

Hashes for fastapi-sqlmodel-crud-0.0.11.tar.gz
Algorithm Hash digest
SHA256 71fe4638f52b09eb52bf942b3c7badeb3f3e2c890a7c3be93fd6a321f2eb4544
MD5 049ebc11346153cdc5c24550c753a32f
BLAKE2b-256 0145247d5acd212d511f270f0bd9281c38635fb29bdc5d9f8a0a0bf61940c326

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_sqlmodel_crud-0.0.11-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5

File hashes

Hashes for fastapi_sqlmodel_crud-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 d82d29a722819c8ae4b6c8644e3410e1eeb3b1ffac31da32c8e5b7a4c1314cd8
MD5 5a0ce8e1d5e924cf53831ae4a05e32de
BLAKE2b-256 d5a2f8ffeb35715a4118f74e28f9b556b049b453041ad8b3d4ab8390fba9e975

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