db.js 7.63 KB
Newer Older
1
const _ = require('lodash')
2
const autoload = require('auto-load')
3
const path = require('path')
4
const Promise = require('bluebird')
5
const Knex = require('knex')
NGPixel's avatar
NGPixel committed
6
const fs = require('fs')
7
const Objection = require('objection')
8

Nicolas Giard's avatar
Nicolas Giard committed
9
const migrationSource = require('../db/migrator-source')
10
const migrateFromBeta = require('../db/beta')
Nicolas Giard's avatar
Nicolas Giard committed
11

12
/* global WIKI */
13

14
/**
15
 * ORM DB module
16 17
 */
module.exports = {
18 19
  Objection,
  knex: null,
20
  listener: null,
21 22 23 24 25 26 27 28
  /**
   * Initialize DB
   *
   * @return     {Object}  DB instance
   */
  init() {
    let self = this

NGPixel's avatar
NGPixel committed
29 30
    // Fetch DB Config

31
    let dbClient = null
Nick's avatar
Nick committed
32
    let dbConfig = (!_.isEmpty(process.env.DATABASE_URL)) ? process.env.DATABASE_URL : {
33 34 35 36
      host: WIKI.config.db.host.toString(),
      user: WIKI.config.db.user.toString(),
      password: WIKI.config.db.pass.toString(),
      database: WIKI.config.db.db.toString(),
37
      port: WIKI.config.db.port
38
    }
39

NGPixel's avatar
NGPixel committed
40 41 42
    // 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')
NGPixel's avatar
NGPixel committed
43
    let sslOptions = null
NGPixel's avatar
NGPixel committed
44 45 46 47 48
    if (dbUseSSL && _.isPlainObject(dbConfig) && _.get(WIKI.config.db, 'sslOptions.auto', null) === false) {
      sslOptions = WIKI.config.db.sslOptions
      // eslint-disable-next-line no-unneeded-ternary
      sslOptions.rejectUnauthorized = sslOptions.rejectUnauthorized === false ? false : true
      if (sslOptions.ca && sslOptions.ca.indexOf('-----') !== 0) {
NGPixel's avatar
NGPixel committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62
        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
    }
63

NGPixel's avatar
NGPixel committed
64
    // Handle inline SSL CA Certificate mode
65 66 67 68 69 70
    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))
      }

NGPixel's avatar
NGPixel committed
71 72 73
      dbUseSSL = true
      sslOptions = {
        rejectUnauthorized: true,
74
        ca: '-----BEGIN CERTIFICATE-----\n' + chunks.join('\n') + '\n-----END CERTIFICATE-----\n'
NGPixel's avatar
NGPixel committed
75 76 77 78
      }
    }

    // Engine-specific config
79 80 81
    switch (WIKI.config.db.type) {
      case 'postgres':
        dbClient = 'pg'
82 83

        if (dbUseSSL && _.isPlainObject(dbConfig)) {
84
          dbConfig.ssl = (sslOptions === true) ? { rejectUnauthorized: true } : sslOptions
85
        }
86
        break
87
      case 'mariadb':
88 89
      case 'mysql':
        dbClient = 'mysql2'
90

91
        if (dbUseSSL && _.isPlainObject(dbConfig)) {
NGPixel's avatar
NGPixel committed
92
          dbConfig.ssl = sslOptions
93 94
        }

95 96 97 98 99 100 101 102
        // 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()
        }
103 104 105
        break
      case 'mssql':
        dbClient = 'mssql'
106 107 108 109 110 111 112

        if (_.isPlainObject(dbConfig)) {
          dbConfig.appName = 'Wiki.js'
          if (dbUseSSL) {
            dbConfig.encrypt = true
          }
        }
113 114 115
        break
      case 'sqlite':
        dbClient = 'sqlite3'
116
        dbConfig = { filename: WIKI.config.db.storage }
