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'sINSTALLED_APPSsetting - migrate your database for this Django app
- set the environment variable
PERSISTENCE_MODULEto'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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file eventsourcing-django-0.2.0.tar.gz.
File metadata
- Download URL: eventsourcing-django-0.2.0.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.10.1 Darwin/19.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da6a6204932cec3a02c0b6f2d9176b108e7a7ef99aee509715c28c52188ded36
|
|
| MD5 |
85efc4938d2cd1558ddf48d1402ffb9b
|
|
| BLAKE2b-256 |
5f9a30e78c793986b9fc541880e215bee7fa39c3e73a30794a56e7fee1a97edb
|
File details
Details for the file eventsourcing_django-0.2.0-py3-none-any.whl.
File metadata
- Download URL: eventsourcing_django-0.2.0-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.10.1 Darwin/19.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cfb1047fd2aacc62f5f7176179a841acf2bda6ed5de3eeb1c89bd27c79221471
|
|
| MD5 |
2f760d2b9c2d628abbd06d2f810f26a8
|
|
| BLAKE2b-256 |
ddb6af2f624fa1ae5a804657c3bb5824127cbea1474ee469607de6ad37c62724
|