pub struct D1Connection { /* private fields */ }
Expand description
§Example
# Cargo.toml
[dependencies]
sqlx_d1 = { version = "0.1", features = ["macros"] }
worker = { version = "0.5", features = ["d1"] }
serde = { version = "1.0", features = ["derive"] }
# wrangler.toml
[[d1_database]]
binding = "DB"
database_name = "..."
database_id = "..."
ⓘ
// src/lib.rs
#[worker::event(fetch)]
async fn main(
mut req: worker::Request,
env: worker::Env,
_ctx: worker::Context,
) -> worker::Result<worker::Response> {
let d1 = env.d1("DB")?;
let conn = sqlx_d1::D1Connection::new(d1);
#[derive(serde::Deserialize)]
struct CreateUser {
name: String,
age: Option<u8>,
}
let req = req.json::<CreateUser>().await?;
let id = sqlx_d1::query!(
"
INSERT INTO users (name, age) VALUES (?, ?)
RETURNING id
",
req.name,
req.age
)
.fetch_one(&conn)
.await
.map_err(|e| worker::Error::RustError(e.to_string()))?
.id;
worker::Response::ok(format!("Your id is {id}!"))
}
Implementations§
Source§impl D1Connection
impl D1Connection
pub fn new(d1: D1Database) -> D1Connection
pub async fn connect(url: impl AsRef<str>) -> Result<D1Connection, Error>
Trait Implementations§
Source§impl Clone for D1Connection
impl Clone for D1Connection
Source§fn clone(&self) -> D1Connection
fn clone(&self) -> D1Connection
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Connection for D1Connection
impl Connection for D1Connection
type Database = D1
type Options = D1ConnectOptions
Source§fn close(self) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>
fn close(self) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>
Explicitly close this database connection. Read more
Source§fn ping(
&mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + '_>>
fn ping( &mut self, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + '_>>
Checks if a connection to the database is still valid.
Source§fn begin(
&mut self,
) -> Pin<Box<dyn Future<Output = Result<Transaction<'_, <D1Connection as Connection>::Database>, Error>> + Send + '_>>where
D1Connection: Sized,
fn begin(
&mut self,
) -> Pin<Box<dyn Future<Output = Result<Transaction<'_, <D1Connection as Connection>::Database>, Error>> + Send + '_>>where
D1Connection: Sized,
Begin a new transaction or establish a savepoint within the active transaction. Read more
Source§fn shrink_buffers(&mut self)
fn shrink_buffers(&mut self)
Restore any buffers in the connection to their default capacity, if possible. Read more
Source§fn transaction<'a, F, R, E>(
&'a mut self,
callback: F,
) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'a>>
fn transaction<'a, F, R, E>( &'a mut self, callback: F, ) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'a>>
Execute the function inside a transaction. Read more
Source§fn cached_statements_size(&self) -> usizewhere
Self::Database: HasStatementCache,
fn cached_statements_size(&self) -> usizewhere
Self::Database: HasStatementCache,
The number of statements currently cached in the connection.
Source§fn clear_cached_statements(
&mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + '_>>where
Self::Database: HasStatementCache,
fn clear_cached_statements(
&mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + '_>>where
Self::Database: HasStatementCache,
Removes all statements from the cache, closing them on the server if
needed.
Source§impl Debug for D1Connection
impl Debug for D1Connection
Source§impl<'c> Executor<'c> for &'c D1Connection
impl<'c> Executor<'c> for &'c D1Connection
type Database = D1
Source§fn fetch_many<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<Either<<<&'c D1Connection as Executor<'c>>::Database as Database>::QueryResult, <<&'c D1Connection as Executor<'c>>::Database as Database>::Row>, Error>> + Send + 'e>>
fn fetch_many<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<Either<<<&'c D1Connection as Executor<'c>>::Database as Database>::QueryResult, <<&'c D1Connection as Executor<'c>>::Database as Database>::Row>, Error>> + Send + 'e>>
Execute multiple queries and return the generated results as a stream
from each query, in a stream.
Source§fn fetch_optional<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<Option<<<&'c D1Connection as Executor<'c>>::Database as Database>::Row>, Error>> + Send + 'e>>
fn fetch_optional<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<Option<<<&'c D1Connection as Executor<'c>>::Database as Database>::Row>, Error>> + Send + 'e>>
Execute the query and returns at most one row.
Source§fn prepare_with<'e, 'q>(
self,
sql: &'q str,
_parameters: &'e [<<&'c D1Connection as Executor<'c>>::Database as Database>::TypeInfo],
) -> Pin<Box<dyn Future<Output = Result<<<&'c D1Connection as Executor<'c>>::Database as Database>::Statement<'q>, Error>> + Send + 'e>>where
'q: 'e,
'c: 'e,
fn prepare_with<'e, 'q>(
self,
sql: &'q str,
_parameters: &'e [<<&'c D1Connection as Executor<'c>>::Database as Database>::TypeInfo],
) -> Pin<Box<dyn Future<Output = Result<<<&'c D1Connection as Executor<'c>>::Database as Database>::Statement<'q>, Error>> + Send + 'e>>where
'q: 'e,
'c: 'e,
Prepare the SQL query, with parameter type information, to inspect the
type information about its parameters and results. Read more
Source§fn execute<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Execute the query and return the total number of rows affected.
Source§fn execute_many<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute_many<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Execute multiple queries and return the rows affected from each query, in a stream.
Source§fn fetch<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
fn fetch<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
Execute the query and return the generated results as a stream.
Source§fn fetch_all<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
fn fetch_all<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
Execute the query and return all the generated results, collected into a
Vec
.Source§impl<'c> Executor<'c> for &'c mut D1Connection
impl<'c> Executor<'c> for &'c mut D1Connection
type Database = D1
Source§fn fetch_many<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<Either<<<&'c mut D1Connection as Executor<'c>>::Database as Database>::QueryResult, <<&'c mut D1Connection as Executor<'c>>::Database as Database>::Row>, Error>> + Send + 'e>>
fn fetch_many<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<Either<<<&'c mut D1Connection as Executor<'c>>::Database as Database>::QueryResult, <<&'c mut D1Connection as Executor<'c>>::Database as Database>::Row>, Error>> + Send + 'e>>
Execute multiple queries and return the generated results as a stream
from each query, in a stream.
Source§fn fetch_optional<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<Option<<<&'c mut D1Connection as Executor<'c>>::Database as Database>::Row>, Error>> + Send + 'e>>
fn fetch_optional<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<Option<<<&'c mut D1Connection as Executor<'c>>::Database as Database>::Row>, Error>> + Send + 'e>>
Execute the query and returns at most one row.
Source§fn prepare_with<'e, 'q>(
self,
sql: &'q str,
_parameters: &'e [<<&'c mut D1Connection as Executor<'c>>::Database as Database>::TypeInfo],
) -> Pin<Box<dyn Future<Output = Result<<<&'c mut D1Connection as Executor<'c>>::Database as Database>::Statement<'q>, Error>> + Send + 'e>>where
'q: 'e,
'c: 'e,
fn prepare_with<'e, 'q>(
self,
sql: &'q str,
_parameters: &'e [<<&'c mut D1Connection as Executor<'c>>::Database as Database>::TypeInfo],
) -> Pin<Box<dyn Future<Output = Result<<<&'c mut D1Connection as Executor<'c>>::Database as Database>::Statement<'q>, Error>> + Send + 'e>>where
'q: 'e,
'c: 'e,
Prepare the SQL query, with parameter type information, to inspect the
type information about its parameters and results. Read more
Source§fn execute<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Execute the query and return the total number of rows affected.
Source§fn execute_many<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute_many<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Execute multiple queries and return the rows affected from each query, in a stream.
Source§fn fetch<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
fn fetch<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
Execute the query and return the generated results as a stream.
Source§fn fetch_all<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
fn fetch_all<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
Execute the query and return all the generated results, collected into a
Vec
.impl Send for D1Connection
impl Sync for D1Connection
Auto Trait Implementations§
impl Freeze for D1Connection
impl !RefUnwindSafe for D1Connection
impl Unpin for D1Connection
impl !UnwindSafe for D1Connection
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more