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 60)
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}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 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 {
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}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/orm_demo.rs (line 49)
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}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 71)
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}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 75)
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}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".