db.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict'
  2. /* global wiki */
  3. const fs = require('fs')
  4. const path = require('path')
  5. const _ = require('lodash')
  6. const Promise = require('bluebird')
  7. const Sequelize = require('sequelize')
  8. const Op = Sequelize.Op
  9. const operatorsAliases = {
  10. $eq: Op.eq,
  11. $ne: Op.ne,
  12. $gte: Op.gte,
  13. $gt: Op.gt,
  14. $lte: Op.lte,
  15. $lt: Op.lt,
  16. $not: Op.not,
  17. $in: Op.in,
  18. $notIn: Op.notIn,
  19. $is: Op.is,
  20. $like: Op.like,
  21. $notLike: Op.notLike,
  22. $iLike: Op.iLike,
  23. $notILike: Op.notILike,
  24. $regexp: Op.regexp,
  25. $notRegexp: Op.notRegexp,
  26. $iRegexp: Op.iRegexp,
  27. $notIRegexp: Op.notIRegexp,
  28. $between: Op.between,
  29. $notBetween: Op.notBetween,
  30. $overlap: Op.overlap,
  31. $contains: Op.contains,
  32. $contained: Op.contained,
  33. $adjacent: Op.adjacent,
  34. $strictLeft: Op.strictLeft,
  35. $strictRight: Op.strictRight,
  36. $noExtendRight: Op.noExtendRight,
  37. $noExtendLeft: Op.noExtendLeft,
  38. $and: Op.and,
  39. $or: Op.or,
  40. $any: Op.any,
  41. $all: Op.all,
  42. $values: Op.values,
  43. $col: Op.col
  44. }
  45. /**
  46. * PostgreSQL DB module
  47. */
  48. module.exports = {
  49. Sequelize,
  50. Op: Sequelize.Op,
  51. /**
  52. * Initialize DB
  53. *
  54. * @return {Object} DB instance
  55. */
  56. init() {
  57. let self = this
  58. let dbModelsPath = path.join(wiki.SERVERPATH, 'models')
  59. // Define Sequelize instance
  60. self.inst = new self.Sequelize(wiki.config.db.db, wiki.config.db.user, wiki.config.db.pass, {
  61. host: wiki.config.db.host,
  62. port: wiki.config.db.port,
  63. dialect: 'postgres',
  64. pool: {
  65. max: 10,
  66. min: 0,
  67. idle: 10000
  68. },
  69. logging: false,
  70. operatorsAliases
  71. })
  72. // Attempt to connect and authenticate to DB
  73. self.inst.authenticate().then(() => {
  74. wiki.logger.info('Database (PostgreSQL) connection: OK')
  75. }).catch(err => {
  76. wiki.logger.error('Failed to connect to PostgreSQL instance.')
  77. return err
  78. })
  79. // Load DB Models
  80. fs
  81. .readdirSync(dbModelsPath)
  82. .filter(function (file) {
  83. return (file.indexOf('.') !== 0 && file.indexOf('_') !== 0)
  84. })
  85. .forEach(function (file) {
  86. let modelName = _.upperFirst(_.camelCase(_.split(file, '.')[0]))
  87. self[modelName] = self.inst.import(path.join(dbModelsPath, file))
  88. })
  89. // Associate DB Models
  90. require(path.join(dbModelsPath, '_relations.js'))(self)
  91. // Set init tasks
  92. let initTasks = {
  93. // -> Sync DB Schemas
  94. syncSchemas() {
  95. return self.inst.sync({
  96. force: false,
  97. logging: false
  98. })
  99. },
  100. // -> Set Connection App Name
  101. setAppName() {
  102. return self.inst.query(`set application_name = 'Wiki.js'`, { raw: true })
  103. }
  104. }
  105. let initTasksQueue = (wiki.IS_MASTER) ? [
  106. initTasks.syncSchemas,
  107. initTasks.setAppName
  108. ] : [
  109. initTasks.setAppName
  110. ]
  111. // Perform init tasks
  112. self.onReady = Promise.each(initTasksQueue, t => t())
  113. return self
  114. }
  115. }