Skip to main content

Publish

Trait Publish 

Source
pub trait Publish<DB: Database>: InboxRow {
    // Required method
    fn publish(
        conn: &mut DB::Connection,
        message: &OutgoingMessage<'_>,
    ) -> impl Future<Output = Result<(), Error>> + Send;
}
Expand description

Writes a published message into the table: the name, the bytes and the headers reach the service’s SQL, which lays them out in its columns.

It has no default. With it, the struct’s Repository is a publisher and a route can lead names to it; without it, a mount that needs one does not compile, rather than publish into nothing. A message carrying a header the struct cannot hold byte for byte (any header, without a #[field(headers)] field; see HeaderColumn) is refused before this runs.

§Examples

use ruststream::OutgoingMessage;
use ruststream_sqlx::{Inbox, Publish};
use sqlx::{PgConnection, Postgres};

#[derive(Inbox, sqlx::FromRow)]
#[inbox(table = "email_jobs")]
pub struct SendEmail {
    #[field(id, generated)]
    job_id: i64,
    #[field(group)]
    name: String,
    #[field(payload)]
    payload: Vec<u8>,
}

impl Publish<Postgres> for SendEmail {
    async fn publish(
        conn: &mut PgConnection,
        message: &OutgoingMessage<'_>,
    ) -> Result<(), sqlx::Error> {
        sqlx::query("INSERT INTO email_jobs (name, payload) VALUES ($1, $2)")
            .bind(message.name())
            .bind(message.payload())
            .execute(conn)
            .await?;
        Ok(())
    }
}

Required Methods§

Source

fn publish( conn: &mut DB::Connection, message: &OutgoingMessage<'_>, ) -> impl Future<Output = Result<(), Error>> + Send

Writes message into the table.

§Errors

The database’s error, which the publish returns.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§