1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use js_sys::Array;
use js_sys::ArrayBuffer;
use js_sys::Uint8Array;
use serde::de::Deserialize;
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;
use worker_sys::d1::D1Database as D1DatabaseSys;
use worker_sys::d1::D1ExecResult;
use worker_sys::d1::D1PreparedStatement as D1PreparedStatementSys;
use worker_sys::d1::D1Result as D1ResultSys;
use crate::env::EnvBinding;
use crate::Result;
pub struct D1Database(D1DatabaseSys);
impl D1Database {
pub fn prepare(&self, query: &str) -> D1PreparedStatement {
self.0.prepare(query).into()
}
pub async fn dump(&self) -> Result<Vec<u8>> {
let array_buffer = JsFuture::from(self.0.dump()).await?;
let array_buffer = array_buffer.dyn_into::<ArrayBuffer>()?;
let array = Uint8Array::new(&array_buffer);
let mut vec = Vec::with_capacity(array.length() as usize);
array.copy_to(&mut vec);
Ok(vec)
}
pub async fn batch(&self, statements: Vec<D1PreparedStatement>) -> Result<Vec<D1Result>> {
let statements = statements.into_iter().map(|s| s.0).collect::<Array>();
let results = JsFuture::from(self.0.batch(statements)).await?;
let results = results.dyn_into::<Array>()?;
let mut vec = Vec::with_capacity(results.length() as usize);
for result in results.iter() {
let result = result.dyn_into::<D1ResultSys>()?;
vec.push(D1Result(result));
}
Ok(vec)
}
pub async fn exec(&self, query: &str) -> Result<D1ExecResult> {
let result = JsFuture::from(self.0.exec(query)).await?;
Ok(result.into())
}
}
impl EnvBinding for D1Database {
const TYPE_NAME: &'static str = "D1Database";
}
impl JsCast for D1Database {
fn instanceof(val: &JsValue) -> bool {
val.is_instance_of::<D1DatabaseSys>()
}
fn unchecked_from_js(val: JsValue) -> Self {
Self(val.into())
}
fn unchecked_from_js_ref(val: &JsValue) -> &Self {
unsafe { &*(val as *const JsValue as *const Self) }
}
}
impl From<D1Database> for JsValue {
fn from(database: D1Database) -> Self {
JsValue::from(database.0)
}
}
impl AsRef<JsValue> for D1Database {
fn as_ref(&self) -> &JsValue {
&self.0
}
}
impl From<D1DatabaseSys> for D1Database {
fn from(inner: D1DatabaseSys) -> Self {
Self(inner)
}
}
pub struct D1PreparedStatement(D1PreparedStatementSys);
impl D1PreparedStatement {
pub fn bind<T>(&self, value: &T) -> Result<()>
where
T: serde::ser::Serialize + ?Sized,
{
let value = serde_wasm_bindgen::to_value(value)?;
let array = Array::of1(&value);
self.0.bind(array)?;
Ok(())
}
pub async fn first<T>(&self, col_name: Option<&str>) -> Result<T>
where
T: for<'a> Deserialize<'a>,
{
let js_value = JsFuture::from(self.0.first(col_name)).await?;
let value = serde_wasm_bindgen::from_value(js_value)?;
Ok(value)
}
pub async fn run(&self) -> Result<D1Result> {
let result = JsFuture::from(self.0.run()).await?;
Ok(D1Result(result.into()))
}
pub async fn all(&self) -> Result<D1Result> {
let result = JsFuture::from(self.0.all()).await?;
Ok(D1Result(result.into()))
}
pub async fn raw<T>(&self) -> Result<Vec<T>>
where
T: for<'a> Deserialize<'a>,
{
let result = JsFuture::from(self.0.raw()).await?;
let result = result.dyn_into::<Array>()?;
let mut vec = Vec::with_capacity(result.length() as usize);
for value in result.iter() {
let value = serde_wasm_bindgen::from_value(value)?;
vec.push(value);
}
Ok(vec)
}
}
impl From<D1PreparedStatementSys> for D1PreparedStatement {
fn from(inner: D1PreparedStatementSys) -> Self {
Self(inner)
}
}
pub struct D1Result(D1ResultSys);
impl D1Result {
pub fn success(&self) -> bool {
self.0.success()
}
pub fn error(&self) -> Option<String> {
self.0.error()
}
pub fn results<T>(&self) -> Result<Vec<T>>
where
T: for<'a> Deserialize<'a>,
{
if let Some(results) = self.0.results() {
let mut vec = Vec::with_capacity(results.length() as usize);
for result in results.iter() {
let result = serde_wasm_bindgen::from_value(result)?;
vec.push(result);
}
Ok(vec)
} else {
Ok(Vec::new())
}
}
}