Skip to main content

An extension package for the Python Eventsourcing library that provides a persistence module for MongoDB.

Project description

Event Sourcing in Python with MongoDB

This is an extension package for the Python eventsourcing library that provides a persistence module for MongoDB.

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-mongodb

Getting started

Define Aggregates and Applications the usual way. Please refer to the Eventsourcing Documentation for more detailed examples.

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

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

    @event('TrickAdded')
    def add_trick(self, trick: str):
        self.tricks.append(trick)
    
class DogSchool(Application):
    def register_dog(self, name: str) -> UUID:
        dog = Dog(name)
        self.save(dog)
        return dog.id

    def add_trick(self, dog_id: UUID, trick: str):
        dog = self.repository.get(dog_id)
        dog.add_trick(trick)
        self.save(dog)

    def get_dog(self, dog_id: UUID) -> dict:
        dog = self.repository.get(dog_id)
        return {'id': dog.id, 'name': dog.name, 'tricks': dog.tricks}

To configure your Application for using MongoDB for persistence, you need to set the environment variable PERSISTENCE_MODULE to 'eventsourcing_mongodb'. Furthermore, you need to set the environment variables MONGO_CONNECTION_STRING and MONGO_DB_NAME. Please refer to the MongoDB Documentation to learn more about connection strings. You can read more about the available variables here .

import os
os.environ['PERSISTENCE_MODULE'] = 'eventsourcing_mongodb'
os.environ['MONGO_CONNECTION_STRING'] = 'mongodb://localhost'
os.environ['MONGO_DB_NAME'] = 'EventSourcing'

Instead of setting the variables on environment level, you can also set them on application level.

class DogSchool(Application):
    env = {
        'PERSISTENCE_MODULE': 'eventsourcing_mongodb',
        'MONGO_CONNECTION_STRING': 'mongodb://localhost',
        'MONGO_DB_NAME': 'EventSourcing',
        ...
    }
    ...

Construct and use the application in the usual way.

dog_school = DogSchool()
dog_id = dog_school.register_dog('Fido')
dog_school.add_trick(dog_id, 'roll over')
dog = dog_school.get_dog(dog_id)
print(dog) # {'id': UUID('...'), 'name': 'Fido', 'tricks': ['roll over']}

And retrieve the data later:

dog_school = DogSchool()
dog_id = UUID('...')
dog = dog_school.get_dog(dog_id)
print(dog) # {'id': UUID('...'), 'name': 'Fido', 'tricks': ['roll over']}

Available Environment Variables

You can use the following variables to configure mongodb persistence:

Variable Type / Possible values Required Description
PERSISTENCE_MODULE 'eventsourcing_mongodb' true configures the application to use this module for persistence.
MONGO_CONNECTION_STRING string true MongoDB Connection String. Please refer to the MongoDB Documentation to learn more about connection strings.
MONGO_DB_NAME string true Name of the Database the data sould be stored in
MONGO_COL_PREFIX string false Prefix for the MongoDB Collections for events, snapshots and trackings. The default is an empty String.

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-mongodb-4.0.3.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

eventsourcing_mongodb-4.0.3-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file eventsourcing-mongodb-4.0.3.tar.gz.

File metadata

  • Download URL: eventsourcing-mongodb-4.0.3.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for eventsourcing-mongodb-4.0.3.tar.gz
Algorithm Hash digest
SHA256 4c9fada66c47ace5197477bf8d9596f7bb5d51e4eb50f9e949f0ba293ef05837
MD5 e2eb444aa1df00d66126c6ec5e8104de
BLAKE2b-256 0d642ecfa0582c292d77b6637678d6082d37ec02017535a245d5eb0a7b941891

See more details on using hashes here.

File details

Details for the file eventsourcing_mongodb-4.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for eventsourcing_mongodb-4.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9923416c5c083f9b6d7c52d80ceb7496bf6f8caa1477710a9d0e6daee8605b0a
MD5 c7a1c5672d65d1a6694b329a6d47a8a2
BLAKE2b-256 ea1143c0a82ef696fc059d6375f3790c502b5e4d8f719af36f393a1b2d5e93d2

See more details on using hashes here.

Supported by

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