pub trait OutboxDatabase: Database { }Expand description
A sqlx database the default events of an outbox record run on.
The registry builds the statements from the record’s
OutboxTable::TABLE in each built-in dialect the database’s
connections speak, once, when the record type is registered, and a connection picks its own.
Postgres, MySql (MySQL and MariaDB) and
Sqlite implement it under their features, and Any under any,
where the database the connection reached picks the text. A record’s default event on a
backend whose dialect the crate was built without fails with sqlx::Error::Configuration
naming the record and the event.
§Examples
use ruststream::OutgoingMessage;
use ruststream_sqlx::{Outbox, OutboxDatabase, outbox};
use sqlx::{PgConnection, Postgres};
// outbox: id BIGSERIAL PRIMARY KEY, name TEXT, payload BYTEA; a processed record is deleted
#[derive(Outbox, sqlx::FromRow)]
#[outbox(table = "outbox")]
pub struct OrderEvent {
#[field(id)]
id: i64,
#[field(name)]
name: String,
#[field(payload)]
payload: Vec<u8>,
}
// The record of a published message is the service's own statement.
impl outbox::Publish<Postgres> for OrderEvent {
async fn publish(
conn: &mut PgConnection,
msg: &OutgoingMessage<'_>,
) -> Result<i64, sqlx::Error> {
sqlx::query_scalar("INSERT INTO outbox (name, payload) VALUES ($1, $2) RETURNING id")
.bind(msg.name())
.bind(msg.payload())
.fetch_one(conn)
.await
}
}
// The default events run on any `OutboxDatabase`: here the mark of a processed record.
pub async fn mark<DB: OutboxDatabase>(
conn: &mut DB::Connection,
id: &i64,
) -> Result<(), sqlx::Error>
where
OrderEvent: outbox::Ack<DB>,
{
<OrderEvent as outbox::Ack<DB>>::ack(conn, id).await
}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".