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.1.tar.gz
(5.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file crudal-0.3.1.tar.gz.
File metadata
- Download URL: crudal-0.3.1.tar.gz
- Upload date:
- Size: 5.4 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 |
8f2b6468363f2a113b29fc365c79a0eb6ae933f91c91f8376cd85a1e00709c47
|
|
| MD5 |
c9f1839db65aa7ebe1336564ac7df88c
|
|
| BLAKE2b-256 |
edbead444b8113b4dcab995272c702e14db11cb34b0008c43a539c873e591300
|
File details
Details for the file crudal-0.3.1-py3-none-any.whl.
File metadata
- Download URL: crudal-0.3.1-py3-none-any.whl
- Upload date:
- Size: 8.5 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 |
713f57cb10cee9847cf9c50fa50d92d6230ea8dd2f4c0dd7ad2368eae4c10c5b
|
|
| MD5 |
a7f7a450b9ab3d386af3c0060741b081
|
|
| BLAKE2b-256 |
42190fa0c7046090f63851bc1c3677b58285f0224e301b3b519b9bb05bbb78f9
|