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

Commit 25491005 by Sascha Depold

safeDelete is now paranoid

1 parent ce1e0099
...@@ -25,7 +25,7 @@ ModelDefinition.prototype.addDefaultAttributes = function() { ...@@ -25,7 +25,7 @@ ModelDefinition.prototype.addDefaultAttributes = function() {
defaultAttributes[this.options.camelcase ? 'created_at' : 'createdAt'] = {type: DataTypes.DATE, allowNull: false} defaultAttributes[this.options.camelcase ? 'created_at' : 'createdAt'] = {type: DataTypes.DATE, allowNull: false}
defaultAttributes[this.options.camelcase ? 'updated_at' : 'updatedAt'] = {type: DataTypes.DATE, allowNull: false} defaultAttributes[this.options.camelcase ? 'updated_at' : 'updatedAt'] = {type: DataTypes.DATE, allowNull: false}
if(this.options.safeDelete) if(this.options.paranoid)
defaultAttributes[this.options.camelcase ? 'deleted_at' : 'deletedAt'] = {type: DataTypes.DATE} defaultAttributes[this.options.camelcase ? 'deleted_at' : 'deletedAt'] = {type: DataTypes.DATE}
} }
......
...@@ -21,7 +21,7 @@ var Model = module.exports = function(values, options) { ...@@ -21,7 +21,7 @@ var Model = module.exports = function(values, options) {
defaults[this.options.camelcase ? 'created_at' : 'createdAt'] = new Date() defaults[this.options.camelcase ? 'created_at' : 'createdAt'] = new Date()
defaults[this.options.camelcase ? 'updated_at' : 'updatedAt'] = new Date() defaults[this.options.camelcase ? 'updated_at' : 'updatedAt'] = new Date()
if(this.options.safeDelete) if(this.options.paranoid)
defaults[this.options.camelcase ? 'deleted_at' : 'deletedAt'] = null defaults[this.options.camelcase ? 'deleted_at' : 'deletedAt'] = null
} }
...@@ -65,7 +65,7 @@ Model.prototype.save = function() { ...@@ -65,7 +65,7 @@ Model.prototype.save = function() {
} }
Model.prototype.destroy = function() { Model.prototype.destroy = function() {
if(this.options.timestamps && this.options.safeDelete) { if(this.options.timestamps && this.options.paranoid) {
this[this.options.camelcase ? 'deleted_at' : 'deletedAt'] = new Date() this[this.options.camelcase ? 'deleted_at' : 'deletedAt'] = new Date()
return this.save() return this.save()
} else { } else {
...@@ -78,7 +78,7 @@ Model.prototype.__defineGetter__('isNewRecord', function() { ...@@ -78,7 +78,7 @@ Model.prototype.__defineGetter__('isNewRecord', function() {
}) })
Model.prototype.__defineGetter__('isDeleted', function() { Model.prototype.__defineGetter__('isDeleted', function() {
var result = this.options.timestamps && this.options.safeDelete var result = this.options.timestamps && this.options.paranoid
result = result && this[this.options.camelcase ? 'deleted_at' : 'deletedAt'] != null result = result && this[this.options.camelcase ? 'deleted_at' : 'deletedAt'] != null
return result return result
......
...@@ -20,8 +20,8 @@ module.exports = { ...@@ -20,8 +20,8 @@ module.exports = {
}) })
}) })
}, },
'destroy should mark the record as deleted if safeDelete is activated': function(exit) { 'destroy should mark the record as deleted if paranoid is activated': function(exit) {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }, {safeDelete:true}) var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }, {paranoid:true})
User.sync({force: true}).on('success', function() { User.sync({force: true}).on('success', function() {
User.create({name: 'asd', bio: 'asd'}).on('success', function(u) { User.create({name: 'asd', bio: 'asd'}).on('success', function(u) {
assert.isNull(u.deletedAt) assert.isNull(u.deletedAt)
......
...@@ -40,12 +40,12 @@ module.exports = { ...@@ -40,12 +40,12 @@ module.exports = {
assert.eql(User1.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"}) assert.eql(User1.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
assert.eql(User2.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"}) assert.eql(User2.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
}, },
'it should add deletedAt if safeDelete is true': function() { 'it should add deletedAt if paranoid is true': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {}, { safeDelete: true }) var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {}, { paranoid: true })
assert.eql(User.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", deletedAt:"DATETIME", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"}) assert.eql(User.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", deletedAt:"DATETIME", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
}, },
'timestamp columns should be camelcase if camelcase is passed': function() { 'timestamp columns should be camelcase if camelcase is passed': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {}, { safeDelete: true, camelcase: true }) var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {}, { paranoid: true, camelcase: true })
assert.eql(User.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", deleted_at:"DATETIME", updated_at:"DATETIME NOT NULL", created_at:"DATETIME NOT NULL"}) assert.eql(User.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", deleted_at:"DATETIME", updated_at:"DATETIME NOT NULL", created_at:"DATETIME NOT NULL"})
} }
} }
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!