Skip to main content

The CQRS and Event Sourcing framework for Python

Project description

# Kant Framework
[![Build Status](https://travis-ci.org/patrickporto/kant.svg?branch=master)](https://travis-ci.org/patrickporto/kant)
[![codecov.io](https://codecov.io/github/patrickporto/kant/coverage.svg?branch=master)](https://codecov.io/github/patrickporto/kant?branch=master)
[![PyPI Package latest release](https://img.shields.io/pypi/v/kant.svg)](https://pypi.python.org/pypi/kant)
[![Supported versions](https://img.shields.io/pypi/pyversions/kant.svg)](https://pypi.python.org/pypi/kant)
[![Supported implementations](https://img.shields.io/pypi/implementation/kant.svg)](https://pypi.python.org/pypi/kant)


A CQRS and Event Sourcing framework for Python 3.

## Installing

```bash
pip install kant
```

## Getting started

Create declarative events

```python
from kant.events import models

class BankAccountCreated(models.EventModel):
id = models.CUIDField(primary_key=True)
owner = models.CharField()

class DepositPerformed(models.EventModel):
amount = models.DecimalField()
```

Create aggregate to apply events

```python
from kant.aggregates import Aggregate

class BankAccount(Aggregate):
def apply_bank_account_created(self, event):
self.id = event.get('id')
self.owner = event.get('owner')
self.balance = 0

def apply_deposit_performed(self, event):
self.balance += event.get('amount')
```

Now, manage events by EventRepository

```python
# database connection
import aiopg
conn = await aiopg.connect(user='user', password='user', database='database')

# create events
bank_account_created = BankAccountCreated(
id=123,
owner='John Doe',
)
deposit_performed = DepositPerformed(
amount=20,
)

bank_account = BankAccount()
# apply event BankAccountCreated { id: 123, owner: 'John Doe' }
bank_account.dispatch(bank_account_created)
# apply event DepositPerformed { amount: 20 }
bank_account.dispatch(deposit_performed)

async with conn as cursor:
event_store_repository = EventStoreRepository(cursor)
event_store_repository.save(
aggregate_id=bank_account.id,
events=bank_account.get_events(),
)
# get aggregate by id
stored_events = await event_store_repository.get(123)
stored_bank_account = BankAccount()
# apply stored events in stored_bank_account
stored_bank_account.fetch_events(stored_events)
```

## Features

* Optimistic Concurrency


## Contributing

Please, read the contribute guide [CONTRIBUTING](CONTRIBUTING.md).

## License

**MIT**

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

kant-1.0.1.tar.gz (5.9 kB view details)

Uploaded Source

File details

Details for the file kant-1.0.1.tar.gz.

File metadata

  • Download URL: kant-1.0.1.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for kant-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2b54dacf98ca3ceeb28628a83f6660d4a7de0e60d9ea02e2ddadbdce471c5332
MD5 44562038678b6c09672a9fd66995e1b6
BLAKE2b-256 1bba8cd65536cf56768c1fae5d20ae76a92a92663318cdb369e94ad5877b25be

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