Last active
September 14, 2017 09:16
-
-
Save dani8art/23e6dc6183c6226cd8f84f693d62434a to your computer and use it in GitHub Desktop.
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
'use strict'; | |
/** | |
* Module dependecies. | |
* */ | |
var jsyaml = require('js-yaml'); | |
var fs = require('fs'); | |
var path = require('path'); | |
/* | |
* Export functions and Objects | |
*/ | |
var config = { | |
addConfiguration: _addConfiguration | |
}; | |
module.exports = config; | |
/* | |
* Implement the functions | |
*/ | |
function _addConfiguration(uri, encoding) { | |
var configString = null; | |
if (!uri) { | |
throw new Error("Parameter URI is required"); | |
} else { | |
configString = fs.readFileSync(path.join(__dirname, uri), encoding); | |
} | |
var newConfigurations = jsyaml.safeLoad(configString)[process.env.NODE_ENV ? process.env.NODE_ENV : 'development']; | |
for (var c in newConfigurations) { | |
this[c] = newConfigurations[c]; | |
} | |
} | |
/* | |
* Setup default config location | |
*/ | |
config.addConfiguration('config.yaml', 'utf8'); |
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
'use strict'; | |
/** | |
* Database module | |
* @module bouncer-api/database | |
*/ | |
var Promise = require('bluebird'), | |
mongoose = require('mongoose'), | |
config = require('../config/config'); | |
mongoose.Promise = Promise; | |
var Schema = mongoose.Schema; | |
var host = process.env.DATABASE_HOST || config.database.host, | |
port = process.env.DATABASE_PORT || config.database.port, | |
name = process.env.DATABASE_NAME || config.database.name; | |
var username, password, url, | |
auth = process.env.DATABASE_AUTH === null || process.env.DATABASE_AUTH === undefined ? config.database.auth : (process.env.DATABASE_AUTH == 'true'); | |
url = 'mongodb://' + host + ':' + port + '/' + name; | |
var opt = { | |
useMongoClient: true | |
}; | |
//PREPARING SERVICE SCHEMA | |
// var urlRegExp = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/g; | |
var serviceSchema = require('./service-schema.json'); | |
// serviceSchema.url.match = urlRegExp; | |
module.exports = { | |
/** | |
* Class representing a Service an its db CRUD | |
* @class | |
*/ | |
Service: mongoose.model('Service', new Schema(serviceSchema, { | |
versionKey: false | |
}, { strict: true })), | |
/** | |
* Function for stablish database connection with mongo | |
* @function | |
* @return Promise | |
*/ | |
connect: connect, | |
/** | |
* Function for close database connection with mongo | |
* @function | |
* @return Promise | |
*/ | |
close: close | |
}; | |
function close() { | |
return new Promise((resolve, reject) => { | |
try { | |
mongoose.connection.close(); | |
resolve(); | |
} catch (e) { | |
reject(e); | |
} | |
}); | |
} | |
function connect() { | |
return new Promise((resolve, reject) => { | |
logger.info("Connecting mongo with URL => " + url); | |
logger.info("Connection mongo with OPT => " + JSON.stringify(opt)); | |
mongoose.connect(url, opt, err => { | |
err ? reject(err) : resolve(); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment