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.

136 lines
2.9 KiB

  1. /* global wiki */
  2. const fs = require('fs')
  3. const path = require('path')
  4. const _ = require('lodash')
  5. const Promise = require('bluebird')
  6. const Sequelize = require('sequelize')
  7. const Op = Sequelize.Op
  8. const operatorsAliases = {
  9. $eq: Op.eq,
  10. $ne: Op.ne,
  11. $gte: Op.gte,
  12. $gt: Op.gt,
  13. $lte: Op.lte,
  14. $lt: Op.lt,
  15. $not: Op.not,
  16. $in: Op.in,
  17. $notIn: Op.notIn,
  18. $is: Op.is,
  19. $like: Op.like,
  20. $notLike: Op.notLike,
  21. $iLike: Op.iLike,
  22. $notILike: Op.notILike,
  23. $regexp: Op.regexp,
  24. $notRegexp: Op.notRegexp,
  25. $iRegexp: Op.iRegexp,
  26. $notIRegexp: Op.notIRegexp,
  27. $between: Op.between,
  28. $notBetween: Op.notBetween,
  29. $overlap: Op.overlap,
  30. $contains: Op.contains,
  31. $contained: Op.contained,
  32. $adjacent: Op.adjacent,
  33. $strictLeft: Op.strictLeft,
  34. $strictRight: Op.strictRight,
  35. $noExtendRight: Op.noExtendRight,
  36. $noExtendLeft: Op.noExtendLeft,
  37. $and: Op.and,
  38. $or: Op.or,
  39. $any: Op.any,
  40. $all: Op.all,
  41. $values: Op.values,
  42. $col: Op.col
  43. }
  44. /**
  45. * PostgreSQL DB module
  46. */
  47. module.exports = {
  48. Sequelize,
  49. Op: Sequelize.Op,
  50. /**
  51. * Initialize DB
  52. *
  53. * @return {Object} DB instance
  54. */
  55. init() {
  56. let self = this
  57. let dbModelsPath = path.join(wiki.SERVERPATH, 'models')
  58. // Define Sequelize instance
  59. self.inst = new self.Sequelize(wiki.config.db.db, wiki.config.db.user, wiki.config.db.pass, {
  60. host: wiki.config.db.host,
  61. port: wiki.config.db.port,
  62. dialect: 'postgres',
  63. pool: {
  64. max: 10,
  65. min: 0,
  66. idle: 10000
  67. },
  68. logging: log => { wiki.logger.log('debug', log) },
  69. operatorsAliases
  70. })
  71. // Attempt to connect and authenticate to DB
  72. self.inst.authenticate().then(() => {
  73. wiki.logger.info('Database (PostgreSQL) connection: [ OK ]')
  74. }).catch(err => {
  75. wiki.logger.error('Failed to connect to PostgreSQL instance.')
  76. wiki.logger.error(err)
  77. process.exit(1)
  78. })
  79. // Load DB Models
  80. fs
  81. .readdirSync(dbModelsPath)
  82. .filter(file => {
  83. return (file.indexOf('.') !== 0 && file.indexOf('_') !== 0)
  84. })
  85. .forEach(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: log => { wiki.logger.log('debug', log) }
  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()).return(true)
  113. return self
  114. }
  115. }