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.

132 lines
3.2 KiB

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