mongo-lazy-connect
v2.3.0
Published
MongoClient.connect alternative
Downloads
209
Readme
Similarly to other DBs (like const pool=new pg.Pool('pg://...'); pool.query('select ..').then(console.log)
), let's allow a lazy access to the MongoDB driver pool
const db = require('mongo-lazy-connect')('mongodb://localhost:27017/connect-test');
const coll = db.collection('foo');
coll.insertOne({bar: 2}).then(() => {
coll.find({}).limit(5).toArray().then(console.log);
});
is equivalent to:
const mongo = require('mongodb');
mongo('mongodb://localhost:27017')
.then(client => {
const coll = client.db('connect-test').collection('foo');
coll.insertOne({bar: 2}).then(() => {
coll.find({}).limit(5).toArray().then(console.log);
});
});
Note: for local testing, add a user on the DB
docker exec mongo-lazy-connect mongo admin --eval 'db.createUser({user: "mlc", pwd: "hunter2", roles: [{role: "root", db: "admin"}]})'