You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
6.9 KiB

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