New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

before/afterSync hooks for Models and sequelize instance #4479

Closed
alekbarszczewski opened this Issue Sep 12, 2015 · 1 comment

Comments

2 participants
@alekbarszczewski
Contributor

alekbarszczewski commented Sep 12, 2015

PR #4544

  • before/afterSync hooks on Model executed before/after Model.sync(...)
  • before/afterBulkSync hooks on sequelize (instance) executed before/after sequelize.sync(...)
  • useful for plugins (for example to create a trigger after model table has beem created etc)
  • TODO: add new tests
var Sequelize = require('sequelize')

var sequelize = new Sequelize('postgres://localhost/sequelize_test', {
  logging: false
})

// executed "before" `sequelize.sync(...)`
sequelize.addHook('beforeBulkSync', function (options) {
  // this = sequelize instance
  console.log('beforeBulkSync')
})

// executed "after" `sequelize.sync(...)`
sequelize.addHook('afterBulkSync', function (options) {
  // this = sequelize instance
  console.log('afterBulkSync')
})

var Model = sequelize.define('Model', {}, {
  hooks: {
    // executed "before" `Model.sync(...)`
    beforeSync: function (options) {
      // this = Model
      console.log('beforeSync')
    },
    // executed "after" `Model.sync(...)`
    afterSync: function (options) {
      // this = Model
      console.log('afterSync')
    }
  }
})

sequelize.sync()
.then(function(){
  console.log('done')
})

output

beforeBulkSync
beforeSync
afterSync
afterBulkSync
done
@janmeier

This comment has been minimized.

Member

janmeier commented Sep 30, 2015

PR merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment