Skip to main content

Python package for eventsourcing with SQLAlchemy.

Project description

Event Sourcing in Python with SQLAlchemy

This package supports using the Python eventsourcing library with SQLAlchemy.

To use SQLAlchemy with your Python eventsourcing applications:

  • install the Python package eventsourcing_sqlalchemy
  • set the environment variable PERSISTENCE_MODULE to 'eventsourcing_sqlalchemy'
  • set the environment variable SQLALCHEMY_URL to an SQLAlchemy database URL

See below for more information.

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

Getting started

Define aggregates and applications in the usual way.

from eventsourcing.application import Application
from eventsourcing.domain import Aggregate, event
from uuid import uuid5, NAMESPACE_URL


class TrainingSchool(Application):
    def register(self, name):
        dog = Dog(name)
        self.save(dog)

    def add_trick(self, name, trick):
        dog = self.repository.get(Dog.create_id(name))
        dog.add_trick(trick)
        self.save(dog)

    def get_tricks(self, name):
        dog = self.repository.get(Dog.create_id(name))
        return dog.tricks


class Dog(Aggregate):
    @event('Registered')
    def __init__(self, name):
        self.name = name
        self.tricks = []

    @staticmethod
    def create_id(name):
        return uuid5(NAMESPACE_URL, f'/dogs/{name}')

    @event('TrickAdded')
    def add_trick(self, trick):
        self.tricks.append(trick)

To use this module as the persistence module for your application, set the environment variable PERSISTENCE_MODULE to 'eventsourcing_sqlalchemy'.

When using this module, you need to set the environment variable SQLALCHEMY_URL to an SQLAlchemy database URL for your database. Please refer to the SQLAlchemy documentation for more information about SQLAlchemy Database URLs.

import os

os.environ["PERSISTENCE_MODULE"] = "eventsourcing_sqlalchemy"
os.environ["SQLALCHEMY_URL"] = "sqlite:///:memory:"

Construct and use the application in the usual way.

school = TrainingSchool()
school.register('Fido')
school.add_trick('Fido', 'roll over')
school.add_trick('Fido', 'play dead')
tricks = school.get_tricks('Fido')
assert tricks == ['roll over', 'play dead']

See the library's documentation and the SQLAlchemy project for more 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

eventsourcing-sqlalchemy-0.2.1.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file eventsourcing-sqlalchemy-0.2.1.tar.gz.

File metadata

  • Download URL: eventsourcing-sqlalchemy-0.2.1.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.10.2 Darwin/19.6.0

File hashes

Hashes for eventsourcing-sqlalchemy-0.2.1.tar.gz
Algorithm Hash digest
SHA256 6834141124e514f33156008df9517d1f77e9596104a86a973bf8c0ac1c1294cf
MD5 15b2879d0fcdf790c4690d2ec0a8b8ee
BLAKE2b-256 27cbd765357281f06fb09cf672a841f4eccbcaa31c59a126ba4813fdfaa98e31

See more details on using hashes here.

File details

Details for the file eventsourcing_sqlalchemy-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for eventsourcing_sqlalchemy-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e31a0612bc61138e03613e5f24160dafdfd30b34e6a0c92c1f7b961a743ad01d
MD5 96508b1cf5596ca8ff1c2bccded2cffc
BLAKE2b-256 13e4a3028dd7d9893f62ee3600316095b4c7d032adfc79c0213b093ba582e902

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page