Skip to content

Instantly share code, notes, and snippets.

@janmeier
Last active August 29, 2015 14:05
Show Gist options
  • Save janmeier/e288fa01fc116c3d7928 to your computer and use it in GitHub Desktop.
Save janmeier/e288fa01fc116c3d7928 to your computer and use it in GitHub Desktop.
Sequelize promises vs. event emitter
it('supports transactions', function() {
return Support.prepareTransactionTest(this.sequelize).bind({}}).then(function(sequelize) {
this.sequelize = sequelize;
this.Article = sequelize.define('Article', { 'title': DataTypes.STRING });
this.Label = sequelize.define('Label', { 'text': DataTypes.STRING });
this.Article.hasMany(this.Label);
return sequelize.sync({ force: true });
}).then(function() {
return Promise.all([
this.Article.create({ title: 'foo' }),
this.Label.create({ text: 'bar' })
]);
}).spread(function (article, label) {
this.article = article;
this.label = label;
return this.sequelize.transaction();
}).then(function(t) {
this.t = t;
return this.article.setLabels([ this.label ], { transaction: this.t });
}).then(function() {
return this.Article.all({ transaction: this.t });
}).then(function(articles) {
return articles[0].hasLabel(this.label).then(function(hasLabel) {
expect(hasLabel).to.be.false;
});
}).then(function () {
return this.Article.all({ transaction: this.t });
}).then(function(articles) {
return articles[0].hasLabel(label, { transaction: this.t }).then(function(hasLabel) {
expect(hasLabel).to.be.true;
return this.t.rollback();
});
});
});
it('supports transactions', function(done) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var Article = sequelize.define('Article', { 'title': DataTypes.STRING })
, Label = sequelize.define('Label', { 'text': DataTypes.STRING })
Article.hasMany(Label)
sequelize.sync({ force: true }).success(function() {
Article.create({ title: 'foo' }).success(function(article) {
Label.create({ text: 'bar' }).success(function(label) {
sequelize.transaction(function(t) {
article.setLabels([ label ], { transaction: t }).success(function() {
Article.all({ transaction: t }).success(function(articles) {
articles[0].hasLabel(label).success(function(hasLabel) {
expect(hasLabel).to.be.false
Article.all({ transaction: t }).success(function(articles) {
articles[0].hasLabel(label, { transaction: t }).success(function(hasLabel) {
expect(hasLabel).to.be.true
t.rollback().success(function() { done() })
})
})
})
})
})
})
})
})
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment