AlchemyLite


NameAlchemyLite JSON
Version 1.1.1 PyPI version JSON
download
home_pagehttps://github.com/Garinelli/AlchemyLite
SummaryA library that simplifies CRUD operations with PostgreSQL database.
upload_time2025-02-16 06:00:47
maintainerNone
docs_urlNone
authorvoskan
requires_python>=3.10
licenseNone
keywords alchemylite alchemylite alchemy lite alchemy lite crud sqlalchemy python crud
VCS
bugtrack_url
requirements psycopg2 asyncpg psycopg-pool SQLAlchemy psycopg psycopg2-binary psycopg-binary
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # AlchemyLite
## A library that simplifies CRUD operations with PostgreSQL database.

# What is new in 1.1.1 release?
[Full docs](https://alchemylite.readthedocs.io/)
1. With this release, you can create a table in a database without using sqlalchemy syntax.  
How to do this?
```python
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.  
```python
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

2. There is no need to transfer config.session, just config  
Example  
```python
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:  
```python
crud = SyncCrudOperation(config.session, User)
```

3. 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
```python
crud = SyncCrudOperation(config, User)
```
4. You can also add a foreign key row  
Example
```python
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
```python
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
```python
from alchemylite.sync import SyncCrudOperation

crud = SyncCrudOperation(
    config.session, YourModel, Base
)
```
For async operation
```python
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

```python
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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Garinelli/AlchemyLite",
    "name": "AlchemyLite",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "AlchemyLite, alchemylite, Alchemy lite, alchemy lite, crud sqlalchemy, python crud",
    "author": "voskan",
    "author_email": "yura.voskanyan.2003@mail.ru",
    "download_url": "https://files.pythonhosted.org/packages/e3/dc/e0d0d72d3ce1675829259d180ccb3d15f8fbaa3c8aad6b0578c8e3910034/alchemylite-1.1.1.tar.gz",
    "platform": null,
    "description": "# AlchemyLite\r\n## A library that simplifies CRUD operations with PostgreSQL database.\r\n\r\n# What is new in 1.1.1 release?\r\n[Full docs](https://alchemylite.readthedocs.io/)\r\n1. With this release, you can create a table in a database without using sqlalchemy syntax.  \r\nHow to do this?\r\n```python\r\nfrom alchemylite import Table\r\n\r\nuser = Table(\r\n        table_name='user',\r\n        fields=\r\n        {\r\n            \"name\": {\"type\": str, \"max_len\": 255},\r\n            \"age\": {\"type\": int},\r\n            \"email\": {\"type\": str, \"unique\": True, \"index\": True},\r\n            \"is_admin\": {\"type\": bool, \"default\": False},\r\n            \"balance\": {\"type\": float},\r\n            \"joined_date\": {\"type\": \"datetime\"},\r\n            \"about_me\": {\"type\": \"text\", \"null\": True},\r\n        }\r\n    )\r\n```  \r\nThere is no need to create a row (id) with a primary key, this is done automatically by the library   \r\nFor a class to become a sqlalchemy model, you need to access the .model property.  \r\n```python\r\nuser = user.model\r\n```\r\nThe class accepts two params, the first is table name, the second is fields of table\r\nTypes can be as follows:\r\n* int\r\n* str,\r\n* bool\r\n* float\r\n* \"date\"\r\n* \"datetime\"\r\n* \"time\"\r\n* \"text\"  \r\n\r\nIf you specify a str type, you must specify a maximum length for it, using \"max_len\"  \r\nIf there is no need to use max_len then use type \"text\"  \r\nYou can also specify additional parameters for the row  \r\n* nullable - True or False. Default value - True\r\n* default - Your value. Default - None\r\n* unique - True or False. Default - False\r\n* index - True or False. Default - False\r\n\r\n2. There is no need to transfer config.session, just config  \r\nExample  \r\n```python\r\nfrom alchemylite.sync import SyncCrudOperation, SyncConfig\r\nfrom alchemylite import Table\r\n\r\nUser = Table(\r\n    table_name='user',\r\n    fields=\r\n    {\r\n        \"name\": {\"type\": str, \"max_len\": 255},\r\n        \"age\": {\"type\": int},\r\n        \"email\": {\"type\": str, \"unique\": True, \"index\": True},\r\n        \"is_admin\": {\"type\": bool, \"default\": False},\r\n        \"balance\": {\"type\": float},\r\n        \"joined_date\": {\"type\": \"datetime\"},\r\n        \"about_me\": {\"type\": \"text\", \"null\": True},\r\n    }\r\n)\r\n\r\nUser = User.model\r\n\r\nconfig = SyncConfig(\r\n    db_host=\"localhost\",\r\n    db_port=\"5432\",\r\n    db_user=\"postgres\",\r\n    db_pass=\"qwertyQ\",\r\n    db_name=\"AlchemyLite\"\r\n)\r\n\r\ncrud = SyncCrudOperation(config, User)\r\n```\r\nPreviously it was necessary to transfer it like this:  \r\n```python\r\ncrud = SyncCrudOperation(config.session, User)\r\n```\r\n\r\n3. It is not necessary to pass Base to a class with CRUD operations  \r\nOnly need to pass if you want to use the create_all_tables() and delete_all_tables() methods\r\nTo create and delete a table\r\nExample\r\n```python\r\ncrud = SyncCrudOperation(config, User)\r\n```\r\n4. You can also add a foreign key row  \r\nExample\r\n```python\r\nfrom alchemylite import Table\r\n\r\norder = Table(\r\n    table_name='orders',\r\n    fields={\r\n        \"user\": {\"type\": int, \"foreignkey\": \"users.id\"},\r\n        \"item\": {\"type\": str}\r\n    }\r\n)\r\norder = order.model\r\n```\r\n# How to use it?\r\nFirst, install the library with the command ```pip install AlchemyLite```  \r\nFirst you need to create a configuration in which you need to register the database parameters  \r\nFor synchronous operation\r\n```python\r\nfrom alchemylite.sync impoty SyncConfig\r\n\r\nconfig = SyncConfig(\r\n    db_host=\"your_host\",\r\n    db_port=\"your_port\",\r\n    db_user=\"your_user\",\r\n    db_pass=\"your_password\",\r\n    db_name=\"your_db_name\"\r\n)\r\n```\r\nThen, we create a class to which we pass our configuration, model class and base class of model\r\n```python\r\nfrom alchemylite.sync import SyncCrudOperation\r\n\r\ncrud = SyncCrudOperation(\r\n    config.session, YourModel, Base\r\n)\r\n```\r\nFor async operation\r\n```python\r\nfrom alchemylite.async_ import AsyncConfig, AsyncCrudOperation\r\n\r\nconfig = AsyncConfig(\r\n    db_host=\"your_host\",\r\n    db_port=\"your_port\",\r\n    db_user=\"your_user\",\r\n    db_pass=\"your_password\",\r\n    db_name=\"your_db_name\"\r\n)\r\n\r\ncrud = AsyncCrudOperation(\r\n    config.session, YourModel, Base\r\n)\r\n```\r\n# How to perform CRUD operations?\r\nThe library supports the following methods\r\n* create - Creates new data in the table.\r\n* read_all - Reads all data from a table.\r\n* limited_read - Reads a certain amount of data. Default values: limit = 50, offset = 0\r\n* read_by_id - Reads all data from a table by id\r\n* update_by_id - Update data by id\r\n* delete_by_id - Delete data by id\r\n* create_all_tables - Creates all tables in database\r\n* delete_all_tables - Delete all tables in database\r\n\r\n# Examples of use\r\n\r\n```python\r\nfrom alchemylite.sync import SyncCrudOperation, SyncConfig\r\nfrom sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase\r\n\r\n\r\nconfig = SyncConfig(\r\n    db_host=\"localhost\",\r\n    db_port=\"5432\",\r\n    db_user=\"postgres\",\r\n    db_pass=\"postgres\",\r\n    db_name=\"alchemylite\"\r\n)\r\n\r\n\r\nclass Base(DeclarativeBase):\r\n    pass\r\n    \r\n    \r\nclass User(Base):\r\n    __tablename__ = \"users\"\r\n    id: Mapped[int] = mapped_column(primary_key=True)\r\n    name: Mapped[str]\r\n    email: Mapped[str]\r\n   \r\n\r\ncrud = SyncCrudOperation(\r\n    config.session, User, Base\r\n)\r\n\r\ncrud.create_all_tables()\r\ncrud.create(name=\"User\", email=\"email@mail.ru\")\r\ncrud.read_all()\r\ncrud.limited_read(limit=5, offset=0)\r\ncrud.read_by_id(id=1)\r\ncrud.update_by_id(id=1, name=\"new value\", email=\"new_emal\")\r\ncrud.delete_by_id(id=1)\r\ncrud.delete_all_tables()\r\n```\r\n## The library will be supported, this is the first version for now. New features will be added in the future.\r\n### If you have suggestions for improvements or any comments, I'm ready to listen to you\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A library that simplifies CRUD operations with PostgreSQL database.",
    "version": "1.1.1",
    "project_urls": {
        "Documentation": "https://github.com/Garinelli/AlchemyLite",
        "Homepage": "https://github.com/Garinelli/AlchemyLite"
    },
    "split_keywords": [
        "alchemylite",
        " alchemylite",
        " alchemy lite",
        " alchemy lite",
        " crud sqlalchemy",
        " python crud"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "053d17a85b905b61f087b60c12982925c47451bef932ec9145e2eef86a927320",
                "md5": "6bea0a66b2167ed09f54519dab49626b",
                "sha256": "fbbbd1f699d31c5fc02c22f9bcda75de6fab6bbf71f33b9123a1ea9722200a5f"
            },
            "downloads": -1,
            "filename": "AlchemyLite-1.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6bea0a66b2167ed09f54519dab49626b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 10815,
            "upload_time": "2025-02-16T06:00:44",
            "upload_time_iso_8601": "2025-02-16T06:00:44.972398Z",
            "url": "https://files.pythonhosted.org/packages/05/3d/17a85b905b61f087b60c12982925c47451bef932ec9145e2eef86a927320/AlchemyLite-1.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e3dce0d0d72d3ce1675829259d180ccb3d15f8fbaa3c8aad6b0578c8e3910034",
                "md5": "959bbaa4ed9dcc65aef0b34c76599577",
                "sha256": "23f7c63fbeedacb7328158588cf66ff55854b7044bafbd493758308e10ce39ab"
            },
            "downloads": -1,
            "filename": "alchemylite-1.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "959bbaa4ed9dcc65aef0b34c76599577",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 7922,
            "upload_time": "2025-02-16T06:00:47",
            "upload_time_iso_8601": "2025-02-16T06:00:47.183637Z",
            "url": "https://files.pythonhosted.org/packages/e3/dc/e0d0d72d3ce1675829259d180ccb3d15f8fbaa3c8aad6b0578c8e3910034/alchemylite-1.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-16 06:00:47",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Garinelli",
    "github_project": "AlchemyLite",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "psycopg2",
            "specs": [
                [
                    ">=",
                    "2.9.10"
                ]
            ]
        },
        {
            "name": "asyncpg",
            "specs": [
                [
                    ">=",
                    "0.30.0"
                ]
            ]
        },
        {
            "name": "psycopg-pool",
            "specs": [
                [
                    ">=",
                    "3.2.3"
                ]
            ]
        },
        {
            "name": "SQLAlchemy",
            "specs": [
                [
                    ">=",
                    "2.0.36"
                ]
            ]
        },
        {
            "name": "psycopg",
            "specs": [
                [
                    ">=",
                    "3.2.3"
                ]
            ]
        },
        {
            "name": "psycopg2-binary",
            "specs": [
                [
                    ">=",
                    "2.9.10"
                ]
            ]
        },
        {
            "name": "psycopg-binary",
            "specs": [
                [
                    "==",
                    "3.2.4"
                ]
            ]
        }
    ],
    "lcname": "alchemylite"
}
        
Elapsed time: 0.50982s