javascript 在 sequelize.js 上查询“偏执”表时如何包含已删除的元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24611778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:07:34  来源:igfitidea点击:

How to include the deleted elements when querying a "paranoid" table on sequelize.js?

javascriptsequelize.js

提问by alilloig

I want that previously existing users on my application, that have been deleted using the paranoid mode (so now its field deletedAtis NOT null), to be able to register again using the same email. So when the API notices a user creation with a previously used email it sets to nullthe deletedAtfield of the previously existing register instead of creating a new user.

我希望我的应用程序中以前存在的用户,已经使用偏执模式删除(所以现在它的字段deletedAt不是null),能够使用相同的电子邮件再次注册。因此,当API通知的方式与以前使用过的电子邮件地址的用户创建其设置为nulldeletedAt先前存在的寄存器,而不是创建一个新用户的领域。

Usually for looking for an user I will do,

通常为了寻找我会做的用户,

User.find( { where: { email: req.body.user.email } })

User.find( { where: { email: req.body.user.email } })

But upon inspection of the SQL query created, it includes

但是在检查创建的 SQL 查询时,它包括

Users.deletedAt IS NULL

Users.deletedAt IS NULL

Is there any special way of finding when dealing with paranoid models?

处理偏执模型时有什么特殊的查找方法吗?

回答by lao

Just run your query like this:

只需像这样运行您的查询:

User.find({
  where: {email: req.body.user.email}, 
  paranoid: false
})

回答by rudd

Sequelize has this feature built in. Per their API docs, you can include the 'paranoid' flag in the options of your find call.

Sequelize 内置了此功能。根据他们的 API 文档,您可以在 find 调用的选项中包含“偏执”标志。

e.g.

例如

User.find({where: {email: req.body.user.email}}, {paranoid: false}).success(models) {
    //models contains both deleted and non-deleted users
}

Reference: http://docs.sequelizejs.com/en/latest/api/model/#findalloptions-promisearrayinstance

参考:http: //docs.sequelizejs.com/en/latest/api/model/#findalloptions-promisearrayinstance

回答by Enxtur

You can use hook.

你可以使用钩子。

var uuid = require('node-uuid')
module.exports = function (sequelize, DataTypes) {
    return sequelize.define("multi_route", {
        email: {
            type: DataTypes.STRING,
            unique: false,
        }
        //other fields
    }, {
        timestamps: true,
        paranoid: true,
        hooks: {
            beforeUpdate: function (multiFare, next) {                  // berforeUpdate will called after beforeDestroy
                if (multiFare.email.indexOf("_____destroyed") > -1) {   // check contains '_____destroyed' string
                    multiFare.email = multiFare.email + uuid.v1()       // set unique value
                }
                next()
            },
            beforeDestroy: [function (multiFare, next) {                // beforeDestroy will called before one instance destroyed
                multiFare.email = multiFare.email + '_____destroyed'    // flag this will destroy
                next()
            }]
        }
    })
}

Sorry for my bad comments :(

抱歉我的不好评论:(