Skip to main content

lexigram-events

Event Sourcing and CQRS engine for Lexigram Framework — domain events, aggregates, and projections.


Overview

CQRS, Event Sourcing, and messaging for Lexigram — command bus, event bus, event store, sagas, and projections. Provides a full CQRS stack: a typed command bus, an in-process pub/sub event bus, an append-only event store (PostgreSQL, SQLite, MongoDB, in-memory), saga orchestration, projections, and outbox processing.

Use EventsModule.configure() to register the event system and dispatch commands or subscribe to events via decorators.

Full documentation: docs.lexigram.dev

Install

uv add lexigram-events
# Optional extras
uv add "lexigram-events[postgres,sqlite,mongo]"

Quick Start

from lexigram import Application
from lexigram.di.module import Module, module
from lexigram.events import EventsModule, EventsConfig


@module(imports=[EventsModule.configure()])
class AppModule(Module):
    pass


async def main():
    async with Application.boot(modules=[AppModule]) as app:
        # your event sourcing code
        ...


if __name__ == "__main__":
    import asyncio

    asyncio.run(main())

Configuration

Zero-config usage: Call EventsModule.configure() with no arguments to use defaults (in-memory event store and bus).

Option 1 — YAML file

# application.yaml
events:
  event_store_backend: postgres
  postgres:
    dsn: "${DATABASE_URL}"

Option 2 — Profiles + Environment Variables (recommended)

export LEX_EVENTS__EVENT_STORE_BACKEND=postgres
export LEX_EVENTS__POSTGRES__DSN="postgresql://user:pass@host/db"

Option 3 — Python

from lexigram.events import EventsConfig, EventsModule, PostgresEventStoreConfig
from lexigram.events.types import EventStoreBackend

config = EventsConfig(
    event_store_backend=EventStoreBackend.POSTGRES,
    postgres=PostgresEventStoreConfig(dsn="${DATABASE_URL}"),
)
EventsModule.configure(config)

Config reference

Field Default Env var Description
event_store_backend memory LEX_EVENTS__EVENT_STORE_BACKEND Store backend: postgres, sqlite, mongodb, memory
event_bus.max_concurrent_handlers 10 LEX_EVENTS__EVENT_BUS__MAX_CONCURRENT_HANDLERS Max concurrent handler tasks
event_bus.enable_dead_letter True LEX_EVENTS__EVENT_BUS__ENABLE_DEAD_LETTER Send failed events to dead-letter queue

Module Factory Methods

Method Description
EventsModule.configure(...) Configure with explicit EventsConfig
EventsModule.stub() In-memory event store for testing

WebSocket Event Streaming

EventWebSocketEndpoint streams every event published to a StreamDispatcher to connected WebSocket clients in real time.

Security: the endpoint does not authenticate connections by default. With no authorize callback, any client that can reach the endpoint receives a live, unauthenticated stream of all dispatched events — potentially business-sensitive or PII-bearing. Always pass an authorize callback in production:

from lexigram.events.streaming import EventWebSocketEndpoint, StreamDispatcher


def authorize(scope: dict) -> bool:
    headers = dict(scope.get("headers") or [])
    return headers.get(b"authorization") == b"Bearer secret"


dispatcher = StreamDispatcher()
ws_app = EventWebSocketEndpoint(dispatcher, authorize=authorize)

The callback receives the ASGI connection scope (headers, query string, client) and may be synchronous or asynchronous; returning a falsy value rejects the connection with a 4401 close before the handshake is accepted.

Key Features

  • CommandBus — Typed async command dispatch with middleware
  • EventBus — In-process pub/sub with dead-letter handling
  • EventStore — Append-only store (PostgreSQL, SQLite, MongoDB, in-memory)
  • Saga — Long-running process orchestration with compensating transactions
  • Projection — Read-model rebuilding from event streams
  • Outbox — Reliable event delivery via transactional outbox pattern
  • Schema migration — Versioned event schema evolution

Testing

async with Application.boot(modules=[EventsModule.stub()]) as app:
    # your test code
    ...

Key Source Files

File What it contains
src/lexigram/events/module.py EventsModule definition
src/lexigram/events/config.py EventsConfig and all config sub-models
src/lexigram/events/di/provider.py EventsProvider wiring
src/lexigram/events/buses/ CommandBus, EventBus, QueryBus implementations
src/lexigram/events/stores/ Event store implementations (memory, postgres, etc.)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lexigram_events-0.1.5001.tar.gz (268.2 kB view details)

Uploaded Source

Built Distribution

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

lexigram_events-0.1.5001-py3-none-any.whl (253.6 kB view details)

Uploaded Python 3

File details

Details for the file lexigram_events-0.1.5001.tar.gz.

File metadata

  • Download URL: lexigram_events-0.1.5001.tar.gz
  • Upload date:
  • Size: 268.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.14

File hashes

Hashes for lexigram_events-0.1.5001.tar.gz
Algorithm Hash digest
SHA256 88678c2d01a32bf470d3d96522cc1b1b064e7abb10da3fd0b008fa58f8bf1c1a
MD5 ba7993b28b2c8c0a7a88ce9851ef355b
BLAKE2b-256 6fec5c3bd54f016929c27845af852c79414dc56718099caa9d97676e6bbee45e

See more details on using hashes here.

File details

Details for the file lexigram_events-0.1.5001-py3-none-any.whl.

File metadata

File hashes

Hashes for lexigram_events-0.1.5001-py3-none-any.whl
Algorithm Hash digest
SHA256 6a3470a86c9f50de036631c162b4f18fa00613b9bae3177edc163cb2e99b632b
MD5 ab5a89132e5dba3202816925bebb8cef
BLAKE2b-256 4d0d78bd162ec3cedf17699404413fb3dc90c828d387f5c7c8271ed227263a4b

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.5010

1 file

0.1.5007

2 files

This release

0.1.5001 This release

2 files

0.1.3007

1 file

0.1.3006

1 file

0.1.3005

1 file

0.1.4

2 files

0.1.2

1 file

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page