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

Commit 47d8baef by Sushant Committed by Jan Aagaard Meier

fix: groupedLimit on paranoid junction table (#6833)

* fix: groupedLimit on paranoid junction table

* change log and esproject file
1 parent 7c866905
# Future # Future
- [FIXED] Set `timestamps` and `paranoid` options from through model on `belongsToMany` association
- [FIXED] Properly apply paranoid condition when `groupedLimit.on` association is `paranoid`
- [FIXED] `restore` now uses `field` from `deletedAt` - [FIXED] `restore` now uses `field` from `deletedAt`
- [ADDED] `option.silent` for increment and decrement [#6793](https://github.com/sequelize/sequelize/pull/6793) - [ADDED] `option.silent` for increment and decrement [#6793](https://github.com/sequelize/sequelize/pull/6793)
......
...@@ -169,6 +169,10 @@ var BelongsToMany = function(source, target, options) { ...@@ -169,6 +169,10 @@ var BelongsToMany = function(source, target, options) {
} }
} }
this.options = _.assign(this.options, _.pick(this.through.model.options, [
'timestamps', 'createdAt', 'updatedAt', 'deletedAt', 'paranoid'
]));
if (this.paired) { if (this.paired) {
if (this.otherKeyDefault) { if (this.otherKeyDefault) {
this.otherKey = this.paired.foreignKey; this.otherKey = this.paired.foreignKey;
......
...@@ -130,6 +130,14 @@ var paranoidClause = function(model, options) { ...@@ -130,6 +130,14 @@ var paranoidClause = function(model, options) {
}); });
} }
// apply paranoid when groupedLimit is used
if (_.get(options, 'groupedLimit.on.options.paranoid')) {
var throughModel = _.get(options, 'groupedLimit.on.through.model');
if (throughModel) {
options.groupedLimit.through = paranoidClause(throughModel, options.groupedLimit.through);
}
}
if (!model.options.timestamps || !model.options.paranoid || options.paranoid === false) { if (!model.options.timestamps || !model.options.paranoid || options.paranoid === false) {
// This model is not paranoid, nothing to do here; // This model is not paranoid, nothing to do here;
return options; return options;
......
...@@ -24,10 +24,19 @@ if (current.dialect.supports['UNION ALL']) { ...@@ -24,10 +24,19 @@ if (current.dialect.supports['UNION ALL']) {
this.Task = this.sequelize.define('task'); this.Task = this.sequelize.define('task');
this.ProjectUser = this.sequelize.define('project_user', {}, {timestamps: false}); this.ProjectUser = this.sequelize.define('project_user', {}, {timestamps: false});
this.ProjectUserParanoid = this.sequelize.define('project_user_paranoid', {}, {
timestamps: true,
paranoid: true,
createdAt: false,
updatedAt: false
});
this.User.Projects = this.User.belongsToMany(this.Project, {through: this.ProjectUser}); this.User.Projects = this.User.belongsToMany(this.Project, {through: this.ProjectUser});
this.Project.belongsToMany(this.User, {as: 'members', through: this.ProjectUser}); this.Project.belongsToMany(this.User, {as: 'members', through: this.ProjectUser});
this.User.ParanoidProjects = this.User.belongsToMany(this.Project, {through: this.ProjectUserParanoid});
this.Project.belongsToMany(this.User, {as: 'paranoidMembers', through: this.ProjectUserParanoid});
this.User.Tasks = this.User.hasMany(this.Task); this.User.Tasks = this.User.hasMany(this.Task);
return this.sequelize.sync({force: true}).bind(this).then(function() { return this.sequelize.sync({force: true}).bind(this).then(function() {
...@@ -43,6 +52,8 @@ if (current.dialect.supports['UNION ALL']) { ...@@ -43,6 +52,8 @@ if (current.dialect.supports['UNION ALL']) {
return Promise.join( return Promise.join(
projects[0].setMembers(users.slice(0, 4)), projects[0].setMembers(users.slice(0, 4)),
projects[1].setMembers(users.slice(2)), projects[1].setMembers(users.slice(2)),
projects[0].setParanoidMembers(users.slice(0, 4)),
projects[1].setParanoidMembers(users.slice(2)),
users[2].setTasks(tasks) users[2].setTasks(tasks)
); );
}); });
...@@ -139,6 +150,55 @@ if (current.dialect.supports['UNION ALL']) { ...@@ -139,6 +150,55 @@ if (current.dialect.supports['UNION ALL']) {
expect(users.map(function (u) { return u.get('id'); })).to.deep.equal([1, 3, 5, 7, 4]); expect(users.map(function (u) { return u.get('id'); })).to.deep.equal([1, 3, 5, 7, 4]);
}); });
}); });
it('works with paranoid junction models', function () {
return this.User.findAll({
attributes: ['id'],
groupedLimit: {
limit: 3,
on: this.User.ParanoidProjects,
values: this.projects.map(function (item) { return item.get('id'); })
},
order: [
Sequelize.fn('ABS', Sequelize.col('age')),
['id', 'DESC']
],
include: [this.User.Tasks]
}).bind(this).then(function (users) {
/*
project1 - 1, 3, 4
project2 - 3, 5, 7
*/
expect(users).to.have.length(5);
expect(users.map(function (u) { return u.get('id'); })).to.deep.equal([1, 3, 5, 7, 4]);
return Sequelize.Promise.join(
this.projects[0].setParanoidMembers(users.slice(0, 2)),
this.projects[1].setParanoidMembers(users.slice(4))
);
}).then(function () {
return this.User.findAll({
attributes: ['id'],
groupedLimit: {
limit: 3,
on: this.User.ParanoidProjects,
values: this.projects.map(function (item) { return item.get('id'); })
},
order: [
Sequelize.fn('ABS', Sequelize.col('age')),
['id', 'DESC']
],
include: [this.User.Tasks]
});
}).then(function (users) {
/*
project1 - 1, 3
project2 - 4
*/
expect(users).to.have.length(3);
expect(users.map(function (u) { return u.get('id'); })).to.deep.equal([1, 3, 4]);
});
});
}); });
describe('on: hasMany', function () { describe('on: hasMany', function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!