SimpleCRUD is a library that provides a simple way to create CRUD commands for SQLAlchemy models.
pip install hexfrost-simplecrud
poetry add hexfrost-simplecrud
- Create a model
- Create a sessionmaker from SQLAlchemy
- Import CRUD functions
- Use CRUD functions and enjoy
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
from sqlalchemy.orm import DeclarativeBase
from simplecrud import get_all, create_obj, update_obj, delete_object
engine = create_async_engine("sqlite+aiosqlite:///test.db", echo=True)
session = async_sessionmaker(async_engine, expire_on_commit=False, class_=AsyncSession)
# Create a model
class ExampleModel(DeclarativeBase):
__tablename__ = "example_model"
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False)
description = Column(String(50), nullable=False)
async def example_func():
# Create a model
new_model = await create_object(model, name="test", description="test", conn=session())
# Get all models
all_objs = await get_all(model, conn=session())
# Update a model
updated_obj = await update_object(model, name="test2", description="test2", conn=session())
# Delete a model
await delete_object(model, name="test2", description="test2", conn=session())
get_object
- get a single objectget_all
- get all objectsget_all_with_filter
- get all objects with filterget_objects
- get all objects with filter, limit and offsetget_or_create_object
- get or create an objectcreate_object
- create an objectbulk_create
- create multiple objectsupdate_object
- update an objectupdate_or_error
- update an object or raise an errorupdate_object_by_id
- update an object by idupdate_or_create_object
- update or create an objectdelete_object
- delete an objectdelete_object_by_id
- delete an object by idbulk_delete
- bulk delete objectsbulk_delete_by_id
- bulk delete objects by id
This project is open for contributions. Feel free to open an issue or create a pull request.
GNU GENERAL PUBLIC LICENSE Version 3