db.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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')
  6. const fs = require('fs')
  7. const Objection = require('objection')
  8. const migrationSource = require('../db/migrator-source')
  9. const migrateFromBeta = require('../db/beta')
  10. /* global WIKI */
  11. /**
  12. * ORM DB module
  13. */
  14. module.exports = {
  15. Objection,
  16. knex: null,
  17. listener: null,
  18. /**
  19. * Initialize DB
  20. *
  21. * @return {Object} DB instance
  22. */
  23. init() {
  24. let self = this
  25. // Fetch DB Config
  26. let dbClient = null
  27. let dbConfig = (!_.isEmpty(process.env.DATABASE_URL)) ? process.env.DATABASE_URL : {
  28. host: WIKI.config.db.host.toString(),
  29. user: WIKI.config.db.user.toString(),
  30. password: WIKI.config.db.pass.toString(),
  31. database: WIKI.config.db.db.toString(),
  32. port: WIKI.config.db.port
  33. }
  34. // Handle SSL Options
  35. let dbUseSSL = (WIKI.config.db.ssl === true || WIKI.config.db.ssl === 'true' || WIKI.config.db.ssl === 1 || WIKI.config.db.ssl === '1')
  36. let sslOptions = null
  37. if (dbUseSSL && _.isPlainObject(dbConfig) && _.get(WIKI.config.db, 'sslOptions.auto', null) === false) {
  38. sslOptions = WIKI.config.db.sslOptions
  39. // eslint-disable-next-line no-unneeded-ternary
  40. sslOptions.rejectUnauthorized = sslOptions.rejectUnauthorized === false ? false : true
  41. if (sslOptions.ca && sslOptions.ca.indexOf('-----') !== 0) {
  42. sslOptions.ca = fs.readFileSync(path.resolve(WIKI.ROOTPATH, sslOptions.ca))
  43. }
  44. if (sslOptions.cert) {
  45. sslOptions.cert = fs.readFileSync(path.resolve(WIKI.ROOTPATH, sslOptions.cert))
  46. }
  47. if (sslOptions.key) {
  48. sslOptions.key = fs.readFileSync(path.resolve(WIKI.ROOTPATH, sslOptions.key))
  49. }
  50. if (sslOptions.pfx) {
  51. sslOptions.pfx = fs.readFileSync(path.resolve(WIKI.ROOTPATH, sslOptions.pfx))
  52. }
  53. } else {
  54. sslOptions = true
  55. }
  56. // Handle inline SSL CA Certificate mode
  57. if (!_.isEmpty(process.env.DB_SSL_CA)) {
  58. const chunks = []
  59. for (let i = 0, charsLength = process.env.DB_SSL_CA.length; i < charsLength; i += 64) {
  60. chunks.push(process.env.DB_SSL_CA.substring(i, i + 64))
  61. }
  62. dbUseSSL = true
  63. sslOptions = {
  64. rejectUnauthorized: true,
  65. ca: '-----BEGIN CERTIFICATE-----\n' + chunks.join('\n') + '\n-----END CERTIFICATE-----\n'
  66. }
  67. }
  68. // Engine-specific config
  69. switch (WIKI.config.db.type) {
  70. case 'postgres':
  71. dbClient = 'pg'
  72. if (dbUseSSL && _.isPlainObject(dbConfig)) {
  73. dbConfig.ssl = (sslOptions === true) ? { rejectUnauthorized: true } : sslOptions
  74. }
  75. break
  76. case 'mariadb':
  77. case 'mysql':
  78. dbClient = 'mysql2'
  79. if (dbUseSSL && _.isPlainObject(dbConfig)) {
  80. dbConfig.ssl = sslOptions
  81. }
  82. // Fix mysql boolean handling...
  83. dbConfig.typeCast = (field, next) => {
  84. if (field.type === 'TINY' && field.length === 1) {
  85. let value = field.string()
  86. return value ? (value === '1') : null
  87. }
  88. return next()
  89. }
  90. break
  91. case 'mssql':
  92. dbClient = 'mssql'
  93. if (_.isPlainObject(dbConfig)) {
  94. dbConfig.appName = 'Wiki.js'
  95. _.set(dbConfig, 'options.appName', 'Wiki.js')
  96. dbConfig.enableArithAbort = true
  97. _.set(dbConfig, 'options.enableArithAbort', true)
  98. if (dbUseSSL) {
  99. dbConfig.encrypt = true
  100. _.set(dbConfig, 'options.encrypt', true)
  101. }
  102. }
  103. break
  104. case 'sqlite':
  105. dbClient = 'sqlite3'
  106. dbConfig = { filename: WIKI.config.db.storage }
  107. break
  108. default:
  109. WIKI.logger.error('Invalid DB Type')
  110. process.exit(1)
  111. }
  112. // Initialize Knex
  113. this.knex = Knex({
  114. client: dbClient,
  115. useNullAsDefault: true,
  116. asyncStackTraces: WIKI.IS_DEBUG,
  117. connection: dbConfig,
  118. pool: {
  119. ...WIKI.config.pool,
  120. async afterCreate(conn, done) {
  121. // -> Set Connection App Name
  122. switch (WIKI.config.db.type) {
  123. case 'postgres':
  124. await conn.query(`set application_name = 'Wiki.js'`)
  125. done()
  126. break
  127. default:
  128. done()
  129. break
  130. }
  131. }
  132. },
  133. debug: WIKI.IS_DEBUG
  134. })
  135. Objection.Model.knex(this.knex)
  136. // Load DB Models
  137. const models = autoload(path.join(WIKI.SERVERPATH, 'models'))
  138. // Set init tasks
  139. let conAttempts = 0
  140. let initTasks = {
  141. // -> Attempt initial connection
  142. async connect () {
  143. try {
  144. WIKI.logger.info('Connecting to database...')
  145. await self.knex.raw('SELECT 1 + 1;')
  146. WIKI.logger.info('Database Connection Successful [ OK ]')
  147. } catch (err) {
  148. if (conAttempts < 10) {
  149. if (err.code) {
  150. WIKI.logger.error(`Database Connection Error: ${err.code} ${err.address}:${err.port}`)
  151. } else {
  152. WIKI.logger.error(`Database Connection Error: ${err.message}`)
  153. }
  154. WIKI.logger.warn(`Will retry in 3 seconds... [Attempt ${++conAttempts} of 10]`)
  155. await new Promise(resolve => setTimeout(resolve, 3000))
  156. await initTasks.connect()
  157. } else {
  158. throw err
  159. }
  160. }
  161. },
  162. // -> Migrate DB Schemas
  163. async syncSchemas () {
  164. return self.knex.migrate.latest({
  165. tableName: 'migrations',
  166. migrationSource
  167. })
  168. },
  169. // -> Migrate DB Schemas from beta
  170. async migrateFromBeta () {
  171. return migrateFromBeta.migrate(self.knex)
  172. }
  173. }
  174. let initTasksQueue = (WIKI.IS_MASTER) ? [
  175. initTasks.connect,
  176. initTasks.migrateFromBeta,
  177. initTasks.syncSchemas
  178. ] : [
  179. () => { return Promise.resolve() }
  180. ]
  181. // Perform init tasks
  182. WIKI.logger.info(`Using database driver ${dbClient} for ${WIKI.config.db.type} [ OK ]`)
  183. this.onReady = Promise.each(initTasksQueue, t => t()).return(true)
  184. return {
  185. ...this,
  186. ...models
  187. }
  188. },
  189. /**
  190. * Subscribe to database LISTEN / NOTIFY for multi-instances events
  191. */
  192. async subscribeToNotifications () {
  193. const useHA = (WIKI.config.ha === true || WIKI.config.ha === 'true' || WIKI.config.ha === 1 || WIKI.config.ha === '1')
  194. if (!useHA) {
  195. return
  196. } else if (WIKI.config.db.type !== 'postgres') {
  197. WIKI.logger.warn(`Database engine doesn't support pub/sub. Will not handle concurrent instances: [ DISABLED ]`)
  198. return
  199. }
  200. const PGPubSub = require('pg-pubsub')
  201. this.listener = new PGPubSub(this.knex.client.connectionSettings, {
  202. log (ev) {
  203. WIKI.logger.debug(ev)
  204. }
  205. })
  206. // -> Outbound events handling
  207. this.listener.addChannel('wiki', payload => {
  208. if (_.has(payload, 'event') && payload.source !== WIKI.INSTANCE_ID) {
  209. WIKI.logger.info(`Received event ${payload.event} from instance ${payload.source}: [ OK ]`)
  210. WIKI.events.inbound.emit(payload.event, payload.value)
  211. }
  212. })
  213. WIKI.events.outbound.onAny(this.notifyViaDB)
  214. // -> Listen to inbound events
  215. WIKI.auth.subscribeToEvents()
  216. WIKI.configSvc.subscribeToEvents()
  217. WIKI.models.pages.subscribeToEvents()
  218. WIKI.logger.info(`High-Availability Listener initialized successfully: [ OK ]`)
  219. },
  220. /**
  221. * Unsubscribe from database LISTEN / NOTIFY
  222. */
  223. async unsubscribeToNotifications () {
  224. if (this.listener) {
  225. WIKI.events.outbound.offAny(this.notifyViaDB)
  226. WIKI.events.inbound.removeAllListeners()
  227. this.listener.close()
  228. }
  229. },
  230. /**
  231. * Publish event via database NOTIFY
  232. *
  233. * @param {string} event Event fired
  234. * @param {object} value Payload of the event
  235. */
  236. notifyViaDB (event, value) {
  237. WIKI.models.listener.publish('wiki', {
  238. source: WIKI.INSTANCE_ID,
  239. event,
  240. value
  241. })
  242. }
  243. }