Library with built-in CRUD operations in SQLAlchemy
Project description
AlchemyLite
A library that simplifies CRUD operations with PostgreSQL database.
How to use it?
First you need to create a configuration in which you need to register the database parameters
For synchronous operation
from alchemylite.sync impoty SyncConfig
config = SyncConfig(
db_host="your_host",
db_port="your_port",
db_user="your_user",
db_pass="your_password",
db_name="your_db_name"
)
Then, we create a class to which we pass our configuration, model class and base class of model
from alchemylite.sync import SyncCrudOperation
crud = SyncCrudOperation(
config.session, YourModel, Base
)
For async operation
from alchemylite.async_ import AsyncConfig, AsyncCrudOperation
config = AsyncConfig(
db_host="your_host",
db_port="your_port",
db_user="your_user",
db_pass="your_password",
db_name="your_db_name"
)
crud = AsyncCrudOperation(
config.session, YourModel, Base
)
If you have your session, you don't need to create config class, just transfer your session
crud = AsyncCrudOperation(
your_session, YourModel, Base
)
How to perform CRUD operations?
The library supports the following methods
- create - Creates new data in the table.
- read_all - Reads all data from a table.
- limited_read - Reads a certain amount of data. Default values: limit = 50, offset = 0
- read_by_id - Reads all data from a table by id
- update_by_id - Update data by id
- delete_by_id - Delete data by id
- create_all_tables - Creates all tables in database
- delete_all_tables - Delete all tables in database
Examples of use
from alchemylite.sync import SyncCrudOperation, SyncConfig
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase
config = SyncConfig(
db_host="localhost",
db_port="5432",
db_user="postgres",
db_pass="postgres",
db_name="alchemylite"
)
class Base(DeclarativeBase):
pass
class User(Base):
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str]
email: Mapped[str]
crud = SyncCrudOperation(
config.session, User, Base
)
crud.create_all_tables()
crud.create(name="User", email="email@mail.ru")
crud.read_all()
crud.limited_read(limit=5, offset=0)
crud.read_by_id(id=1)
crud.update_by_id(id=1, name="new value", email="new_emal")
crud.delete_by_id(id=1)
crud.delete_all_tables()
The library will be supported, this is the first version for now. New features will be added in the future.
If you have suggestions for improvements or any comments, I'm ready to listen to you
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
AlchemyLite-1.0.1.tar.gz
(5.7 kB
view details)
Built Distribution
File details
Details for the file AlchemyLite-1.0.1.tar.gz
.
File metadata
- Download URL: AlchemyLite-1.0.1.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7d4a87dd2d6db34c2c5a3c89cf270c61d48968fe8a99aa2d82a3cd68d095cd9 |
|
MD5 | 0f0f96571010670b0470c53f82aefda6 |
|
BLAKE2b-256 | 661bfc18bbf7e974c5287208bb6694c8801166152082e10f2cfdac86e85f2e6e |
File details
Details for the file AlchemyLite-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: AlchemyLite-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18fcd782861256f03864c60af61e3361c864fb82a51fc8c2d193fca77702bb97 |
|
MD5 | 663703e36d9035e48a04b513e2f55f73 |
|
BLAKE2b-256 | ce6f15ab5c2ca7a54f49d5195cdd13d2985bbec7549043d3003008d0bcb1627c |