Created
July 30, 2019 08:25
-
-
Save jimmiehansson/38e2030440445690f54c090404902776 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
/** | |
* @description | |
* | |
* DOING: Should node modules and files required | |
* to separate them from the rest. | |
*/ | |
const Sequelize = require('sequelize'); | |
const SequelizeAutoBin = 'node_modules/sequelize-auto/bin/sequelize-auto'; | |
const Joi = require('joi'); | |
const LRU = require('lru-cache-node'); | |
const shell = require('shelljs'); | |
/** | |
* @description | |
* | |
* DOING: Should libraries and files required | |
* to separate them from the rest. | |
*/ | |
const language = require('../locale/default'); | |
const dataSchema = Joi.object().keys({ | |
handler: Joi.string().required(), | |
env: Joi.string().optional(), | |
config: Joi.object().required(), | |
}); | |
const cacheOptions = { | |
maxSize: 100, | |
maxAge: 1000 * 60 * 60, | |
}; | |
const cache = new LRU(cacheOptions); | |
/** | |
* @method sequelize | |
* | |
* @description | |
* | |
* DOING: Should return a sequelize connection instance object | |
* upon request using the retrieved configuration. | |
* | |
* {@link lib/sequelize} | |
* @returns {Promise} | |
*/ | |
module.exports = config => () => { | |
const { error, value } = Joi.validate(config, dataSchema, { abortEarly: false }); | |
if (error) { | |
throw error; | |
} | |
const { | |
database, | |
user, | |
pass, | |
extraConf, | |
} = value.config; | |
const defaultConfiguration = { | |
dialect: 'postgres', | |
logging: (process.env.DB_VERBOSE) ? (process.env.DB_VERBOSE.toLowerCase() === 'true') : false, | |
pool: { | |
max: 25, | |
min: 0, | |
idle: 2000, | |
evict: 5000, | |
handleDisconnects: true, | |
acquire: 2000, | |
}, | |
dialectOptions: { | |
requestTimeout: 5000, | |
}, | |
isolationLevel: 'READ UNCOMMITTED', | |
}; | |
const pgsqlConfig = Object.assign({}, extraConf, defaultConfiguration); | |
if (Array.isArray(extraConf.schema) && extraConf.directory) { | |
const cmd = []; | |
extraConf.port = (extraConf.port) ? extraConf.port : 5432; | |
/** | |
* @description | |
* | |
* CLARIFY: Should iterate the schemas from the configuration | |
* and write files to disk using SequelizeAutoBin | |
* expects model directory path and schema array. | |
* @returns {*.<stdout>} | |
*/ | |
const build = () => Object.values(extraConf.schema).forEach((schema, index) => { | |
cmd[index] = `${SequelizeAutoBin} -o ${extraConf.directory} -d ${database} -h ${extraConf.host} -s ${schema} -u ${user} -p ${extraConf.port} -x ${pass} -e ${extraConf.dialect}`; | |
return shell.exec(cmd[index], (code, stdout, stderror) => | |
((code !== 0 || stderror) ? process.exit(language.ERROR_FS) : process.exit(stdout))); | |
}); | |
return setImmediate(build); | |
} | |
let db = cache.get(pgsqlConfig); | |
if (!db) { | |
db = new Sequelize(database, user, pass, pgsqlConfig); | |
cache.set(pgsqlConfig, db); | |
} | |
return db; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment