Querying - 查询

此系列文章的应用示例已发布于 GitHub: sequelize-docs-Zh-CN. 可以 Fork 帮助改进或 Star 关注更新. 欢迎 Star.

属性

想要只选择某些属性,可以使用 attributes 选项。 通常是传递一个数组:

Model.findAll({

attributes: ['foo', 'bar']

});

SELECT foo, bar ...

属性可以使用嵌套数组来重命名:

Model.findAll({

attributes: ['foo', ['bar', 'baz']]

});

SELECT foo, bar AS baz ...

也可以使用 sequelize.fn 来进行聚合:

Model.findAll({

attributes: [[sequelize.fn('COUNT', sequelize.col('hats')), 'no_hats']]

});

SELECT COUNT(hats) AS no_hats ...

使用聚合功能时,必须给它一个别名,以便能够从模型中访问它。 在上面的例子中,您可以使用 instance.get('no_hats') 获得帽子数量。

有时,如果您只想添加聚合,则列出模型的所有属性可能令人厌烦:

// This is a tiresome way of getting the number of hats...

Model.findAll({

attributes: ['id', 'foo', 'bar', 'baz', 'quz', [sequelize.fn('COUNT', sequelize.col('hats')), 'no_hats']]

});

// This is shorter, and less error prone because it still works if you add / remove attributes

Model.findAll({

attributes: { include: [[sequelize.fn('COUNT', sequelize.col('hats')), 'no_hats']] }

});

SELECT id, foo, bar, baz, quz, COUNT(hats) AS no_hats ...

同样,它也可以删除一些指定的属性:

Model.findAll({

attributes: { exclude: ['baz'] }

});

SELECT id, foo, bar, quz ...

Where

无论您是通过 findAll/find 或批量 updates/destroys 进行查询,都可以传递一个 where 对象来过滤查询。

where 通常用 attribute:value 键值对获取一个对象,其中 value 可以是匹配等式的数据或其他运算符的键值对象。

也可以通过嵌套 or 和 and 运算符 的集合来生成复杂的 AND/OR 条件。

基础

const Op = Sequelize.Op;

Post.findAll({

where: {

authorId: 2

}

});

// SELECT * FROM post WHERE authorId = 2

Post.findAll({

where: {

authorId: 12,

status: 'active'

}

});

// SELECT * FROM post WHERE authorId = 12 AND status = 'active';

Post.destroy({

where: {

status: 'inactive'

}

});

// DELETE FROM post WHERE status = 'inactive';

Post.update({

updatedAt: null,

}, {

where: {

deletedAt: {

[Op.ne]: null

}

}

});

// UPDATE post SET updatedAt = null WHERE deletedAt NOT NULL;

Post.findAll({

where: sequelize.where(sequelize.fn('char_length', sequelize.col('status')), 6)

});

// SELECT * FROM post WHERE char_length(status) = 6;

操作符

Sequelize 可用于创建更复杂比较的符号运算符 -

const Op = Sequelize.Op

[Op.and]: {a: 5} // 且 (a = 5)

[Op.or]: [{a: 5}, {a: 6}] // (a = 5 或 a = 6)

[Op.gt]: 6, // id > 6

[Op.gte]: 6, // id >= 6

[Op.lt]: 10, // id < 10

[Op.lte]: 10, // id <= 10

[Op.ne]: 20, // id != 20

[Op.eq]: 3, // = 3

[Op.not]: true, // 不是 TRUE

[Op.between]: [6, 10], // 在 6 和 10 之间

[Op.notBetween]: [11, 15], // 不在 11 和 15 之间

[Op.in]: [1, 2], // 在 [1, 2] 之中

[Op.notIn]: [1, 2], // 不在 [1, 2] 之中

[Op.like]: '%hat', // 包含 '%hat'

[Op.notLike]: '%hat' // 不包含 '%hat'

[Op.iLike]: '%hat' // 包含 '%hat' (不区分大小写) (仅限 PG)

[Op.notILike]: '%hat' // 不包含 '%hat' (仅限 PG)

[Op.regexp]: '^[h|a|t]' // 匹配正则表达式/~ '^[h|a|t]' (仅限 MySQL/PG)

