Skip to content

Instantly share code, notes, and snippets.

@Mr-Malomz
Created July 24, 2022 10:45
Show Gist options
  • Save Mr-Malomz/dafc7c738e7c477b1d9c3658650aec33 to your computer and use it in GitHub Desktop.
Save Mr-Malomz/dafc7c738e7c477b1d9c3658650aec33 to your computer and use it in GitHub Desktop.
use dotenv::dotenv;
use std::{env, io::Error};
use mongodb::{
bson::{doc, oid::ObjectId},
sync::{Client, Collection, Database},
};
use crate::schemas::project_schema::{Owner, Project};
pub struct DBMongo {
db: Database,
}
impl DBMongo {
pub 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).unwrap();
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