Skip to content

Instantly share code, notes, and snippets.

@dani8art

dani8art/config.js

Last active Sep 14, 2017
Embed
What would you like to do?
'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');
'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
You can’t perform that action at this time.