Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions .editorconfig

This file was deleted.

8 changes: 0 additions & 8 deletions .eslintrc

This file was deleted.

1 change: 0 additions & 1 deletion .gitattributes

This file was deleted.

3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
node_modules
.nyc_output
node_modules/
1 change: 0 additions & 1 deletion .npmignore

This file was deleted.

8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# mongo-express-req [![Build Status](https://travis-ci.org/floatdrop/mongo-express-req.svg?branch=master)](https://travis-ci.org/floatdrop/mongo-express-req)

> Get db connection in request


## Install

```
$ npm install --save mongo-express-req
$ npm install mongodb-express-req
```


Expand All @@ -15,11 +13,12 @@ $ npm install --save mongo-express-req
```js
var app = require('express')();

var expressMongoDb = require('mongo-express-req');
app.use(expressMongoDb('mongodb://localhost/test'));
var expressMongoDb = require('mongodb-express-req');
app.use(expressMongoDb('mongodb://localhost/db_name'));

app.get('/', function (req, res, next) {
req.db // => Db object
req.db // => Db object
req.db.collection('collection_name').insertOne({ email, password })
});
```

Expand All @@ -46,7 +45,10 @@ Default: `db`

Property on `request` object in which db connection will be stored.

## Credentials

Fork of [titanscouting/mongo-express-req](https://github.com/titanscouting/mongo-express-req)

## License

MIT © [Vsevolod Strukchinsky](http://github.com/floatdrop) and [Dev Singh](http://github.com/devksingh4)
MIT © [Roman Shmigelsky](http://github.com/roman-sh)
48 changes: 26 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
'use strict';
import whatwgUrl from 'whatwg-url'
import { MongoClient } from 'mongodb'

var MongoClient = require('mongodb').MongoClient;

module.exports = (uri, opts) => {
module.exports = (uri, opts = {}) => {
if (typeof uri !== 'string') {
throw new TypeError('Expected uri to be a string');
throw new TypeError('Expected uri to be a string')
}

opts = opts || {};
var property = opts.property || 'db';
delete opts.property;
const dbName = whatwgUrl.basicURLParse(uri).path.toString()
if (!dbName) {
throw new Error('Database name not found in connection string')
}

const property = opts.property || 'db'
delete opts.property

var connection;
let connection

return function expressMongoDb(req, res, next) {
return async function expressMongoDb(req, res, next) {
console.log(`mongodb_connection: ${!!connection}`)
if (!connection) {
connection = MongoClient.connect(uri, opts);
try {
const res = await MongoClient.connect(uri, opts)
connection = true
req[property] = res.db(dbName)
next()
}
catch (e) {
connection = false
next(e)
}
}

connection
.then(function (db) {
req[property] = db;
next();
})
.catch(function (err) {
connection = undefined;
next(err);
});
};
};
}
}
Loading