pub struct Db { /* private fields */ }Implementations§
Source§impl Db
impl Db
pub async fn connect(url: &str) -> Result<Self, Error>
Sourcepub async fn memory() -> Result<Self, Error>
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 {
51 id: 0,
52 name: "Alice".into(),
53 is_admin: false,
54 }
55 .create(&db)
56 .await
57 .expect("seed alice");
58 User {
59 id: 0,
60 name: "Bob".into(),
61 is_admin: true,
62 }
63 .create(&db)
64 .await
65 .expect("seed bob");
66
67 let router = with_defaults(Router::new()).wrap(authenticate);
68 let router = admin::register::<User>(router, &db);
69
70 let addr: SocketAddr = ([127, 0, 0, 1], 3000).into();
71 eprintln!("admin demo: hit /admin/users with `Authorization: Bearer dev-admin` header");
72 Server::bind(addr).serve_router(router).await
73}More 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 {
45 id: 0,
46 name: "Alice".into(),
47 is_admin: false,
48 }
49 .create(&db)
50 .await?;
51 let bob_id = User {
52 id: 0,
53 name: "Bob".into(),
54 is_admin: true,
55 }
56 .create(&db)
57 .await?;
58 println!("created ids: alice={alice_id} bob={bob_id}");
59
60 let alice = User::find(&db, alice_id).await?.expect("alice");
61 println!("find alice: {alice:?}");
62
63 let all = User::all(&db).await?;
64 println!("all: {all:?}");
65
66 let renamed = User {
67 id: alice_id,
68 name: "Alicia".into(),
69 is_admin: false,
70 };
71 renamed.update(&db).await?;
72 let after_update = User::find(&db, alice_id).await?.unwrap();
73 println!("after update: {after_update:?}");
74
75 User::delete(&db, bob_id).await?;
76 println!("remaining after delete bob: {:?}", User::all(&db).await?);
77
78 Ok(())
79}Sourcepub async fn execute(&self, sql: &str) -> Result<(), Error>
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 {
51 id: 0,
52 name: "Alice".into(),
53 is_admin: false,
54 }
55 .create(&db)
56 .await
57 .expect("seed alice");
58 User {
59 id: 0,
60 name: "Bob".into(),
61 is_admin: true,
62 }
63 .create(&db)
64 .await
65 .expect("seed bob");
66
67 let router = with_defaults(Router::new()).wrap(authenticate);
68 let router = admin::register::<User>(router, &db);
69
70 let addr: SocketAddr = ([127, 0, 0, 1], 3000).into();
71 eprintln!("admin demo: hit /admin/users with `Authorization: Bearer dev-admin` header");
72 Server::bind(addr).serve_router(router).await
73}More 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 {
45 id: 0,
46 name: "Alice".into(),
47 is_admin: false,
48 }
49 .create(&db)
50 .await?;
51 let bob_id = User {
52 id: 0,
53 name: "Bob".into(),
54 is_admin: true,
55 }
56 .create(&db)
57 .await?;
58 println!("created ids: alice={alice_id} bob={bob_id}");
59
60 let alice = User::find(&db, alice_id).await?.expect("alice");
61 println!("find alice: {alice:?}");
62
63 let all = User::all(&db).await?;
64 println!("all: {all:?}");
65
66 let renamed = User {
67 id: alice_id,
68 name: "Alicia".into(),
69 is_admin: false,
70 };
71 renamed.update(&db).await?;
72 let after_update = User::find(&db, alice_id).await?.unwrap();
73 println!("after update: {after_update:?}");
74
75 User::delete(&db, bob_id).await?;
76 println!("remaining after delete bob: {:?}", User::all(&db).await?);
77
78 Ok(())
79}Trait Implementations§
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> 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