diff --git a/worker/src/d1/mod.rs b/worker/src/d1/mod.rs index 7f2389c6..bb62b133 100644 --- a/worker/src/d1/mod.rs +++ b/worker/src/d1/mod.rs @@ -343,6 +343,18 @@ impl From for D1PreparedStatement { // The result of a D1 query execution. pub struct D1Result(D1ResultSys); +// The meta object of D1 result. +#[derive(Debug, Clone, Deserialize)] +pub struct D1ResultMeta { + pub changed_db: Option, + pub changes: Option, + pub duration: Option, + pub last_row_id: Option, + pub rows_read: Option, + pub rows_written: Option, + pub size_after: Option, +} + impl D1Result { /// Returns `true` if the result indicates a success, otherwise `false`. pub fn success(&self) -> bool { @@ -372,6 +384,18 @@ impl D1Result { Ok(Vec::new()) } } + + /// Return the meta data in this result. + /// + /// Returns `None` if `meta` field is not populated. + pub fn meta(&self) -> Result> { + if let Ok(meta) = self.0.meta() { + let meta: D1ResultMeta = serde_wasm_bindgen::from_value(meta.into())?; + Ok(Some(meta)) + } else { + Ok(None) + } + } } #[derive(Clone)]