Skip to main content

No project description provided

Project description

Aiochemy Documentation

Aiochemy is a Python library that simplifies database interactions by providing an Active Record pattern and automatic session management. It supports both synchronous and asynchronous operations, making it flexible for various application needs.

Installation

pip install aiochemy

Configuration

Aiochemy uses a configuration schema to manage database connection details. Here's an example using the PostgreSQLConfigSchema:

from aiochemy.config import PostgreSQLConfigSchema

config = PostgreSQLConfigSchema(
    db="mydatabase",
    user="myuser",
    password="mypassword",
    host="localhost",
    port=5432,
)

See mise.local.toml and other examples for different configuration options.

Setting up the Engine

The ActiveEngine class manages database connections. You can set it up for synchronous or asynchronous operations:

Synchronous:

from aiochemy import ActiveEngine

engine = ActiveEngine(config)

Asynchronous:

from aiochemy.aio import ActiveEngine

engine = ActiveEngine(config)

Set the engine for your models:

from aiochemy import ActiveRecord
from myapp.models import MyModel # Assuming 'MyModel' inherits from ActiveRecord

MyModel.set_engine(engine)

Basic CRUD Operations

Creating and Saving Records

from myapp.models import User

# Method 1: Create and save.
user = User(name="Alice", email="alice@example.com")
user.save()

# Method 2: Using add.
user = User(name="Bob")
User.add(user, commit=True) # commit=True commits the change immediately.

Querying

# Retrieve all users
users = User.all()

# Find a user by primary key.
user_pk = uuid.UUID("some uuid here")
user = User.find(user_pk) # using PKMixin which adds find().


# Find a user by email
user = User.find_by(email="alice@example.com")

# Find users with a specific name
users = User.where(User.name == "Alice").all()


# Find the first user in the database.
first_user = User.first() # Orders by the primary key.


# Find the last user in the database, ordered by a column.
last_user = User.last(User.name) # Orders by User.name DESC.


# Use a custom query with advanced selects
query = User.select().where(User.created_at < some_date)
users = User.all(query=query) # Can combine custom queries with other operations

# Find the first created user
first_created_user = User.first_created()

# Find the last created user
last_created_user = User.last_created()

# Count users
user_count = User.count()

Updating Records

user = User.find_by(name="Alice")
if user:
    user.name = "Alicia"
    user.save()

Deleting Records

user = User.find_by(name="Bob")
if user:
    User.delete(user)

Asynchronous Operations

Aiochemy supports asynchronous operations using asyncio and async/await. Use aiochemy.aio.ActiveRecord, aiochemy.aio.ActiveEngine, and aiochemy.aio.Schema:

import asyncio
from aiochemy.aio import ActiveRecord, Schema

# ... (setup engine as described above, using the async ActiveEngine) ...

async def create_user():
    user = User(name="Dave")
    await user.save()
    print(f"Created user: {user}")

asyncio.run(create_user())

# Use async methods for querying and operations
users = await User.all()

# Convert model instances to dictionaries and vice-versa
user_data = user.dump_model()
restored_user = User.load(**user_data)

schema = User.Schema() # Convert schemas to model instances and back
user_instance = schema.to_model(User)
user_schema = User.Schema.model_validate(user_instance)

See aiochemy/demo/amodels.py for a detailed example.

Explicit sessions

session = User.new_session()
try:
    user = User(name='Bob')
    session.add(user)
    session.commit()
finally:
    session.close()

# async
async def async_example():
    async_session = await User.new_session()
    try:
        user = User(name='Bob')
        async_session.add(user)
        await async_session.commit()
    finally:
        await async_session.close()

async def context_session():
    async with User.new_session() as session:    
        user = User(name='Bob')        
        async_session.add(user)
        await async_session.commit(session)
        # or 
        user2 = User(name='Alice')
        user2 = await Use.add(user2, commit=True, session=session)

Transactions

Transactions are automatically handled for add(), delete(), and save().

For fine-grained control use a context manager with begin() and begin_nested():

with User.new_session() as session
    try:
        # Operations here
        # Rollback will happen automatically
        session.add(user1)
        session.add(user2)
        1 / 0 # Will rollback to here
    except Exception as e:
        print(f"Error: {e}") # Will rollback to here
    session.commit()
    
## Session is closed on context exit

See aiochemy/utils/retry.py for how the Q context manager implements retries and transactions.

Custom Schemas

Create custom schemas by inheriting from aiochemy.Schema or aiochemy.aio.Schema and using Pydantic's features.

Examples

For more comprehensive examples, refer to the following:

  • aiochemy/demo for sample models and usage.
  • models.py and amodels.py for synchronous and asynchronous model definitions.

This documentation provides a starting point for using Aiochemy. Explore the code examples and docstrings for more in-depth information.

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

achemy-0.2.0.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

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

achemy-0.2.0-py3-none-any.whl (25.0 kB view details)

Uploaded Python 3

File details

Details for the file achemy-0.2.0.tar.gz.

File metadata

  • Download URL: achemy-0.2.0.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.12.8 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for achemy-0.2.0.tar.gz
Algorithm Hash digest
SHA256 740e1ca22cd24447daf975a06a94a85ac07c0d49d581384727d1e069b57ac685
MD5 62f980d1b459daf3c794941df278c839
BLAKE2b-256 ee58880bfbd621c816dc91ac97a230a1b4ada2f1892c76affd2837161e21460a

See more details on using hashes here.

File details

Details for the file achemy-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: achemy-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 25.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.12.8 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for achemy-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 00b982eecd3683c1145963f3fe74d0369dc4049507e0ff3da787ab7b7c3a1103
MD5 202a93137645bf4c14186330cd4e504c
BLAKE2b-256 daaf1d11b63a99da4b06fae86b0095ec98dfcb6e687d9633271af9f317f54a0d

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