Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 13x 13x 13x 12x 1x 11x 9x 9x 9x 3x 6x 6x 6x 4x 4x 6x 3x 2x 2x 2x 13x | /**
* An adapter for the official node.js MongoDB driver
* https://github.com/mongodb/node-mongodb-native
* @class
*/
class MongodbAdapter {
constructor (mongodb) {
this.MongoClient = mongodb.MongoClient
this.client = null
this.db = null
}
/**
* @return {Boolean} Whether there is an active mongo connection
*/
get isConnected () {
if (this.client) {
return this.client.isConnected()
}
return false
}
/**
* @param {String} url MongoDB connection url
* @param {Number} connectTimeout How long to wait when connecting, before timing out
*/
connect (url, connectTimeoutMS) {
return new Promise((resolve, reject) => {
this.MongoClient.connect(url, {
connectTimeoutMS
}, (err, client) => {
if (err !== null) {
return reject(err)
}
this.client = client
this.db = client.db()
this.client.on('close', () => {
this.client = null
this.db = null
})
return resolve(true)
})
})
}
/**
* Destroys the mongodb connection
*/
close () {
if (this.client !== null) {
this.client.close()
this.client = null
this.db = null
}
}
/**
* Attempts to ping mongodb
* @return {Promise.<Boolean>|Error}
*/
ping () {
return new Promise((resolve, reject) => {
this.db.command({ ping: 1 }, (err) => {
if (err) {
this.client = null
this.db = null
return reject(err)
}
return resolve(true)
})
})
}
}
module.exports = mongodb => new MongodbAdapter(mongodb)
|