sequelize 文档

340 阅读7分钟

中文文档

demopark/sequelize-docs-Zh-CN: Sequelize 文档的中文版本: v7.1.0 | v6.16.0 (github.com)

简单 SELECT 查询


你可以使用  findAll 方法从数据库中读取整个表:

  • attributes : [array]  选择某些特定属性 ❤ 注意表名需要大写
// 查询所有用户 
const users = await User.findAll();
// 使用嵌套数组表示别名
Model.findAll({
  attributes: ['foo', ['bar', 'baz'], 'qux']
});
SELECT * FROM ...
SELECT foo, bar AS baz, qux FROM ...

使用聚合函数

使用聚合函数时,必须为它提供一个别名,以便能够从模型中访问它.   sequelize.fn

Model.findAll({
  attributes: [
    'foo',
    [sequelize.fn('COUNT', sequelize.col('hats')), 'n_hats'],
    'bar'
  ]
});
// include 会在当前表的所有列中查询
Model.findAll({
  attributes: {
    include: [
      [sequelize.fn('COUNT', sequelize.col('hats')), 'n_hats']
    ]
  }
});
// exclued 会排除指定列
Model.findAll({
  attributes: { exclude: ['baz'] }
});
SELECT foo, COUNT(hats) AS n_hats, bar FROM ...

使用 where 添加查询条件

Post.findAll({
  where: {
    authorId: 2
  }
});
// SELECT * FROM post WHERE authorId = 2;
  • 使用运算符 Op
    运算符 Op.and, Op.orOp.not 可用于创建任意复杂的嵌套逻辑比较.
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.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 的范围运算符,请参见下文
    }
  }
});

sequelize.fn  使用聚合函数    sequelize.col 列

Post.findAll({
  where: sequelize.where(sequelize.fn('char_length', sequelize.col('content')), 7)
});
// SELECT ... FROM "posts" AS "post" WHERE char_length("content") = 7

排序


Sequelize 提供了 order and group 参数,来与 ORDER BYGROUP BY 一起使用.

order 参数采用一系列 来让 sequelize 方法对查询进行排序. 这些 本身是 [column, direction] 形式的数组. 该列将被正确转义,并且将在有效方向列表中进行验证(例如 ASC, DESC, NULLS FIRST 等).

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']
  ]
});

回顾一下,order 数组的元素可以如下:

  • 一个字符串 (它将被自动引用)
  • 一个数组, 其第一个元素将被引用,第二个将被逐字追加
  • 一个具有raw字段的对象:
    • raw 内容将不加引用地逐字添加
    • 其他所有内容都将被忽略,如果未设置 raw,查询将失败
  • 调用 Sequelize.fn (这将在 SQL 中生成一个函数调用)
  • 调用 Sequelize.col (这将引用列名)

分组

分组和排序的语法相同,只是分组不接受方向作为数组的最后一个参数(不存在 ASC, DESC, NULLS FIRST 等).

你还可以将字符串直接传递给 group,该字符串将直接(普通)包含在生成的 SQL 中. 请谨慎使用,请勿与用户生成的内容一起使用.

Project.findAll({ group: 'name' });
// 生成 'GROUP BY name'

限制和分页

使用 limitoffset 参数可以进行 限制/分页:

// 提取10个实例/行
Project.findAll({ limit: 10 });

// 跳过8个实例/行
Project.findAll({ offset: 8 });

// 跳过5个实例,然后获取5个实例
Project.findAll({ offset: 5, limit: 5 });

通常这些与 order 参数一起使用.

实用方法

  • count 方法仅计算数据库中元素出现的次数.
console.log(`这有 ${await Project.count()} 个项目`);

const amount = await Project.count({
  where: {
    id: {
      [Op.gt]: 25
    }
  }
});
console.log(`这有 ${amount} 个项目 id 大于 25`);
  • Sequelize 还提供了 max,min 和 sum 便捷方法.

      假设我们有三个用户,分别是10、5和40岁.

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
  • Sequelize 还提供了 increment 简便方法。

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

await User.increment({age: 5}, { where: { id: 1 } }) // 将年龄增加到15岁
await User.increment({age: -5}, { where: { id: 1 } }) // 将年龄降至5岁

简单update查询

  • Update 查询也接受 where 参数,就像上面的读取查询一样.
// 将所有没有姓氏的人更改为 "Doe"
await User.update({ lastName: "Doe" }, {
  where: {
    lastName: null
  }
});

简单delete查询

  • Delete 查询也接受 where 参数,就像上面的读取查询一样.
// 删除所有名为 "Jane" 的人 
await User.destroy({
  where: {
    firstName: "Jane"
  }
});
  • 要销毁所有内容,可以使用 TRUNCATE SQL:
// 截断表格
await User.destroy({
  truncate: true
});

创建 create

Sequelize 提供了 Model.bulkCreate 方法,以允许仅一次查询即可一次创建多个记录.

通过接收数组对象而不是单个对象,Model.bulkCreate 的用法与 Model.create 非常相似.

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 // (或另一个自动生成的值)

高级关联


Sequelize 提供了四种关联类型 :

1、HasOne      2、 HasMany     3、  BelongTo    4、BelongtoMany

定义关联

const A = sequelize.define('A', /* ... */);
const B = sequelize.define('B', /* ... */);
// A称为源模型,B称为目标模型
A.hasOne(B); // A 有一个 B
A.belongsTo(B); // A 属于 B
A.hasMany(B); // A 有多个 B
A.belongsToMany(B, { through: 'C' }); // A 属于多个 B , 通过联结表 C

        关联的定义顺序是有关系的. 换句话说,对于这四种情况,定义顺序很重要. 在上述所有示例中,A 称为 模型,而 B 称为 目标 模型.

  • A.hasOne(B) 关联意味着 AB 之间存在一对一的关系,外键在目标模型(B)中定义.
  • A.belongsTo(B)关联意味着 AB 之间存在一对一的关系,外键在源模型中定义(A).
  • A.hasMany(B) 关联意味着 AB 之间存在一对多关系,外键在目标模型(B)中定义.

这三个调用将导致 Sequelize 自动将外键添加到适当的模型中(除非它们已经存在).

  • A.belongsToMany(B, { through: 'C' }) 关联意味着将表 C 用作联结表,在 AB 之间存在多对多关系. 具有外键(例如,aIdbId). Sequelize 将自动创建此模型 C(除非已经存在),并在其上定义适当的外键.

注意:在上面的 belongsToMany 示例中,字符串('C')被传递给 through 参数. 在这种情况下,Sequelize 会自动使用该名称生成模型. 但是,如果已经定义了模型,也可以直接传递模型.*


创建标准关系需要成对定义

  • 一对一  hasOne + belongsTo
  • 一对多  hasMany + belongsTo
  • 多对多  belongToMany + belongToMany