Skip to main content

Simple SQLAlchemy connector manager.

Project description

Alchemynger

SQLAlchemy Connection Manager

Python

Python Зostgres SQLite Flask MySQL FastAPI

Alchemynger is a versatile Python library that simplifies database connectivity and interaction using SQLAlchemy. It offers both synchronous and asynchronous database management for applications that require efficient database operations.

Installation

You can install Alchemynger using pip:

pip install alchemynger

Usage

SyncManager: Synchronous Database Operations

SyncManager is designed for traditional synchronous applications. Here's how to use it:

from alchemynger import SyncManager
from alchemynger.sqlalchemy import Column, String

# Create a SyncManager instance
manager = SyncManager('sqlite:///path/to/db')

# Define your SQLAlchemy model class
class User(manager.Base):
    __tablename__ = 'user'
    name = Column(String(30), primary_key=True)

# Create User table
manager.create_all()

# Create an insert statement and execute it
stmt = manager[User].insert.values(name='username')

manager.execute(stmt, commit=True) # or manager(stmt, commit=True)

AsyncManager: Asynchronous Database Operations

AsyncManager is tailored for asyncio-based applications. Here's how to use it:

from asyncio import run
from alchemynger import AsyncManager
from alchemynger.sqlalchemy import Column, String

# Create an AsyncManager instance
manager = AsyncManager('sqlite+aiosqlite:///path/to/db')

# Define your SQLAlchemy model class
class User(manager.Base):
    __tablename__ = 'user'
    name = Column(String(30), primary_key=True)

# Define an async main function
async def main():
    await manager.create_all()

    stmt = manager[User].insert.values(name='username')

    await manager.execute(stmt, commit=True) # or await manager(stmt, commit=True)

if __name__ == "__main__":
    run(main())

Selector: Useage

Selectors can be used with Columns, a Column, or the Table Model.

# Create a select statements with column or columns
manager[User.name].select
manager[User.name, User.any_col].select

Selectors are simply shortcuts.

# Create statements with model
manager[User].select -> select(User)
manager[User].delete -> delete(User)
manager[User].insert -> insert(User)
manager[User].update -> update(User)

then execute

# execute statement
manager.execute(stmt, scalars=False)

Native use of SQLAlchemy queries

You can also utilize the standard query-writing methods provided by SQLAlchemy, for example, if you find that the library's functionality is insufficient for your needs. Just user from sqlalchemy import select, insert, ... or import from from alchemynger.sqlalchemy import select, insert

from alchemynger import SyncManager
from alchemynger.sqlalchemy import select, insert, Column, String, Integer

# Create a SyncManager instance
manager = SyncManager('sqlite:///path/to/db')

# Define your SQLAlchemy model class
class User(manager.Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(30))

# Create User table
manager.create_all()

# Create a select statement and execute it

stmt = select(User).where(User.id > 5)

manager.execute(stmt)

# Create an insert statement and execute it
stmt = insert(User).values(name="Lex")

manager.execute(stmt, commit=True) # or manager(stmt, commit=True)

Context Managers and Error Handling

Both SyncManager and AsyncManager provide context managers for managing database sessions. This ensures that sessions are properly closed, even in the event of an exception.

# Example using a context manager
with manager.get_session() as session:
    stmt = manager[User].select

    result = session.execute(stmt)
    # Use the result

Contribution

Contributions to Alchemynger are welcome! If you have ideas for improvements, bug fixes, or new features, please open an issue or submit a pull request on the GitHub repository.

License

Alchemynger is licensed under the MIT License. See the LICENSE file for more details.

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

alchemynger-0.2.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

alchemynger-0.2.2-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file alchemynger-0.2.2.tar.gz.

File metadata

  • Download URL: alchemynger-0.2.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.9 Windows/11

File hashes

Hashes for alchemynger-0.2.2.tar.gz
Algorithm Hash digest
SHA256 488c5b20a76cc12b3af7401097a5f0922f374151afd059a8859ba2e7ddd8e176
MD5 475b0c1b712e82c63d8641b72f69c538
BLAKE2b-256 1ccc20bd5f32981e73dc1847b862cc46976f80f9958809eaabe5ea8475eb8cbe

See more details on using hashes here.

File details

Details for the file alchemynger-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: alchemynger-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.9 Windows/11

File hashes

Hashes for alchemynger-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 027125668ede8fa80f48cce3b39978fb3989e4f01e2c7577550ccba3e6547067
MD5 3b4bd67ef449b5edc01338f966391503
BLAKE2b-256 7e5fac3f234972fbed4f536398ae568fbc6ecd2d87347d635e225976ab77060e

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