[Op.notRegexp]: '^[h|a|t]' // 不匹配正则表达式/!~ '^[h|a|t]' (仅限 MySQL/PG)

[Op.iRegexp]: '^[h|a|t]' // ~* '^[h|a|t]' (仅限 PG)

[Op.notIRegexp]: '^[h|a|t]' // !~* '^[h|a|t]' (仅限 PG)

[Op.like]: { [Op.any]: ['cat', 'hat']} // 包含任何数组['cat', 'hat'] - 同样适用于 iLike 和 notLike

[Op.overlap]: [1, 2] // && [1, 2] (PG数组重叠运算符)

[Op.contains]: [1, 2] // @> [1, 2] (PG数组包含运算符)

[Op.contained]: [1, 2] //

[Op.any]: [2,3] // 任何数组[2, 3]::INTEGER (仅限PG)

[Op.col]: 'user.organization_id' // = 'user'.'organization_id', 使用数据库语言特定的列标识符, 本例使用 PG

范围选项

所有操作符都支持支持的范围类型查询。

// 所有上述相等和不相等的操作符加上以下内容:

[Op.contains]: 2 // @> '2'::integer (PG range contains element operator)

[Op.contains]: [1, 2] // @> [1, 2) (PG range contains range operator)

[Op.contained]: [1, 2] //

[Op.overlap]: [1, 2] // && [1, 2) (PG range overlap (have points in common) operator)

[Op.adjacent]: [1, 2] // -|- [1, 2) (PG range is adjacent to operator)

[Op.strictLeft]: [1, 2] // << [1, 2) (PG range strictly left of operator)

[Op.strictRight]: [1, 2] // >> [1, 2) (PG range strictly right of operator)

[Op.noExtendRight]: [1, 2] // &< [1, 2) (PG range does not extend to the right of operator)

[Op.noExtendLeft]: [1, 2] // &> [1, 2) (PG range does not extend to the left of operator)

组合

