Skip to content

loryth-persistence — Consumer Guide

Audience: Consumers — developers using loryth-persistence in their services.

A database persistence library that simplifies working with databases.

Overview

The loryth-persistence library provides a set of abstractions and utilities for working with databases. It simplifies common database operations and provides a consistent interface for database access across the application.

Key Components

Base Entity and Repository

  • BaseEntity: A base class for all database entities, providing common functionality for entity management.
  • BaseRepository: A base class for repositories, providing common CRUD operations for entities.

Database Configuration

  • DbConfig: A configuration class for database connections.

Transaction Management

  • transactional: A decorator for managing database transactions, ensuring that operations are atomic.

Session Management

  • SessionFactory: An interface for creating database sessions.
  • DefaultSessionFactory: A default implementation of the session factory.
  • SessionManager: A manager for database sessions, handling session lifecycle.

Query Types

  • SelectQuery: A type for database select queries.

Usage

To use the loryth-persistence library, import the specific components you need:

from loryth.issues.persistence import BaseEntity, BaseRepository, transactional
from loryth.issues.persistence import DbConfig, SessionFactory, TransactionManager


# Example entity definition
class User(BaseEntity):
    def __init__(self, user_id, name, email):
        super().__init__(user_id)
        self.name = name
        self.email = email


# Example repository definition
class UserRepository(BaseRepository):
    def __init__(self, session_factory):
        super().__init__(session_factory, User)

    def find_by_email(self, email):
        session = self.session_factory.create_session()
        try:
            return session.query(User).filter(User.email == email).first()
        finally:
            session.close()


# Example usage with transaction management
@transactional
def create_user(user_repository, name, email):
    user = User(None, name, email)  # ID will be assigned by the database
    user_repository.save(user)
    return user


# Example configuration and setup
db_config = DbConfig(
    host="localhost",
    port=5432,
    database="myapp",
    username="user",
    password="password"
)
session_factory = DefaultSessionFactory(db_config)
session_manager = TransactionManager(session_factory)
user_repository = UserRepository(session_factory)

# Example usage
user = create_user(user_repository, "John Doe", "john@example.com")
print(f"Created user with ID: {user.id}")

Database Migration

The library includes utilities for database migration, allowing you to manage database schema changes over time.

Integration with Other Libraries

The loryth-persistence library is designed to work seamlessly with other libraries in the Loryth ecosystem:

  • It uses loryth-common for common utilities and patterns.
  • It integrates with loryth-domain for persisting domain entities and events.
  • It can be used by any service that needs to interact with a database.