learn_orm_word/src/start_db.rs
2024-07-20 23:27:20 +08:00

25 lines
901 B
Rust

use log::info;
use sea_orm::{ConnectOptions, ConnectionTrait, Database, DatabaseConnection};
use std::error::Error;
use sea_orm::Schema;
pub struct Starter {
pub conn: DatabaseConnection,
}
impl Starter {
pub async fn connect(db_url:&str) -> Result<Self, Box<dyn Error>> {
let opt: ConnectOptions = ConnectOptions::new(db_url);
let conn_new: DatabaseConnection = Database::connect(opt).await?;
Ok(Starter{conn:conn_new})
}
pub async fn create_table(&self) -> Result<(), Box<dyn Error>> {
let backend = self.conn.get_database_backend();
let schema = Schema::new(backend);
let table_create_statement = schema.create_table_from_entity(super::word_entity::Entity);
let table_create_result = self.conn.execute(backend.build(&table_create_statement)).await?;
info!("{:?}",table_create_result);
Ok(())
}
}