From 2741f38e49be90e984ce64ba76a99ae9a68149c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Fri, 5 Jul 2024 03:57:40 +0800 Subject: [PATCH 1/2] fix: Add a binding to meta object for `D1Result`. --- worker/src/d1/mod.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/worker/src/d1/mod.rs b/worker/src/d1/mod.rs index 7f2389c6..bc213cde 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 it's not exists. + pub fn meta(&self) -> Result> { + if let Ok(meta) = self.0.meta() { + let meta: D1ResultMeta = serde_wasm_bindgen::from_value(meta.into()).unwrap(); + Ok(Some(meta)) + } else { + Ok(None) + } + } } #[derive(Clone)] From 2340d4e1790a27407e0cbe4d3ba1e88fb439f18b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Fri, 5 Jul 2024 04:44:47 +0800 Subject: [PATCH 2/2] Fix typo. --- worker/src/d1/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/worker/src/d1/mod.rs b/worker/src/d1/mod.rs index bc213cde..bb62b133 100644 --- a/worker/src/d1/mod.rs +++ b/worker/src/d1/mod.rs @@ -387,10 +387,10 @@ impl D1Result { /// Return the meta data in this result. /// - /// Returns `None` if it's not exists. + /// 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()).unwrap(); + let meta: D1ResultMeta = serde_wasm_bindgen::from_value(meta.into())?; Ok(Some(meta)) } else { Ok(None)