Sequelize-nodejs-5-Querying
Querying查询
Attributes
To select only some attributes, you can use the attributes
option. Most often, you pass an array:
为了收集一些属性,可以使用attributes
选项:
Model.findAll({
attributes: ['foo', 'bar']
}); //等价于
SELECT foo, bar ...
Attributes can be renamed using a nested array:
属性能够使用嵌套数组进行重命名:
Model.findAll({
attributes: ['foo', ['bar', 'baz']]
}); //等价于
SELECT foo, bar AS baz ...
You can use sequelize.fn
to do aggregations:
可以使用sequelize.fn
进行聚合:
Model.findAll({
attributes: [[sequelize.fn('COUNT', sequelize.col('hats')), 'no_hats']]
}); SELECT COUNT(hats) AS no_hats ...
When using aggregation function, you must give it an alias to be able to access it from the model. In the example above you can get the number of hats with instance.get('no_hats')
.
当使用聚合函数时,你一定要给它一个别名来使其能够从模型中被访问(上面就是为聚合函数COUNT(hats)起了no_hats别名)。在上面的例子中,你可以通过instance.get('no_hats')
得到hats的数量
Sometimes it may be tiresome to list all the attributes of the model if you only want to add an aggregation:
有时当你想要添加聚合时,你可能会厌倦列举模型的所有的属性:
// 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 ...
Similarly, it's also possible to remove a selected few attributes:
相似地,可以移除选出的一些属性
Model.findAll({
attributes: { exclude: ['baz'] }
}); SELECT id, foo, bar, quz ...
Where
Whether you are querying with findAll/find or doing bulk updates/destroys you can pass a where
object to filter the query.
无论你是通过findAll/find进行查询或是进行大量地updates/destroys操作,你都可以使用where
对象来过滤查询
where
generally takes an object from attribute:value pairs, where value can be primitives for equality matches or keyed objects for other operators.
通常where从attribute:value对中取出对象,value可能是平等匹配的原语或其他操作符的键值对象
It's also possible to generate complex AND/OR conditions by nesting sets of or
and and
Operators
.
通过嵌套or
和and
操作符集生成复杂的AND/OR条件是可能的
Basics
const Op = Sequelize.Op; Post.findAll({
where: {
authorId:
}
});
//等价于
// SELECT * FROM post WHERE authorId = 2 Post.findAll({
where: {
authorId: ,
status: 'active'
}
});
// SELECT * FROM post WHERE authorId = 12 AND status = 'active'; Post.findAll({
where: {
[Op.or]: [{authorId: }, {authorId: }]
}
});
// SELECT * FROM post WHERE authorId = 12 OR authorId = 13; Post.findAll({
where: {
authorId: {
[Op.or]: [, ]
}
}
});
// SELECT * FROM post WHERE authorId = 12 OR authorId = 13; 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')), )
});
// SELECT * FROM post WHERE char_length(status) = 6;
Operators
Sequelize exposes symbol operators that can be used for to create more complex comparisons -
Sequelize暴露symbol操作符,用于创建更多复杂的比较:
const Op = Sequelize.Op [Op.and]: {a: } // AND (a = 5)
[Op.or]: [{a: }, {a: }] // (a = 5 OR a = 6)
[Op.gt]: , // > 6
[Op.gte]: , // >= 6
[Op.lt]: , // < 10
[Op.lte]: , // <= 10
[Op.ne]: , // != 20
[Op.eq]: , // = 3
[Op.not]: true, // IS NOT TRUE
[Op.between]: [, ], // BETWEEN 6 AND 10
[Op.notBetween]: [, ], // NOT BETWEEN 11 AND 15
[Op.in]: [, ], // IN [1, 2]
[Op.notIn]: [, ], // NOT IN [1, 2]
[Op.like]: '%hat', // LIKE '%hat'
[Op.notLike]: '%hat' // NOT LIKE '%hat'
[Op.iLike]: '%hat' // ILIKE '%hat' (case insensitive) (PG only)
[Op.notILike]: '%hat' // NOT ILIKE '%hat' (PG only)
[Op.regexp]: '^[h|a|t]' // REGEXP/~ '^[h|a|t]' (MySQL/PG only)
[Op.notRegexp]: '^[h|a|t]' // NOT REGEXP/!~ '^[h|a|t]' (MySQL/PG only)
[Op.iRegexp]: '^[h|a|t]' // ~* '^[h|a|t]' (PG only)
[Op.notIRegexp]: '^[h|a|t]' // !~* '^[h|a|t]' (PG only)
[Op.like]: { [Op.any]: ['cat', 'hat']}
// LIKE ANY ARRAY['cat', 'hat'] - also works for iLike and notLike
[Op.overlap]: [, ] // && [1, 2] (PG array overlap operator)
[Op.contains]: [, ] // @> [1, 2] (PG array contains operator)
[Op.contained]: [, ] // <@ [1, 2] (PG array contained by operator)
[Op.any]: [,] // ANY ARRAY[2, 3]::INTEGER (PG only) [Op.col]: 'user.organization_id' // = "user"."organization_id", with dialect specific column identifiers, PG in this example
Range Operators范围操作符
Range types can be queried with all supported operators.
Range类型可以在所有支持的操作符中使用
Keep in mind, the provided range value can define the bound inclusion/exclusion as well.
注意,提供的范围值也可以定义边界的闭包,即[为闭口,(为开口。
// All the above equality and inequality operators plus the following: [Op.contains]: // @> '2'::integer (PG range contains element operator)
[Op.contains]: [, ] // @> [1, 2) (PG range contains range operator)
[Op.contained]: [, ] // <@ [1, 2) (PG range is contained by operator)
[Op.overlap]: [, ] // && [1, 2) (PG range overlap (have points in common) operator)
[Op.adjacent]: [, ] // -|- [1, 2) (PG range is adjacent to operator)
[Op.strictLeft]: [, ] // << [1, 2) (PG range strictly left of operator)
[Op.strictRight]: [, ] // >> [1, 2) (PG range strictly right of operator)
[Op.noExtendRight]: [, ] // &< [1, 2) (PG range does not extend to the right of operator)
[Op.noExtendLeft]: [, ] // &> [1, 2) (PG range does not extend to the left of operator)
Combinations连接起来使用
const Op = Sequelize.Op; {
rank: {
[Op.or]: {
[Op.lt]: ,
[Op.eq]: null
}
}
}
// rank < 1000 OR rank IS NULL {
createdAt: {
[Op.lt]: new Date(),
[Op.gt]: new Date(new Date() - * * * )
}
}
// createdAt < [timestamp] AND createdAt > [timestamp] {
[Op.or]: [
{
title: {
[Op.like]: 'Boat%'
}
},
{
description: {
[Op.like]: '%boat%'
}
}
]
}
// title LIKE 'Boat%' OR description LIKE '%boat%'
Operators Aliases操作符别名
Sequelize allows setting specific strings as aliases for operators -
Sequelize允许设置指定的字符串作为操作符的别名
const Op = Sequelize.Op;
const operatorsAliases = {
$gt: Op.gt
}
const connection = new Sequelize(db, user, pass, { operatorsAliases }) [Op.gt]: // > 6
$gt: // same as using Op.gt (> 6)
Operators security操作符安全性
Using Sequelize without any aliases improves security. Some frameworks automatically parse user input into js objects and if you fail to sanitize your input it might be possible to inject an Object with string operators to Sequelize.
使用没有任何别名的Sequelize提高了安全性。有些框架自动解析用户输入到js对象如果你未能清洁你的输入有可能注入Sequelize对象和字符串运算符
Not having any string aliases will make it extremely unlikely that operators could be injected but you should always properly validate and sanitize user input.
向后兼容性原因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, &&, @>, <@
For better security it is highly advised to use Sequelize.Op
and not depend on any string alias at all. You can limit alias your application will need by setting operatorsAliases
option, remember to sanitize user input especially when you are directly passing them to Sequelize methods.
为了更好的安全,强烈建议使用Sequelize.Op
和不依赖于任何字符串的别名。你可以通过设置别名operatorsAliases选项来限制你的应用程序需要的别名,记得要检查用户输入特别是当你直接传递他们给Sequelize的方法。
const Op = Sequelize.Op; //use sequelize without any operators aliases,不使用别名
const connection = new Sequelize(db, user, pass, { operatorsAliases: false }); //use sequelize with only alias for $and => Op.and,只使用Op.and一个别名
const connection2 = new Sequelize(db, user, pass, { operatorsAliases: { $and: Op.and } });
Sequelize will warn you if you're using the default aliases and not limiting them if you want to keep using all default aliases (excluding legacy ones) without the warning you can pass the following operatorsAliases option -
如果你使用默认的别名并没有进行限制,Sequelize会警告你,所以我得到了上面的警告。
如果你想继续使用所有缺省别名(不包括遗留的)没有警告,您可以通过设置以下operatorsAliases选项:
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 });
JSON
The JSON data type is supported by the PostgreSQL, SQLite and MySQL dialects only.
JSON数据类型只被PostgreSQL, SQLite and MySQL支持
PostgreSQL
The JSON data type in PostgreSQL stores the value as plain text, as opposed to binary representation. If you simply want to store and retrieve a JSON representation, using JSON will take less disk space and less time to build from its input representation. However, if you want to do any operations on the JSON value, you should prefer the JSONB data type described below.
在PostgreSQL中的JSON数据类型以纯文本的形式存储值,而不是二进制表示法。如果你只想存储和检索JSON表示法,那么使用JSON将会占用更少内存和时间来从它的输入表示法中构建数据。可是,如果你想要对json值进行一些操作,你会更偏向于下面提到的JSONB数据类型
MSSQL
MSSQL does not have a JSON data type, however it does provide support for JSON stored as strings through certain functions since SQL Server 2016. Using these functions, you will be able to query the JSON stored in the string, but any returned values will need to be parsed seperately.
MSSQL没有JSON数据类型,可是从SQL Server 2016起,他就通过一些函数提供将JSON存储为字符串的支持。使用这些函数,你将能够查询存储在字符串中的JSON,但是任何返回结果都需要分开解析
// ISJSON - to test if a string contains valid JSON
User.findAll({
where: sequelize.where(sequelize.fn('ISJSON', sequelize.col('userDetails')), )
}) // JSON_VALUE - extract a scalar value from a JSON string
User.findAll({
attributes: [[ sequelize.fn('JSON_VALUE', sequelize.col('userDetails'), '$.address.Line1'), 'address line 1']]
}) // JSON_VALUE - query a scalar value from a JSON string
User.findAll({
where: sequelize.where(sequelize.fn('JSON_VALUE', sequelize.col('userDetails'), '$.address.Line1'), '14, Foo Street')
}) // JSON_QUERY - extract an object or array
User.findAll({
attributes: [[ sequelize.fn('JSON_QUERY', sequelize.col('userDetails'), '$.address'), 'full address']]
})
JSONB
JSONB can be queried in three different ways.
可以以下面三种不同方式进行查询:
Nested object
{
meta: {
video: {
url: {
[Op.ne]: null
}
}
}
}
Nested key
{
"meta.audio.length": {
[Op.gt]:
}
}
Containment
{
"meta": {
[Op.contains]: {
site: {
url: 'http://google.com'
}
}
}
}
Relations / Associations关联性
// Find all projects with a least one task where task.state === project.state
Project.findAll({
include: [{
model: Task,
where: { state: Sequelize.col('project.state') }
}]
})
Pagination / Limiting限制
// Fetch 10 instances/rows
Project.findAll({ limit: }) // Skip 8 instances/rows
Project.findAll({ offset: }) // Skip 5 instances and fetch the 5 after that
Project.findAll({ offset: , limit: })
Ordering排序
order
takes an array of items to order the query by or a sequelize method. Generally you will want to use a tuple/array of either attribute, direction or just direction to ensure proper escaping.
order通过sequelize方获得
数组项去排序查询。通常需要使用每个元组/数组的属性,方向或仅仅是方向,以确保适当的排除。
Subtask.findAll({
order: [
// Will escape title and validate DESC against a list of valid direction parameters
['title', 'DESC'], // Will order by max(age)
sequelize.fn('max', sequelize.col('age')), // Will order by max(age) DESC
[sequelize.fn('max', sequelize.col('age')), 'DESC'], // Will order by otherfunction(`col1`, 12, 'lalala') DESC
[sequelize.fn('otherfunction', sequelize.col('col1'), , 'lalala'), 'DESC'], // Will order an associated model's created_at using the model name as the association's name.
[Task, 'createdAt', 'DESC'], // Will order through an associated model's created_at using the model names as the associations' names.
[Task, Project, 'createdAt', 'DESC'], // Will order by an associated model's created_at using the name of the association.
['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. (preferred method)
[Subtask.associations.Task, 'createdAt', 'DESC'], // Will order by a nested associated model's created_at using association objects. (preferred method)
[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'], // Will order by a nested associated model's created_at simple association objects.
[{model: Task, as: 'Task'}, {model: Project, as: 'Project'}, 'createdAt', 'DESC']
] // Will order by max age descending
order: sequelize.literal('max(age) DESC') // Will order by max age ascending assuming ascending is the default order when direction is omitted
order: sequelize.fn('max', sequelize.col('age')) // Will order by age ascending assuming ascending is the default order when direction is omitted
order: sequelize.col('age') // Will order randomly based on the dialect (instead of fn('RAND') or fn('RANDOM'))
order: sequelize.random()
})
Table Hint表提示
tableHint
can be used to optionally pass a table hint when using mssql. The hint must be a value from Sequelize.TableHints
and should only be used when absolutely necessary. Only a single table hint is currently supported per query.
tableHint
可以在使用mssql时选择性地传递一些表提示。提示必须是来自Sequelize.TableHints
的值并只有在必须的时候才使用。现在在每个查询中只有单表提示被支持
Table hints override the default behavior of mssql query optimizer by specifing certain options. They only affect the table or view referenced in that clause.
表提示通过指定某些选项来覆盖mssql查询优化器的默认行为。他们仅仅影响表或子句中的视图引用
const TableHints = Sequelize.TableHints; Project.findAll({
// adding the table hint NOLOCK
tableHint: TableHints.NOLOCK
// this will generate the SQL 'WITH (NOLOCK)'
})
Sequelize-nodejs-5-Querying的更多相关文章
- 【前端】nodejs的ORM框架sequelize的工厂化
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/sequelize_factory.html 一.什么是sequelize nodejs的后台在操作数据库的时候,需 ...
- nodejs项目mysql使用sequelize支持存储emoji
nodejs项目mysql使用sequelize支持存储emoji 本篇主要记录nodejs项目阿里云mysql如何支持存储emoji表情. 因由 最近项目遇到用户在文本输入emoji进行存储的时候导 ...
- koa2+log4js+sequelize搭建的nodejs服务
主要参考http://www.jianshu.com/p/6b816c609669这篇文章 npm安装使用国内taobao镜像,速度更快些 npm --registry https://registr ...
- 从 moment -> nodejs -> sequelize -> postgres,你都得设置好时区
背景 最近在做报表统计,因为 sequelize 的时区配置没加导致了统计数字对不上的问题. 问:大家都知道时区,但是你清楚 UTC 和 GMT 的区别吗? 答:UTC 是我们现在用的时间标准,GMT ...
- nodejs sequelize 对应数据库操作符的定义
const Op = Sequelize.Op [Op.and]: {a: } // 且 (a = 5) [Op.or]: [{a: }, {a: }] // (a = 5 或 a = 6) [Op. ...
- postgresql on centos (sequelize+pg+nodejs):Failed to find PostgresSQL server.Pleast double check your settings
公司的一个项目,使用的nodejs做服务端,数据库是postgresql,在本地时一切ok,放在centos时,postgresql配置ok,可以远程访问,但是nodejs在centos启动时,就会报 ...
- 全栈项目|小书架|服务器开发-NodeJS 中使用 Sequelize 操作 MySQL数据库
安装 官网:https://sequelize.org/v5/manual/getting-started.html 安装sequelize及数据库连接驱动 npm install --save se ...
- 项目总结,彻底掌握NodeJS中如何使用Sequelize
前言 sequelize是什么? sequelize是基于NodeJs的ORM框架,它适用于不同的数据库,如:Postgres.MySQL.SQLite.MariaDB,我们可以通过sequelize ...
- 基于Nodejs的sequelize操纵数据库
## 使用基于ORM架构的sequelize操纵数据库 ### 1.技术背景 ```Sequelize是一个基于promise的关系型数据库ORM框架,*********************技术文 ...
- Nodejs ORM框架Sequelize快速入门
Nodejs ORM框架Sequelize快速入门 什么是ORM? 简单的讲就是对SQL查询语句的封装,让我们可以用OOP的方式操作数据库,优雅的生成安全.可维护的SQL代码.直观上,是一种Model ...
随机推荐
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- K:有限状态自动机
有限状态自动机是一种特殊的状态机.它表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型.有限状态自动机分为两种,一种是 确定有限状态自动机(DFA) ,一种是 非确定有限状态自动机(NF ...
- 中小型研发团队架构实践五:Redis快速入门及应用
Redis的使用难吗?不难,Redis用好容易吗?不容易.Redis的使用虽然不难,但与业务结合的应用场景特别多.特别紧,用好并不容易.我们希望通过一篇文章及Demo,即可轻松.快速入门并学会应用. ...
- BZOJ4566: [Haoi2016]找相同字符(后缀自动机)
题意 题目链接 Sol 直接在SAM上乱搞 枚举前缀,用SAM统计可以匹配的后缀,具体在匹配的时候维护和当前节点能匹配的最大值 然后再把parent树上的点的贡献也统计上,这部分可以爆跳parent树 ...
- 百万级数据 MySQL处理(转)
转自 http://www.cnblogs.com/win7xt/p/3156334.html 使用MySQL处理百万级以上数据时,不得不知道的几个常识 最近一段时间参与的项目要操作百万级数据量的 ...
- Pwn with File结构体(四)
前言 前面几篇文章说道,glibc 2.24 对 vtable 做了检测,导致我们不能通过伪造 vtable 来执行代码.今天逛 twitter 时看到了一篇通过绕过 对vtable 的检测 来执行代 ...
- Eclipse 校验取消
eclipse Multiple annotations found at this line错误,eclipse开发过程中,一些XML配置文件会报错,但是这些其实不是错,飘红的原因是因为eclips ...
- 带你从零学ReactNative开发跨平台App开发(十一)
ReactNative跨平台开发系列教程: 带你从零学ReactNative开发跨平台App开发(一) 带你从零学ReactNative开发跨平台App开发(二) 带你从零学ReactNative开发 ...
- qt cmake
写在前面的话:强烈建议优先阅读Qt官网文档对cmake的使用介绍——CMake Manual 前言我去年用clion写Qt的时候,找了很多教程,也没有什么让我觉得很满意的.后来自己摸索,构建了一个我自 ...
- Week4——Hello.java分析
如下图源码所示: 该段代码声明了一个entity实体类,该类有一个变量name,对该变量写了对应的get和set方法.类中还有一个空的构造方法hello(). @RequestScoped用于指定一个 ...