pub struct Store { /* private fields */ }Expand description
A PostgreSQL-backed snapshot store with configurable policy.
This implementation stores snapshots in a dedicated PostgreSQL table
(es_snapshots), using the same database as the event store for
consistency.
§Schema
The store uses the following table schema (created by
migrate()):
CREATE TABLE IF NOT EXISTS es_snapshots (
aggregate_kind TEXT NOT NULL,
aggregate_id UUID NOT NULL,
position BIGINT NOT NULL,
data JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (aggregate_kind, aggregate_id)
)§Example
ⓘ
use sourcery_postgres::{Store as EventStore};
use sourcery_postgres::snapshot::Store as SnapshotStore;
use sourcery_core::Repository;
let pool = PgPool::connect("postgres://...").await?;
let event_store = EventStore::new(pool.clone());
let snapshot_store = SnapshotStore::every(pool, 100);
// Run migrations
event_store.migrate().await?;
snapshot_store.migrate().await?;
let repo = Repository::new(event_store).with_snapshots(snapshot_store);Implementations§
Source§impl Store
impl Store
Sourcepub const fn always(pool: PgPool) -> Self
pub const fn always(pool: PgPool) -> Self
Create a snapshot store that saves after every command.
Best for aggregates with expensive replay or many events.
See the policy guidelines in SnapshotPolicy for choosing an
appropriate cadence.
Sourcepub const fn every(pool: PgPool, n: u64) -> Self
pub const fn every(pool: PgPool, n: u64) -> Self
Create a snapshot store that saves every N events.
Recommended for most use cases. Start with n = 50-100 and tune
based on your aggregate’s replay cost.
Trait Implementations§
Source§impl SnapshotStore<Uuid> for Store
impl SnapshotStore<Uuid> for Store
Source§async fn load<T>(
&self,
kind: &str,
id: &Uuid,
) -> Result<Option<Snapshot<Self::Position, T>>, Self::Error>where
T: DeserializeOwned,
async fn load<T>(
&self,
kind: &str,
id: &Uuid,
) -> Result<Option<Snapshot<Self::Position, T>>, Self::Error>where
T: DeserializeOwned,
Load the most recent snapshot for an aggregate. Read more
Source§async fn offer_snapshot<CE, T, Create>(
&self,
kind: &str,
id: &Uuid,
events_since_last_snapshot: u64,
create_snapshot: Create,
) -> Result<SnapshotOffer, OfferSnapshotError<Self::Error, CE>>
async fn offer_snapshot<CE, T, Create>( &self, kind: &str, id: &Uuid, events_since_last_snapshot: u64, create_snapshot: Create, ) -> Result<SnapshotOffer, OfferSnapshotError<Self::Error, CE>>
Whether to store a snapshot, with lazy snapshot creation. Read more
Auto Trait Implementations§
impl Freeze for Store
impl !RefUnwindSafe for Store
impl Send for Store
impl Sync for Store
impl Unpin for Store
impl !UnwindSafe for Store
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more