Skip to main content

OutboxSpec

Struct OutboxSpec 

Source
pub struct OutboxSpec<Settings = ()> { /* private fields */ }
Expand description

The description of an outbox table, with its settings as types.

new names the table and its three required columns: the id, the name a record was published under, and the payload. The column-only setters (within, data, selecting_all) keep the type. Each typed setter (headers, processed_at, own) adds its marker of outbox::spec to the type, and a setting set twice does not compile. The outbox reads the database’s clock. Every setter is a const fn.

§Examples

use ruststream_sqlx::dialect::Column;
use ruststream_sqlx::outbox::OutboxSpec;
use ruststream_sqlx::outbox::spec::{Headers, ProcessedAt, own};

/// An outbox in the `shop` schema whose records keep their headers, are marked when
/// processed, and are recovered by the service's own statement.
pub const SHOP_OUTBOX: OutboxSpec<(Headers, ProcessedAt, own::Recover)> = OutboxSpec::new(
    "outbox",
    Column::new("id"),
    Column::new("topic"),
    Column::new("body"),
)
.within("shop")
.headers(Column::new("headers"))
.processed_at(Column::new("processed_at"))
.own::<own::Recover>();

Implementations§

Source§

impl OutboxSpec

Source

pub const fn new( table: &'static str, id: Column<'static>, name: Column<'static>, payload: Column<'static>, ) -> Self

The outbox table table, whose records are identified by id, published under the name in name, with the bytes in payload.

§Examples
use ruststream_sqlx::dialect::Column;
use ruststream_sqlx::outbox::OutboxSpec;

/// The plainest outbox: a processed record is deleted.
pub const OUTBOX: OutboxSpec = OutboxSpec::new(
    "outbox",
    Column::new("id"),
    Column::new("name"),
    Column::new("payload"),
);
Source§

impl<Settings> OutboxSpec<Settings>

Source

pub const fn spec(&self) -> TableSpec<'static>

The table’s description, as the dialects read it.

§Examples
use ruststream_sqlx::dialect::{Column, OutboxDialect, Postgres, StatementError};
use ruststream_sqlx::outbox::OutboxSpec;

const OUTBOX: OutboxSpec = OutboxSpec::new(
    "outbox",
    Column::new("id"),
    Column::new("name"),
    Column::new("payload"),
);

/// The statement the outbox's default recovery runs on Postgres.
pub fn recovery() -> Result<String, StatementError> {
    Ok(Postgres.outbox_recover(&OUTBOX.spec())?.sql().to_owned())
}
Source

pub const fn within(self, schema: &'static str) -> Self

The table in schema.

§Examples
use ruststream_sqlx::dialect::Column;
use ruststream_sqlx::outbox::OutboxSpec;

pub const OUTBOX: OutboxSpec = OutboxSpec::new(
    "outbox",
    Column::new("id"),
    Column::new("name"),
    Column::new("payload"),
)
.within("shop");
Source

pub const fn data(self, columns: &'static [Column<'static>]) -> Self

The columns the record reads besides its roles; the default fetch and recovery select them.

§Examples
use ruststream_sqlx::dialect::Column;
use ruststream_sqlx::outbox::OutboxSpec;

/// A record that also reads when it was created.
pub const OUTBOX: OutboxSpec = OutboxSpec::new(
    "outbox",
    Column::new("id"),
    Column::new("name"),
    Column::new("payload"),
)
.data(&[Column::new("created_at")]);
Source

pub const fn selecting_all(self) -> Self

The default fetch and recovery select every column (SELECT *): for a record whose columns the description cannot list, such as one that flattens another struct.

§Examples
use ruststream_sqlx::dialect::Column;
use ruststream_sqlx::outbox::OutboxSpec;

pub const OUTBOX: OutboxSpec = OutboxSpec::new(
    "outbox",
    Column::new("id"),
    Column::new("name"),
    Column::new("payload"),
)
.selecting_all();
Source

