mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.2 KiB
133 lines
3.2 KiB
7 years ago
|
const _ = require('lodash')
|
||
7 years ago
|
const fs = require('fs')
|
||
|
const path = require('path')
|
||
7 years ago
|
const Promise = require('bluebird')
|
||
7 years ago
|
const Sequelize = require('sequelize')
|
||
7 years ago
|
|
||
|
/* global wiki */
|
||
7 years ago
|
|
||
|
const operatorsAliases = {
|
||
7 years ago
|
$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
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
|
/**
|
||
|
* PostgreSQL DB module
|
||
|
*/
|
||
|
module.exports = {
|
||
7 years ago
|
Sequelize,
|
||
|
Op: Sequelize.Op,
|
||
7 years ago
|
|
||
|
/**
|
||
|
* Initialize DB
|
||
|
*
|
||
|
* @return {Object} DB instance
|
||
|
*/
|
||
|
init() {
|
||
|
let self = this
|
||
|
let dbModelsPath = path.join(wiki.SERVERPATH, 'models')
|
||
|
|
||
|
// Define Sequelize instance
|
||
|
|
||
7 years ago
|
this.inst = new this.Sequelize(wiki.config.db.db, wiki.config.db.user, wiki.config.db.pass, {
|
||
7 years ago
|
host: wiki.config.db.host,
|
||
|
port: wiki.config.db.port,
|
||
|
dialect: 'postgres',
|
||
|
pool: {
|
||
|
max: 10,
|
||
|
min: 0,
|
||
|
idle: 10000
|
||
7 years ago
|
},
|
||
7 years ago
|
logging: log => { wiki.logger.log('debug', log) },
|
||
7 years ago
|
operatorsAliases
|
||
7 years ago
|
})
|
||
|
|
||
|
// Attempt to connect and authenticate to DB
|
||
|
|
||
7 years ago
|
this.inst.authenticate().then(() => {
|
||
7 years ago
|
wiki.logger.info('Database (PostgreSQL) connection: [ OK ]')
|
||
7 years ago
|
}).catch(err => {
|
||
7 years ago
|
wiki.logger.error('Failed to connect to PostgreSQL instance.')
|
||
7 years ago
|
wiki.logger.error(err)
|
||
|
process.exit(1)
|
||
7 years ago
|
})
|
||
|
|
||
|
// Load DB Models
|
||
|
|
||
|
fs
|
||
|
.readdirSync(dbModelsPath)
|
||
7 years ago
|
.filter(file => {
|
||
7 years ago
|
return (file.indexOf('.') !== 0 && file.indexOf('_') !== 0)
|
||
|
})
|
||
7 years ago
|
.forEach(file => {
|
||
7 years ago
|
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)
|
||
|
|
||
7 years ago
|
// Set init tasks
|
||
|
|
||
|
let initTasks = {
|
||
|
// -> Sync DB Schemas
|
||
|
syncSchemas() {
|
||
|
return self.inst.sync({
|
||
7 years ago
|
force: false,
|
||
7 years ago
|
logging: log => { wiki.logger.log('debug', log) }
|
||
7 years ago
|
})
|
||
|
},
|
||
|
// -> Set Connection App Name
|
||
|
setAppName() {
|
||
|
return self.inst.query(`set application_name = 'Wiki.js'`, { raw: true })
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let initTasksQueue = (wiki.IS_MASTER) ? [
|
||
|
initTasks.syncSchemas,
|
||
|
initTasks.setAppName
|
||
|
] : [
|
||
|
initTasks.setAppName
|
||
|
]
|
||
|
|
||
|
// Perform init tasks
|
||
|
|
||
7 years ago
|
this.onReady = Promise.each(initTasksQueue, t => t()).return(true)
|
||
7 years ago
|
|
||
7 years ago
|
return this
|
||
7 years ago
|
}
|
||
|
}
|