Skip to main content

Db

Struct Db 

Source
pub struct Db { /* private fields */ }

Implementations§

Source§

impl Db

Source

pub async fn connect(url: &str) -> Result<Self, Error>

Source

pub async fn memory() -> Result<Self, Error>

Examples found in repository?
examples/admin_demo.rs (line 39)
38async fn main() -> std::io::Result<()> {
39    let db = Db::memory().await.expect("db connect");
40    db.execute(
41        "CREATE TABLE users (
42            id INTEGER PRIMARY KEY AUTOINCREMENT,
43            name TEXT NOT NULL,
44            is_admin INTEGER NOT NULL
45        )",
46    )
47    .await
48    .expect("create schema");
49
50    User { id: 0, name: "Alice".into(), is_admin: false }
51        .create(&db)
52        .await
53        .expect("seed alice");
54    User { id: 0, name: "Bob".into(), is_admin: true }
55        .create(&db)
56        .await
57        .expect("seed bob");
58
59    let router = with_defaults(Router::new()).wrap(authenticate);
60    let router = admin::register::<User>(router, &db);
61
62    let addr: SocketAddr = ([127, 0, 0, 1], 3000).into();
63    eprintln!("admin demo: hit /admin/users with `Authorization: Bearer dev-admin` header");
64    Server::bind(addr).serve_router(router).await
65}
More examples
Hide additional examples
examples/orm_demo.rs (line 34)
33async fn main() -> Result<(), Error> {
34    let db = Db::memory().await?;
35    db.execute(
36        "CREATE TABLE users (
37            id INTEGER PRIMARY KEY AUTOINCREMENT,
38            name TEXT NOT NULL,
39            is_admin INTEGER NOT NULL
40        )",
41    )
42    .await?;
43
44    let alice_id = User { id: 0, name: "Alice".into(), is_admin: false }
45        .create(&db)
46        .await?;
47    let bob_id = User { id: 0, name: "Bob".into(), is_admin: true }
48        .create(&db)
49        .await?;
50    println!("created ids: alice={alice_id} bob={bob_id}");
51
52    let alice = User::find(&db, alice_id).await?.expect("alice");
53    println!("find alice: {alice:?}");
54
55    let all = User::all(&db).await?;
56    println!("all: {all:?}");
57
58    let renamed = User { id: alice_id, name: "Alicia".into(), is_admin: false };
59    renamed.update(&db).await?;
60    let after_update = User::find(&db, alice_id).await?.unwrap();
61    println!("after update: {after_update:?}");
62
63    User::delete(&db, bob_id).await?;
64    println!("remaining after delete bob: {:?}", User::all(&db).await?);
65
66    Ok(())
67}
Source

pub async fn execute(&self, sql: &str) -> Result<(), Error>

Examples found in repository?
examples/admin_demo.rs (lines 40-46)
38async fn main() -> std::io::Result<()> {
39    let db = Db::memory().await.expect("db connect");
40    db.execute(
41        "CREATE TABLE users (
42            id INTEGER PRIMARY KEY AUTOINCREMENT,
43            name TEXT NOT NULL,
44            is_admin INTEGER NOT NULL
45        )",
46    )
47    .await
48    .expect("create schema");
49
50    User { id: 0, name: "Alice".into(), is_admin: false }
51        .create(&db)
52        .await
53        .expect("seed alice");
54    User { id: 0, name: "Bob".into(), is_admin: true }
55        .create(&db)
56        .await
57        .expect("seed bob");
58
59    let router = with_defaults(Router::new()).wrap(authenticate);
60    let router = admin::register::<User>(router, &db);
61
62    let addr: SocketAddr = ([127, 0, 0, 1], 3000).into();
63    eprintln!("admin demo: hit /admin/users with `Authorization: Bearer dev-admin` header");
64    Server::bind(addr).serve_router(router).await
65}
More examples
Hide additional examples
examples/orm_demo.rs (lines 35-41)
33async fn main() -> Result<(), Error> {
34    let db = Db::memory().await?;
35    db.execute(
36        "CREATE TABLE users (
37            id INTEGER PRIMARY KEY AUTOINCREMENT,
38            name TEXT NOT NULL,
39            is_admin INTEGER NOT NULL
40        )",
41    )
42    .await?;
43
44    let alice_id = User { id: 0, name: "Alice".into(), is_admin: false }
45        .create(&db)
46        .await?;
47    let bob_id = User { id: 0, name: "Bob".into(), is_admin: true }
48        .create(&db)
49        .await?;
50    println!("created ids: alice={alice_id} bob={bob_id}");
51
52    let alice = User::find(&db, alice_id).await?.expect("alice");
53    println!("find alice: {alice:?}");
54
55    let all = User::all(&db).await?;
56    println!("all: {all:?}");
57
58    let renamed = User { id: alice_id, name: "Alicia".into(), is_admin: false };
59    renamed.update(&db).await?;
60    let after_update = User::find(&db, alice_id).await?.unwrap();
61    println!("after update: {after_update:?}");
62
63    User::delete(&db, bob_id).await?;
64    println!("remaining after delete bob: {:?}", User::all(&db).await?);
65
66    Ok(())
67}

Trait Implementations§

Source§

impl Clone for Db

Source§

fn clone(&self) -> Db

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Db

§

impl !RefUnwindSafe for Db

§

impl Send for Db

§

impl Sync for Db

§

impl Unpin for Db

§

impl UnsafeUnpin for Db

§

impl !UnwindSafe for Db

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more