pub const fn headers( self, column: Column<'static>, ) -> OutboxSpec<<Settings as Push<Headers>>::Out>
where Settings: Push<Headers>, <Settings as Push<Headers>>::Out: Declaration,

The column the published headers are kept in. The record implements HeaderRow, and a republished record carries them again.

§Examples
use ruststream_sqlx::dialect::Column;
use ruststream_sqlx::outbox::OutboxSpec;
use ruststream_sqlx::outbox::spec::Headers;

pub const OUTBOX: OutboxSpec<(Headers,)> = OutboxSpec::new(
    "outbox",
    Column::new("id"),
    Column::new("name"),
    Column::new("payload"),
)
.headers(Column::new("headers"));
Source

pub const fn processed_at( self, column: Column<'static>, ) -> OutboxSpec<<Settings as Push<ProcessedAt>>::Out>
where Settings: Push<ProcessedAt>, <Settings as Push<ProcessedAt>>::Out: Declaration,

The column the default acknowledgement writes the database’s current time to; the default fetch and recovery read only the records where it is NULL. Without it a processed record is deleted.

§Examples
use ruststream_sqlx::dialect::Column;
use ruststream_sqlx::outbox::OutboxSpec;
use ruststream_sqlx::outbox::spec::ProcessedAt;

pub const OUTBOX: OutboxSpec<(ProcessedAt,)> = OutboxSpec::new(
    "outbox",
    Column::new("id"),
    Column::new("name"),
    Column::new("payload"),
)
.processed_at(Column::new("processed_at"));
Source

pub const fn own<Event: OwnEvent>( self, ) -> OutboxSpec<<Settings as Push<Event>>::Out>
where Settings: Push<Event>, <Settings as Push<Event>>::Out: Declaration,

The record runs Event itself, through its impl of the event’s trait of outbox, instead of the crate’s default; a record that names the event without the impl is not registered.

§Examples
use ruststream_sqlx::dialect::Column;
use ruststream_sqlx::outbox::spec::{ProcessedAt, own};
use ruststream_sqlx::outbox::{self, OutboxSpec, OutboxTable};
use sqlx::{PgConnection, Postgres};

#[derive(sqlx::FromRow)]
pub struct OrderEvent {
    id: i64,
    name: String,
    payload: Vec<u8>,
}

impl OutboxTable for OrderEvent {
    type Id = i64;
    type Table = OutboxSpec<(ProcessedAt, own::Retry)>;
    const TABLE: Self::Table = OutboxSpec::new(
        "outbox",
        Column::new("id"),
        Column::new("name"),
        Column::new("payload"),
    )
    .processed_at(Column::new("processed_at"))
    .own::<own::Retry>();

    fn id(&self) -> &i64 {
        &self.id
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn payload(&self) -> &[u8] {
        &self.payload
    }
}

// The service counts how often a consumer asked for a record again.
impl outbox::Retry<Postgres> for OrderEvent {
    async fn retry(conn: &mut PgConnection, id: &i64) -> sqlx::Result<()> {
        sqlx::query("UPDATE outbox SET retries = retries + 1 WHERE id = $1")
            .bind(id)
            .execute(conn)
            .await?;
        Ok(())
    }
}

Trait Implementations§

Source§

impl<Settings> Clone for OutboxSpec<Settings>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<Settings> Copy for OutboxSpec<Settings>

Source§

impl<Settings> Debug for OutboxSpec<Settings>

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Settings: Declaration + 'static> Described for OutboxSpec<Settings>

Source§

type Declared = Settings

The settings, folded.
Source§

fn spec(&self) -> TableSpec<'static>

The table’s description, as the dialects read it.
Source§

impl<Settings> Eq for OutboxSpec<Settings>

Source§

impl<Settings> PartialEq for OutboxSpec<Settings>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<Settings> Freeze for OutboxSpec<Settings>
where PhantomData<fn() -> Settings>: Freeze,

§

impl<Settings> RefUnwindSafe for OutboxSpec<Settings>
where PhantomData<fn() -> Settings>: RefUnwindSafe,

§

impl<Settings> Send for OutboxSpec<Settings>
where PhantomData<fn() -> Settings>: Send,

§

impl<Settings> Sync for OutboxSpec<Settings>
where PhantomData<fn() -> Settings>: Sync,

§

impl<Settings> Unpin for OutboxSpec<Settings>
where PhantomData<fn() -> Settings>: Unpin,

§

impl<Settings> UnsafeUnpin for OutboxSpec<Settings>
where PhantomData<fn() -> Settings>: UnsafeUnpin,

§

impl<Settings> UnwindSafe for OutboxSpec<Settings>
where PhantomData<fn() -> Settings>: UnwindSafe,

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

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

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
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> PublishedThrough<DefaultSlot> for T

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 = !

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

fn try_from(value: U) -> Result<T, !>

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