A thin wrapper 📦 to MongoDB node.js driver using ES6.
npm install mongolux
const { bootstrap, db } = require('mongolux');
....
await bootstrap(require('./path/to/config/database'));
This is what your database.js file should look like
module.exports = {
database1: {
uri: 'mongodb://127.0.0.1:4000/?retryWrites=true',
db: 'db1',
useNewUrlParser: true,
poolSize: 5,
ssl: false,
sslValidate: true,
sslCA: null,
sslCert: null,
sslKey: null,
sslPass: null,
autoReconnect: true,
noDelay: true,
keepAlive: 30000,
connectTimeoutMS: 30000,
socketTimeoutMS: 360000,
reconnectTries: 30,
reconnectInterval: 1000,
ha: true,
haInterval: 10000,
replicaSet: null,
secondaryAcceptableLatencyMS: 15,
acceptableLatencyMS: 15,
connectWithNoPrimary: false,
authSource: null,
w: null,
wtimeout: null,
j: false,
forceServerObjectId: false,
serializeFunctions: false,
ignoreUndefined: false,
raw: false,
promoteLongs: true,
promoteBuffers: false,
promoteValues: true,
domainsEnabled: false,
bufferMaxEntries: -1,
readPreference: null,
pkFactory: null,
promiseLibrary: null,
readConcern: null,
maxStalenessSeconds: null,
appname: null,
loggerLevel: null,
logger: null,
},
database2: {
uri: 'mongodb://127.0.0.1:4001/?retryWrites=true',
db: 'db2',
useNewUrlParser: true,
poolSize: 5,
ssl: false,
sslValidate: true,
sslCA: null,
sslCert: null,
sslKey: null,
sslPass: null,
autoReconnect: true,
noDelay: true,
keepAlive: 30000,
connectTimeoutMS: 30000,
socketTimeoutMS: 360000,
reconnectTries: 30,
reconnectInterval: 1000,
ha: true,
haInterval: 10000,
replicaSet: null,
secondaryAcceptableLatencyMS: 15,
acceptableLatencyMS: 15,
connectWithNoPrimary: false,
authSource: null,
w: null,
wtimeout: null,
j: false,
forceServerObjectId: false,
serializeFunctions: false,
ignoreUndefined: false,
raw: false,
promoteLongs: true,
promoteBuffers: false,
promoteValues: true,
domainsEnabled: false,
bufferMaxEntries: -1,
readPreference: null,
pkFactory: null,
promiseLibrary: null,
readConcern: null,
maxStalenessSeconds: null,
appname: null,
loggerLevel: null,
logger: null,
},
}
All options
passed to MongoClient is supported except for db
(mongolux uses this internally)
const { db } = require('mongolux');
...
const users = await db('database1').collection('users').find({}).toArray();
.collection('users').find({}).toArray();
Seems familiar? Well, those are just functions from the MongoDB node.js driver API. Everything is exactly that because we just wrapped the mongodb package.
db().forEach((conn, name) => {
conn.on('close', () => console.log(`Disconnected from mongolux ${name}...`))
conn.on('reconnect', () => console.log(`Reconnected from mongolux ${name}...`))
})
I use this on my site. If you need any help, please file an issue @Github