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
impl OutboxSpec
Sourcepub const fn new(
table: &'static str,
id: Column<'static>,
name: Column<'static>,
payload: Column<'static>,
) -> Self
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>
impl<Settings> OutboxSpec<Settings>
Sourcepub const fn spec(&self) -> TableSpec<'static>
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())
}Sourcepub const fn within(self, schema: &'static str) -> Self
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");Sourcepub const fn data(self, columns: &'static [Column<'static>]) -> Self
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")]);Sourcepub const fn selecting_all(self) -> Self
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();Sourcepub const fn headers(
self,
column: Column<'static>,
) -> OutboxSpec<<Settings as Push<Headers>>::Out>
pub const fn headers( self, column: Column<'static>, ) -> OutboxSpec<<Settings as Push<Headers>>::Out>
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"));Sourcepub const fn processed_at(
self,
column: Column<'static>,
) -> OutboxSpec<<Settings as Push<ProcessedAt>>::Out>
pub const fn processed_at( self, column: Column<'static>, ) -> OutboxSpec<<Settings as Push<ProcessedAt>>::Out>
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"));Sourcepub const fn own<Event: OwnEvent>(
self,
) -> OutboxSpec<<Settings as Push<Event>>::Out>
pub const fn own<Event: OwnEvent>( self, ) -> OutboxSpec<<Settings as Push<Event>>::Out>
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>
impl<Settings> Clone for OutboxSpec<Settings>
impl<Settings> Copy for OutboxSpec<Settings>
Source§impl<Settings> Debug for OutboxSpec<Settings>
impl<Settings> Debug for OutboxSpec<Settings>
Source§impl<Settings: Declaration + 'static> Described for OutboxSpec<Settings>
impl<Settings: Declaration + 'static> Described for OutboxSpec<Settings>
impl<Settings> Eq for OutboxSpec<Settings>
Auto Trait Implementations§
impl<Settings> Freeze for OutboxSpec<Settings>
impl<Settings> RefUnwindSafe for OutboxSpec<Settings>
impl<Settings> Send for OutboxSpec<Settings>
impl<Settings> Sync for OutboxSpec<Settings>
impl<Settings> Unpin for OutboxSpec<Settings>
impl<Settings> UnsafeUnpin for OutboxSpec<Settings>
impl<Settings> UnwindSafe for OutboxSpec<Settings>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
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> ⓘ
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> ⓘ
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