Struct PgIdIndexer

Source
pub struct PgIdIndexer<E: Event + Clone> { /* private fields */ }
Expand description

The PgIdIndexer is a helper to index existing fields that have been newly tagged with the #[id] attribute in events.

§Overview

The PgIdIndexer is an EventListener responsible for indexing fields in the event store that are tagged with the #[id] attribute. This allows querying of old events based on this new domain identifier once the indexing is complete. The PgIdIndexer listens to events and updates the event table in the database with the appropriate values.

§Workflow

After you have tagged an existing field in your event structure with the #[id] attribute to mark it as a domain identifier:

use disintegrate_macros::Event;
use serde::{Serialize, Deserialize};
#[derive(Event, Clone, Serialize, Deserialize)]
struct MyEvent {
    #[id]
    existing_id: String,
    other_field: String,
}
  1. Register the PgIdIndexer as an EventListener: Integrate the indexer with the event listener system to process the newly tagged domain identifier:

    use disintegrate_postgres::PgIdIndexer;
    use disintegrate_postgres::PgEventListenerConfig;
    use disintegrate_postgres::PgEventListener;
    use disintegrate_postgres::PgEventStore;
    use std::time::Duration;
    use disintegrate_macros::Event;
    use disintegrate::serde::json::Json;
    use serde::{Serialize, Deserialize};
    use sqlx::PgPool;
    
    #[derive(Event, Clone, Serialize, Deserialize)]
    struct MyEvent {
        #[id]
        existing_id: String,
        other_field: String,
    }
    
    async fn setup_listener(pool: PgPool, event_store: PgEventStore<MyEvent, Json<MyEvent>>) {
        let id_indexer = PgIdIndexer::<MyEvent>::new("index_exsting_id", pool);
        PgEventListener::builder(event_store)
            .register_listener(
                id_indexer,
                PgEventListenerConfig::poller(Duration::from_secs(5)).with_notifier()
            )
            .start_with_shutdown(shutdown())
            .await
            .expect("start event listener failed");
    }
    
    async fn shutdown() {
        tokio::signal::ctrl_c().await.expect("ctrl_c signal failed");
    }
  2. Deploy the application: Start the application with the updated event structure and the PgIdIndexer integration. Newly created events with the new domain identifier will automatically have the identifier indexed.

Once the indexing process is complete, you can query the event store using the new domain identifier to fetch events.

If indexing is done, you can remove the PgIdIndexer from the list of registered event listeners.

Implementations§

Source§

impl<E: Event + Clone> PgIdIndexer<E>

Source

pub fn new(id: &'static str, pool: PgPool) -> Self

Creates a new PgIdIndexer instance for indexing events.

§Arguments
  • id - A unique identifier for the listener, used to store the last processed event_id in the database.
  • pool - A PgPool instance for Postgres.

Trait Implementations§

Source§

impl<E: Event + Clone + Send + Sync> EventListener<i64, E> for PgIdIndexer<E>

Source§

type Error = Error

The type of error that may occur during the handle of an event.
Source§

fn id(&self) -> &'static str

Returns the unique identifier of the event listener. Read more
Source§

fn query(&self) -> &StreamQuery<PgEventId, E>

Returns the stream query used by the event listener. Read more
Source§

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

Handles an event. Read more

Auto Trait Implementations§

§

impl<E> Freeze for PgIdIndexer<E>

§

impl<E> !RefUnwindSafe for PgIdIndexer<E>

§

impl<E> Send for PgIdIndexer<E>
where E: Send,

§

impl<E> Sync for PgIdIndexer<E>
where E: Sync,

§

impl<E> Unpin for PgIdIndexer<E>
where E: Unpin,

§

impl<E> !UnwindSafe for PgIdIndexer<E>

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

impl<T> ErasedDestructor for T
where T: 'static,