-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.DS_Store | ||
|
||
# IDEA specific | ||
.idea | ||
*.iml | ||
|
||
# Eclipse specific | ||
.jshintrc | ||
.project | ||
.settings/ | ||
|
||
# Logs | ||
logs/* | ||
*.log | ||
|
||
# Dependency directory | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
The MIT License (MIT) | ||
Copyright (c) 2016 KTH Royal Institute of Technology | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,96 @@ | ||
# kth-node-mongo | ||
Database connection module for Node.js applications using Mongoose. | ||
Database connection wrapping Mongoose (for MongoDB) | ||
|
||
This module connects to mongoDB using Mongoose default connection. | ||
|
||
To use this module | ||
|
||
## Connect to database | ||
|
||
1. Import module | ||
const nodeMongo =require('kth-node-mongo') | ||
|
||
1. Connect to mongoDB over SSL: | ||
nodeMongo.connect(options, sslOptions) or | ||
|
||
1. Connect to mongoDB without SSL: | ||
nodeMongo.connect(options) | ||
|
||
1. Use Mongoose schema and model to interact with mongoDB | ||
|
||
Function connect() returns a promise to be resolved upon completed connection or rejected on error. | ||
|
||
|
||
|
||
### Options | ||
|
||
* **dbUsername** (required) | ||
Credentials, the database user | ||
|
||
* **dbPassword** (required) | ||
Credentials, the password for the database user | ||
|
||
* **dbUri** (required) | ||
The URI for the mongoDb to connect to | ||
|
||
* **logger** (optional) | ||
A logger to use, defaults to stdout(console.log) | ||
|
||
* **reconnectTries** (optional) | ||
Max attempts to reconnect to mongoDB if connection is lost, defaults to 1000 | ||
|
||
* **reconnectInterval** (optional) | ||
Time between reconnect attempts in milliseconds, defaults to 30000 | ||
|
||
### SSL(TLS) Options | ||
|
||
* **ssl** (optional) | ||
Boolean flag if database connection shoold be encrypted or not | ||
|
||
* **authDatabase** (optional) | ||
If login credentials are used, specifies which database to use for authentication of user. Can also be sent as part of dbUri. | ||
|
||
* **caCerts** (optional) | ||
A list of buffers or strings containing the ca certificates (.pem) we accept when setting up the secure connetion. | ||
|
||
|
||
Example without secure database connction | ||
|
||
nodeMongo.connect({ | ||
dbUsername: 'user', | ||
dbPassword: 'himligt', | ||
dbUri: 'mongodb://localhost/le_database?authSource=authDB', | ||
logger: log | ||
}) | ||
.then(() => log.debug('Connected to Mongo')) | ||
.catch((err) => log.error(err)) | ||
|
||
Example with secure database connction | ||
|
||
nodeMongo.connect({ | ||
dbUsername: 'user', | ||
dbPassword: 'himligt', | ||
dbUri: 'mongodb://localhost/le_database', | ||
logger: log | ||
}, | ||
{ | ||
ssl: true, | ||
authenticationDatabase: 'authDB', | ||
sslCA: certs | ||
}) | ||
.then(() => log.debug('Connected to Mongo')) | ||
.catch((err) => log.error(err)) | ||
|
||
## Check status of connection | ||
|
||
1. Import module | ||
const nodeMongo =require('kth-node-mongo') | ||
|
||
1. Check connection: | ||
if (nodeMongo.isOk()) { | ||
// OK | ||
} else { | ||
// ERROR | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
const mongoose = require('mongoose') | ||
|
||
// setup Mongoose connection to mongoDB | ||
module.exports = { | ||
connect: _connect, | ||
isOk: _isOk | ||
} | ||
|
||
const stdLogger = { | ||
debug: consoleLogger('debug'), | ||
info: consoleLogger('info'), | ||
warn: consoleLogger('warn'), | ||
error: consoleLogger('error') | ||
} | ||
|
||
function consoleLogger (level) { | ||
return function (file, msg) { | ||
console.log(new Date().toISOString(), level, file, msg) | ||
} | ||
} | ||
|
||
var isOk | ||
function _isOk () { | ||
return isOk === true | ||
} | ||
|
||
function _connect (options, sslOptions) { | ||
mongoose.Promise = global.Promise | ||
var log = options.logger || stdLogger | ||
// Mongoose connect is called once by the app.js & connection established | ||
var dbUri = options.dbUri | ||
var dbOptions | ||
if (typeof sslOptions === 'object') { | ||
dbOptions = getMongoOptionsSsl(options, sslOptions) | ||
} else { | ||
dbOptions = getMongoOptions(options) | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
mongoose.connect(dbUri, dbOptions) | ||
log.info('Connect DB: ' + options.dbUri) | ||
|
||
mongoose.connection.on('error', dbErr => { | ||
log.warn({ err: dbErr }, 'DB connection error') | ||
isOk = false | ||
reject(dbErr) | ||
}) | ||
|
||
mongoose.connection.on('connected', () => { | ||
log.info('DB connection connected') | ||
isOk = true | ||
resolve(true) | ||
}) | ||
|
||
mongoose.connection.on('disconnected', () => { | ||
log.info('DB connection disconnected') | ||
isOk = false | ||
}) | ||
|
||
mongoose.connection.on('reconnected', () => { | ||
log.info('DB connection reconnected') | ||
isOk = true | ||
}) | ||
}) | ||
} | ||
|
||
function getMongoOptions (options) { | ||
var dbOptions = { | ||
db: { | ||
native_parser: true | ||
}, | ||
server: { | ||
socketOptions: { | ||
keepAlive: 1 | ||
} | ||
}, | ||
reconnectTries: options.reconnectTries || 1000, | ||
reconnectInterval: options.reconnectInterval || 30000, | ||
replset: { | ||
socketOptions: { | ||
keepAlive: 1 | ||
} | ||
}, | ||
user: options.dbUsername, | ||
pass: options.dbPassword | ||
} | ||
|
||
return dbOptions | ||
} | ||
|
||
function getMongoOptionsSsl (options, sslOptions) { | ||
var dbOptions = { | ||
db: { | ||
native_parser: true | ||
}, | ||
server: { | ||
socketOptions: { | ||
keepAlive: 1 | ||
}, | ||
ssl: sslOptions.ssl, | ||
authenticationDatabase: sslOptions.authDatabase, | ||
sslCA: sslOptions.sslCA | ||
}, | ||
reconnectTries: options.reconnectTries || 1000, | ||
reconnectInterval: options.reconnectInterval || 30000, | ||
replset: { | ||
socketOptions: { | ||
keepAlive: 1 | ||
} | ||
}, | ||
user: options.dbUsername, | ||
pass: options.dbPassword | ||
} | ||
|
||
return dbOptions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "kth-node-mongo", | ||
"version": "1.0.0", | ||
"description": "Database connection module for Node.js applications using Mongoose.", | ||
"main": "index.js", | ||
"scripts": { | ||
"codecheck": "standard", | ||
"preversion": "npm run codecheck", | ||
"postversion": "git push && git push --tags" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/KTH/kth-node-mongo" | ||
}, | ||
"author": "", | ||
"license": "MIT", | ||
"dependencies": { | ||
"mongoose": "^4.4.14" | ||
}, | ||
"devDependencies": { | ||
"standard": "^7.0.0" | ||
} | ||
} |