Skip to content

Instantly share code, notes, and snippets.

@Mr-Malomz
Created July 24, 2022 10:36
Show Gist options
  • Save Mr-Malomz/0cc31ed542aaef397c8d3acf9a5e98db to your computer and use it in GitHub Desktop.
Save Mr-Malomz/0cc31ed542aaef397c8d3acf9a5e98db to your computer and use it in GitHub Desktop.
use dotenv::dotenv;
use futures::TryStreamExt;
use std::{env, io::Error};
use mongodb::{
bson::{doc, oid::ObjectId},
Client, Collection, Database,
};
use crate::schemas::project_schema::{Owner, Project};
pub struct DBMongo {
db: Database,
}
impl DBMongo {
pub async fn init() -> Self {
dotenv().ok();
let uri = match env::var("MONGOURI") {
Ok(v) => v.to_string(),
Err(_) => format!("Error loading env variable"),
};
let client = Client::with_uri_str(uri)
.await
.expect("error connecting to database");
let db = client.database("projectMngt");
DBMongo { db }
}
fn col_helper<T>(data_source: &Self, collection_name: &str) -> Collection<T> {
data_source.db.collection(collection_name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment