mongo-lazy-connect
TypeScript icon, indicating that this package has built-in type declarations

2.3.0 • Public • Published

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

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.3.0
    2
    • latest

Version History

Package Sidebar

Install

npm i mongo-lazy-connect

Weekly Downloads

2

Version

2.3.0

License

ISC

Unpacked Size

4.59 kB

Total Files

4

Last publish

Collaborators

  • caub