

Sequelize官网翻译十 —— Paranoid
source link: https://zhuanlan.zhihu.com/p/125027110
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Sequelize官网翻译十 —— Paranoid
Sequelize
也支持paranoid
表。paranoid
表是在被告之要删除记录时并不会真正的物理上删除,而是添加一个存有删除请求时间戳deletedAt
的特殊字段。
paranoid
表就是软删除而非硬删除机制的表。
把模型定义为paranoid
传递paranoid: true
参数给模型定义中。paranoid
要求必须启用时间戳,即必须传timestamps: true
。
也能把默认的deletedAt
的名字给换掉。
class Post extends Model {}
Post.init({ /* attributes here */ }, {
sequelize,
paranoid: true,
// If you want to give a custom name to the deletedAt column
deletedAt: 'destroyTime'
});
删除
当调用destroy
方法的时候,就会发生软删除
await Post.destroy({
where: {
id: 1
}
});
// UPDATE "posts" SET "deletedAt"=[timestamp] WHERE "deletedAt" IS NULL AND "id" = 1
如果此时还想是硬删除,你可以使用force: true
参数
await Post.destroy({
where: {
id: 1
},
force: true
});
// DELETE FROM "posts" WHERE "id" = 1
上面的例子用了静态的destroy
方法,但对于实例方法也是相同的
const post = await Post.create({ title: 'test' });
console.log(post instanceof Post); // true
await post.destroy(); // Would just set the `deletedAt` flag
await post.destroy({ force: true }); // Would really delete the record
恢复
想要恢复软删除的记录,可以调用restore
方法,它也同时存在有静态和实例方法两种形式。
// Example showing the instance `restore` method
// We create a post, soft-delete it and then restore it back
const post = await Post.create({ title: 'test' });
console.log(post instanceof Post); // true
await post.destroy();
console.log('soft-deleted!');
await post.restore();
console.log('restored!');
// Example showing the static `restore` method.
// Restoring every soft-deleted post with more than 100 likes
await Post.restore({
where: {
likes: {
[Op.gt]: 100
}
}
});
查询的行为
Sequelize
执行的查询都会忽略软删除掉的记录(但原生查询不会)。
这意味着,findAll
方法不会看到软删除掉的记录,只会拿到那些没有软删除的记录。
即便你调用findByPk
用主键来查询软删除掉的记录,也将返回null
。
如果仍想把软删除掉的记录查询出来,可以传递paranoid: false
参数给查询方法,比如
await Post.findByPk(123); // This will return `null` if the record of id 123 is soft-deleted
await Post.findByPk(123, { paranoid: false }); // This will retrieve the record
await Post.findAll({
where: { foo: 'bar' }
}); // This will not retrieve soft-deleted records
await Post.findAll({
where: { foo: 'bar' },
paranoid: false
}); // This will also retrieve soft-deleted records
Recommend
-
137
-
43
README.md Store System A Store System built with Electron, React, Material-UI, Redux, Redux-Saga, MySQL and Sequelize. Some Screens
-
52
README.md Sequelize
-
20
🎇🎇🎇新年快乐🎇🎇🎇 2020 鼠你最帅, 鼠你最强, 鼠你最棒, 鼠你最红, 鼠你最美, 鼠年吉祥❓:...
-
38
Sequelize en tiempos de SQL
-
17
How To Build Simple Node.js Rest APIs with Express, Sequelize & MySQLWe’ll be start to build a Node.js Rest API with Express, Sequelize & MySQL. Here we’ll use Sequelize for interacting with the M...
-
52
SQLite 是一种嵌入式数据库,它的数据库就是一个文件。 Sequelize 是一个基于 promise 的 Node.js ORM , 目前支持
-
14
Sequelize官网翻译九 —— 关联Sequelize支持标准的关联关系: 一对一,一对多,多对多。Sequelize里可以创建四种关联类型HasOneBelongsToHasMany
-
17
Sequelize官网翻译八 —— 原生查询有些场景下使用原生查询更方便,此时你可以使用sequelize.query方法。默认方法调用会返回两个结果——一个是结果数组,另一个是元数据对象(比如影响结果的行数)。注意由于是原生查询,元数据是数据...
-
7
Sequelize官网翻译七 —— 校验与约束本教程里你将会学到如何设置Sequelize里的校验和约束。假设有如下的模型 const { Sequelize, Op, Model, DataTypes } = require("sequelize"); const sequelize = new Seq...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK