diff options
Diffstat (limited to 'src/sequelize.js')
-rw-r--r-- | src/sequelize.js | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/sequelize.js b/src/sequelize.js new file mode 100644 index 0000000..5afa78a --- /dev/null +++ b/src/sequelize.js @@ -0,0 +1,70 @@ +const Sequelize = require('sequelize'); +const { Op } = Sequelize; +const operatorsAliases = { + $eq: Op.eq, + $ne: Op.ne, + $gte: Op.gte, + $gt: Op.gt, + $lte: Op.lte, + $lt: Op.lt, + $not: Op.not, + $in: Op.in, + $notIn: Op.notIn, + $is: Op.is, + $like: Op.like, + $notLike: Op.notLike, + $iLike: Op.iLike, + $notILike: Op.notILike, + $regexp: Op.regexp, + $notRegexp: Op.notRegexp, + $iRegexp: Op.iRegexp, + $notIRegexp: Op.notIRegexp, + $between: Op.between, + $notBetween: Op.notBetween, + $overlap: Op.overlap, + $contains: Op.contains, + $contained: Op.contained, + $adjacent: Op.adjacent, + $strictLeft: Op.strictLeft, + $strictRight: Op.strictRight, + $noExtendRight: Op.noExtendRight, + $noExtendLeft: Op.noExtendLeft, + $and: Op.and, + $or: Op.or, + $any: Op.any, + $all: Op.all, + $values: Op.values, + $col: Op.col +}; + +module.exports = function (app) { + const connectionString = app.get('mysql'); + const sequelize = new Sequelize(connectionString, { + dialect: 'mysql', + logging: false, + operatorsAliases, + define: { + freezeTableName: true + } + }); + const oldSetup = app.setup; + + app.set('sequelizeClient', sequelize); + + app.setup = function (...args) { + const result = oldSetup.apply(this, args); + + // Set up data relationships + const models = sequelize.models; + Object.keys(models).forEach(name => { + if ('associate' in models[name]) { + models[name].associate(models); + } + }); + + // Sync to the database + sequelize.sync(); + + return result; + }; +}; |