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

Commit 04583cf7 by Alexander Mochalin Committed by Mick Hansen

support string in include (in association field too) (#6512)

1 parent e0e21726
...@@ -523,6 +523,26 @@ User.findAll({ include: ['Instruments'] }).then(function(users) { ...@@ -523,6 +523,26 @@ User.findAll({ include: ['Instruments'] }).then(function(users) {
}] }]
*/ */
}) })
User.findAll({ include: [{ association: 'Instruments' }] }).then(function(users) {
console.log(JSON.stringify(users))
/*
[{
"name": "John Doe",
"id": 1,
"createdAt": "2013-03-20T20:31:45.000Z",
"updatedAt": "2013-03-20T20:31:45.000Z",
"Instruments": [{
"name": "Toothpick",
"id": 1,
"createdAt": null,
"updatedAt": null,
"userId": 1
}]
}]
*/
})
``` ```
When eager loading we can also filter the associated model using `where`. This will return all `User`s in which the `where` clause of `Tool` model matches rows. When eager loading we can also filter the associated model using `where`. This will return all `User`s in which the `where` clause of `Tool` model matches rows.
......
...@@ -182,17 +182,22 @@ class Model { ...@@ -182,17 +182,22 @@ class Model {
options.include = options.include.map(include => this._conformInclude(include, self)); options.include = options.include.map(include => this._conformInclude(include, self));
} }
static _conformInclude(include, self) { static _transformStringAssociation (include, self) {
let model;
if (include._pseudo) return include;
if (self && typeof include === 'string') { if (self && typeof include === 'string') {
if (!self.associations.hasOwnProperty(include)) { if (!self.associations.hasOwnProperty(include)) {
throw new Error('Association with alias "' + include + '" does not exists'); throw new Error('Association with alias "' + include + '" does not exists');
} }
include = self.associations[include]; return self.associations[include];
} }
return include;
}
static _conformInclude(include, self) {
let model;
if (include._pseudo) return include;
include = this._transformStringAssociation(include, self);
if (include instanceof Association) { if (include instanceof Association) {
if (self && include.target.name === self.name) { if (self && include.target.name === self.name) {
...@@ -206,6 +211,9 @@ class Model { ...@@ -206,6 +211,9 @@ class Model {
include = { model: include }; include = { model: include };
} else if (_.isPlainObject(include)) { } else if (_.isPlainObject(include)) {
if (include.association) { if (include.association) {
include.association = this._transformStringAssociation(include.association, self);
if (self && include.association.target.name === self.name) { if (self && include.association.target.name === self.name) {
model = include.association.source; model = include.association.source;
} else { } else {
......
...@@ -266,6 +266,23 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -266,6 +266,23 @@ describe(Support.getTestDialectTeaser('Model'), function() {
as: 'Owner' as: 'Owner'
}); });
}); });
it('should expand string association', function () {
const options = {
include: [{
association: 'Owner',
attributes: ['id']
}]
};
Sequelize.Model._conformOptions(options, this.Company);
expect(options.include[0]).to.deep.equal({
model: this.User,
association: this.Company.Owner,
attributes: ['id'],
as: 'Owner'
});
});
}); });
describe('subQuery', function () { describe('subQuery', function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!