This commit is contained in:
Sam Samai 2021-08-22 14:18:27 +10:00
parent c6480635c0
commit 0998b64ed4
2 changed files with 61 additions and 29 deletions

View File

@ -10,10 +10,7 @@ rocket = { path = "../../../Rocket/core/lib", features = ["json"] }
# "json", # "json",
# ] } # ] }
# async-std = { version = "^1.9", features = ["attributes"] } # async-std = { version = "^1.9", features = ["attributes"] }
sea-orm = { path = "../../", features = [ sea-orm = { path = "../../", features = ["sqlx-all"] }
"sqlx-all",
# "runtime-async-std-native-tls",
] }
sea-query = { version = "^0.12.8" } sea-query = { version = "^0.12.8" }
serde_json = { version = "^1" } serde_json = { version = "^1" }
@ -32,4 +29,4 @@ features = ["macros", "offline", "migrate"]
# features = ["sea_orm"] # features = ["sea_orm"]
[dependencies.rocket_db_pools] [dependencies.rocket_db_pools]
path = "../../../Rocket/contrib/db_pools/lib" path = "../../../Rocket/contrib/db_pools/lib"
features = ["sqlx_sqlite"] features = ["seaorm_sqlx_sqlite"]

View File

@ -7,13 +7,15 @@ use rocket_db_pools::{sqlx, Connection, Database};
use futures::{future::TryFutureExt, stream::TryStreamExt}; use futures::{future::TryFutureExt, stream::TryStreamExt};
use sea_orm::entity::*; use sea_orm::entity::*;
use sea_orm::QueryFilter; use sea_orm::{
DatabaseBackend, QueryFilter, SqlxSqliteConnector, SqlxSqlitePoolConnection, Statement,
};
mod setup; mod setup;
#[derive(Database, Debug)] #[derive(Database, Debug)]
#[database("blog")] #[database("blog")]
struct Db(rocket_db_pools::sqlx::SqlitePool); struct Db(sea_orm::DatabaseConnection);
type Result<T, E = rocket::response::Debug<sqlx::Error>> = std::result::Result<T, E>; type Result<T, E = rocket::response::Debug<sqlx::Error>> = std::result::Result<T, E>;
@ -21,6 +23,7 @@ type Result<T, E = rocket::response::Debug<sqlx::Error>> = std::result::Result<T
mod post; mod post;
pub use post::Entity as Post; pub use post::Entity as Post;
use sea_orm::DatabaseConnection; use sea_orm::DatabaseConnection;
// #[derive(Debug, Clone, Deserialize, Serialize)] // #[derive(Debug, Clone, Deserialize, Serialize)]
// #[serde(crate = "rocket::serde")] // #[serde(crate = "rocket::serde")]
// struct Post { // struct Post {
@ -88,9 +91,18 @@ async fn list(mut db: Connection<Db>) -> Result<Json<Vec<i64>>> {
// let ids: Vec<i64> = recs.into(); // let ids: Vec<i64> = recs.into();
// println!("recs: {:#?}", ids); // println!("recs: {:#?}", ids);
println!("db: {:#?}", &*db);
let res = db
.execute(Statement::from_string(
DatabaseBackend::Sqlite,
"SELECT * from posts".to_owned(),
))
.await;
println!("res: {:#?}", res);
let posts = Post::find().all(&mut *db).await.unwrap(); // let con = SqlxSqliteConnector::from_sqlx_sqlite_pool(db);
assert_eq!(posts.len(), 0); // let posts = Post::find().all(&db).await.unwrap();
// assert_eq!(posts.len(), 0);
let ids: Vec<i64> = vec![]; let ids: Vec<i64> = vec![];
Ok(Json(ids)) Ok(Json(ids))
@ -172,27 +184,50 @@ pub fn stage() -> AdHoc {
.attach(Db::init()) .attach(Db::init())
.attach(AdHoc::try_on_ignite("Create init post", |rocket| async { .attach(AdHoc::try_on_ignite("Create init post", |rocket| async {
let db = Db::fetch(&rocket).expect("database mounted"); let db = Db::fetch(&rocket).expect("database mounted");
let res = sqlx::query( // let res = sqlx::query(
r#" // r#"
CREATE TABLE posts ( // CREATE TABLE posts (
id INTEGER PRIMARY KEY AUTOINCREMENT, // id INTEGER PRIMARY KEY AUTOINCREMENT,
title VARCHAR NOT NULL, // title VARCHAR NOT NULL,
text VARCHAR NOT NULL, // text VARCHAR NOT NULL,
published BOOLEAN NOT NULL DEFAULT 0 // published BOOLEAN NOT NULL DEFAULT 0
)"#, // )"#,
) // )
.execute(&**db) // .execute(&**db)
.await; // .await;
println!("res: {:#?}", res);
let res2 = sqlx::query( let create_post_table = db
r#" .execute(Statement::from_string(
INSERT INTO posts (title, text) VALUES ('a post', 'content of a post') DatabaseBackend::Sqlite,
"#, r#"
) CREATE TABLE posts (
.execute(&**db) id INTEGER PRIMARY KEY AUTOINCREMENT,
.await; title VARCHAR NOT NULL,
println!("res2: {:#?}", res2); text VARCHAR NOT NULL,
published BOOLEAN NOT NULL DEFAULT 0
)"#
.to_owned(),
))
.await;
println!("create_post_table: {:#?}", create_post_table);
let create_post = db
.execute(Statement::from_string(
DatabaseBackend::Sqlite,
"INSERT INTO posts (title, text) VALUES ('a post', 'content of a post')"
.to_owned(),
))
.await;
println!("create_post: {:#?}", create_post);
// let res2 = sqlx::query(
// r#"
// INSERT INTO posts (title, text) VALUES ('a post', 'content of a post')
// "#,
// )
// .execute(&**db)
// .await;
// println!("res2: {:#?}", res2);
// Db::fetch(&rocket) // Db::fetch(&rocket)
// .run(|db| { // .run(|db| {