node-sequelize学习笔记四(增删改查)
1、查
// 查询所有用户 const users = await User.findAll(); console.log(users.every(user => user instanceof User)); // true console.log("All users:", JSON.stringify(users, null, 2)); //查询指定属性 Model.findAll({ attributes: ['foo', 'bar'] }); //重命名属性 Model.findAll({ attributes: ['foo', ['bar', 'baz'], 'qux'] });
你可以使用 sequelize.fn
进行聚合:
Model.findAll({ attributes: [ 'foo', [sequelize.fn('COUNT', sequelize.col('hats')), 'n_hats'], 'bar' ] });
使用聚合函数时,必须为它提供一个别名,以便能够从模型中访问它. 在上面的示例中,你可以通过 instance.n_hats
获取帽子数量.
有时,如果只想添加聚合,那么列出模型的所有属性可能会很麻烦:
// 这是获取帽子数量的烦人方法(每列都有) Model.findAll({ attributes: [ 'id', 'foo', 'bar', 'baz', 'qux', 'hats', // 我们必须列出所有属性... [sequelize.fn('COUNT', sequelize.col('hats')), 'n_hats'] // 添加聚合... ] }); // 这个更短,并且更不易出错. 如果以后在模型中添加/删除属性,它仍然可以正常工作 Model.findAll({ attributes: { include: [ [sequelize.fn('COUNT', sequelize.col('hats')), 'n_hats'] ] } });
同样,也可以排除某些属性:
Model.findAll({ attributes: { exclude: ['baz'] } });
2.1通过where条件查询
Post.findAll({ where: { authorId: 2 } }); // SELECT * FROM post WHERE authorId = 2
const { Op } = require("sequelize"); Post.findAll({ where: { [Op.or]: [ { authorId: 12 }, { authorId: 13 } ] } }); // SELECT * FROM post WHERE authorId = 12 OR authorId = 13;
const { Op } = require("sequelize"); Post.destroy({ where: { authorId: { [Op.or]: [12, 13] } } }); // DELETE FROM post WHERE authorId = 12 OR authorId = 13;
2.2操作符
const { Op } = require("sequelize"); Post.findAll({ where: { [Op.and]: [{ a: 5 }, { b: 6 }], // (a = 5) AND (b = 6) [Op.or]: [{ a: 5 }, { b: 6 }], // (a = 5) OR (b = 6) someAttribute: { // 基本 [Op.eq]: 3, // = 3 [Op.ne]: 20, // != 20 [Op.is]: null, // IS NULL [Op.not]: true, // IS NOT TRUE [Op.or]: [5, 6], // (someAttribute = 5) OR (someAttribute = 6) // 使用方言特定的列标识符 (以下示例中使用 PG): [Op.col]: 'user.organization_id', // = "user"."organization_id" // 数字比较 [Op.gt]: 6, // > 6 [Op.gte]: 6, // >= 6 [Op.lt]: 10, // < 10 [Op.lte]: 10, // <= 10 [Op.between]: [6, 10], // BETWEEN 6 AND 10 [Op.notBetween]: [11, 15], // NOT BETWEEN 11 AND 15 // 其它操作符 [Op.all]: sequelize.literal('SELECT 1'), // > ALL (SELECT 1) [Op.in]: [1, 2], // IN [1, 2] [Op.notIn]: [1, 2], // NOT IN [1, 2] [Op.like]: '%hat', // LIKE '%hat' [Op.notLike]: '%hat', // NOT LIKE '%hat' [Op.startsWith]: 'hat', // LIKE 'hat%' [Op.endsWith]: 'hat', // LIKE '%hat' [Op.substring]: 'hat', // LIKE '%hat%' [Op.iLike]: '%hat', // ILIKE '%hat' (不区分大小写) (仅 PG) [Op.notILike]: '%hat', // NOT ILIKE '%hat' (仅 PG) [Op.regexp]: '^[h|a|t]', // REGEXP/~ '^[h|a|t]' (仅 MySQL/PG) [Op.notRegexp]: '^[h|a|t]', // NOT REGEXP/!~ '^[h|a|t]' (仅 MySQL/PG) [Op.iRegexp]: '^[h|a|t]', // ~* '^[h|a|t]' (仅 PG) [Op.notIRegexp]: '^[h|a|t]', // !~* '^[h|a|t]' (仅 PG) [Op.any]: [2, 3], // ANY ARRAY[2, 3]::INTEGER (仅 PG) [Op.match]: Sequelize.fn('to_tsquery', 'fat & rat') // 匹配文本搜索字符串 'fat' 和 'rat' (仅 PG) // 在 Postgres 中, Op.like/Op.iLike/Op.notLike 可以结合 Op.any 使用: [Op.like]: { [Op.any]: ['cat', 'hat'] } // LIKE ANY ARRAY['cat', 'hat'] // 还有更多的仅限 postgres 的范围运算符,请参见下文 } } });
Op.in
的简写语法
Post.findAll({ where: { id: [1,2,3] // 等同使用 `id: { [Op.in]: [1,2,3] }` } }); // SELECT ... FROM "posts" AS "post" WHERE "post"."id" IN (1, 2, 3);
运算符的逻辑组合
const { Op } = require("sequelize"); Foo.findAll({ where: { rank: { [Op.or]: { [Op.lt]: 1000, [Op.eq]: null } }, // rank < 1000 OR rank IS NULL { createdAt: { [Op.lt]: new Date(), [Op.gt]: new Date(new Date() - 24 * 60 * 60 * 1000) } }, // createdAt < [timestamp] AND createdAt > [timestamp] { [Op.or]: [ { title: { [Op.like]: 'Boat%' } }, { description: { [Op.like]: '%boat%' } } ] } // title LIKE 'Boat%' OR description LIKE '%boat%' } });
使用 Op.not
示例
Project.findAll({ where: { name: 'Some Project', [Op.not]: [ { id: [1,2,3] }, { description: { [Op.like]: 'Hello%' } } ] } });
高级查询(不仅限于列)
Post.findAll({ where: sequelize.where(sequelize.fn('char_length', sequelize.col('content')), 7) }); // SELECT ... FROM "posts" AS "post" WHERE char_length("content") = 7
Post.findAll({ where: { [Op.or]: [ sequelize.where(sequelize.fn('char_length', sequelize.col('content')), 7), { content: { [Op.like]: 'Hello%' } }, { [Op.and]: [ { status: 'draft' }, sequelize.where(sequelize.fn('char_length', sequelize.col('content')), { [Op.gt]: 10 }) ] } ] } });
仅限 Postgres 的范围运算符
[Op.contains]: 2, // @> '2'::integer (PG range 包含元素运算符) [Op.contains]: [1, 2], // @> [1, 2) (PG range 包含范围运算符) [Op.contained]: [1, 2], // <@ [1, 2) (PG range 包含于运算符) [Op.overlap]: [1, 2], // && [1, 2) (PG range 重叠(有共同点)运算符) [Op.adjacent]: [1, 2], // -|- [1, 2) (PG range 相邻运算符) [Op.strictLeft]: [1, 2], // << [1, 2) (PG range 左严格运算符) [Op.strictRight]: [1, 2], // >> [1, 2) (PG range 右严格运算符) [Op.noExtendRight]: [1, 2], // &< [1, 2) (PG range 未延伸到右侧运算符) [Op.noExtendLeft]: [1, 2], // &> [1, 2) (PG range 未延伸到左侧运算符)
排序和分组
排序
Sequelize 提供了 order
and group
参数,来与 ORDER BY
和 GROUP BY
一起使用.
Subtask.findAll({ order: [ // 将转义 title 并针对有效方向列表进行降序排列 ['title', 'DESC'], // 将按最大年龄进行升序排序 sequelize.fn('max', sequelize.col('age')), // 将按最大年龄进行降序排序 [sequelize.fn('max', sequelize.col('age')), 'DESC'], // 将按 otherfunction(`col1`, 12, 'lalala') 进行降序排序 [sequelize.fn('otherfunction', sequelize.col('col1'), 12, 'lalala'), 'DESC'], // 将使用模型名称作为关联名称按关联模型的 createdAt 排序. [Task, 'createdAt', 'DESC'], // 将使用模型名称作为关联名称通过关联模型的 createdAt 排序. [Task, Project, 'createdAt', 'DESC'], // 将使用关联名称按关联模型的 createdAt 排序. ['Task', 'createdAt', 'DESC'], // 将使用关联的名称按嵌套的关联模型的 createdAt 排序. ['Task', 'Project', 'createdAt', 'DESC'], // 将使用关联对象按关联模型的 createdAt 排序. (首选方法) [Subtask.associations.Task, 'createdAt', 'DESC'], // 将使用关联对象按嵌套关联模型的 createdAt 排序. (首选方法) [Subtask.associations.Task, Task.associations.Project, 'createdAt', 'DESC'], // 将使用简单的关联对象按关联模型的 createdAt 排序. [{model: Task, as: 'Task'}, 'createdAt', 'DESC'], // 将由嵌套关联模型的 createdAt 简单关联对象排序. [{model: Task, as: 'Task'}, {model: Project, as: 'Project'}, 'createdAt', 'DESC'] ], // 将按最大年龄降序排列 order: sequelize.literal('max(age) DESC'), // 如果忽略方向,则默认升序,将按最大年龄升序排序 order: sequelize.fn('max', sequelize.col('age')), // 如果省略方向,则默认升序, 将按年龄升序排列 order: sequelize.col('age'), // 将根据方言随机排序(但不是 fn('RAND') 或 fn('RANDOM')) order: sequelize.random() }); Foo.findOne({ order: [ // 将返回 `name` ['name'], // 将返回 `username` DESC ['username', 'DESC'], // 将返回 max(`age`) sequelize.fn('max', sequelize.col('age')), // 将返回 max(`age`) DESC [sequelize.fn('max', sequelize.col('age')), 'DESC'], // 将返回 otherfunction(`col1`, 12, 'lalala') DESC [sequelize.fn('otherfunction', sequelize.col('col1'), 12, 'lalala'), 'DESC'], // 将返回 otherfunction(awesomefunction(`col`)) DESC, 这种嵌套可能是无限的! [sequelize.fn('otherfunction', sequelize.fn('awesomefunction', sequelize.col('col'))), 'DESC'] ] });
限制和分页
// 提取10个实例/行 Project.findAll({ limit: 10 }); // 跳过8个实例/行 Project.findAll({ offset: 8 }); // 跳过5个实例,然后获取5个实例 Project.findAll({ offset: 5, limit: 5 });
实用方法
count
console.log(`这有 ${await Project.count()} 个项目`); const amount = await Project.count({ where: { id: { [Op.gt]: 25 } } }); console.log(`这有 ${amount} 个项目 id 大于 25`);
max
, min
和 sum
await User.max('age'); // 40 await User.max('age', { where: { age: { [Op.lt]: 20 } } }); // 10 await User.min('age'); // 5 await User.min('age', { where: { age: { [Op.gt]: 5 } } }); // 10 await User.sum('age'); // 55 await User.sum('age', { where: { age: { [Op.gt]: 5 } } }); // 50
2、改
// 将所有没有姓氏的人更改为 "Doe" await User.update({ lastName: "Doe" }, { where: { lastName: null } });
3、删
// 删除所有名为 "Jane" 的人 await User.destroy({ where: { firstName: "Jane" } }); // 截断表格、删除所有内容 await User.destroy({ truncate: true });
4、增
const captains = await Captain.bulkCreate([ { name: 'Jack Sparrow' }, { name: 'Davy Jones' } ]); console.log(captains.length); // 2 console.log(captains[0] instanceof Captain); // true console.log(captains[0].name); // 'Jack Sparrow' console.log(captains[0].id); // 1 // (或另一个自动生成的值)
但是,默认情况下,bulkCreate
不会在要创建的每个对象上运行验证(而 create
可以做到). 为了使 bulkCreate
也运行这些验证,必须通过validate: true
参数. 但这会降低性能. 用法示例:
const Foo = sequelize.define('foo', { bar: { type: DataTypes.TEXT, validate: { len: [4, 6] } } }); // 这不会引发错误,两个实例都将被创建 await Foo.bulkCreate([ { name: 'abc123' }, { name: 'name too long' } ]); // 这将引发错误,不会创建任何内容 await Foo.bulkCreate([ { name: 'abc123' }, { name: 'name too long' } ], { validate: true });