117 118 119 120 121
        break
      default:
        WIKI.logger.error('Invalid DB Type')
        process.exit(1)
    }
122

NGPixel's avatar
NGPixel committed
123
    // Initialize Knex
124 125 126
    this.knex = Knex({
      client: dbClient,
      useNullAsDefault: true,
127
      asyncStackTraces: WIKI.IS_DEBUG,
128
      connection: dbConfig,
129
      pool: {
130
        ...WIKI.config.pool,
131 132 133 134 135 136 137 138 139 140 141 142 143
        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
          }
        }
      },
144
      debug: WIKI.IS_DEBUG
145 146
    })

147
    Objection.Model.knex(this.knex)
148

149
    // Load DB Models
150

151
    const models = autoload(path.join(WIKI.SERVERPATH, 'models'))
152

NGPixel's avatar
NGPixel committed
153
    // Set init tasks
154
    let conAttempts = 0
NGPixel's avatar
NGPixel committed
155
    let initTasks = {
156
      // -> Attempt initial connection
157
      async connect () {
158 159 160 161 162 163
        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) {
164 165 166 167 168
            if (err.code) {
              WIKI.logger.error(`Database Connection Error: ${err.code} ${err.address}:${err.port}`)
            } else {
              WIKI.logger.error(`Database Connection Error: ${err.message}`)
            }
169 170 171 172 173 174 175
            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
          }
        }
176 177 178 179 180 181 182 183 184 185 186
      },
      // -> 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)
NGPixel's avatar
NGPixel committed
187 188 189
      }
    }

190
    let initTasksQueue = (WIKI.IS_MASTER) ? [
191
      initTasks.connect,
192
      initTasks.migrateFromBeta,
193
      initTasks.syncSchemas
NGPixel's avatar
NGPixel committed
194
    ] : [
195
      () => { return Promise.resolve() }
NGPixel's avatar
NGPixel committed
196 197 198 199
    ]

    // Perform init tasks

200
    WIKI.logger.info(`Using database driver ${dbClient} for ${WIKI.config.db.type} [ OK ]`)
201
    this.onReady = Promise.each(initTasksQueue, t => t()).return(true)
202

203 204 205 206
    return {
      ...this,
      ...models
    }
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  },
  /**
   * 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)
      }
    })
227 228 229

    // -> Outbound events handling

230
    this.listener.addChannel('wiki', payload => {
NGPixel's avatar
NGPixel committed
231 232
      if (_.has(payload, 'event') && payload.source !== WIKI.INSTANCE_ID) {
        WIKI.logger.info(`Received event ${payload.event} from instance ${payload.source}: [ OK ]`)
233
        WIKI.events.inbound.emit(payload.event, payload.value)
234 235
      }
    })
236 237 238 239 240 241 242
    WIKI.events.outbound.onAny(this.notifyViaDB)

    // -> Listen to inbound events

    WIKI.auth.subscribeToEvents()
    WIKI.configSvc.subscribeToEvents()
    WIKI.models.pages.subscribeToEvents()
243 244 245 246 247 248 249 250

    WIKI.logger.info(`High-Availability Listener initialized successfully: [ OK ]`)
  },
  /**
   * Unsubscribe from database LISTEN / NOTIFY
   */
  async unsubscribeToNotifications () {
    if (this.listener) {
251 252
      WIKI.events.outbound.offAny(this.notifyViaDB)
      WIKI.events.inbound.removeAllListeners()
253 254 255 256 257 258 259 260 261 262
      this.listener.close()
    }
  },
  /**
   * Publish event via database NOTIFY
   *
   * @param {string} event Event fired
   * @param {object} value Payload of the event
   */
  notifyViaDB (event, value) {
NGPixel's avatar
NGPixel committed
263
    WIKI.models.listener.publish('wiki', {
264 265 266 267
      source: WIKI.INSTANCE_ID,
      event,
      value
    })
268 269
  }
}