Skip to content

Commit

Permalink
feat(paranoid): Add an option to have paranoid work like in v3
Browse files Browse the repository at this point in the history
  • Loading branch information
holm committed Feb 6, 2018
1 parent 0cf1911 commit bb8eb25
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/model.js
Expand Up @@ -69,7 +69,8 @@ class Model {
}
}

if (!model.options.timestamps || !model.options.paranoid || options.paranoid === false) {
const paranoid = options.paranoid !== undefined ? options.paranoid : model.options.paranoid;
if (!model.options.timestamps || !paranoid) {
// This model is not paranoid, nothing to do here;
return options;
}
Expand All @@ -81,12 +82,20 @@ class Model {

let deletedAtDefaultValue = deletedAtAttribute.hasOwnProperty('defaultValue') ? deletedAtAttribute.defaultValue : null;

deletedAtDefaultValue = deletedAtDefaultValue || {
[Op.or]: {
[Op.gt]: now,
[Op.eq]: null
if (!deletedAtDefaultValue) {
if (paranoid === 'simple') {
deletedAtDefaultValue = {
[Op.eq]: null
};
} else {
deletedAtDefaultValue = {
[Op.or]: {
[Op.gt]: now,
[Op.eq]: null
}
};
}
};
}

deletedAtObject[deletedAtAttribute.field || deletedAtCol] = deletedAtDefaultValue;

Expand Down Expand Up @@ -686,7 +695,7 @@ class Model {
* @param {Object} [options.scopes] More scopes, defined in the same way as defaultScope above. See `Model.scope` for more information about how scopes are defined, and what you can do with them
* @param {Boolean} [options.omitNull] Don't persist null values. This means that all columns with null values will not be saved
* @param {Boolean} [options.timestamps=true] Adds createdAt and updatedAt timestamps to the model.
* @param {Boolean} [options.paranoid=false] Calling `destroy` will not delete the model, but instead set a `deletedAt` timestamp if this is true. Needs `timestamps=true` to work
* @param {Boolean|String} [options.paranoid=false] Calling `destroy` will not delete the model, but instead set a `deletedAt` timestamp if this is true. Needs `timestamps=true` to work. If set to `'simple'`, paranoid queries will use `deletedAt is null` instead of also checking that `deletedAt` is in the future.
* @param {Boolean} [options.underscored=false] Converts all camelCased columns to underscored if true. Will not affect timestamp fields named explicitly by model options and will not affect fields with explicitly set `field` option
* @param {Boolean} [options.underscoredAll=false] Converts camelCased model names to underscored table names if true. Will not change model name if freezeTableName is set to true
* @param {Boolean} [options.freezeTableName=false] If freezeTableName is true, sequelize will not try to alter the model name to get the table name. Otherwise, the model name will be pluralized
Expand Down
24 changes: 24 additions & 0 deletions test/integration/model/find.test.js
Expand Up @@ -1017,5 +1017,29 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});

describe('with simple paranoid', () => {
it('should not find records where deletedAt set to future', function() {
const User = this.sequelize.define('paranoiduser', {
username: Sequelize.STRING
}, { paranoid: 'simple' });

return User.sync({ force: true }).then(() => {
return User.bulkCreate([
{username: 'Bob'},
{username: 'Tobi', deletedAt: moment().add(30, 'minutes').format()},
{username: 'Max', deletedAt: moment().add(30, 'days').format()},
{username: 'Tony', deletedAt: moment().subtract(30, 'days').format()}
]);
}).then(() => {
return User.find({ where: {username: 'Tobi'} });
}).then(tobi => {
expect(tobi).to.be.null;
}).then(() => {
return User.findAll();
}).then(users => {
expect(users.length).to.be.eql(1);
});
});
});
});
});

0 comments on commit bb8eb25

Please sign in to comment.