pub trait Model:
Sized
+ Send
+ Sync
+ Unpin
+ 'static {
const TABLE: &'static str;
const COLUMNS: &'static [&'static str];
const INSERT_COLUMNS: &'static [&'static str];
// Required methods
fn id(&self) -> i64;
fn from_row(row: Row<'_>) -> Result<Self, Error>;
fn insert_values(&self) -> Vec<Value>;
// Provided methods
fn find(
db: &Db,
id: i64,
) -> impl Future<Output = Result<Option<Self>, Error>> + Send
where Self: Send { ... }
fn all(db: &Db) -> impl Future<Output = Result<Vec<Self>, Error>> + Send { ... }
fn create<'a>(
&'a self,
db: &'a Db,
) -> impl Future<Output = Result<i64, Error>> + Send + 'a { ... }
fn update<'a>(
&'a self,
db: &'a Db,
) -> impl Future<Output = Result<(), Error>> + Send + 'a { ... }
fn delete(
db: &Db,
id: i64,
) -> impl Future<Output = Result<(), Error>> + Send { ... }
}Required Associated Constants§
const TABLE: &'static str
const COLUMNS: &'static [&'static str]
const INSERT_COLUMNS: &'static [&'static str]
Required Methods§
fn id(&self) -> i64
fn from_row(row: Row<'_>) -> Result<Self, Error>
fn insert_values(&self) -> Vec<Value>
Provided Methods§
Sourcefn find(
db: &Db,
id: i64,
) -> impl Future<Output = Result<Option<Self>, Error>> + Sendwhere
Self: Send,
fn find(
db: &Db,
id: i64,
) -> impl Future<Output = Result<Option<Self>, Error>> + Sendwhere
Self: Send,
Examples found in repository?
examples/orm_demo.rs (line 52)
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}Sourcefn all(db: &Db) -> impl Future<Output = Result<Vec<Self>, Error>> + Send
fn all(db: &Db) -> impl Future<Output = Result<Vec<Self>, Error>> + Send
Examples found in repository?
examples/orm_demo.rs (line 55)
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}Sourcefn create<'a>(
&'a self,
db: &'a Db,
) -> impl Future<Output = Result<i64, Error>> + Send + 'a
fn create<'a>( &'a self, db: &'a Db, ) -> impl Future<Output = Result<i64, Error>> + Send + 'a
Examples found in repository?
examples/admin_demo.rs (line 51)
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
examples/orm_demo.rs (line 45)
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}Sourcefn update<'a>(
&'a self,
db: &'a Db,
) -> impl Future<Output = Result<(), Error>> + Send + 'a
fn update<'a>( &'a self, db: &'a Db, ) -> impl Future<Output = Result<(), Error>> + Send + 'a
Examples found in repository?
examples/orm_demo.rs (line 59)
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}Sourcefn delete(db: &Db, id: i64) -> impl Future<Output = Result<(), Error>> + Send
fn delete(db: &Db, id: i64) -> impl Future<Output = Result<(), Error>> + Send
Examples found in repository?
examples/orm_demo.rs (line 63)
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}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.