use variable db name instead of hardcoded one
This commit is contained in:
parent
9bbf481002
commit
4be9c034c9
@ -142,14 +142,15 @@ pub struct Media {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct DB {
|
pub struct DB {
|
||||||
client: Client,
|
client: Client,
|
||||||
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DB {
|
impl DB {
|
||||||
pub async fn new<S: Into<String>>(db_url: S) -> DbResult<Self> {
|
pub async fn new<S: Into<String>>(db_url: S, name: String) -> DbResult<Self> {
|
||||||
let options = ClientOptions::parse(db_url.into()).await?;
|
let options = ClientOptions::parse(db_url.into()).await?;
|
||||||
let client = Client::with_options(options)?;
|
let client = Client::with_options(options)?;
|
||||||
|
|
||||||
Ok(DB { client })
|
Ok(DB { client, name })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn migrate(&mut self) -> DbResult<()> {
|
pub async fn migrate(&mut self) -> DbResult<()> {
|
||||||
@ -186,8 +187,8 @@ impl DB {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn init<S: Into<String>>(db_url: S) -> DbResult<Self> {
|
pub async fn init<S: Into<String>>(db_url: S, name: String) -> DbResult<Self> {
|
||||||
let mut db = Self::new(db_url).await?;
|
let mut db = Self::new(db_url, name).await?;
|
||||||
db.migrate().await?;
|
db.migrate().await?;
|
||||||
|
|
||||||
Ok(db)
|
Ok(db)
|
||||||
@ -205,7 +206,7 @@ pub trait GetCollection {
|
|||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl CallDB for DB {
|
impl CallDB for DB {
|
||||||
async fn get_database(&mut self) -> Database {
|
async fn get_database(&mut self) -> Database {
|
||||||
self.client.database("gongbot")
|
self.client.database(&self.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,7 @@ async fn setup_db() -> DB {
|
|||||||
dotenvy::dotenv().unwrap();
|
dotenvy::dotenv().unwrap();
|
||||||
let db_url = std::env::var("DATABASE_URL").unwrap();
|
let db_url = std::env::var("DATABASE_URL").unwrap();
|
||||||
|
|
||||||
DB::new(db_url).await.unwrap()
|
DB::new(db_url, "gongbot".to_string()).await.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user