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

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

11
/* global WIKI */
12

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

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

36 37
    const dbUseSSL = (WIKI.config.db.ssl === true || WIKI.config.db.ssl === 'true' || WIKI.config.db.ssl === 1 || WIKI.config.db.ssl === '1')

38 39 40
    switch (WIKI.config.db.type) {
      case 'postgres':
        dbClient = 'pg'
41 42 43 44

        if (dbUseSSL && _.isPlainObject(dbConfig)) {
          dbConfig.ssl = true
        }
45
        break
46
      case 'mariadb':
47 48
      case 'mysql':
        dbClient = 'mysql2'
49

50 51 52 53
        if (dbUseSSL && _.isPlainObject(dbConfig)) {
          dbConfig.ssl = true
        }

54 55 56 57 58 59 60 61
        // 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()
        }
62 63 64
        break
      case 'mssql':
        dbClient = 'mssql'
65 66 67 68 69 70 71

        if (_.isPlainObject(dbConfig)) {
          dbConfig.appName = 'Wiki.js'
          if (dbUseSSL) {
            dbConfig.encrypt = true
          }
        }
72 73 74
        break
      case 'sqlite':
        dbClient = 'sqlite3'
75
        dbConfig = { filename: WIKI.config.db.storage }
76 77 78 79 80
        break
      default:
        WIKI.logger.error('Invalid DB Type')
        process.exit(1)
    }
81

82 83 84
    this.knex = Knex({
      client: dbClient,
      useNullAsDefault: true,
85
      asyncStackTraces: WIKI.IS_DEBUG,
86
      connection: dbConfig,
87
      pool: {
88
        ...WIKI.config.pool,
89 90 91 92 93 94 95 96 97 98 99 100 101
        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
          }
        }
      },
102
      debug: WIKI.IS_DEBUG
103 104
    })

105
    Objection.Model.knex(this.knex)
106

107
    // Load DB Models
108

109
    const models = autoload(path.join(WIKI.SERVERPATH, 'models'))
110

NGPixel's avatar
NGPixel committed
111
    // Set init tasks
112
    let conAttempts = 0
NGPixel's avatar
NGPixel committed
113
    let initTasks = {
114
      // -> Attempt initial connection
115
      async connect () {
116 117 118 119 120 121 122 123 124 125 126 127 128 129
        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) {
            WIKI.logger.error(`Database Connection Error: ${err.code} ${err.address}:${err.port}`)
            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
          }
        }
130 131 132 133 134 135 136 137 138 139 140
      },
      // -> 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
141 142 143
      }
    }

144
    let initTasksQueue = (WIKI.IS_MASTER) ? [
145
      initTasks.connect,
146
      initTasks.migrateFromBeta,
147
      initTasks.syncSchemas
NGPixel's avatar
NGPixel committed
148
    ] : [
149
      () => { return Promise.resolve() }
NGPixel's avatar
NGPixel committed
150 151 152 153
    ]

    // Perform init tasks

154
    this.onReady = Promise.each(initTasksQueue, t => t()).return(true)
155

156 157 158 159
    return {
      ...this,
      ...models
    }
160 161
  }
}