Skip to main content

A library that simplifies CRUD operations with PostgreSQL database.

Project description

AlchemyLite

A library that simplifies CRUD operations with PostgreSQL database.

What is new in 0.1.0 release?

  1. With this release, you can create a table in a database without using sqlalchemy syntax.
    How to do this?
from alchemylite import Table

user = Table(
        table_name='user',
        fields=
        {
            "name": {"type": str, "max_len": 255},
            "age": {"type": int},
            "email": {"type": str, "unique": True, "index": True},
            "is_admin": {"type": bool, "default": False},
            "balance": {"type": float},
            "joined_date": {"type": "datetime"},
            "about_me": {"type": "text", "null": True},
        }
    )

There is no need to create a row (id) with a primary key, this is done automatically by the library
For a class to become a sqlalchemy model, you need to access the .model property.

user = user.model

The class accepts two params, the first is table name, the second is fields of table Types can be as follows:

  • int
  • str,
  • bool
  • float
  • "date"
  • "datetime"
  • "time"
  • "text"

If you specify a str type, you must specify a maximum length for it, using "max_len"
If there is no need to use max_len then use type "text"
You can also specify additional parameters for the row

  • nullable - True or False. Default value - True
  • default - Your value. Default - None
  • unique - True or False. Default - False
  • index - True or False. Default - False
  1. There is no need to transfer config.session, just config
    Example
from alchemylite.sync import SyncCrudOperation, SyncConfig
from alchemylite import Table

User = Table(
    table_name='user',
    fields=
    {
        "name": {"type": str, "max_len": 255},
        "age": {"type": int},
        "email": {"type": str, "unique": True, "index": True},
        "is_admin": {"type": bool, "default": False},
        "balance": {"type": float},
        "joined_date": {"type": "datetime"},
        "about_me": {"type": "text", "null": True},
    }
)

User = User.model

config = SyncConfig(
    db_host="localhost",
    db_port="5432",
    db_user="postgres",
    db_pass="qwertyQ",
    db_name="AlchemyLite"
)

crud = SyncCrudOperation(config, User)

Previously it was necessary to transfer it like this:

crud = SyncCrudOperation(config.session, User)
  1. It is not necessary to pass Base to a class with CRUD operations
    Only need to pass if you want to use the create_all_tables() and delete_all_tables() methods To create and delete a table Example
crud = SyncCrudOperation(config, User)
  1. You can also add a foreign key row
    Example
from alchemylite import Table

order = Table(
    table_name='orders',
    fields={
        "user": {"type": int, "foreignkey": "users.id"},
        "item": {"type": str}
    }
)
order = order.model

How to use it?

First, install the library with the command pip install AlchemyLite
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
)

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):
    __tablename__ = "users"
    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


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.1.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

AlchemyLite-1.1.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file AlchemyLite-1.1.0.tar.gz.

File metadata

  • Download URL: AlchemyLite-1.1.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.4

File hashes

Hashes for AlchemyLite-1.1.0.tar.gz
Algorithm Hash digest
SHA256 e49e1ab26231d5cb9f71982814e668094c10fb4427f3696450a27499726b7635
MD5 a67b7e9970d069cce85f6d2f6dd12dba
BLAKE2b-256 2125ea5b38aa1cf37e2ff02963810f6263b34c3b8fd82601bde5e287d6528ed9

See more details on using hashes here.

File details

Details for the file AlchemyLite-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: AlchemyLite-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.4

File hashes

Hashes for AlchemyLite-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f9d30196e310e34acc090eabcc49de17f28aba3a2b5737b0fc97195326fd4bc4
MD5 b736f0a4c54090835143d05e9d531145
BLAKE2b-256 36fcea31af38c9c539b4966dc5ef661780ffd5dfea845ba5956b49919b81b715

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page