This is simple implementation of the MongoDB driver, you can use this brick when you start to work with MongoDB
install : npm i -s mongodb
| /** | |
| * Simple MongoDB driver to handle basic calls | |
| * @author Sebastien Hideux <[email protected]> | |
| */ | |
| const { MongoClient, ObjectID } = require('mongodb') | |
| module.exports = (app) => { | |
| const config = { | |
| useNewUrlParser: true, | |
| reconnectTries: Number.MAX_VALUE, | |
| reconnectInterval: 1000 | |
| } | |
| /** | |
| * Execute a MongoDB Function | |
| * @param database | |
| * @param collection | |
| * @param {function(Collection): (*)} func - MongoDB function to execute | |
| * @return {Promise<CommandResult|*>} | |
| * @private | |
| */ | |
| const _execute = (database, collection, func) => { | |
| try { | |
| return MongoClient | |
| .connect(app.config.mongo.host, config) | |
| .then(async client => { | |
| const col = client.db(database).collection(collection) | |
| const result = await func(col) | |
| await client.close() | |
| return result | |
| }) | |
| } catch (e) { | |
| console.error('MongoDB error', e) | |
| } | |
| } | |
| return { | |
| /** | |
| * Export ObjectID | |
| */ | |
| ObjectID, | |
| /** | |
| * Find objects | |
| * @param {String} database - Mongo Database, ex : 'mydb' | |
| * @param {String} collection - Mongo collection, ex : 'users' | |
| * @param {Object} filter - Mongo filter options | |
| * @return {Promise<Array<*>>} | |
| */ | |
| async find(database, collection, filter) { | |
| if ('_id' in filter && typeof filter._id === 'string') | |
| filter._id = new ObjectID(filter._id) | |
| const func = async col => col.find(filter).toArray() | |
| return _execute(database, collection, func) | |
| }, | |
| /** | |
| * Create one or many objects | |
| * @param {String} database - Mongo Database, ex : 'mydb' | |
| * @param {String} collection - Mongo collection, ex : 'users' | |
| * @param {object, array} data | |
| * @param {*} options | |
| * @return {{insertedCount: Number, insertedId: String}} | |
| */ | |
| async create(database, collection, data, options) { | |
| /** | |
| * func | |
| * @param {Collection} col | |
| * @return {Promise<*>} | |
| */ | |
| const func = async col => col[!Array.isArray(data) ? 'insertOne' : 'insertMany'](data) | |
| const { insertedId, insertedCount } = await _execute(database, collection, func) | |
| return { insertedId, insertedCount } | |
| }, | |
| /** | |
| * Delete a single object | |
| * @param {String} database - Mongo Database, ex : 'mydb' | |
| * @param {String} collection - Mongo collection, ex : 'users' | |
| * @param filter | |
| * @return {Promise<{deletedCount: Number}>} | |
| */ | |
| async delete(database, collection, filter) { | |
| const func = async col => col.deleteOne(filter) | |
| const { deletedCount } = await _execute(database, collection, func) | |
| return { deletedCount } | |
| }, | |
| /** | |
| * Update a single object | |
| * @param {String} database - Mongo Database, ex : 'mydb' | |
| * @param {String} collection - Mongo collection, ex : 'users' | |
| * @param filter | |
| * @param {object} data | |
| * @return {Promise<{upsertedId, upsertedCount, modifiedCount, matchedCount}>} | |
| */ | |
| async updateOne(database, collection, filter, data) { | |
| if (typeof data !== 'object') | |
| throw new Error('MongoDB Error, expected data to be an Object') | |
| const func = async col => col.updateOne(filter, { $set: data }) | |
| const { modifiedCount, upsertedId, upsertedCount, matchedCount } = await _execute(database, collection, func) | |
| return { modifiedCount, upsertedId, upsertedCount, matchedCount } | |
| }, | |
| /** | |
| * Update many objects | |
| * @param {String} database - Mongo Database, ex : 'mydb' | |
| * @param {String} collection - Mongo collection, ex : 'users' | |
| * @param filter | |
| * @param {Array} data | |
| * @return {Promise<{nModified: Number}>} | |
| */ | |
| async updateMany(database, collection, filter, data) { | |
| if (typeof data !== 'object') | |
| throw new Error('MongoDB Error, expected data to be an Object') | |
| const func = async col => col.updateOne(filter, { $set: data }) | |
| const { nModified } = await _execute(database, collection, func) | |
| return { nModified } | |
| } | |
| } | |
| } |