2018-02-04 05:53:13 +00:00
|
|
|
const _ = require('lodash')
|
2018-05-19 20:40:07 +00:00
|
|
|
const autoload = require('auto-load')
|
2017-07-23 03:56:46 +00:00
|
|
|
const path = require('path')
|
2017-07-29 21:33:08 +00:00
|
|
|
const Promise = require('bluebird')
|
2018-05-19 20:40:07 +00:00
|
|
|
const Knex = require('knex')
|
|
|
|
const Objection = require('objection')
|
2018-02-04 05:53:13 +00:00
|
|
|
|
2019-01-26 23:35:56 +00:00
|
|
|
const migrationSource = require('../db/migrator-source')
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-10-07 01:35:02 +00:00
|
|
|
|
2017-07-23 03:56:46 +00:00
|
|
|
/**
|
2018-08-16 01:57:28 +00:00
|
|
|
* ORM DB module
|
2017-07-23 03:56:46 +00:00
|
|
|
*/
|
|
|
|
module.exports = {
|
2018-05-19 20:40:07 +00:00
|
|
|
Objection,
|
|
|
|
knex: null,
|
2017-07-23 03:56:46 +00:00
|
|
|
/**
|
|
|
|
* Initialize DB
|
|
|
|
*
|
|
|
|
* @return {Object} DB instance
|
|
|
|
*/
|
|
|
|
init() {
|
|
|
|
let self = this
|
|
|
|
|
2018-05-19 20:40:07 +00:00
|
|
|
let dbClient = null
|
2018-08-16 01:57:28 +00:00
|
|
|
let dbConfig = (!_.isEmpty(process.env.WIKI_DB_CONNSTR)) ? process.env.WIKI_DB_CONNSTR : {
|
2018-03-05 20:49:36 +00:00
|
|
|
host: WIKI.config.db.host,
|
2018-05-19 20:40:07 +00:00
|
|
|
user: WIKI.config.db.user,
|
|
|
|
password: WIKI.config.db.pass,
|
|
|
|
database: WIKI.config.db.db,
|
2018-08-16 01:57:28 +00:00
|
|
|
port: WIKI.config.db.port
|
2018-05-19 20:40:07 +00:00
|
|
|
}
|
2017-07-23 03:56:46 +00:00
|
|
|
|
2018-05-19 20:40:07 +00:00
|
|
|
switch (WIKI.config.db.type) {
|
|
|
|
case 'postgres':
|
|
|
|
dbClient = 'pg'
|
|
|
|
break
|
2018-09-08 19:49:36 +00:00
|
|
|
case 'mariadb':
|
2018-05-19 20:40:07 +00:00
|
|
|
case 'mysql':
|
|
|
|
dbClient = 'mysql2'
|
2018-11-11 04:40:55 +00:00
|
|
|
|
|
|
|
// Fix mysql boolean handling...
|
|
|
|
dbConfig.typeCast = (field, next) => {
|
|
|
|
if (field.type === 'TINY' && field.length === 1) {
|
|
|
|
let value = field.string()
|
|
|
|
return value ? (value === '1') : null
|
|
|
|
}
|
|
|
|
return next()
|
|
|
|
}
|
2018-05-19 20:40:07 +00:00
|
|
|
break
|
|
|
|
case 'mssql':
|
|
|
|
dbClient = 'mssql'
|
|
|
|
break
|
|
|
|
case 'sqlite':
|
|
|
|
dbClient = 'sqlite3'
|
2018-08-16 01:57:28 +00:00
|
|
|
dbConfig = { filename: WIKI.config.db.storage }
|
2018-05-19 20:40:07 +00:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
WIKI.logger.error('Invalid DB Type')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2017-07-23 03:56:46 +00:00
|
|
|
|
2018-05-19 20:40:07 +00:00
|
|
|
this.knex = Knex({
|
|
|
|
client: dbClient,
|
|
|
|
useNullAsDefault: true,
|
2018-09-08 19:49:36 +00:00
|
|
|
asyncStackTraces: WIKI.IS_DEBUG,
|
2018-05-19 20:40:07 +00:00
|
|
|
connection: dbConfig,
|
2018-05-28 18:46:55 +00:00
|
|
|
pool: {
|
2019-05-13 05:15:27 +00:00
|
|
|
...WIKI.config.pool,
|
2018-05-28 18:46:55 +00:00
|
|
|
async afterCreate(conn, done) {
|
|
|
|
// -> Set Connection App Name
|
|
|
|
switch (WIKI.config.db.type) {
|
|
|
|
case 'postgres':
|
|
|
|
await conn.query(`set application_name = 'Wiki.js'`)
|
|
|
|
done()
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
done()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-05-19 20:40:07 +00:00
|
|
|
debug: WIKI.IS_DEBUG
|
2017-07-23 03:56:46 +00:00
|
|
|
})
|
|
|
|
|
2018-05-19 20:40:07 +00:00
|
|
|
Objection.Model.knex(this.knex)
|
2017-07-23 03:56:46 +00:00
|
|
|
|
2018-05-19 20:40:07 +00:00
|
|
|
// Load DB Models
|
2017-07-23 03:56:46 +00:00
|
|
|
|
2018-07-30 02:23:33 +00:00
|
|
|
const models = autoload(path.join(WIKI.SERVERPATH, 'models'))
|
2017-07-23 03:56:46 +00:00
|
|
|
|
2017-08-03 03:47:11 +00:00
|
|
|
// Set init tasks
|
|
|
|
let initTasks = {
|
2018-05-19 20:40:07 +00:00
|
|
|
// -> Migrate DB Schemas
|
|
|
|
async syncSchemas() {
|
|
|
|
return self.knex.migrate.latest({
|
2019-01-26 23:35:56 +00:00
|
|
|
tableName: 'migrations',
|
|
|
|
migrationSource
|
2017-08-03 03:47:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
let initTasksQueue = (WIKI.IS_MASTER) ? [
|
2018-05-28 18:46:55 +00:00
|
|
|
initTasks.syncSchemas
|
2017-08-03 03:47:11 +00:00
|
|
|
] : [
|
2018-05-28 18:46:55 +00:00
|
|
|
() => { return Promise.resolve() }
|
2017-08-03 03:47:11 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
// 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-05-19 20:40:07 +00:00
|
|
|
return {
|
|
|
|
...this,
|
|
|
|
...models
|
|
|
|
}
|
2017-07-23 03:56:46 +00:00
|
|
|
}
|
|
|
|
}
|