Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a binding to meta object for D1Result. #591

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions worker/src/d1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,18 @@ impl From<D1PreparedStatementSys> 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<bool>,
pub changes: Option<usize>,
pub duration: Option<f64>,
pub last_row_id: Option<i64>,
pub rows_read: Option<usize>,
pub rows_written: Option<usize>,
pub size_after: Option<usize>,
}

impl D1Result {
/// Returns `true` if the result indicates a success, otherwise `false`.
pub fn success(&self) -> bool {
Expand Down Expand Up @@ -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<Option<D1ResultMeta>> {
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)]
Expand Down
Loading