@examgoal/mongodb
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -7,5 +7,7 @@ import { Db, MongoClient } from 'mongodb'; | ||
private isConnected; | ||
private connectPromise; | ||
constructor(config: ConfigOptions); | ||
private resetClient; | ||
getDb(name?: string): Promise<Db>; | ||
close(force?: boolean): Promise<void>; | ||
get client(): MongoClient; | ||
@@ -12,0 +14,0 @@ get name(): string; |
@@ -7,2 +7,3 @@ "use strict"; | ||
this.isConnected = false; | ||
this.connectPromise = null; | ||
this.configuration = config; | ||
@@ -13,12 +14,12 @@ this.mongoClient = new mongodb_1.MongoClient(config.uri, config.mongodbOptions || {}); | ||
resetClient() { | ||
this.mongoClient.removeListener("serverClosed", this.resetClient); | ||
if (this.mongoClient) { | ||
this.mongoClient.removeAllListeners("serverClosed"); | ||
} | ||
this.mongoClient = new mongodb_1.MongoClient(this.configuration.uri, this.configuration.mongodbOptions || {}); | ||
this.mongoClient.on("serverClosed", this.resetClient); | ||
this.isConnected = false; | ||
this.connectPromise = null; | ||
} | ||
getDb(name) { | ||
return new Promise((resolve, reject) => { | ||
if (this.isConnected) { | ||
return resolve(this.mongoClient.db(name || this.configuration.db)); | ||
} | ||
this.connectPromise = this.connectPromise || new Promise((resolve, reject) => { | ||
this.mongoClient.connect() | ||
@@ -35,5 +36,13 @@ .then(res => { | ||
reject(err); | ||
setImmediate(() => { | ||
this.connectPromise = null; | ||
}); | ||
}); | ||
}); | ||
return this.connectPromise; | ||
} | ||
async close(force) { | ||
this.isConnected = false; | ||
await this.mongoClient.close(force); | ||
} | ||
get client() { | ||
@@ -40,0 +49,0 @@ return this.mongoClient; |
{ | ||
"name": "@examgoal/mongodb", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"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
3839
88