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

Commit 7d96bb1e by Daniel Durante

You can now delete paranoid records manually by submitting a {force: true} optio…

…n through the destroy methods. Closes https://github.com/sequelize/sequelize/issues/352
1 parent abd0f717
......@@ -635,7 +635,10 @@ module.exports = (function() {
* @return {Object} A promise which fires `success`, `error`, `complete` and `sql`.
*/
DAOFactory.prototype.destroy = function(where, options) {
if (this.options.timestamps && this.options.paranoid) {
options = options || {}
options.force = options.force === undefined ? false : Boolean(options.force)
if (this.options.timestamps && this.options.paranoid && options.force === false) {
var attr = Utils._.underscoredIf(this.options.deletedAt, this.options.underscored)
var attrValueHash = {}
attrValueHash[attr] = Utils.now()
......
......@@ -276,8 +276,11 @@ module.exports = (function() {
this.isDirty = isDirty
}
DAO.prototype.destroy = function() {
if (this.__options.timestamps && this.__options.paranoid) {
DAO.prototype.destroy = function(options) {
options = options || {}
options.force = options.force === undefined ? false : Boolean(options.force)
if (this.__options.timestamps && this.__options.paranoid && options.force === false) {
var attr = Utils._.underscoredIf(this.__options.deletedAt, this.__options.underscored)
this.dataValues[attr] = new Date()
return this.save()
......
......@@ -382,7 +382,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
, pad = function (number) {
if (number > 9) {
return number
}
}
return '0' + number
}
expect(user.year).to.equal(now.getFullYear() + '-' + pad(now.getMonth() + 1) + '-' + pad(now.getDate()))
......@@ -1151,6 +1151,49 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
it('should delete a paranoid record if I set force to true', function(done) {
var self = this
var User = this.sequelize.define('paranoiduser', {
username: Sequelize.STRING
}, { paranoid: true })
User.sync({ force: true }).success(function() {
User.bulkCreate([
{username: 'Bob'},
{username: 'Tobi'},
{username: 'Max'},
{username: 'Tony'}
]).success(function() {
User.find({where: {username: 'Bob'}}).success(function(user) {
user.destroy({force: true}).success(function() {
User.find({where: {username: 'Bob'}}).success(function(user) {
expect(user).to.be.null
User.find({where: {username: 'Tobi'}}).success(function(tobi) {
tobi.destroy().success(function() {
self.sequelize.query('SELECT * FROM paranoidusers WHERE username=\'Tobi\'', null, {raw: true, plain: true}).success(function(result) {
expect(result.username).to.equal('Tobi')
User.destroy({username: 'Tony'}).success(function() {
self.sequelize.query('SELECT * FROM paranoidusers WHERE username=\'Tony\'', null, {raw: true, plain: true}).success(function(result) {
expect(result.username).to.equal('Tony')
User.destroy({username: ['Tony', 'Max']}, {force: true}).success(function() {
User.all().success(function(users) {
expect(users).to.have.length(1)
expect(users[0].username).to.equal('Tobi')
done()
})
})
})
})
})
})
})
})
})
})
})
})
})
})
describe('special where conditions/smartWhere object', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!