migration to JS engine #1

Merged
akulij merged 131 commits from dev into main 2025-05-31 08:49:52 +00:00
2 changed files with 7 additions and 6 deletions
Showing only changes of commit 4be9c034c9 - Show all commits

View File

@ -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)
} }
} }

View File

@ -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]