简单的INSERT查询

简单的SELECT查询

选择查询的特殊属性

应用where子句

基本用法

操作符

Op.in的短写语法

简单的UPDATE查询

简单的DELETE查询

批量创建

排序和分组

限制和分页

实用方法

Sequelize还提供了一些实用的方法。

count

count方法只统计数据库中元素的出现次数。

  1. console.log(`There are ${await Project.count()} projects`);
  2. const amount = await Project.count({
  3. where: {
  4. id: {
  5. [Op.gt]: 25
  6. }
  7. }
  8. });
  9. console.log(`There are ${amount} projects with an id greater than 25`);

max min和sum

Sequelize还提供了max、min和sum便利方法。

假设我们有三个用户,年龄分别为10岁、5岁和40岁。

  1. await User.max('age'); // 40
  2. await User.max('age', { where: { age: { [Op.lt]: 20 } } }); // 10
  3. await User.min('age'); // 5
  4. await User.min('age', { where: { age: { [Op.gt]: 5 } } }); // 10
  5. await User.sum('age'); // 55
  6. await User.sum('age', { where: { age: { [Op.gt]: 5 } } }); // 50

increment、decrement

Sequelize还提供了增量便利方法。

假设我们有一个用户,他的年龄是10岁。

  1. await User.increment({age: 5}, { where: { id: 1 } }) // Will increase age to 15
  2. await User.increment({age: -5}, { where: { id: 1 } }) // Will decrease age to 5