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

Commit ed8a11ca by sdepold

merge

2 parents 12b58b68 1dde34ec
...@@ -6,11 +6,11 @@ module.exports = (function() { ...@@ -6,11 +6,11 @@ module.exports = (function() {
this.instance = instance this.instance = instance
} }
HasManyDoubleLinked.prototype.injectGetter = function() { HasManyDoubleLinked.prototype.injectGetter = function(options) {
var self = this var self = this, _options = options
var customEventEmitter = new Utils.CustomEventEmitter(function() { var customEventEmitter = new Utils.CustomEventEmitter(function() {
var where = {} var where = {}, options = _options || {};
//fully qualify //fully qualify
where[self.__factory.connectorDAO.tableName+"."+self.__factory.identifier] = self.instance.id where[self.__factory.connectorDAO.tableName+"."+self.__factory.identifier] = self.instance.id
...@@ -19,7 +19,19 @@ module.exports = (function() { ...@@ -19,7 +19,19 @@ module.exports = (function() {
, foreignKey = primaryKeys.filter(function(pk) { return pk != self.__factory.identifier })[0] , foreignKey = primaryKeys.filter(function(pk) { return pk != self.__factory.identifier })[0]
where[self.__factory.connectorDAO.tableName+"."+foreignKey] = {join: self.__factory.target.tableName+".id"} where[self.__factory.connectorDAO.tableName+"."+foreignKey] = {join: self.__factory.target.tableName+".id"}
self.__factory.target.findAllJoin(self.__factory.connectorDAO.tableName, {where: where})
if (options.where) {
Utils._.each(options.where, function(value, index) {
delete options.where[index];
options.where[self.__factory.target.tableName+"."+index] = value;
});
options.where = options.where ? Utils.merge(options.where, where) : where
} else {
options.where = where;
}
self.__factory.target.findAllJoin(self.__factory.connectorDAO.tableName, options)
.on('success', function(objects) { customEventEmitter.emit('success', objects) }) .on('success', function(objects) { customEventEmitter.emit('success', objects) })
.on('error', function(err){ customEventEmitter.emit('error', err) }) .on('error', function(err){ customEventEmitter.emit('error', err) })
.on('sql', function(sql) { customEventEmitter.emit('sql', sql)}) .on('sql', function(sql) { customEventEmitter.emit('sql', sql)})
......
...@@ -6,11 +6,13 @@ module.exports = (function() { ...@@ -6,11 +6,13 @@ module.exports = (function() {
this.instance = instance this.instance = instance
} }
HasManySingleLinked.prototype.injectGetter = function() { HasManySingleLinked.prototype.injectGetter = function(options) {
var where = {} var where = {}, options = options || {}
where[this.__factory.identifier] = this.instance.id where[this.__factory.identifier] = this.instance.id
return this.__factory.target.findAll({where: where})
options.where = options.where ? Utils.merge(options.where, where) : where
return this.__factory.target.findAll(options)
} }
HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations) { HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations) {
......
...@@ -72,9 +72,9 @@ module.exports = (function() { ...@@ -72,9 +72,9 @@ module.exports = (function() {
HasMany.prototype.injectGetter = function(obj) { HasMany.prototype.injectGetter = function(obj) {
var self = this var self = this
obj[this.accessors.get] = function() { obj[this.accessors.get] = function(options) {
var Class = self.connectorDAO ? HasManyMultiLinked : HasManySingleLinked var Class = self.connectorDAO ? HasManyMultiLinked : HasManySingleLinked
return new Class(self, this).injectGetter() return new Class(self, this).injectGetter(options)
} }
obj[this.accessors.hasAll] = function(objects) { obj[this.accessors.hasAll] = function(objects) {
......
...@@ -189,9 +189,91 @@ describe('HasMany', function() { ...@@ -189,9 +189,91 @@ describe('HasMany', function() {
}) })
}) })
}) })
describe("getting assocations with options", function() {
before(function(done) {
var self = this;
this.User = sequelize.define('User', { username: Sequelize.STRING })
this.Task = sequelize.define('Task', { title: Sequelize.STRING, active: Sequelize.BOOLEAN })
self.User.hasMany(self.Task)
sequelize.sync({ force: true }).done(function() {
var chainer = new Sequelize.Utils.QueryChainer([
self.User.create({ username: 'John'}),
self.Task.create({ title: 'Get rich', active: true}),
self.Task.create({ title: 'Die trying', active: false})
])
chainer.run().success(function (results, john, task1, task2) {
john.setTasks([task1, task2]).success(done)
})
})
})
it("gets all associated objects when no options are passed", function(done) {
this.User.find({where: {username: 'John'}}).success(function (john) {
john.getTasks().success(function (tasks) {
expect(tasks.length).toEqual(2)
done();
})
})
})
it("only get objects that fullfil the options", function(done) {
this.User.find({where: {username: 'John'}}).success(function (john) {
john.getTasks({where: {active: true}, limit: 10, order: 'ID DESC'}).success(function (tasks) {
expect(tasks.length).toEqual(1)
done();
})
})
})
})
}) })
describe('(N:M)', function() { describe('(N:M)', function() {
describe("getting assocations with options", function() {
before(function(done) {
var self = this;
this.User = sequelize.define('User', { username: Sequelize.STRING })
this.Task = sequelize.define('Task', { title: Sequelize.STRING, active: Sequelize.BOOLEAN })
self.User.hasMany(self.Task)
self.Task.hasMany(self.User)
sequelize.sync({ force: true }).done(function() {
var chainer = new Sequelize.Utils.QueryChainer([
self.User.create({ username: 'John'}),
self.Task.create({ title: 'Get rich', active: true}),
self.Task.create({ title: 'Die trying', active: false})
])
chainer.run().success(function (results, john, task1, task2) {
john.setTasks([task1, task2]).success(done)
})
})
})
it("gets all associated objects when no options are passed", function(done) {
this.User.find({where: {username: 'John'}}).success(function (john) {
john.getTasks().success(function (tasks) {
expect(tasks.length).toEqual(2)
done();
})
})
})
it("only get objects that fullfil the options", function(done) {
this.User.find({where: {username: 'John'}}).success(function (john) {
john.getTasks({where: {active: true}}).success(function (tasks) {
expect(tasks.length).toEqual(1)
done();
})
})
})
})
it("removes the reference id, which was added in the first place", function() { it("removes the reference id, which was added in the first place", function() {
var User = sequelize.define('User', { username: Sequelize.STRING }) var User = sequelize.define('User', { username: Sequelize.STRING })
, Task = sequelize.define('Task', { title: Sequelize.STRING }) , Task = sequelize.define('Task', { title: Sequelize.STRING })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!