Skip to content

caub/mongo-lazy-connect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm version build status coverage status

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"}]})'

About

🍌 Wrapper for MongoClient.connect for a lazy access to db

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published