forked from weiznich/diesel_async
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
89 lines (70 loc) · 2.62 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use diesel::prelude::*;
use diesel::sqlite::{Sqlite, SqliteConnection};
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
use diesel_async::sync_connection_wrapper::SyncConnectionWrapper;
use diesel_async::{AsyncConnection, RunQueryDsl, SimpleAsyncConnection};
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
// ordinary diesel model setup
table! {
users {
id -> Integer,
name -> Text,
}
}
#[derive(Debug, Queryable, Selectable)]
#[diesel(table_name = users)]
struct User {
id: i32,
name: String,
}
const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
type InnerConnection = SqliteConnection;
type InnerDB = Sqlite;
async fn establish(db_url: &str) -> ConnectionResult<SyncConnectionWrapper<InnerConnection>> {
// It is necessary to specify the specific inner connection type because of inference issues
SyncConnectionWrapper::<SqliteConnection>::establish(db_url).await
}
async fn run_migrations<A>(async_connection: A) -> Result<(), Box<dyn std::error::Error>>
where
A: AsyncConnection<Backend = InnerDB> + 'static,
{
let mut async_wrapper: AsyncConnectionWrapper<A> =
AsyncConnectionWrapper::from(async_connection);
tokio::task::spawn_blocking(move || {
async_wrapper.run_pending_migrations(MIGRATIONS).unwrap();
})
.await
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>)
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let db_url = std::env::var("DATABASE_URL").expect("Env var `DATABASE_URL` not set");
// create an async connection for the migrations
let sync_wrapper: SyncConnectionWrapper<InnerConnection> = establish(&db_url).await?;
run_migrations(sync_wrapper).await?;
let mut sync_wrapper: SyncConnectionWrapper<InnerConnection> = establish(&db_url).await?;
sync_wrapper.batch_execute("DELETE FROM users").await?;
sync_wrapper
.batch_execute("INSERT INTO users(id, name) VALUES (3, 'toto')")
.await?;
let data: Vec<User> = users::table
.select(User::as_select())
.load(&mut sync_wrapper)
.await?;
println!("{data:?}");
diesel::delete(users::table)
.execute(&mut sync_wrapper)
.await?;
diesel::insert_into(users::table)
.values((users::id.eq(1), users::name.eq("iLuke")))
.execute(&mut sync_wrapper)
.await?;
let data: Vec<User> = users::table
.filter(users::id.gt(0))
.or_filter(users::name.like("%Luke"))
.select(User::as_select())
.load(&mut sync_wrapper)
.await?;
println!("{data:?}");
Ok(())
}