Skip to main content

Python package for eventsourcing with Django.

Project description

Event Sourcing with Django

This package is a Django app that uses the Django ORM as persistence infrastructure for the Python eventsourcing library.

Installation

Install using pip. It is recommended to install Python packages into a Python virtual environment.

$ pip install eventsourcing_django

Add 'eventsourcing_django' to your Django project's INSTALLED_APPS setting.

INSTALLED_APPS = [
    ...
    'eventsourcing_django',
]

Run Django's manage.py migrate command.

$ python manage.py migrate eventsourcing_django

Aggregates and application

You can develop event-sourced aggregates and applications independently of persistence infrastructure. Please refer to the core library docs for more information.

The example below defines an event-sourced aggregate World. It will be created with a history attribute. The command method make_it_so() triggers an event SomethingHappened that appends the command argument what to the history.

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)

The application class Universe has three methods. The method create_world() creates a new World aggregate. The method make_it_so() calls make_it_so() on an existing World aggregate. The method get_world_history() returns the current history value of an existing World aggregate.

Automatic snapshotting is enabled by using the snapshotting_intervals attribute of the application class.

from eventsourcing.application import Application

class Universe(Application):
    snapshotting_intervals = {
        World: 5,  # automatic snapshotting
    }

    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

Initialize application object

To use the Django ORM as the application's persistence infrastructure, you must set the application's environment variable INFRASTRUCTURE_FACTORY to eventsourcing_django.factory:Factory. Environment variables can be set in the environment, or set on the application class, or passed in when constructing the application object as seen below.

# Construct the application.
app = Universe(env={
    "INFRASTRUCTURE_FACTORY": "eventsourcing_django.factory:Factory",
    "COMPRESSOR_TOPIC": "zlib",
})

The application object brings together the domain model and the persistence infrastructure, and provides an interface for views and forms.

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.

The application can use other environment variables supported by the library, for example to enable application-level compression of stored events, set COMPRESSOR_TOPIC. You may wish to arrange for settings to be defined in and used from your Django project's settings.py.

Views and forms

After migrating the database and constructing the application object, the application object's methods can be called. The application object's methods may be called from Django view and form classes.

# 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")
app.make_it_so(world_id, "covid")

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

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

from eventsourcing_django.factory import Factory
from eventsourcing_django.recorders import DjangoAggregateRecorder
from eventsourcing_django.recorders import DjangoApplicationRecorder
from eventsourcing_django.models import StoredEventRecord
from eventsourcing_django.models import SnapshotRecord
import zlib

assert isinstance(app.factory, Factory)
assert isinstance(app.events.recorder, DjangoApplicationRecorder)
assert isinstance(app.snapshots.recorder, DjangoAggregateRecorder)
assert issubclass(app.events.recorder.model, StoredEventRecord)
assert issubclass(app.snapshots.recorder.model, SnapshotRecord)
assert app.mapper.compressor == zlib

We can see automatic snapshotting is working, by looking in the snapshots store.

snapshots = list(app.snapshots.get(world_id))
assert len(snapshots) == 1

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.1.2.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

eventsourcing_django-0.1.2-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: eventsourcing-django-0.1.2.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.10.0 Darwin/19.6.0

File hashes

Hashes for eventsourcing-django-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e5be41bb5bafb3141d114afac4a42e9a08b5ac83f86091dda4e1ad49a661684f
MD5 5c6d14ca17f53a0a6eb948b8ff261b5a
BLAKE2b-256 c07d30b251ee3791a802b86da625786dd05d13b63875c3c9972d70dfc73f184a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for eventsourcing_django-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 da70dbc58d77ddf17b149f999acaee0e13a48d6f6e2e89dadff99441a57ef4bf
MD5 e0eccdf8a25a6b4e865cb62a9a66dfeb
BLAKE2b-256 ceb84a7a925b3267c1518e164ed4df03c1a186b74370bc1a444aa36c781a92f4

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