Skip to main content

PgEventStore

Struct PgEventStore 

Source
pub struct PgEventStore<E, S>
where S: Serde<E> + Send + Sync,
{ /* private fields */ }
Expand description

PostgreSQL event store implementation.

Implementations§

Source§

impl<E, S> PgEventStore<E, S>
where S: Serde<E> + Send + Sync + Clone, E: Event + Clone,

Source

pub async fn try_new(pool: PgPool, serde: S) -> Result<Self, Error>

Initializes the PostgreSQL DB and returns a new instance of PgEventStore.

§Arguments
  • pool - The PostgreSQL connection pool.
  • serde - The serialization implementation for the event payload.
Source

pub fn new_uninitialized(pool: PgPool, serde: S) -> Self

Creates a new instance of PgEventStore.

This constructor does not initialize the database or add the domain_id columns necessary for disintegrate to function properly. If you need to initialize the database, use PgEventStore::new instead.

If you plan to use this constructor, ensure that the disintegrate is properly initialized. Refer to the SQL files in the “event_store/sql” directory to recreate the default structure. Additionally, all domain_id columns and their corresponding indexes must be created manually.

§Arguments
  • pool - The PostgreSQL connection pool.
  • serde - The serialization implementation for the event payload.

Trait Implementations§

Source§

impl<E: Clone, S> Clone for PgEventStore<E, S>
where S: Serde<E> + Send + Sync + Clone,

Source§

fn clone(&self) -> PgEventStore<E, S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E, S> EventStore<i64, E> for PgEventStore<E, S>
where E: Event + Send + Sync, S: Serde<E> + Send + Sync,

Implementation of the event store using PostgreSQL.

This module provides the implementation of the EventStore trait for PgEventStore, allowing interaction with a PostgreSQL event store. It enables streaming events based on a query and appending new events to the event store.

Source§

fn stream<'a, QE>( &'a self, query: &'a StreamQuery<PgEventId, QE>, ) -> BoxStream<'a, Result<StreamItem<PgEventId, QE>, Self::Error>>
where QE: TryFrom<E> + Event + 'static + Clone + Send + Sync, <QE as TryFrom<E>>::Error: StdError + 'static + Send + Sync,

Streams events based on the provided query.

This function fetches events from the PostgreSQL event store based on the provided query. It constructs a SQL query using the SqlEventsCriteriaBuilder and executes the query using the sqlx crate. The fetched events are then converted into PersistedEvent instances and streamed as a boxed stream.

§Arguments
  • query - The stream query specifying the criteria for filtering events.
§Returns

A Result containing a boxed stream of PersistedEvent that matches the query criteria, or an error of type Self::Error.

Source§

fn append<'life0, 'async_trait, QE>( &'life0 self, events: Vec<E>, query: StreamQuery<PgEventId, QE>, version: PgEventId, ) -> Pin<Box<dyn Future<Output = Result<Vec<PersistedEvent<PgEventId, E>>, Self::Error>> + Send + 'async_trait>>
where E: Clone + 'async_trait, QE: Event + Clone + Send + Sync + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Appends new events to the event store.

This function inserts the provided events into the PostgreSQL-backed event store. Before inserting, it queries the event table to ensure that no events have been appended since the given version. If newer events are found, a concurrency error is returned to prevent invalid state transitions.

If the concurrency check succeeds, the events are inserted into the event table.

§Arguments
  • events - The events to append to the event store.
  • query - The stream query that identifies the target event stream.
  • version - The ID of the last consumed event, used for optimistic concurrency control.
§Returns

A Result containing a vector of PersistedEvent representing the appended events, or an error of type Self::Error.

Source§

fn append_without_validation<'life0, 'async_trait>( &'life0 self, events: Vec<E>, ) -> Pin<Box<dyn Future<Output = Result<Vec<PersistedEvent<PgEventId, E>>, Self::Error>> + Send + 'async_trait>>
where E: Clone + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Appends a batch of events to the PostgreSQL-backed event store without verifying whether new events have been added since the last read.

§Arguments
  • events - A vector of events to be appended.
§Returns

A Result containing a vector of PersistedEvent representing the appended events, or an error of type Self::Error.

Source§

type Error = Error

Auto Trait Implementations§

§

impl<E, S> Freeze for PgEventStore<E, S>
where S: Freeze,

§

impl<E, S> !RefUnwindSafe for PgEventStore<E, S>

§

impl<E, S> Send for PgEventStore<E, S>
where E: Send,

§

impl<E, S> Sync for PgEventStore<E, S>
where E: Sync,

§

impl<E, S> Unpin for PgEventStore<E, S>
where S: Unpin, E: Unpin,

§

impl<E, S> UnsafeUnpin for PgEventStore<E, S>
where S: UnsafeUnpin,

§

impl<E, S> !UnwindSafe for PgEventStore<E, S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more