{

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%'

运算符别名

Sequelize 允许将特定字符串设置为操作符的别名 -

const Op = Sequelize.Op;

const operatorsAliases = {

$gt: Op.gt

}

const connection = new Sequelize(db, user, pass, { operatorsAliases })

[Op.gt]: 6 // > 6

$gt: 6 // 等同于使用 Op.gt (> 6)

运算符安全性

使用没有任何别名的 Sequelize 可以提高安全性。

一些框架会自动将用户输入解析为js对象,如果您无法清理输入,则可能会将具有字符串运算符的对象注入到 Sequelize。

不带任何字符串别名将使运算符不太可能被注入,但您应该始终正确验证和清理用户输入。

由于向后兼容性原因Sequelize默认设置以下别名 -

$eq, $ne, $gte, $gt, $lte, $lt, $not, $in, $notIn, $is, $like, $notLike, $iLike, $notILike, $regexp, $notRegexp, $iRegexp, $notIRegexp, $between, $notBetween, $overlap, $contains, $contained, $adjacent, $strictLeft, $strictRight, $noExtendRight, $noExtendLeft, $and, $or, $any, $all, $values, $col

目前,以下遗留别名也被设置,但计划在不久的将来完全删除 -

ne, not, in, notIn, gte, gt, lte, lt, like, ilike, $ilike, nlike, $notlike, notilike, .., between, !.., notbetween, nbetween, overlap, &&, @>,

为了更好的安全性,建议使用 Sequelize.Op,而不是依赖任何字符串别名。 您可以通过设置operatorsAliases选项来限制应用程序需要的别名,请记住要清理用户输入,特别是当您直接将它们传递给 Sequelize 方法时。

const Op = Sequelize.Op;

// 不用任何操作符别名使用 sequelize

const connection = new Sequelize(db, user, pass, { operatorsAliases: false });

// 只用 $and => Op.and 操作符别名使用 sequelize

const connection2 = new Sequelize(db, user, pass, { operatorsAliases: { $and: Op.and } });

如果你使用默认别名并且不限制它们,Sequelize会发出警告。如果您想继续使用所有默认别名(不包括旧版别名)而不发出警告,您可以传递以下运算符参数 -

const Op = Sequelize.Op;

const operatorsAliases = {

$eq: Op.eq,

$ne: Op.ne,

$gte: Op.gte,

$gt: Op.gt,

$lte: Op.lte,

$lt: Op.lt,

$not: Op.not,

$in: Op.in,

$notIn: Op.notIn,

$is: Op.is,

$like: Op.like,

$notLike: Op.notLike,

$iLike: Op.iLike,

$notILike: Op.notILike,

$regexp: Op.regexp,

$notRegexp: Op.notRegexp,

$iRegexp: Op.iRegexp,

$notIRegexp: Op.notIRegexp,

$between: Op.between,

$notBetween: Op.notBetween,

$overlap: Op.overlap,

$contains: Op.contains,

$contained: Op.contained,

$adjacent: Op.adjacent,

$strictLeft: Op.strictLeft,

$strictRight: Op.strictRight,

$noExtendRight: Op.noExtendRight,

$noExtendLeft: Op.noExtendLeft,

$and: Op.and,

$or: Op.or,

$any: Op.any,

$all: Op.all,

$values: Op.values,

$col: Op.col

};

const connection = new Sequelize(db, user, pass, { operatorsAliases });

JSONB

JSONB 可以以三种不同的方式进行查询。

嵌套对象

{

meta: {

video: {

url: {

[Op.ne]: null

}

}

}

}

嵌套键

{

"meta.audio.length": {

[Op.gt]: 20

}

}

外包裹

{

"meta": {

[Op.contains]: {

site: {

url: 'http://google.com'

}

}

}

}

关系 / 关联

// 找到所有具有至少一个 task 的 project,其中 task.state === project.state

Project.findAll({

include: [{

model: Task,

where: { state: Sequelize.col('project.state') }

}]

})

分页 / 限制

// 获取10个实例/行

Project.findAll({ limit: 10 })

// 跳过8个实例/行

Project.findAll({ offset: 8 })

// 跳过5个实例,然后取5个

Project.findAll({ offset: 5, limit: 5 })

排序

order 需要一个条目的数组来排序查询或者一个 sequelize 方法。一般来说,你将要使用任一属性的 tuple/array,并确定排序的正反方向。

Subtask.findAll({

order: [

// 将转义用户名,并根据有效的方向参数列表验证DESC

['title', 'DESC'],

// 将按最大值排序(age)

sequelize.fn('max', sequelize.col('age')),

// 将按最大顺序(age) DESC

[sequelize.fn('max', sequelize.col('age')), 'DESC'],

// 将按 otherfunction 排序(`col1`, 12, 'lalala') DESC

[sequelize.fn('otherfunction', sequelize.col('col1'), 12, 'lalala'), 'DESC'],

// 将使用模型名称作为关联的名称排序关联模型的 created_at。

[Task, 'createdAt', 'DESC'],

// Will order through an associated model's created_at using the model names as the associations' names.

[Task, Project, 'createdAt', 'DESC'],

// 将使用关联的名称由关联模型的created_at排序。

['Task', 'createdAt', 'DESC'],

// Will order by a nested associated model's created_at using the names of the associations.

['Task', 'Project', 'createdAt', 'DESC'],

// Will order by an associated model's created_at using an association object. (优选方法)

[Subtask.associations.Task, 'createdAt', 'DESC'],

// Will order by a nested associated model's created_at using association objects. (优选方法)

[Subtask.associations.Task, Task.associations.Project, 'createdAt', 'DESC'],

// Will order by an associated model's created_at using a simple association object.

[{model: Task, as: 'Task'}, 'createdAt', 'DESC'],

// 嵌套关联模型的 created_at 简单关联对象排序

[{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')

})

如果这篇文章对您有帮助, 感谢 下方点赞 或 Star GitHub: sequelize-docs-Zh-CN 支持, 谢谢.

php sequelize,Sequelize 中文文档 v4 - Querying - 查询相关推荐

  1. Sequelize 中文文档 v4 - Querying - 查询

    Querying - 查询 此系列文章的应用示例已发布于 GitHub: sequelize-docs-Zh-CN. 可以 Fork 帮助改进或 Star 关注更新. 欢迎 Star. 属性 想要只选 ...

  2. php sequelize,Sequelize 中文文档 v4 - Getting started - 入门

    Getting started - 入门 此系列文章的应用示例已发布于 GitHub: sequelize-docs-Zh-CN. 可以 Fork 帮助改进或 Star 关注更新. 欢迎 Star. ...

  3. 【Android 逆向】x86 汇编 ( 参考资料 | Intel 官方的文档 | x86 汇编中文文档 | 汇编指令查询器 )

    文章目录 一.x86 汇编参考文档 1.Intel 官方的文档 2.常用 x86 汇编文档 3.x86 汇编指令查询器 总结 一.x86 汇编参考文档 下面的所有资料 , 都可以在博客资源 https ...

  4. Bootstrap 一篇就够 快速入门使用(中文文档)

    目录 一.Bootstrap 简介 什么是 Bootstrap? 历史 为什么使用 Bootstrap? Bootstrap 包的内容 在线实例 Bootstrap 实例 更多实例 Bootstrap ...

  5. Spring Security中文文档

    Spring Security中文文档 来源:https://www.springcloud.cc/spring-security.html#overall-architecture 作者 Ben A ...

  6. React Router 中文文档(一)

    React Router 中文文档(一) React Router 中文文档(一) 官方英文文档 - https://reacttraining.com/rea... 版本 - v4.2.0 < ...

  7. PyTorch官方中文文档:torch.optim 优化器参数

    内容预览: step(closure) 进行单次优化 (参数更新). 参数: closure (callable) –...~ 参数: params (iterable) – 待优化参数的iterab ...

  8. golang中文文档_Golang 标准库 限流器 time/rate 设计与实现

    限流器是后台服务中十分重要的组件,在实际的业务场景中使用居多,其设计在微服务.网关.和一些后台服务中会经常遇到.限流器的作用是用来限制其请求的速率,保护后台响应服务,以免服务过载导致服务不可用现象出现 ...

  9. springboot中文文档_登顶 Github 的 Spring Boot 仓库!艿艿写的最肝系列

    源码精品专栏 中文详细注释的开源项目 RPC 框架 Dubbo 源码解析 网络应用框架 Netty 源码解析 消息中间件 RocketMQ 源码解析 数据库中间件 Sharding-JDBC 和 My ...

最新文章

  1. 【radar】毫米波雷达相关开源项目代码汇总(工具箱、仿真、2D毫米波检测、融合、4D毫米波检测、分割、SLAM、跟踪)(6)
  2. iOS Sprite Kit教程之真机测试以及场景的添加与展示
  3. 【docker】修改docker容器配置,设置/修改端口映射
  4. TCP/IP协议栈模型分析
  5. python获取程序文件中的全局变量和局部变量的函数
  6. windows API 第13篇 MoveFileEx
  7. 移动端前端月历日历_前端基础:必须要知道的移动端适配(2)——分辨率
  8. 魅族6支持html吗,魅族手机浏览器兼容性调优最佳实践_html/css_WEB-ITnose
  9. 问题记录:既然MAC能唯一标识一台设备,那为什么我们还需要IP呢?
  10. Hive 架构知识体系及语法详解
  11. IDEF企业软件系统建模方法
  12. python怎么看自己安装的第三方包_安装第三方包查看python版本/第三方包版本
  13. 从零开始一起学习SLAM | 学习SLAM到底需要学什么?
  14. 彻底删除顽固dll文件
  15. 【论文笔记】CIRNet:基于CycleGAN的无监督循环配准模型
  16. virtualbox导入虚拟机(ubuntu)
  17. 窗体中添加标签Label、Icon图标
  18. win7计算机无法连接投影仪,win7系统下投影仪无法输出信号到电脑的解决方法
  19. python3爬小说_python3小说爬虫
  20. 人体存在感应+毫米波雷达技术,你知道多少?

热门文章

  1. 百度云提速方法 免费+非破解+不下载软件
  2. 你是否愿意为他人撑起一把伞?
  3. 《汇编语言》王爽(第四版) 第十章 实验10
  4. R语言实战-第九章 R in action-chapter9
  5. 如何把两段即以上视频合并成一个
  6. 关于CSDN获取博客内容接口的x-ca-signature签名算法研究
  7. 常见面试题(无答案)
  8. Java项目:SSM药品进货销售仓储信息管理系统
  9. 【随记】没Windows咋办?看看国产桌面操作系统Deepin
  10. jQuery Marquee