Generate CRUD routers for you schema in a simple way
Project description
FastAPI Simple CRUD Generator
Repository
Installation
pip install fastapi-simple-crud
Description
A package to generate a CRUD routers and endpoints in a very simple way. Based on SQLAlchemy asynchronous operation and schema.
How to use ?
from dotenv import load_dotenv
load_dotenv()
from fastapi import FastAPI
from typing import Optional
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from .dependencies.simpleCRUD import SimpleCRUDGenerator, RouterMap
engine = create_async_engine("sqlite+aiosqlite:///./test.db", echo=True, future=True)
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
Base = declarative_base()
async def get_session() -> AsyncSession:
async with async_session() as session:
yield session
class Country(Base):
__tablename__ = "country"
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
name = Column(String(100), nullable=False)
class President(Base):
__tablename__ = "president"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, nullable=False)
country_id = Column(Integer, ForeignKey("country.id"))
country = relationship("Country")
class People(Base):
__tablename__ = "people"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, nullable=False)
age = Column(Integer)
country_id = Column(Integer, ForeignKey("country.id"))
country = relationship("Country")
app = FastAPI()
@app.on_event("startup")
async def startup():
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
## ULTRA SIMPLE OH MY GOD!
RouterMap.create_router_map_from_base(base=Base)
crudGenerator = SimpleCRUDGenerator(application=app, session_getter=get_session)
Results
- your endpoints
- your pydantic schema
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
fastapi-simple-crud-0.0.3.tar.gz
(114.0 kB
view hashes)
Built Distribution
Close
Hashes for fastapi-simple-crud-0.0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcf91ea06506aedc47222022646aa16b591fd4033e1f42bb1ed585078a3a7353 |
|
MD5 | fb05d965b820b71b7a29742bbf1f57aa |
|
BLAKE2b-256 | 9ad027d87519ef3c6e56282541e299c131d2966531e8549bfe056f30adef81a3 |
Close
Hashes for fastapi_simple_crud-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a621a9b9e3b22761f08ef6273bab8ad4bfd63bfaf31653bd599702413b0b2ddc |
|
MD5 | e390f6925f0901505697e0cc134aae88 |
|
BLAKE2b-256 | 58e589aff2e5201c79aeffed63c4f6be63e4b44e8a7d35159147444ce2eb9f1c |