https://github.com/caub/mongo-lazy-connect
:banana: Wrapper for MongoClient.connect for a lazy access to db
https://github.com/caub/mongo-lazy-connect
Last synced: about 1 year ago
JSON representation
:banana: Wrapper for MongoClient.connect for a lazy access to db
- Host: GitHub
- URL: https://github.com/caub/mongo-lazy-connect
- Owner: caub
- Created: 2017-10-30T11:53:44.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-10-17T17:55:25.000Z (over 6 years ago)
- Last Synced: 2025-04-12T13:12:57.570Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 83 KB
- Stars: 8
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[![npm version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![coverage status][codecov-image]][codecov-url]
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
```js
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:
```js
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
```sh
docker exec mongo-lazy-connect mongo admin --eval 'db.createUser({user: "mlc", pwd: "hunter2", roles: [{role: "root", db: "admin"}]})'
```
[npm-image]: https://img.shields.io/npm/v/mongo-lazy-connect.svg?style=flat-square&maxAge=86400
[npm-url]: https://www.npmjs.com/package/mongo-lazy-connect
[travis-image]: https://img.shields.io/travis/caub/mongo-lazy-connect.svg?style=flat-square&maxAge=86400
[travis-url]: https://travis-ci.org/caub/mongo-lazy-connect
[codecov-image]: https://img.shields.io/codecov/c/github/caub/mongo-lazy-connect.svg?style=flat-square&maxAge=86400
[codecov-url]: https://codecov.io/gh/caub/mongo-lazy-connect