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

Commit 077242c0 by sdepold

clearing associations for many to many is working

1 parent f0fb303a
......@@ -28,12 +28,13 @@ module.exports = (function() {
return customEventEmitter.run()
}
HasManyDoubleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations) {
HasManyDoubleLinked.prototype.injectSetter = function(emitterProxy, oldAssociations, newAssociations) {
var self = this
destroyObsoleteAssociations.call(this, oldAssociations, newAssociations)
.error(function(err) { emitter.emit('error', err) })
.on('sql', function(sql) { emitter.emit('sql', sql) })
destroyObsoleteAssociations
.call(this, oldAssociations, newAssociations)
.on('sql', function(sql) { emitterProxy.emit('sql', sql) })
.error(function(err) { emitterProxy.emit('error', err) })
.success(function() {
var chainer = new Utils.QueryChainer
, association = self.__factory.target.associations[self.__factory.associationAccessor]
......@@ -50,9 +51,9 @@ module.exports = (function() {
chainer
.run()
.success(function() { emitter.emit('success', newAssociations) })
.error(function(err) { emitter.emit('error', err) })
.on('sql', function(sql) { emitter.emit('sql', sql) })
.success(function() { emitterProxy.emit('success', newAssociations) })
.error(function(err) { emitterProxy.emit('error', err) })
.on('sql', function(sql) { emitterProxy.emit('sql', sql) })
})
}
......@@ -66,30 +67,39 @@ module.exports = (function() {
var foreignIdentifier = self.__factory.target.associations[self.__factory.associationAccessor].identifier
var obsoleteAssociations = oldAssociations.filter(function(obj) { return !obj.equalsOneOf(newAssociations) })
if(obsoleteAssociations.length == 0) {
if(obsoleteAssociations.length === 0) {
return emitter.emit('success', null)
}
obsoleteAssociations.forEach(function(associatedObject) {
var where = {}
, primaryKeys = Utils._.keys(self.__factory.connectorDAO.rawAttributes)
, foreignKey = primaryKeys.filter(function(pk) { return pk != self.__factory.identifier })[0]
var where = {}
, primaryKeys = Utils._.keys(self.__factory.connectorDAO.rawAttributes)
, foreignKey = primaryKeys.filter(function(pk) { return pk != self.__factory.identifier })[0]
, notFoundEmitters = []
where[self.__factory.identifier] = self.instance.id
where[foreignKey] = associatedObject.id
self.__factory.connectorDAO.find({where: where}).success(function(connector) {
chainer.add(connector.destroy())
if(chainer.emitters.length == obsoleteAssociations.length) {
// found all obsolete connectors and will delete them now
chainer
.run()
.success(function() { emitter.emit('success', null) })
.error(function(err) { emitter.emit('error', err) })
.on('sql', function(sql) { emitter.emit('sql', sql) })
}
})
self.__factory.connectorDAO
.find({ where: where })
.success(function(connector) {
if(connector === null) {
notFoundEmitters.push(null)
} else {
chainer.add(connector.destroy())
}
if((chainer.emitters.length + notFoundEmitters.length) === obsoleteAssociations.length) {
// found all obsolete connectors and will delete them now
chainer
.run()
.success(function() { emitter.emit('success', null) })
.error(function(err) { emitter.emit('error', err) })
.on('sql', function(sql) { emitter.emit('sql', sql) })
}
})
.error(function(err) { emitter.emit('error', err) })
.on('sql', function(sql) { emitter.emit('sql', sql) })
})
}).run()
}
......
......@@ -121,16 +121,26 @@ module.exports = (function() {
var self = this
obj[this.accessors.set] = function(newAssociatedObjects) {
if(newAssociatedObjects === null) {
newAssociatedObjects = []
}
var instance = this
// define the returned customEventEmitter, which will emit the success event once everything is done
var customEventEmitter = new Utils.CustomEventEmitter(function() {
instance[self.accessors.get]().success(function(oldAssociatedObjects) {
var Class = self.connectorDAO ? HasManyMultiLinked : HasManySingleLinked
new Class(self, instance).injectSetter(customEventEmitter, oldAssociatedObjects, newAssociatedObjects)
})
})
return customEventEmitter.run()
return new Utils.CustomEventEmitter(function(emitter) {
instance[self.accessors.get]()
.success(function(oldAssociatedObjects) {
var Class = self.connectorDAO ? HasManyMultiLinked : HasManySingleLinked
new Class(self, instance).injectSetter(emitter, oldAssociatedObjects, newAssociatedObjects)
})
.error(function(err) {
emitter.emit('error', err)
})
.on('sql', function(sql) {
emitter.emit('sql', sql)
})
}).run()
}
obj[this.accessors.add] = function(newAssociatedObject) {
......
......@@ -146,6 +146,8 @@ describe('HasMany', function() {
})
it("adds three items to the query chainer when calling sync", function() {
sequelize.daoFactoryManager.daos = []
var User = sequelize.define('User', { username: Sequelize.STRING })
, Task = sequelize.define('Task', { title: Sequelize.STRING })
......@@ -160,44 +162,28 @@ describe('HasMany', function() {
expect(add).toHaveBeenCalledThrice()
})
it("=>clears associations when passing null to the set-method", function(done) {
it("clears associations when passing null to the set-method", function(done) {
var User = sequelize.define('User', { username: Sequelize.STRING })
, Task = sequelize.define('Task', { title: Sequelize.STRING })
User.hasMany(Task)
Task.hasMany(User)
console.log(sequelize.daoFactoryManager.daos.length)
sequelize.sync({ force: true }).success(function() {
console.log('sync ok')
User.create({ username: 'foo' }).success(function(user) {
console.log('user was created')
Task.create({ title: 'task' }).success(function(task) {
console.log('task was created')
task.setUsers([ user ]).success(function() {
console.log('set users done')
task.getUsers().success(function(_users) {
console.log(_users.length)
expect(_users.length).toEqual(1)
task.getUsers().success(function(_users) {
expect(_users.length).toEqual(1)
task.setUsers(null).success(function() {
task.getUsers().success(function(_users) {
expect(_users.length).toEqual(0)
done()
})
}).error(function(err) {
console.log(err)
task.setUsers(null).success(function() {
task.getUsers().success(function(_users) {
expect(_users.length).toEqual(0)
done()
})
}).error(function(err) {
console.log(err)
})
})
.error(function(err) {
console.log(err)
})
})
})
})
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!