Skip to main content

Event Sourcing in Python with SQLAlchemy

This package supports using the Python eventsourcing library with SQLAlchemy.

Installation

Use pip to install the stable distribution from the Python Package Index. Please note, it is recommended to install Python packages into a Python virtual environment.

$ pip install eventsourcing_sqlalchemy

Synopsis

To use SQLAlchemy with your Python eventsourcing application, use the topic eventsourcing_sqlalchemy.factory:Factory as the INFRASTRUCTURE_FACTORY environment variable, and set an SQLAlchemy database URL as the value of environment variable SQLALCHEMY_URL.

First define a domain model and application, in the usual way.

from eventsourcing.application import Application
from eventsourcing.domain import Aggregate, event


class World(Aggregate):
    def __init__(self):
        self.history = []

    @event("SomethingHappened")
    def make_it_so(self, what):
        self.history.append(what)


class Worlds(Application):
    is_snapshotting_enabled = True

    def create_world(self):
        world = World()
        self.save(world)
        return world.id

    def make_it_so(self, world_id, what):
        world = self.repository.get(world_id)
        world.make_it_so(what)
        self.save(world)

    def get_world_history(self, world_id):
        world = self.repository.get(world_id)
        return world.history

Set environment variables INFRASTRUCTURE_FACTORY and SQLALCHEMY_URL. See the SQLAlchemy documentation for more information about SQLAlchemy Database URLs.

import os

os.environ.update({
    "INFRASTRUCTURE_FACTORY": "eventsourcing_sqlalchemy.factory:Factory",
    "SQLALCHEMY_URL": "sqlite:///:memory:",
})

Construct and use the application.

# Construct the application.
app = Worlds()

# Call application command methods.
world_id = app.create_world()
app.make_it_so(world_id, "dinosaurs")
app.make_it_so(world_id, "trucks")
app.make_it_so(world_id, "internet")

# Call application query methods.
history = app.get_world_history(world_id)
assert history == ["dinosaurs", "trucks", "internet"]    

These settings can be used with others supported by the library, for example to enable application-level compression and encryption of stored events, set COMPRESSOR_TOPIC and CIPHER_KEY.

from eventsourcing.cipher import AESCipher


# Generate a cipher key (keep this safe).
cipher_key = AESCipher.create_key(num_bytes=32)

# Set environment variables.
os.environ.update({
    "COMPRESSOR_TOPIC": "zlib",
    "CIPHER_KEY": cipher_key,
})

# Construct the application.
app = Worlds()

We can see the application is using the SQLAlchemy infrastructure, and that compression and encryption are enabled, by checking the attributes of the application object.

from eventsourcing_sqlalchemy.datastore import SQLAlchemyDatastore
from eventsourcing_sqlalchemy.factory import Factory
from eventsourcing_sqlalchemy.recorders import SQLAlchemyAggregateRecorder
from eventsourcing_sqlalchemy.recorders import SQLAlchemyApplicationRecorder
from eventsourcing_sqlalchemy.models import StoredEventRecord
from eventsourcing_sqlalchemy.models import SnapshotRecord
import zlib

assert isinstance(app.factory, Factory)
assert isinstance(app.factory.datastore, SQLAlchemyDatastore)
assert isinstance(app.events.recorder, SQLAlchemyApplicationRecorder)
assert isinstance(app.snapshots.recorder, SQLAlchemyAggregateRecorder)
assert issubclass(app.events.recorder.events_record_cls, StoredEventRecord)
assert issubclass(app.snapshots.recorder.events_record_cls, SnapshotRecord)
assert isinstance(app.mapper.cipher, AESCipher)
assert app.mapper.compressor == zlib

For more information, please refer to the Python eventsourcing library and the SQLAlchemy project.

Release files for eventsourcing-sqlalchemy 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for eventsourcing-sqlalchemy 0.1.0
File Size Uploaded
eventsourcing-sqlalchemy-0.1.0.tar.gz 9.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for eventsourcing-sqlalchemy 0.1.0
File Interpreter ABI Platform
eventsourcing_sqlalchemy-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.1 kB

Release files / eventsourcing-sqlalchemy-0.1.0.tar.gz

Download URL eventsourcing-sqlalchemy-0.1.0.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
26b7a0d08851f4f64dbc9867377951bb4ede5d398d264bc69c6c30eb4e2868ac
BLAKE2b-256 checksum
How to use checksums
0a5b2fe7cb3250f7f9af499006bed9c88c843317ddaa0e2965da6c4c168e373b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.10.0 Darwin/19.6.0

Release files / eventsourcing_sqlalchemy-0.1.0-py3-none-any.whl

Download URL eventsourcing_sqlalchemy-0.1.0-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
18409a73fbffbc2784a2a3409bc8cc6e2f63c023c59a731809f0605d1b07fcf0
BLAKE2b-256 checksum
How to use checksums
ec5d6686ccc0d67853b93f27c733fcd50eaa8138b50d0537c94f9a8f0990d7c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.10.0 Darwin/19.6.0

Release history Release notifications | RSS feed

1.0.1

2 release files

1.0

2 release files

0.15

2 release files

0.14

2 release files

0.13

2 release files

0.12

2 release files

0.11

2 release files

0.10

2 release files

0.9

2 release files

0.8

2 release files

0.7

2 release files

0.6

2 release files

0.5

2 release files

0.4

2 release files

0.3

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page