不要怂,就是干,撸起袖子干!

Commit a7405663 by Mick Hansen

docs(where/query): update where logic identifiers

1 parent d8576300
Showing with 39 additions and 34 deletions
...@@ -647,7 +647,7 @@ Project ...@@ -647,7 +647,7 @@ Project
.findAndCountAll({ .findAndCountAll({
where: { where: {
title: { title: {
like: 'foo%' $like: 'foo%'
} }
}, },
offset: 10, offset: 10,
...@@ -689,29 +689,25 @@ Project.findAll({ where: { id: [1,2,3] } }).then(function(projects) { ...@@ -689,29 +689,25 @@ Project.findAll({ where: { id: [1,2,3] } }).then(function(projects) {
// this is actually doing an IN query // this is actually doing an IN query
}) })
   
// or
Project.findAll({ where: "name = 'A Project'" }).then(function(projects) {
// the difference between this and the usage of hashes (objects) is, that string usage
// is not sql injection safe. so make sure you know what you are doing!
})
 
// since v1.7.0 we can now improve our where searches
Project.findAll({ Project.findAll({
where: { where: {
id: { id: {
gt: 6, // id > 6 $gt: 6, // id > 6
gte: 6, // id >= 6 $gte: 6, // id >= 6
lt: 10, // id < 10 $lt: 10, // id < 10
lte: 10, // id $lte: 10, // id
ne: 20, // id != 20 $ne: 20, // id != 20
between: [6, 10], // BETWEEN 6 AND 10 $not: 3, // id NOT 3
nbetween: [11, 15], // NOT BETWEEN 11 AND 15 $between: [6, 10], // BETWEEN 6 AND 10
in: [1, 2], // IN [1, 2] $notBetween: [11, 15], // NOT BETWEEN 11 AND 15
like: '%hat', // LIKE '%hat' $in: [1, 2], // IN [1, 2]
nlike: '%hat' // NOT LIKE '%hat' $like: '%hat', // LIKE '%hat'
ilike: '%hat' // ILIKE '%hat' (case insensitive) $notLike: '%hat' // NOT LIKE '%hat'
nilike: '%hat' // NOT ILIKE '%hat' $iLike: '%hat' // ILIKE '%hat' (case insensitive)
overlap: [1, 2] // && [1, 2] (PG array overlap operator) $notILike: '%hat' // NOT ILIKE '%hat'
$overlap: [1, 2] // && [1, 2] (PG array overlap operator)
$contains: [1, 2] // @> [1, 2] (PG array contains operator)
$contained: [1, 2] // <@ [1, 2] (PG array contained by operator)
} }
} }
}) })
...@@ -719,35 +715,44 @@ Project.findAll({ ...@@ -719,35 +715,44 @@ Project.findAll({
### Complex filtering / OR queries ### Complex filtering / OR queries
Since `v1.7.0-rc3`, it is possible to do complex where queries with multiple levels of nested AND and OR conditions. In order to do that you can use `Sequelize.or` and `Sequelize.and` and pass an arbitrary amount of arguments to it. Every argument will get transformed into a proper SQL condition and gets joined with the either `AND` or `OR`. It's possible to do complex where queries with multiple levels of nested AND and OR conditions. In order to do that you can use `$or` and `$and`:
```js ```js
Project.find({ Project.find({
where: Sequelize.and( where: {
{ name: 'a project' }, name: 'a project',
Sequelize.or( $or: [
{ id: [1,2,3] }, { id: [1,2,3] },
{ id: { gt: 10 } } { id: { $gt: 10 } }
) ]
) }
})
Project.find({
where: {
name: 'a project',
id: {
$or: [
[1,2,3],
{ $gt: 10 }
]
}
}
}) })
``` ```
This code will generate the following query: Both pieces of code code will generate the following:
```sql ```sql
SELECT * SELECT *
FROM `Projects` FROM `Projects`
WHERE ( WHERE (
`Projects`.`name`='a project' `Projects`.`name` = 'a project'
AND (`Projects`.`id` IN (1,2,3) OR `Projects`.`id` > 10) AND (`Projects`.`id` IN (1,2,3) OR `Projects`.`id` > 10)
) )
LIMIT 1 LIMIT 1;
;
``` ```
Notice, that instead of `Sequelize.and` you can also use a plain array which will be treated as `Sequelize.and` if it contains objects or hashes or other complex data types. Furthermore you can use `Sequelize.or` as value for the where clause.
### Manipulating the dataset with limit&comma; offset&comma; order and group ### Manipulating the dataset with limit&comma; offset&comma; order and group
To get more relevant data&comma; you can use limit&comma; offset&comma; order and grouping&colon; To get more relevant data&comma; you can use limit&comma; offset&comma; order and grouping&colon;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!