2018-02-04 05:53:13 +00:00
|
|
|
const _ = require('lodash')
|
2017-07-23 03:56:46 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
2017-07-29 21:33:08 +00:00
|
|
|
const Promise = require('bluebird')
|
2017-10-07 01:35:02 +00:00
|
|
|
const Sequelize = require('sequelize')
|
2018-02-04 05:53:13 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-10-07 01:35:02 +00:00
|
|
|
|
|
|
|
const operatorsAliases = {
|
2018-02-04 05:53:13 +00:00
|
|
|
$eq: Sequelize.Op.eq,
|
|
|
|
$ne: Sequelize.Op.ne,
|
|
|
|
$gte: Sequelize.Op.gte,
|
|
|
|
$gt: Sequelize.Op.gt,
|
|
|
|
$lte: Sequelize.Op.lte,
|
|
|
|
$lt: Sequelize.Op.lt,
|
|
|
|
$not: Sequelize.Op.not,
|
|
|
|
$in: Sequelize.Op.in,
|
|
|
|
$notIn: Sequelize.Op.notIn,
|
|
|
|
$is: Sequelize.Op.is,
|
|
|
|
$like: Sequelize.Op.like,
|
|
|
|
$notLike: Sequelize.Op.notLike,
|
|
|
|
$iLike: Sequelize.Op.iLike,
|
|
|
|
$notILike: Sequelize.Op.notILike,
|
|
|
|
$regexp: Sequelize.Op.regexp,
|
|
|
|
$notRegexp: Sequelize.Op.notRegexp,
|
|
|
|
$iRegexp: Sequelize.Op.iRegexp,
|
|
|
|
$notIRegexp: Sequelize.Op.notIRegexp,
|
|
|
|
$between: Sequelize.Op.between,
|
|
|
|
$notBetween: Sequelize.Op.notBetween,
|
|
|
|
$overlap: Sequelize.Op.overlap,
|
|
|
|
$contains: Sequelize.Op.contains,
|
|
|
|
$contained: Sequelize.Op.contained,
|
|
|
|
$adjacent: Sequelize.Op.adjacent,
|
|
|
|
$strictLeft: Sequelize.Op.strictLeft,
|
|
|
|
$strictRight: Sequelize.Op.strictRight,
|
|
|
|
$noExtendRight: Sequelize.Op.noExtendRight,
|
|
|
|
$noExtendLeft: Sequelize.Op.noExtendLeft,
|
|
|
|
$and: Sequelize.Op.and,
|
|
|
|
$or: Sequelize.Op.or,
|
|
|
|
$any: Sequelize.Op.any,
|
|
|
|
$all: Sequelize.Op.all,
|
|
|
|
$values: Sequelize.Op.values,
|
|
|
|
$col: Sequelize.Op.col
|
2017-10-07 01:35:02 +00:00
|
|
|
}
|
2017-07-23 03:56:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PostgreSQL DB module
|
|
|
|
*/
|
|
|
|
module.exports = {
|
2017-10-07 01:35:02 +00:00
|
|
|
Sequelize,
|
|
|
|
Op: Sequelize.Op,
|
2017-07-23 03:56:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize DB
|
|
|
|
*
|
|
|
|
* @return {Object} DB instance
|
|
|
|
*/
|
|
|
|
init() {
|
|
|
|
let self = this
|
2018-03-05 20:49:36 +00:00
|
|
|
let dbModelsPath = path.join(WIKI.SERVERPATH, 'models')
|
2017-07-23 03:56:46 +00:00
|
|
|
|
|
|
|
// Define Sequelize instance
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
this.inst = new this.Sequelize(WIKI.config.db.db, WIKI.config.db.user, WIKI.config.db.pass, {
|
|
|
|
host: WIKI.config.db.host,
|
|
|
|
port: WIKI.config.db.port,
|
2018-03-25 02:35:47 +00:00
|
|
|
dialect: WIKI.config.db.type,
|
|
|
|
storage: WIKI.config.db.storage,
|
2017-07-23 03:56:46 +00:00
|
|
|
pool: {
|
|
|
|
max: 10,
|
|
|
|
min: 0,
|
|
|
|
idle: 10000
|
2017-07-29 21:33:08 +00:00
|
|
|
},
|
2018-03-05 20:49:36 +00:00
|
|
|
logging: log => { WIKI.logger.log('debug', log) },
|
2017-10-07 01:35:02 +00:00
|
|
|
operatorsAliases
|
2017-07-23 03:56:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Attempt to connect and authenticate to DB
|
|
|
|
|
2018-02-04 05:53:13 +00:00
|
|
|
this.inst.authenticate().then(() => {
|
2018-03-25 02:35:47 +00:00
|
|
|
WIKI.logger.info(`Database (${WIKI.config.db.type}) connection: [ OK ]`)
|
2017-07-23 03:56:46 +00:00
|
|
|
}).catch(err => {
|
2018-03-25 02:35:47 +00:00
|
|
|
WIKI.logger.error(`Failed to connect to ${WIKI.config.db.type} instance.`)
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.error(err)
|
2017-11-13 04:48:19 +00:00
|
|
|
process.exit(1)
|
2017-07-23 03:56:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Load DB Models
|
|
|
|
|
|
|
|
fs
|
|
|
|
.readdirSync(dbModelsPath)
|
2017-10-08 02:44:35 +00:00
|
|
|
.filter(file => {
|
2017-07-23 03:56:46 +00:00
|
|
|
return (file.indexOf('.') !== 0 && file.indexOf('_') !== 0)
|
|
|
|
})
|
2017-10-08 02:44:35 +00:00
|
|
|
.forEach(file => {
|
2017-07-23 03:56:46 +00:00
|
|
|
let modelName = _.upperFirst(_.camelCase(_.split(file, '.')[0]))
|
|
|
|
self[modelName] = self.inst.import(path.join(dbModelsPath, file))
|
|
|
|
})
|
|
|
|
|
|
|
|
// Associate DB Models
|
|
|
|
|
|
|
|
require(path.join(dbModelsPath, '_relations.js'))(self)
|
|
|
|
|
2017-08-03 03:47:11 +00:00
|
|
|
// Set init tasks
|
|
|
|
|
|
|
|
let initTasks = {
|
|
|
|
// -> Sync DB Schemas
|
|
|
|
syncSchemas() {
|
|
|
|
return self.inst.sync({
|
2017-10-08 21:21:36 +00:00
|
|
|
force: false,
|
2018-03-05 20:49:36 +00:00
|
|
|
logging: log => { WIKI.logger.log('debug', log) }
|
2017-08-03 03:47:11 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
// -> Set Connection App Name
|
|
|
|
setAppName() {
|
2018-03-25 02:35:47 +00:00
|
|
|
switch (WIKI.config.db.type) {
|
|
|
|
case 'postgres':
|
|
|
|
return self.inst.query(`set application_name = 'WIKI.js'`, { raw: true })
|
|
|
|
}
|
2017-08-03 03:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
let initTasksQueue = (WIKI.IS_MASTER) ? [
|
2017-08-03 03:47:11 +00:00
|
|
|
initTasks.syncSchemas,
|
|
|
|
initTasks.setAppName
|
|
|
|
] : [
|
|
|
|
initTasks.setAppName
|
|
|
|
]
|
|
|
|
|
|
|
|
// Perform init tasks
|
|
|
|
|
2018-02-04 05:53:13 +00:00
|
|
|
this.onReady = Promise.each(initTasksQueue, t => t()).return(true)
|
2017-07-23 03:56:46 +00:00
|
|
|
|
2018-02-04 05:53:13 +00:00
|
|
|
return this
|
2017-07-23 03:56:46 +00:00
|
|
|
}
|
|
|
|
}
|