CRUD operations for SQLAlchemy models
Project description
CRUDAL
Ready to use CRUD methods for SQLAlchemy models. Both Sync and Async
pip install crudal
Examples
Sync
Model initialization
from crudal import DeclarativeCrudBase
from sqlalchemy import Integer, String, create_engine
from sqlalchemy.orm import Mapped, mapped_column, sessionmaker
engine = create_engine("sqlite://")
SessionLocal = sessionmaker(engine, expire_on_commit=False)
class User(DeclarativeCrudBase):
__tablename__ = "person"
__session__ = sessionmaker(engine, expire_on_commit=False)
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String, nullable=False)
DeclarativeCrudBase.metadata.create_all(bind=engine)
Add new item
user = User(name="Andrew")
user.add()
Find item
Find all users with name equals Andrew
users_found = User.find(name="Andrew")
for u in users_found:
assert u.name == "Andrew"
Update item
Change name of all users with name Andrew to John
User.update(session=session, values=dict(name="John"), name="Andrew")
Delete item
User.delete(name="John")
Async
from crudal import DeclarativeCrudBaseAsync
from sqlalchemy import Integer, String, create_engine
from sqlalchemy.orm import Mapped, Session, mapped_column
class User(DeclarativeCrudBaseAsync):
__tablename__ = "person"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String, nullable=False)
Find
with Session(bind=engine) as session:
p = User(name="Andrew")
p.add(session=session)
p2 = User(name="Bob")
p2.add(session=session)
# find person with name "Andrew"
andrew = User.find(session=session, name="Andrew")
all_users = User.all(session=session)
for u in all_users:
print(u.name)
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
crudal-0.3.0.tar.gz
(4.8 kB
view details)
Built Distribution
File details
Details for the file crudal-0.3.0.tar.gz
.
File metadata
- Download URL: crudal-0.3.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.1 CPython/3.10.6 Linux/5.15.90.1-microsoft-standard-WSL2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8994fcf9b8559f1ac872619cd72d99a343d38e9b33f71ef074c3d847d17c6641 |
|
MD5 | ab31ed737980b5ef2dfa7fecf81539e5 |
|
BLAKE2b-256 | af2da9ab21185e8a1bae5a7ec92f376d8a74076cc934d377dca2ee39ea31b4d5 |
File details
Details for the file crudal-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: crudal-0.3.0-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.1 CPython/3.10.6 Linux/5.15.90.1-microsoft-standard-WSL2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb988cf385c561dc460c29e3060f323e451ce9556710e6ffe5a9eec869f4d30c |
|
MD5 | d37988c388ef18b723bae361a4d33eb3 |
|
BLAKE2b-256 | a7f87f7d54dd4a80b5912b89e9bb65cd88a4ef9e3ab5c602ca67331e9d26b443 |