pub trait Fetch<DB: Database>: InboxRow {
// Required method
fn fetch(
conn: &mut DB::Connection,
ids: &[Self::Id],
) -> impl Future<Output = Result<Vec<Self>, Error>> + Send;
}Expand description
Reads the rows of claimed ids, on the claim’s connection.
The dialect builds it, for a flat table and for a message assembled from header fields; a table
names fetch as its own (custom(fetch), or own::Fetch by hand)
to assemble messages itself, from other tables
(a fetch over a join). Rows are matched to the claimed ids by their id field; a
claimed id with no row settles by the decode-failure policy before its handler runs, and the
log names the id. It runs inside the claim’s transaction, or right after a lease claim that
leased the rows and committed in one statement, as on Postgres and SQLite. In the advisory lock
form it runs once per row, with that row’s id alone, after the take that counted its attempt.
Either way a delivery reports the attempt its row held before the claim. It runs only after a
claim that took ids, so ids is never empty.
§Examples
use ruststream_sqlx::{Fetch, Inbox};
use sqlx::{PgConnection, Postgres};
/// A job whose message is the body of the order it refers to.
#[derive(Inbox, sqlx::FromRow)]
#[inbox(table = "jobs", custom(fetch))]
pub struct Job {
#[field(id)]
id: i64,
#[field(payload)]
payload: Vec<u8>,
}
impl Fetch<Postgres> for Job {
async fn fetch(conn: &mut PgConnection, ids: &[i64]) -> Result<Vec<Self>, sqlx::Error> {
sqlx::query_as(
"SELECT j.id, o.body AS payload FROM jobs j JOIN orders o ON o.id = j.order_id \
WHERE j.id = ANY($1)",
)
.bind(ids)
.fetch_all(conn)
.await
}
}Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".