Skip to main content

d1_orm_engine/
batch.rs

1use worker::{D1Database, D1PreparedStatement, D1Result};
2use crate::error::OrmError;
3
4pub struct Batch<'db> {
5    db: &'db D1Database,
6    stmts: Vec<D1PreparedStatement>,
7}
8
9impl<'db> Batch<'db> {
10    pub fn new(db: &'db D1Database) -> Self {
11        Self { db, stmts: vec![] }
12    }
13
14    pub fn add(mut self, stmt: D1PreparedStatement) -> Self {
15        self.stmts.push(stmt);
16        self
17    }
18
19    pub async fn run(self) -> Result<Vec<D1Result>, OrmError> {
20        self.db.batch(self.stmts).await.map_err(|_| OrmError::Execute)
21    }
22}