Skip to main content

A library for event-based systems in Python.

Project description

Python Event Sourcery

A library for event-based systems in Python. For event sourcing, CQRS, and event-driven architectures.

event = EventSourceryIsBorn()

event_store.append(event, stream_id=stream_id)

Under heavy development


Documentation: python-event-sourcery.github.io/python-event-sourcery/


Installation

pip install python-event-sourcery

Easy to setup

Just configure your EventStore instance (or a factory) and call configure_models once. Examples below.

Versatile

Event Sourcery makes no assumptions about your configuration or session management. It's designed to be plugged in into what you already have, without a need to adjust anything. It can be integrated smoothly with thread-scoped SQLAlchemy sessions as well as dependency injection.

NOT opinionated

Although one can easily start with a library, the latter is very customizable and DOES NOT enforce a particular style of using. Also, libraries integrated, i.e. SQLAlchemy and Pydantic are not hardcoded dependencies. They can be replaced with a finite amount of effort.


Use cases & features

Until full documentation is available, you can follow tests

  • Event Sourcing tests
  • Snapshots (for Event Sourcing) tests
  • Event Store - storage for events tests
  • Outbox pattern tests
  • Concurrency control with optimistic locking tests

Quick start

The script is complete, it should run as-is provided FastAPI and library with dependencies are installed. See examples/0-fastapi-integration

from typing import Iterator, Literal
from uuid import uuid4

from fastapi import FastAPI, Depends
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, as_declarative

# Import a couple of things from event_sourcery
from event_sourcery import EventStore  # for type annotations
from event_sourcery import (
    configure_models,  # set-up function for library's models
    get_event_store,  # factory function to start quickly
    Event,  # base class for events
)

# Set up your DB and application as you would normally do
engine = create_engine(
    "postgresql://event_sourcery:event_sourcery@localhost:5432/event_sourcery"
)


def db_session() -> Iterator[Session]:
    session = Session(bind=engine)
    try:
        yield session
    finally:
        session.close()


@as_declarative()
class Base:
    pass


app = FastAPI(on_startup=[lambda: Base.metadata.create_all(bind=engine)])

# initialize Event Sourcery models, so they can be handled by SQLAlchemy and e.g. alembic
configure_models(Base)


# Set up factory for event store to be injected into views
def event_store(session: Session = Depends(db_session)) -> EventStore:
    return get_event_store(session)


# Define your event(s) using base-class provided
class CustomerSubscribed(Event):
    plan_id: int
    model: Literal["monthly", "yearly"]


@app.post("/subscription")
def subscribe(
    event_store: EventStore = Depends(event_store),
    session: Session = Depends(db_session),
):
    # Create an event
    event = CustomerSubscribed(plan_id=1, model="yearly")
    # Put it in the event store!
    event_store.append(event, stream_id=uuid4())
    session.commit()

Feature requests / feedback

Anything missing? Create an issue in this repository. Let others upvote

Inspirations

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

python_event_sourcery-0.5.3a0.tar.gz (40.5 kB view details)

Uploaded Source

Built Distribution

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

python_event_sourcery-0.5.3a0-py3-none-any.whl (62.1 kB view details)

Uploaded Python 3

File details

Details for the file python_event_sourcery-0.5.3a0.tar.gz.

File metadata

  • Download URL: python_event_sourcery-0.5.3a0.tar.gz
  • Upload date:
  • Size: 40.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.10.20 Linux/6.14.0-1017-azure

File hashes

Hashes for python_event_sourcery-0.5.3a0.tar.gz
Algorithm Hash digest
SHA256 efd4d9ed1a94131af7ddb54f9c8afaffbd74814d78d090e5ae2f6942ee868901
MD5 0e2780ef7a676c03491d770b756101fc
BLAKE2b-256 4115bce3a8cc62f8056799931a3a341e78582cc78ccd38134a91a7662f8d827c

See more details on using hashes here.

File details

Details for the file python_event_sourcery-0.5.3a0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_event_sourcery-0.5.3a0-py3-none-any.whl
Algorithm Hash digest
SHA256 4740c185cf00a0c2d2f224ce2969591710c09848ee1ee2e718f0b0b3c2cf721e
MD5 24fc58b7f393567327a83e5ac6d0761c
BLAKE2b-256 8b7be797a7eda1a426e98f49151bbda20d08ffaf3b95d24d387c2e970759f3ea

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