evento-sql
SQL database implementations for the Evento event sourcing library.
Overview
This crate provides SQL-based persistence for events and subscriber state, implementing the Executor trait from evento-core. It supports SQLite, MySQL, and PostgreSQL through feature flags.
Installation
Add to your Cargo.toml:
[]
= "2"
By default, all database backends are enabled. To use only specific databases:
[]
= { = "2", = false, = ["postgres"] }
Features
sqlite- SQLite database supportmysql- MySQL database supportpostgres- PostgreSQL database support
Usage
Basic Setup
use Sql;
use SqlitePoolOptions;
use ;
async
Type Aliases
Convenience type aliases are provided for each database:
use ;
// These are equivalent:
let executor: Sqlite = pool.into;
let executor: = pool.into;
Read-Write Pairs (CQRS)
For CQRS patterns with separate read and write connections:
use ;
Core Types
Sql<DB>
The main executor type that wraps a SQLx connection pool. Implements the Executor trait with:
read- Query events with filtering and cursor-based paginationwrite- Persist events with optimistic concurrency controlget_subscriber_cursor- Get current cursor position for a subscriberis_subscriber_running- Check if a subscriber is activeupsert_subscriber- Create or update a subscriber recordacknowledge- Update subscriber cursor after processing
Reader
Query builder for cursor-based pagination:
use ;
use Query;
let statement = select
.columns
.from
.to_owned;
let result = new
.forward // First 10 events
.
.await?;
// Navigate pages
if result.page_info.has_next_page
Column Identifiers
Sea-query column identifiers for type-safe query building:
Event- Event table columnsSubscriber- Subscriber table columnsSnapshot- Snapshot table columns (deprecated, dropped in M0003)
Serialization
Event data is serialized using bitcode for compact binary representation. The sql_types::Bitcode<T> wrapper provides SQLx integration:
use Bitcode;
// Wrap data for storage
let data = Bitcode;
// Encode to bytes
let bytes = data.encode_to;
// Decode from bytes
let decoded = decode_from_bytes?;
Database Schema
This crate expects the database schema created by evento-sql-migrator. See that crate for the full schema definition.
Event Table
| Column | Type | Description |
|---|---|---|
id |
VARCHAR(26) | Event ID (ULID) |
name |
VARCHAR(50) | Event type name |
aggregator_type |
VARCHAR(50) | Aggregate root type |
aggregator_id |
VARCHAR(26) | Aggregate root instance ID |
version |
INTEGER | Event sequence number |
data |
BLOB | Serialized event data (bitcode) |
metadata |
BLOB | Serialized metadata (bitcode) |
routing_key |
VARCHAR(50) | Optional routing key |
timestamp |
BIGINT | Timestamp (seconds) |
timestamp_subsec |
BIGINT | Sub-second precision |
Subscriber Table
| Column | Type | Description |
|---|---|---|
key |
VARCHAR(50) | Subscriber identifier (primary key) |
worker_id |
VARCHAR(26) | Current worker ID |
cursor |
TEXT | Current stream position |
lag |
INTEGER | Events behind |
enabled |
BOOLEAN | Active status |
License
See the LICENSE file in the repository root.