SQLAlchemy Model CRUD
Model CRUD manager to handle databases with asynchronous SQLAlchemy sessions.
- Source code: https://github.com/lucaslucyk/sa-model-crud
- Package: https://pypi.org/project/sa-modelcrud
- Documentation: Coming soon.
Project Status
⚠️ Warning: This project is currently in development phase.
This project is in an early stage of development and may contain bugs. It is not recommended for use in production environments.
Why use sa_crudmodel?
- 🚀 Fast to code: Increase the speed to develop integrations features.
- ❌ Fewer bugs: Reduce human (developer) induced errors.
- 💡 Intuitive: Great editor support. Completion everywhere. Less time debugging.
- 🤓 Easy: Designed to be easy to use and learn. Less time reading docs.
- 〽️ Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
Requirements
Python 3.8+
SQLAlchemy Model CRUD stands on the soulders of giants:
- SQLAlchemy for the database parts.
- Pydantic for the data parts.
Installation
$ pip install sa-modelcrud
Example
Database Model prepare
- Create a database model with:
from sqlalchemy.orm import Mapped, mapped_column
from sa_modelcrud.models import ModelBase, Timestamp
class Sample(Timestamp, ModelBase):
# ModelBase contains id and uid properties
email: Mapped[str] = mapped_column(nullable=True, unique=True)
Create schemas
from typing import Optional
from uuid import UUID
from pydantic import BaseModel
class SampleBase(BaseModel):
id: Optional[UUID] = None
email: Optional[str] = None
class SampleCreate(SampleBase):
...
class SampleUpdate(SampleBase):
...
Create CRUD
from sa_modelcrud import CRUDBase
class CRUDSample(CRUDBase[Sample, SampleCreate, SampleUpdate]):
model = Sample
samples = CRUDSample()
Create session
from sqlalchemy.ext.asyncio import (
AsyncSession,
create_async_engine,
async_sessionmaker
)
DB_URI = "sqlite+aiosqlite:///./db.sqlite3"
async_engine: AsyncEngine = create_async_engine(
DB_URI,
future=True,
connect_args={"check_same_thread": False}
)
AsyncSessionLocal: AsyncSession = async_sessionmaker(
bind=async_engine,
class_=AsyncSession,
expire_on_commit=False
)
Use the CRUD
async with AsyncSessionLocal() as db:
data = SampleCreate(email="sample@sample")
# save data into database
sample_obj = await samples.create(db=db, element=data)
General CRUD Methods
All inherited CRUDBase instances have the following methods:
.get(..., id): Get row from model by uid..get_or_raise(..., id): Try to get row from model by uid. Raise if not object found..list(...): Get multi items from database..filter(..., whereclause): Get items from database usingwhereclauseto filter..find(..., **kwargs): Find elements with kwargs..find_one(..., **kwargs): Find an element with kwargs..create(..., element): Create an element into database..bulk_create(..., elements): Create elements into database..update(..., obj, data): Update a database obj with an update data schema..delete(..., id): Delete an item from database.
TODO:
- Paginate results of methods
list,filterandfind. - Add default values for
offsetandlimiton paginated methods. - Add support for Sync Sessions.
- Create complete documentation in Readthedocs.
Contributions and Feedback
I would love to receive contributions and feedback! If you'd like to get involved, please contact me through one of the contact methods in my Profile.
License
This project is licensed under the terms of the MIT license.
Release files for sa-modelcrud 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sa_modelcrud-0.4.1.tar.gz | 9.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sa_modelcrud-0.4.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.2 kB
Release files / sa_modelcrud-0.4.1.tar.gz
| Download URL | sa_modelcrud-0.4.1.tar.gz |
|---|---|
| Size | 9.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
918c2b467802bf0943e045402ccaef4c6e179147f6df8dc3e6f5913b19cf4002
|
|
BLAKE2b-256 checksum How to use checksums |
5d71f080029325a6e545ec6af1a98d07ca61e65e938bb076a839bb35d951b4f6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Release files / sa_modelcrud-0.4.1-py3-none-any.whl
| Download URL | sa_modelcrud-0.4.1-py3-none-any.whl |
|---|---|
| Size | 11.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
06ae0fc9f1c2175ccf124656f87c45a927f8076516b24e0bc6d3cd147a9b1d5e
|
|
BLAKE2b-256 checksum How to use checksums |
2addcf6e054295d04de20f6a58971d4047fd13fe7b59c3958fe0c33294656f46
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|