Skip to main content

Python package for eventsourcing with Django.

Project description

Event Sourcing with Django

This package supports using the Python eventsourcing library with Django ORM.

To use Django with your Python eventsourcing applications:

  • install the Python package eventsourcing_django
  • add 'eventsourcing_django' to your Django project's INSTALLED_APPS setting
  • migrate your database for this Django app
  • set the environment variable PERSISTENCE_MODULE to 'eventsourcing_django'

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_django

Django

If you are using Django 3.2 or later, add 'eventsourcing_django' to your Django project's INSTALLED_APPS setting.

INSTALLED_APPS = [
    ...
    'eventsourcing_django',
]

If you are using Django 2.2, 3.0 or 3.1, please add 'eventsourcing_django.apps.EventsourcingConfig' to your Django project's INSTALLED_APPS setting.

INSTALLED_APPS = [
    ...
    'eventsourcing_django.apps.EventsourcingConfig',
]

To migrate your database, please run Django's manage.py migrate command.

$ python manage.py migrate eventsourcing_django

Event sourcing

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)

Construct and use the application in the usual way. Set PERSISTENCE_MODULE to 'eventsourcing_django' in the application's environment. You may wish to construct the application object on a signal when the Django project is "ready". You can use the ready() method of the AppConfig class in the apps.py module of a Django app.

school = TrainingSchool(env={
    "PERSISTENCE_MODULE": "eventsourcing_django",
})

The application's methods may be called from Django views and forms.

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']

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

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-django-0.2.1.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

eventsourcing_django-0.2.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: eventsourcing-django-0.2.1.tar.gz
  • Upload date:
  • Size: 9.0 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-django-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1781926cea91c5803c348487721ed0a1f205aa4cac461bbe2fba8e833106235e
MD5 05c6b3be4f4d9c7b98b8de78a8cf5aa5
BLAKE2b-256 f4629f37fa791c10a4d99aca9b9600ab454ce9b31afc35bd0459b64ad858a6a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for eventsourcing_django-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 675f4239f097b431365c9a1a5705ceafd76d94b3ee8ddcfc21a3c4efa6781e1d
MD5 80a6abc0cb6370f1a21148ca749d9c70
BLAKE2b-256 716b12a8cdff3a1cdebf32edc9d1870be544b4958ce01af240954dc729cbae7f

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