Created
March 23, 2018 14:23
-
-
Save LeighCiechanowski/267f1fd450861e1fdb6d2cd552e48c49 to your computer and use it in GitHub Desktop.
Singleton MongoDB Client
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
import { MongoClient } from 'mongodb'; | |
const DbConnection = function () { | |
var db = null; | |
var instance = 0; | |
async function DbConnect() { | |
try { | |
let url = 'mongodb://localhost:27017/articles'; | |
let _db = await MongoClient.connect(url); | |
return _db | |
} catch (e) { | |
return e; | |
} | |
} | |
async function Get() { | |
try { | |
if (db != null) { | |
return db; | |
} else { | |
db = await DbConnect(); | |
return db; | |
} | |
} catch (e) { | |
return e; | |
} | |
} | |
return { | |
Get: Get | |
} | |
} | |
export default DbConnection(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment