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

Commit 052b5f1d by Mick Hansen

Merge pull request #2083 from meetearnest/find-paranoid

Add flag to retreive deleted paranoid models in queries
2 parents 2bb1845c 22d24b8f
Showing with 76 additions and 1 deletions
......@@ -629,6 +629,7 @@ module.exports = (function() {
* @param {Object} [options] A hash of options to describe the scope of the search
* @param {Object} [options.where] A hash of attributes to describe your search. See above for examples.
* @param {Array<String>} [options.attributes] A list of the attributes that you want to select. To rename an attribute, you can pass an array, with two elements - the first is the name of the attribute in the DB (or some kind of expression such as `Sequelize.literal`, `Sequelize.fn` and so on), and the second is the name you want the attribute to have in the returned instance
* @param {Boolean} [options.paranoid=true] If false, will include columns which have a non-null deletedAt column.
* @param {Array<Object|Model>} [options.include] A list of associations to eagerly load using a left join. Supported is either `{ include: [ Model1, Model2, ...]}` or `{ include: [{ model: Model1, as: 'Alias' }]}`. If your association are set up with an `as` (eg. `X.hasMany(Y, { as: 'Z }`, you need to specify Z in the as attribute when eager loading Y).
* @param {Model} [options.include[].model] The model you want to eagerly load
* @param {String} [options.include[].as] The alias of the relation, in case the model you want to eagerly load is aliassed. For `hasOne` / `belongsTo`, this should be the singular name, and for `hasMany`, it should be the plural
......@@ -1547,7 +1548,7 @@ module.exports = (function() {
// private
var paranoidClause = function(options ) {
if (! this.options.timestamps || ! this.options.paranoid) return options || {};
if (! this.options.timestamps || ! this.options.paranoid || options.paranoid === false) return options || {};
options = options || {};
options.where = options.where || {};
......
......@@ -958,6 +958,80 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
describe('can find paranoid records if paranoid is marked as false in query', function() {
it('with the DAOFactory', function() {
var User = this.sequelize.define('UserCol', {
username: Sequelize.STRING
}, { paranoid: true })
return User.sync({ force: true })
.then(function() {
return User.bulkCreate([
{username: 'Toni'},
{username: 'Tobi'},
{username: 'Max'}
]);
})
.then(function() { return User.find(1) })
.then(function(user) { return user.destroy() })
.then(function() { return User.find({ where: 1, paranoid: false }) })
.then(function(user) {
expect(user).to.exist
return User.find(1)
})
.then(function(user) {
expect(user).to.be.null
return [User.count(), User.count({ paranoid: false })]
})
.spread(function(cnt, cntWithDeleted) {
expect(cnt).to.equal(2)
expect(cntWithDeleted).to.equal(3)
})
})
})
it('should include deleted associated records if include has paranoid marked as false', function() {
var User = this.sequelize.define('User', {
username: Sequelize.STRING
}, { paranoid: true })
var Pet = this.sequelize.define('Pet', {
name: Sequelize.STRING,
UserId: Sequelize.INTEGER
}, { paranoid: true })
User.hasMany(Pet)
Pet.belongsTo(User)
var user;
return User.sync({ force: true })
.then(function() { return Pet.sync({ force: true }) })
.then(function() { return User.create({ username: 'Joe' }) })
.then(function(_user) {
user = _user;
return Pet.bulkCreate([
{ name: 'Fido', UserId: user.id },
{ name: 'Fifi', UserId: user.id }
]);
})
.then(function () { return Pet.find(1) })
.then(function (pet) { return pet.destroy() })
.then(function () {
return [
User.find({ where: user.id, include: Pet }),
User.find({
where: user.id,
include: [{ model: Pet, paranoid: false }]
})
]
})
.spread(function (user, userWithDeletedPets) {
expect(user).to.exist
expect(user.Pets).to.have.length(1)
expect(userWithDeletedPets).to.exist
expect(userWithDeletedPets.Pets).to.have.length(2)
})
})
it('should delete a paranoid record if I set force to true', function(done) {
var self = this
var User = this.sequelize.define('paranoiduser', {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!