db.js 8.13 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
    if (dbUseSSL && _.isPlainObject(dbConfig) && _.get(WIKI.config.db, 'sslOptions.auto', null) === false) {
      sslOptions = WIKI.config.db.sslOptions
46
      sslOptions.rejectUnauthorized = sslOptions.rejectUnauthorized !== false
NGPixel's avatar
NGPixel committed
47
      if (sslOptions.ca && sslOptions.ca.indexOf('-----') !== 0) {
NGPixel's avatar
NGPixel committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61
        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
    }
62

NGPixel's avatar
NGPixel committed
63
    // Handle inline SSL CA Certificate mode
64 65 66 67 68 69
    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
70 71 72
      dbUseSSL = true
      sslOptions = {
        rejectUnauthorized: true,
73
        ca: '-----BEGIN CERTIFICATE-----\n' + chunks.join('\n') + '\n-----END CERTIFICATE-----\n'
NGPixel's avatar
NGPixel committed
74 75 76 77
      }
    }

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

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

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

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

        if (_.isPlainObject(dbConfig)) {
          dbConfig.appName = 'Wiki.js'
108 109 110 111 112
          _.set(dbConfig, 'options.appName', 'Wiki.js')

          dbConfig.enableArithAbort = true
          _.set(dbConfig, 'options.enableArithAbort', true)

113 114
          if (dbUseSSL) {
            dbConfig.encrypt = true
115
            _.set(dbConfig, 'options.encrypt', true)
116 117
          }
        }
118 119 120
        break
      case 'sqlite':
        dbClient = 'sqlite3'
121
        dbConfig = { filename: WIKI.config.db.storage }
122 123 124 125 126
        break
      default:
        WIKI.logger.error('Invalid DB Type')
        process.exit(1)
    }
127

NGPixel's avatar
NGPixel committed
128
    // Initialize Knex
129 130 131
    this.knex = Knex({
      client: dbClient,
      useNullAsDefault: true,
132
      asyncStackTraces: WIKI.IS_DEBUG,
133
      connection: dbConfig,
134
      pool: {
135
        ...WIKI.config.pool,
136 137 138 139 140
        async afterCreate(conn, done) {
          // -> Set Connection App Name
          switch (WIKI.config.db.type) {
            case 'postgres':
              await conn.query(`set application_name = 'Wiki.js'`)
141
              // -> Set schema if it's not public
142 143 144
              if (WIKI.config.db.schema && WIKI.config.db.schema !== 'public') {
                await conn.query(`set search_path TO ${WIKI.config.db.schema}, public;`)
              }
145 146
              done()
              break
147 148 149 150
            case 'mysql':
              await conn.promise().query(`set autocommit = 1`)
              done()
              break
151 152 153 154 155 156
            default:
              done()
              break
          }
        }
      },
157
      debug: WIKI.IS_DEBUG
158 159
    })

160
    Objection.Model.knex(this.knex)
161

162
    // Load DB Models
163

164
    const models = autoload(path.join(WIKI.SERVERPATH, 'models'))
165

NGPixel's avatar
NGPixel committed
166
    // Set init tasks
167
    let conAttempts = 0
NGPixel's avatar
NGPixel committed
168
    let initTasks = {
169
      // -> Attempt initial connection
170
      async connect () {
171 172 173 174 175 176
        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) {
177 178 179 180 181
            if (err.code) {
              WIKI.logger.error(`Database Connection Error: ${err.code} ${err.address}:${err.port}`)
            } else {
              WIKI.logger.error(`Database Connection Error: ${err.message}`)
            }
182 183 184 185 186 187 188
            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
          }
        }
189 190 191 192 193 194 195 196 197 198 199
      },
      // -> 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
200 201 202
      }
    }

203
    let initTasksQueue = (WIKI.IS_MASTER) ? [
204
      initTasks.connect,
205
      initTasks.migrateFromBeta,
206
      initTasks.syncSchemas
NGPixel's avatar
NGPixel committed
207
    ] : [
208
      () => { return Promise.resolve() }
NGPixel's avatar
NGPixel committed
209 210 211 212
    ]

    // Perform init tasks

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

216 217 218 219
    return {
      ...this,
      ...models
    }
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  },
  /**
   * 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)
      }
    })
240 241 242

    // -> Outbound events handling

243
    this.listener.addChannel('wiki', payload => {
NGPixel's avatar
NGPixel committed
244 245
      if (_.has(payload, 'event') && payload.source !== WIKI.INSTANCE_ID) {
        WIKI.logger.info(`Received event ${payload.event} from instance ${payload.source}: [ OK ]`)
246
        WIKI.events.inbound.emit(payload.event, payload.value)
247 248
      }
    })
249 250 251 252 253 254 255
    WIKI.events.outbound.onAny(this.notifyViaDB)

    // -> Listen to inbound events

    WIKI.auth.subscribeToEvents()
    WIKI.configSvc.subscribeToEvents()
    WIKI.models.pages.subscribeToEvents()
256 257 258 259 260 261 262 263

    WIKI.logger.info(`High-Availability Listener initialized successfully: [ OK ]`)
  },
  /**
   * Unsubscribe from database LISTEN / NOTIFY
   */
  async unsubscribeToNotifications () {
    if (this.listener) {
264 265
      WIKI.events.outbound.offAny(this.notifyViaDB)
      WIKI.events.inbound.removeAllListeners()
266 267 268 269 270 271 272 273 274 275
      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
276
    WIKI.models.listener.publish('wiki', {
277 278 279 280
      source: WIKI.INSTANCE_ID,
      event,
      value
    })
281 282
  }
}