SurrealDB Driver
The SurrealDB driver persists events to a SurrealDB database using the official surrealdb JavaScript SDK (v2). It supports SurrealDB server 3.x.
import { createEventStore, createAggregateRoot } from '@requence/event-sourcing/surreal'import { Surreal } from 'surrealdb'
const db = new Surreal()await db.connect(process.env.SURREALDB_URL!)await db.use({ namespace: 'app', database: 'app' })
const eventStore = createEventStore({ database: db, aggregateRoots: [userAggregateRoot],})Database Schema
Section titled “Database Schema”The driver uses SCHEMAFULL tables mirroring the PostgreSQL layout:
| Table | Purpose |
|---|---|
events | Append-only event log with position, stream, and metadata |
event_sourcing_counters | Counter record providing the gapless global event position |
checkpoints | Last-processed position (and optimistic-concurrency version) per projection/process manager |
aggregate_root_snapshots | Cached aggregate state for fast rehydration |
projection_snapshots | Cached projection state for snapshot-based projections |
projection_applied_events | Counter for snapshot frequency tracking |
encryption_keys | Wrapped data keys for encrypted fields (see Encryption keys) |
encryption_master_keys | When each master key was first seen, for rotation |
By default the driver creates the schema itself: idempotent DEFINE ... IF NOT EXISTS statements run lazily before the first operation. Pass initSchema: false to opt out and manage the schema yourself — the DDL is exported for that purpose:
import { initSchema, SCHEMA_STATEMENTS } from '@requence/event-sourcing/surreal'
await initSchema(db) // or run SCHEMA_STATEMENTS through your own toolingEncryption keys
Section titled “Encryption keys”surrealKeyStore stores the data keys for encrypted fields in the encryption_keys table, each wrapped with a master key. The record ID is the key ID. Destroying a key deletes its record.
import { createEventStore, surrealKeyStore } from '@requence/event-sourcing/surreal'
const eventStore = createEventStore({ database: db, aggregateRoots: [userAggregateRoot], encryption: { masterKeys: process.env.MASTER_KEYS, // comma-separated base64 keys, oldest first keyStore: surrealKeyStore(db), },})Like the event store, it creates its tables before the first operation. Pass surrealKeyStore(db, { initSchema: false }) if you manage the schema yourself.
Master key rotation and destroying keys on multiple instances work as described for the Drizzle driver. SurrealDB has no SKIP LOCKED, so instances rewrapping at the same time may rewrap the same keys; each key is only written while it is still wrapped with the master key it was read with, so one write wins.
Configuration
Section titled “Configuration”const eventStore = createEventStore({ // Required — connected Surreal instance (namespace/database selected) or factory database: db,
// Required — one or more aggregate roots aggregateRoots: [user, project],
// Optional — disable automatic schema creation (default: true) initSchema: false,
// Optional — distributed lock for multi-instance deployments lock: redisLock(redis),
// Optional — disable automatic initialization autoInit: false,
// Optional — callback invoked after events are appended onEventsAppended(events) { // e.g. broadcast via WebSocket },
// Optional — replay and refresh progress callbacks onProjectionReplay: { /* ... */ }, onProcessManagerRefresh: { /* ... */ },
// Optional — post-process events before persistence postProcessEvent(event, aggregateRoot) { return event },})