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')
|
2019-12-30 21:18:23 +00:00
|
|
|
const fs = require('fs')
|
2018-05-19 20:40:07 +00:00
|
|
|
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')
|
2019-11-04 02:59:58 +00:00
|
|
|
const migrateFromBeta = require('../db/beta')
|
2019-01-26 23:35:56 +00:00
|
|
|
|
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,
|
2020-02-03 02:55:29 +00:00
|
|
|
listener: null,
|
2017-07-23 03:56:46 +00:00
|
|
|
/**
|
|
|
|
* Initialize DB
|
|
|
|
*
|
|
|
|
* @return {Object} DB instance
|
|
|
|
*/
|
|
|
|
init() {
|
|
|
|
let self = this
|
|
|
|
|
2020-04-20 22:47:06 +00:00
|
|
|
// Fetch DB Config
|
|
|
|
|
2018-05-19 20:40:07 +00:00
|
|
|
let dbClient = null
|
2019-06-02 01:38:21 +00:00
|
|
|
let dbConfig = (!_.isEmpty(process.env.DATABASE_URL)) ? process.env.DATABASE_URL : {
|
2020-03-29 17:42:55 +00:00
|
|
|
host: WIKI.config.db.host.toString(),
|
|
|
|
user: WIKI.config.db.user.toString(),
|
|
|
|
password: WIKI.config.db.pass.toString(),
|
|
|
|
database: WIKI.config.db.db.toString(),
|
2019-06-04 07:14:07 +00:00
|
|
|
port: WIKI.config.db.port
|
2018-05-19 20:40:07 +00:00
|
|
|
}
|
2017-07-23 03:56:46 +00:00
|
|
|
|
2020-04-20 22:47:06 +00:00
|
|
|
// Handle SSL Options
|
|
|
|
|
|
|
|
let dbUseSSL = (WIKI.config.db.ssl === true || WIKI.config.db.ssl === 'true' || WIKI.config.db.ssl === 1 || WIKI.config.db.ssl === '1')
|
2019-12-30 21:18:23 +00:00
|
|
|
let sslOptions = null
|
2020-04-20 22:47:06 +00:00
|
|
|
if (dbUseSSL && _.isPlainObject(dbConfig) && _.get(WIKI.config.db, 'sslOptions.auto', null) === false) {
|
|
|
|
sslOptions = WIKI.config.db.sslOptions
|
2020-10-14 15:16:27 +00:00
|
|
|
sslOptions.rejectUnauthorized = sslOptions.rejectUnauthorized !== false
|
2020-04-20 22:47:06 +00:00
|
|
|
if (sslOptions.ca && sslOptions.ca.indexOf('-----') !== 0) {
|
2019-12-30 21:18:23 +00:00
|
|
|
sslOptions.ca = fs.readFileSync(path.resolve(WIKI.ROOTPATH, sslOptions.ca))
|
|
|
|
}
|
|
|
|
if (sslOptions.cert) {
|
|
|
|
sslOptions.cert = fs.readFileSync(path.resolve(WIKI.ROOTPATH, sslOptions.cert))
|
|
|
|
}
|
|
|
|
if (sslOptions.key) {
|
|
|
|
sslOptions.key = fs.readFileSync(path.resolve(WIKI.ROOTPATH, sslOptions.key))
|
|
|
|
}
|
|
|
|
if (sslOptions.pfx) {
|
|
|
|
sslOptions.pfx = fs.readFileSync(path.resolve(WIKI.ROOTPATH, sslOptions.pfx))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sslOptions = true
|
|
|
|
}
|
2019-06-04 07:14:07 +00:00
|
|
|
|
2020-04-20 22:47:06 +00:00
|
|
|
// Handle inline SSL CA Certificate mode
|
2020-04-21 02:00:59 +00:00
|
|
|
if (!_.isEmpty(process.env.DB_SSL_CA)) {
|
|
|
|
const chunks = []
|
|
|
|
for (let i = 0, charsLength = process.env.DB_SSL_CA.length; i < charsLength; i += 64) {
|
|
|
|
chunks.push(process.env.DB_SSL_CA.substring(i, i + 64))
|
|
|
|
}
|
|
|
|
|
2020-04-20 22:47:06 +00:00
|
|
|
dbUseSSL = true
|
|
|
|
sslOptions = {
|
|
|
|
rejectUnauthorized: true,
|
2020-04-21 02:00:59 +00:00
|
|
|
ca: '-----BEGIN CERTIFICATE-----\n' + chunks.join('\n') + '\n-----END CERTIFICATE-----\n'
|
2020-04-20 22:47:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Engine-specific config
|
2018-05-19 20:40:07 +00:00
|
|
|
switch (WIKI.config.db.type) {
|
|
|
|
case 'postgres':
|
|
|
|
dbClient = 'pg'
|
2019-06-04 07:14:07 +00:00
|
|
|
|
|
|
|
if (dbUseSSL && _.isPlainObject(dbConfig)) {
|
2020-04-20 21:51:27 +00:00
|
|
|
dbConfig.ssl = (sslOptions === true) ? { rejectUnauthorized: true } : sslOptions
|
2019-06-04 07:14:07 +00:00
|
|
|
}
|
2018-05-19 20:40:07 +00:00
|
|
|
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
|
|
|
|
2019-06-04 07:14:07 +00:00
|
|
|
if (dbUseSSL && _.isPlainObject(dbConfig)) {
|
2019-12-30 21:18:23 +00:00
|
|
|
dbConfig.ssl = sslOptions
|
2019-06-04 07:14:07 +00:00
|
|
|
}
|
|
|
|
|
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'
|
2019-06-04 07:14:07 +00:00
|
|
|
|
|
|
|
if (_.isPlainObject(dbConfig)) {
|
|
|
|
dbConfig.appName = 'Wiki.js'
|
2020-10-03 20:46:04 +00:00
|
|
|
_.set(dbConfig, 'options.appName', 'Wiki.js')
|
|
|
|
|
|
|
|
dbConfig.enableArithAbort = true
|
|
|
|
_.set(dbConfig, 'options.enableArithAbort', true)
|
|
|
|
|
2019-06-04 07:14:07 +00:00
|
|
|
if (dbUseSSL) {
|
|
|
|
dbConfig.encrypt = true
|
2020-10-03 20:46:04 +00:00
|
|
|
_.set(dbConfig, 'options.encrypt', true)
|
2019-06-04 07:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-19 20:40:07 +00:00
|
|
|
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
|
|
|
|
2020-04-20 22:47:06 +00:00
|
|
|
// Initialize Knex
|
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'`)
|
2023-01-30 04:09:33 +00:00
|
|
|
// -> Set schema if it's not public
|
2021-09-25 02:56:44 +00:00
|
|
|
if (WIKI.config.db.schema && WIKI.config.db.schema !== 'public') {
|
|
|
|
await conn.query(`set search_path TO ${WIKI.config.db.schema}, public;`)
|
|
|
|
}
|
2018-05-28 18:46:55 +00:00
|
|
|
done()
|
|
|
|
break
|
2020-12-13 19:57:20 +00:00
|
|
|
case 'mysql':
|
|
|
|
await conn.promise().query(`set autocommit = 1`)
|
|
|
|
done()
|
|
|
|
break
|
2018-05-28 18:46:55 +00:00
|
|
|
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
|
2019-09-05 02:23:44 +00:00
|
|
|
let conAttempts = 0
|
2017-08-03 03:47:11 +00:00
|
|
|
let initTasks = {
|
2019-09-05 02:23:44 +00:00
|
|
|
// -> Attempt initial connection
|
2019-11-04 02:59:58 +00:00
|
|
|
async connect () {
|
2019-09-05 02:23:44 +00:00
|
|
|
try {
|
|
|
|
WIKI.logger.info('Connecting to database...')
|
|
|
|
await self.knex.raw('SELECT 1 + 1;')
|
|
|
|
WIKI.logger.info('Database Connection Successful [ OK ]')
|
|
|
|
} catch (err) {
|
|
|
|
if (conAttempts < 10) {
|
2019-12-30 21:45:25 +00:00
|
|
|
if (err.code) {
|
|
|
|
WIKI.logger.error(`Database Connection Error: ${err.code} ${err.address}:${err.port}`)
|
|
|
|
} else {
|
|
|
|
WIKI.logger.error(`Database Connection Error: ${err.message}`)
|
|
|
|
}
|
2019-09-05 02:23:44 +00:00
|
|
|
WIKI.logger.warn(`Will retry in 3 seconds... [Attempt ${++conAttempts} of 10]`)
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 3000))
|
|
|
|
await initTasks.connect()
|
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2019-11-04 02:59:58 +00:00
|
|
|
},
|
|
|
|
// -> Migrate DB Schemas
|
|
|
|
async syncSchemas () {
|
|
|
|
return self.knex.migrate.latest({
|
|
|
|
tableName: 'migrations',
|
|
|
|
migrationSource
|
|
|
|
})
|
|
|
|
},
|
|
|
|
// -> Migrate DB Schemas from beta
|
|
|
|
async migrateFromBeta () {
|
|
|
|
return migrateFromBeta.migrate(self.knex)
|
2017-08-03 03:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
let initTasksQueue = (WIKI.IS_MASTER) ? [
|
2019-09-05 02:23:44 +00:00
|
|
|
initTasks.connect,
|
2019-11-04 02:59:58 +00:00
|
|
|
initTasks.migrateFromBeta,
|
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
|
|
|
|
|
2019-12-30 21:45:25 +00:00
|
|
|
WIKI.logger.info(`Using database driver ${dbClient} for ${WIKI.config.db.type} [ OK ]`)
|
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
|
|
|
|
}
|
2020-02-03 02:55:29 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Subscribe to database LISTEN / NOTIFY for multi-instances events
|
|
|
|
*/
|
|
|
|
async subscribeToNotifications () {
|
|
|
|
const useHA = (WIKI.config.ha === true || WIKI.config.ha === 'true' || WIKI.config.ha === 1 || WIKI.config.ha === '1')
|
|
|
|
if (!useHA) {
|
|
|
|
return
|
|
|
|
} else if (WIKI.config.db.type !== 'postgres') {
|
|
|
|
WIKI.logger.warn(`Database engine doesn't support pub/sub. Will not handle concurrent instances: [ DISABLED ]`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const PGPubSub = require('pg-pubsub')
|
|
|
|
|
|
|
|
this.listener = new PGPubSub(this.knex.client.connectionSettings, {
|
|
|
|
log (ev) {
|
|
|
|
WIKI.logger.debug(ev)
|
|
|
|
}
|
|
|
|
})
|
2020-04-20 00:26:26 +00:00
|
|
|
|
|
|
|
// -> Outbound events handling
|
|
|
|
|
2020-02-03 02:55:29 +00:00
|
|
|
this.listener.addChannel('wiki', payload => {
|
2020-04-20 02:41:19 +00:00
|
|
|
if (_.has(payload, 'event') && payload.source !== WIKI.INSTANCE_ID) {
|
|
|
|
WIKI.logger.info(`Received event ${payload.event} from instance ${payload.source}: [ OK ]`)
|
2020-04-20 00:26:26 +00:00
|
|
|
WIKI.events.inbound.emit(payload.event, payload.value)
|
2020-02-03 02:55:29 +00:00
|
|
|
}
|
|
|
|
})
|
2020-04-20 00:26:26 +00:00
|
|
|
WIKI.events.outbound.onAny(this.notifyViaDB)
|
|
|
|
|
|
|
|
// -> Listen to inbound events
|
|
|
|
|
|
|
|
WIKI.auth.subscribeToEvents()
|
|
|
|
WIKI.configSvc.subscribeToEvents()
|
|
|
|
WIKI.models.pages.subscribeToEvents()
|
2020-02-03 02:55:29 +00:00
|
|
|
|
|
|
|
WIKI.logger.info(`High-Availability Listener initialized successfully: [ OK ]`)
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Unsubscribe from database LISTEN / NOTIFY
|
|
|
|
*/
|
|
|
|
async unsubscribeToNotifications () {
|
|
|
|
if (this.listener) {
|
2020-04-20 00:26:26 +00:00
|
|
|
WIKI.events.outbound.offAny(this.notifyViaDB)
|
|
|
|
WIKI.events.inbound.removeAllListeners()
|
2020-02-03 02:55:29 +00:00
|
|
|
this.listener.close()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Publish event via database NOTIFY
|
|
|
|
*
|
|
|
|
* @param {string} event Event fired
|
|
|
|
* @param {object} value Payload of the event
|
|
|
|
*/
|
|
|
|
notifyViaDB (event, value) {
|
2020-04-20 02:41:19 +00:00
|
|
|
WIKI.models.listener.publish('wiki', {
|
2020-02-03 02:55:29 +00:00
|
|
|
source: WIKI.INSTANCE_ID,
|
|
|
|
event,
|
|
|
|
value
|
|
|
|
})
|
2017-07-23 03:56:46 +00:00
|
|
|
}
|
|
|
|
}
|