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

Commit abf033c0 by Mick Hansen

feat(associatons): add support and tests for include: association reference, closes #2365

1 parent f5ac4be0
...@@ -7,6 +7,7 @@ var Utils = require('./../utils') ...@@ -7,6 +7,7 @@ var Utils = require('./../utils')
, Transaction = require('../transaction') , Transaction = require('../transaction')
, Model = require('../model') , Model = require('../model')
, CounterCache = require('../plugins/counter-cache') , CounterCache = require('../plugins/counter-cache')
, util = require('util')
, HasManyDoubleLinked = require('./has-many-double-linked'); , HasManyDoubleLinked = require('./has-many-double-linked');
module.exports = (function() { module.exports = (function() {
...@@ -159,6 +160,8 @@ module.exports = (function() { ...@@ -159,6 +160,8 @@ module.exports = (function() {
} }
}; };
util.inherits(BelongsToMany, Association);
// the id is in the target table // the id is in the target table
// or in an extra table which connects two tables // or in an extra table which connects two tables
BelongsToMany.prototype.injectAttributes = function() { BelongsToMany.prototype.injectAttributes = function() {
......
...@@ -7,6 +7,7 @@ var Utils = require('./../utils') ...@@ -7,6 +7,7 @@ var Utils = require('./../utils')
, Transaction = require('../transaction') , Transaction = require('../transaction')
, Model = require('../model') , Model = require('../model')
, CounterCache = require('../plugins/counter-cache') , CounterCache = require('../plugins/counter-cache')
, util = require('util')
, deprecatedSeen = {} , deprecatedSeen = {}
, deprecated = function(message) { , deprecated = function(message) {
if (deprecatedSeen[message]) return; if (deprecatedSeen[message]) return;
...@@ -192,6 +193,8 @@ module.exports = (function() { ...@@ -192,6 +193,8 @@ module.exports = (function() {
} }
}; };
util.inherits(HasMany, Association);
// the id is in the target table // the id is in the target table
// or in an extra table which connects two tables // or in an extra table which connects two tables
HasMany.prototype.injectAttributes = function() { HasMany.prototype.injectAttributes = function() {
......
...@@ -2,10 +2,14 @@ ...@@ -2,10 +2,14 @@
var Utils = require('./../utils') var Utils = require('./../utils')
, Helpers = require('./helpers') , Helpers = require('./helpers')
, Transaction = require('../transaction'); , Transaction = require('../transaction')
, Association = require('./base')
, util = require('util');
module.exports = (function() { module.exports = (function() {
var HasOne = function(srcDAO, targetDAO, options) { var HasOne = function(srcDAO, targetDAO, options) {
Association.call(this);
this.associationType = 'HasOne'; this.associationType = 'HasOne';
this.source = srcDAO; this.source = srcDAO;
this.target = targetDAO; this.target = targetDAO;
...@@ -64,6 +68,8 @@ module.exports = (function() { ...@@ -64,6 +68,8 @@ module.exports = (function() {
}; };
}; };
util.inherits(HasOne, Association);
// the id is in the target table // the id is in the target table
HasOne.prototype.injectAttributes = function() { HasOne.prototype.injectAttributes = function() {
var newAttributes = {} var newAttributes = {}
......
...@@ -1951,7 +1951,13 @@ module.exports = (function() { ...@@ -1951,7 +1951,13 @@ module.exports = (function() {
throw new Error('Include malformed. Expected attributes: model or association'); throw new Error('Include malformed. Expected attributes: model or association');
} }
if (include.association && !include._pseudo && !include.model) include.model = include.association.source; if (include.association && !include._pseudo && !include.model) {
if (include.association.associationType === 'BelongsTo') {
include.model = include.association.source;
} else {
include.model = include.association.target;
}
}
tableNames[include.model.getTableName()] = true; tableNames[include.model.getTableName()] = true;
......
...@@ -38,6 +38,22 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -38,6 +38,22 @@ describe(Support.getTestDialectTeaser('Include'), function() {
}); });
}); });
it('should support a belongsTo association reference', function () {
var Company = this.sequelize.define('Company', {})
, User = this.sequelize.define('User', {})
, Employer = User.belongsTo(Company, {as: 'Employer'});
return this.sequelize.sync({force: true}).done(function() {
return User.create();
}).then(function () {
return User.find({
include: [Employer]
});
}).then(function (user) {
expect(user).to.be.ok;
});
});
it('should support a empty hasOne include', function(done) { it('should support a empty hasOne include', function(done) {
var Company = this.sequelize.define('Company', {}) var Company = this.sequelize.define('Company', {})
, Person = this.sequelize.define('Person', {}); , Person = this.sequelize.define('Person', {});
...@@ -56,6 +72,22 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -56,6 +72,22 @@ describe(Support.getTestDialectTeaser('Include'), function() {
}); });
}); });
it('should support a hasOne association reference', function () {
var Company = this.sequelize.define('Company', {})
, Person = this.sequelize.define('Person', {})
, CEO = Company.hasOne(Person, {as: 'CEO'});
return this.sequelize.sync({force: true}).done(function() {
return Company.create();
}).then(function () {
return Company.find({
include: [CEO]
});
}).then(function (user) {
expect(user).to.be.ok;
});
});
it('should support including a belongsTo association rather than a model/as pair', function() { it('should support including a belongsTo association rather than a model/as pair', function() {
var Company = this.sequelize.define('Company', {}) var Company = this.sequelize.define('Company', {})
, Person = this.sequelize.define('Person', {}); , Person = this.sequelize.define('Person', {});
...@@ -81,6 +113,50 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -81,6 +113,50 @@ describe(Support.getTestDialectTeaser('Include'), function() {
}); });
}); });
it('should support a hasMany association reference', function () {
var User = this.sequelize.define('user', {})
, Task = this.sequelize.define('task', {})
, Tasks = User.hasMany(Task);
Task.belongsTo(User);
return this.sequelize.sync({force: true}).then(function () {
return User.create().then(function (user) {
return user.createTask();
}).then(function () {
return User.find({
include: [Tasks]
});
}).then(function (user) {
expect(user).to.be.ok;
expect(user.tasks).to.be.ok;
});
});
});
it('should support a belongsToMany association reference', function () {
var User = this.sequelize.define('user', {})
, Group = this.sequelize.define('group', {})
, Groups
, Users;
Groups = User.belongsToMany(Group);
Users = Group.belongsToMany(User);
return this.sequelize.sync({force: true}).then(function () {
return User.create().then(function (user) {
return user.createGroup();
});
}).then(function () {
return User.find({
include: [Groups]
}).then(function (user) {
expect(user).to.be.ok;
expect(user.groups).to.be.ok;
});
});
});
it('should support a simple nested belongsTo -> belongsTo include', function(done) { it('should support a simple nested belongsTo -> belongsTo include', function(done) {
var Task = this.sequelize.define('Task', {}) var Task = this.sequelize.define('Task', {})
, User = this.sequelize.define('User', {}) , User = this.sequelize.define('User', {})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!