@examgoal/mongodb
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -7,4 +7,4 @@ import { ClientSession, Db, MongoClient } from 'mongodb'; | ||
constructor(config: ConfigOptions); | ||
connect(): Promise<MongodbClient>; | ||
createSession(): ClientSession; | ||
getDb(name?: string): Promise<Db>; | ||
readonly db: Db; | ||
@@ -11,0 +11,0 @@ readonly isConnected: boolean; |
@@ -7,17 +7,4 @@ "use strict"; | ||
this.configuration = config; | ||
this.mongoClient = new mongodb_1.MongoClient(config.uri, Object.assign(Object.assign({}, (config.mongodbOptions || {})), { useNewUrlParser: true })); | ||
this.mongoClient = new mongodb_1.MongoClient(config.uri, Object.assign(Object.assign({}, (config.mongodbOptions || {})), { numberOfRetries: Number.MAX_VALUE, useNewUrlParser: true, useUnifiedTopology: true })); | ||
} | ||
connect() { | ||
return new Promise((resolve, reject) => { | ||
this.mongoClient.connect((err) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
if (this.configuration.logEnabled) { | ||
console.log(`${this.name} App's MongoDB Connected`); | ||
} | ||
return resolve(instances[this.name]); | ||
}); | ||
}); | ||
} | ||
createSession() { | ||
@@ -29,2 +16,12 @@ if (!this.isConnected) { | ||
} | ||
getDb(name) { | ||
return new Promise((resolve, reject) => { | ||
if (this.isConnected) { | ||
return resolve(this.mongoClient.db(name || this.configuration.db)); | ||
} | ||
this.mongoClient.connect() | ||
.then(res => resolve(res.db(name || this.configuration.db))) | ||
.catch(reject); | ||
}); | ||
} | ||
get db() { | ||
@@ -43,7 +40,3 @@ return this.mongoClient.db(this.configuration.db); | ||
static initializeApp(config) { | ||
let a = new MongodbClient(config); | ||
instances[config.name || '[DEFAULT]'] = a; | ||
a.connect().catch(err => { | ||
throw new Error(`${a.name} MongoDB App\'s Error Occurred on first initialization : Error = ${err.toString()}`); | ||
}); | ||
instances[config.name || '[DEFAULT]'] = new MongodbClient(config); | ||
} | ||
@@ -50,0 +43,0 @@ static getInstance(name) { |
{ | ||
"name": "@examgoal/mongodb", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "MongoDB Session Management Helper for ExpressJS or any Node JS Projects", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3341
73