不要怂,就是干,撸起袖子干!

Commit c53c2f2e by Sascha Gehlich

Added transaction support for `.createAssociation`

1 parent 93353db2
var Utils = require("./../utils") var Utils = require("./../utils")
, DataTypes = require('./../data-types') , DataTypes = require('./../data-types')
, Helpers = require('./helpers') , Helpers = require('./helpers')
, _ = require('lodash') , _ = require('lodash')
, Transaction = require('../transaction')
var HasManySingleLinked = require("./has-many-single-linked") var HasManySingleLinked = require("./has-many-single-linked")
, HasManyDoubleLinked = require("./has-many-double-linked") , HasManyDoubleLinked = require("./has-many-double-linked")
...@@ -329,13 +330,20 @@ module.exports = (function() { ...@@ -329,13 +330,20 @@ module.exports = (function() {
obj[this.accessors.create] = function(values, fieldsOrOptions) { obj[this.accessors.create] = function(values, fieldsOrOptions) {
var instance = this var instance = this
, options = {}
if ((fieldsOrOptions || {}).transaction instanceof Transaction) {
options.transaction = fieldsOrOptions.transaction
delete fieldsOrOptions.transaction
}
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
// Create the related model instance // Create the related model instance
self.target self.target
.create(values, fieldsOrOptions) .create(values, fieldsOrOptions)
.proxy(emitter, { events: ['error', 'sql'] }) .proxy(emitter, { events: ['error', 'sql'] })
.success(function(newAssociatedObject) { .success(function(newAssociatedObject) {
instance[self.accessors.add](newAssociatedObject) instance[self.accessors.add](newAssociatedObject, options)
.proxy(emitter) .proxy(emitter)
}) })
}).run() }).run()
......
...@@ -352,6 +352,34 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -352,6 +352,34 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
}) })
}) })
}) })
it('supports transactions', function(done) {
var self = this
Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var Article = sequelize.define('Article', { 'title': DataTypes.STRING })
, Label = sequelize.define('Label', { 'text': DataTypes.STRING })
Article.hasMany(Label)
Article.sync({ force: true }).success(function() {
Label.sync({ force: true }).success(function() {
Article.create({ title: 'foo' }).success(function(article) {
sequelize.transaction(function (t) {
article.createLabel({ text: 'bar' }, { transaction: t }).success(function(label) {
Label.findAll({ where: { ArticleId: article.id }}).success(function(labels) {
expect(labels.length).to.equal(0)
Label.findAll({ where: { ArticleId: article.id }}, { transaction: t }).success(function(labels) {
expect(labels.length).to.equal(1)
t.rollback().success(function() { done() })
})
})
})
})
})
})
})
})
})
}) })
describe("getting assocations with options", function() { describe("getting assocations with options", function() {
...@@ -701,6 +729,36 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -701,6 +729,36 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
}) })
}) })
}) })
it('supports transactions', function(done) {
var self = this
Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: DataTypes.STRING })
, Task = sequelize.define('Task', { title: DataTypes.STRING })
User.hasMany(Task)
Task.hasMany(User)
User.sync({ force: true }).success(function() {
Task.sync({ force: true }).success(function() {
Task.create({ title: 'task' }).success(function(task) {
sequelize.transaction(function (t) {
task.createUser({ username: 'foo' }, { transaction: t }).success(function() {
task.getUsers().success(function(users) {
expect(users).to.have.length(0)
task.getUsers({ transaction: t }).success(function(users) {
expect(users).to.have.length(1)
t.rollback().success(function() { done() })
})
})
})
})
})
})
})
})
})
}) })
describe('addAssociations', function() { describe('addAssociations', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!