gpt4 book ai didi

node.js - Sequelize : How to soft delete conversation from 1 user but not the other?

转载 作者:行者123 更新时间:2023-12-03 22:32:50 24 4
gpt4 key购买 nike

目前在我的应用程序中,用户 1 可以与用户 2 进行对话并且一切正常,但是当用户 1 从他们的帐户中删除对话时,它也会从用户 2 的帐户中删除。
如何删除一个用户而不是另一个用户的对话?
我还使用偏执来记录数据库中的对话。
这是我的对话模型:

const Conversation = db.define("Conversation", {},{
paranoid: true,
timestamps: true,
deletedAt: "deletedAt",
}
);

module.exports = Conversation;

User.hasMany(Conversation, {
foreignKey: 'user1'
});

User.hasMany(Conversation, {
foreignKey: 'user2'
});

Conversation.belongsTo(User, { as: 'Creator', foreignKey: 'user1', allowNull: false })
Conversation.belongsTo(User, { as: 'Recipient', foreignKey: 'user2', allowNull: false })
这是我的删除请求:
router.delete("/:id", async(req, res) => {  
let { id } = req.params;
await Conversation.findByPk(id).then((conversation) => {
if (conversation) {
return conversation.destroy().then(() => {
res.status(204).send();
});
} else {
res.status(404).send();
}
});
});
在我的数据库中,对话表具有以下列:
id, user1, user2, postId, createdAt,updatedAt,deletedAt
预先感谢您的帮助,如果您需要任何其他信息,请询问。

最佳答案

您可以使用选项 paranoid: false 查询对话。这样,您将获得包括软删除在内的所有记录。
如何找出向谁显示已删除的记录以及不向谁显示由您决定。我想你应该存储哪些用户删除了一个转换,以便不显示他/她删除的他/她的对话。

关于node.js - Sequelize : How to soft delete conversation from 1 user but not the other?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65215860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com