Created
July 24, 2022 10:36
-
-
Save Mr-Malomz/0cc31ed542aaef397c8d3acf9a5e98db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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