简单的INSERT查询
简单的SELECT查询
选择查询的特殊属性
应用where子句
基本用法
操作符
Op.in的短写语法
简单的UPDATE查询
简单的DELETE查询
批量创建
排序和分组
限制和分页
实用方法
Sequelize还提供了一些实用的方法。
count
count方法只统计数据库中元素的出现次数。
console.log(`There are ${await Project.count()} projects`);const amount = await Project.count({where: {id: {[Op.gt]: 25}}});console.log(`There are ${amount} projects with an id greater than 25`);
max min和sum
Sequelize还提供了max、min和sum便利方法。
假设我们有三个用户,年龄分别为10岁、5岁和40岁。
await User.max('age'); // 40await User.max('age', { where: { age: { [Op.lt]: 20 } } }); // 10await User.min('age'); // 5await User.min('age', { where: { age: { [Op.gt]: 5 } } }); // 10await User.sum('age'); // 55await User.sum('age', { where: { age: { [Op.gt]: 5 } } }); // 50
increment、decrement
Sequelize还提供了增量便利方法。
假设我们有一个用户,他的年龄是10岁。
await User.increment({age: 5}, { where: { id: 1 } }) // Will increase age to 15await User.increment({age: -5}, { where: { id: 1 } }) // Will decrease age to 5
