Sequelize
  • Introduction
  • 写在前面的话
  • 第一章 搭建环境
  • 第二章 模型定义
    • 2.1 实例变量方法
    • 2.2 定义一个模型
    • 2.3 模型字段类型
    • 2.3 导入与配置
  • 第三章 模型关系
    • 3.1 属于
    • 3.2 包含
    • 3.3 一对多
    • 3.4 多对多
    • 3.5 多态一对多
    • 3.6 多态多对多
    • 3.7 关系嵌套
  • 第四章 模型查询
  • 第五章 查询范围 Scope (预定义查询条件)
  • 第六章 生命周期函数 Hook
  • 第七章 数据库迁移 Migrate
  • 第八章 根据 Table 生成 Model
Powered by GitBook
On this page
  • attributes 属性
  • 指定特定属性字段
  • 重命名(将 name 重命名为 username)
  • 添加一个新的字段属性,通过函数来计算它的值
  • 排除字段属性
  • where 条件
  • 普通用法(等于)
  • 字段操作符
  • limit and offset 限定与偏移量
  • order 排序
  • group 分组

第四章 模型查询

模型的所以实例方法在 d.ts 的 3579 行。

findAll(options?: FindOptions): Promise<TInstance[]>;

对于有查询的配置项的接口是 FindOptions。这个接口在 3188 行。而且接口上面也有英文注释,说明了如何使用。平常所用到的并不会那么难以配置,所以接下来会有一些常用。

attributes 属性

指定特定属性字段

User.findAll({
  attributes: ['name', 'email']
});

重命名(将 name 重命名为 username)

User.findAll({
  attributes: [['name', 'username'], 'email']
});

添加一个新的字段属性,通过函数来计算它的值

Like.findAll({
  attributes: [[sequelize.fn('COUNT', sequelize.col('type')), 'post_like_count']],
  where: { id: 2, type: 'post'}
});

post_like_count 为 id =2 ,type = post 的结果行数。当前语句查出来只有 post_like_count 一个字段。

假如希望保留原来默认的所以字段,然后再添加这个新的函数字段,可以使用 attributes.include 选项。

Like.findAll({
  attributes: { include: [sequelize.fn('COUNT', sequelize.col('type')), 'post_like_count'] },
  where: { id: 2, type: 'post'}
});

排除字段属性

User.findAll({
  attributes: { exclude: ['name'] }
});

where 条件

普通用法(等于)

最简单的用法就是在 where 里面写下键值对,左边属性,右边值。

Post.findAll({
  where: {
    authorId: 2
  }
});

字段操作符

相比较之前的值,现在变成了配置项。

Post.findAll({
  where: {
    authorId: {
        $in: [3,4,5]
    }
  }
});

配置项支持以下操作符 (有 SQL 基础应该一眼就能明白)

$and: {a: 5}           // AND (a = 5)
$or: [{a: 5}, {a: 6}]  // (a = 5 OR a = 6)
$gt: 6,                // > 6
$gte: 6,               // >= 6
$lt: 10,               // < 10
$lte: 10,              // <= 10
$ne: 20,               // != 20
$eq: 3,                // = 3
$not: true,            // IS NOT TRUE
$between: [6, 10],     // 在 6 - 10 之间
$notBetween: [11, 15], // 不再 11 - 15 之间
$in: [1, 2],           // 在数组 [1, 2] 里面
$notIn: [1, 2],        // 不在数组 [1, 2] 里面
$like: '%hat',         // LIKE '%hat'
$notLike: '%hat'       // NOT LIKE '%hat'
$like: { $any: ['cat', 'hat']}

并且操作符是支持嵌套的。

rank: {
    $or: {
      $lt: 1000,
      $gt: 900
    }
}

900 < rank < 1000

{
  $or: [
    {
      title: {
        $like: 'Boat%'
      }
    },
    {
      description: {
        $like: '%boat%'
      }
    }
  ]
}

title LIKE 'Boat%' OR description LIKE '%boat%' 查询 title 或者 description 有关 boat 的数据。

limit and offset 限定与偏移量

在实现分页功能的时候,需要限定返回的数据与偏移量。

Project.findAll({ limit: 10 }) // 限定 10 条
Project.findAll({ offset: 8 }  // 忽略掉前 8 条

order 排序

DESC - ASC

D - 低 从大到小 A 从小到大

根据 createdAt 从大到小

Project.findAll({
    order: ['createdAt', 'DESC']
})

根据 age 排序

Person.findAll({
    order: sequelize.fn('max', sequelize.col('age'))
})

根据 age 从大到小排序

Person.findAll({
    order: sequelize.literal('max(age) DESC')
})

根据相关联的 Task 模型的 createdAt 从大到小排序

Person.findAll({
    order: [Task, 'createdAt', 'DESC'],
})

group 分组

ItemTag.findAll({
    attributes: { include: [sequelize.fn('COUNT', sequelize.col('type_id')), 'post_tag_counts'], exclude: ['type_id', 'type'] },
    where: {
        type: 'post'
    },
    group: 'tag_id'
})

该语句会查出,tag 与 几篇 Post 相建立关系。假如不加 group,查出来的数据会有一些没用的,所以我们通过 group 过滤掉重复的 tag_id。

Previous3.7 关系嵌套Next第五章 查询范围 Scope (预定义查询条件)

Last updated 7 years ago