Skip to content

Commit

Permalink
feat(query-generator): change operators to be represented by symbols (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yonjah authored and eseliger committed Sep 30, 2017
1 parent 9ca86af commit ccb99da
Show file tree
Hide file tree
Showing 25 changed files with 712 additions and 300 deletions.
72 changes: 36 additions & 36 deletions docs/models-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Project
.findAndCountAll({
where: {
title: {
$like: 'foo%'
[Op.like]: 'foo%'
}
},
offset: 10,
Expand Down Expand Up @@ -168,44 +168,44 @@ Project.findAll({ where: { id: [1,2,3] } }).then(projects => {
Project.findAll({
where: {
id: {
$and: {a: 5} // AND (a = 5)
$or: [{a: 5}, {a: 6}] // (a = 5 OR a = 6)
$gt: 6, // id > 6
$gte: 6, // id >= 6
$lt: 10, // id < 10
$lte: 10, // id <= 10
$ne: 20, // id != 20
$between: [6, 10], // BETWEEN 6 AND 10
$notBetween: [11, 15], // NOT BETWEEN 11 AND 15
$in: [1, 2], // IN [1, 2]
$notIn: [1, 2], // NOT IN [1, 2]
$like: '%hat', // LIKE '%hat'
$notLike: '%hat' // NOT LIKE '%hat'
$iLike: '%hat' // ILIKE '%hat' (case insensitive) (PG only)
$notILike: '%hat' // NOT ILIKE '%hat' (PG only)
$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)
$any: [2,3] // ANY ARRAY[2, 3]::INTEGER (PG only)
[Op.and]: {a: 5} // AND (a = 5)
[Op.or]: [{a: 5}, {a: 6}] // (a = 5 OR 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.between]: [6, 10], // BETWEEN 6 AND 10
[Op.notBetween]: [11, 15], // NOT BETWEEN 11 AND 15
[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.iLike]: '%hat' // ILIKE '%hat' (case insensitive) (PG only)
[Op.notILike]: '%hat' // NOT ILIKE '%hat' (PG only)
[Op.overlap]: [1, 2] // && [1, 2] (PG array overlap operator)
[Op.contains]: [1, 2] // @> [1, 2] (PG array contains operator)
[Op.contained]: [1, 2] // <@ [1, 2] (PG array contained by operator)
[Op.any]: [2,3] // ANY ARRAY[2, 3]::INTEGER (PG only)
},
status: {
$not: false, // status NOT FALSE
[Op.not]: false, // status NOT FALSE
}
}
})
```

### Complex filtering / OR / NOT queries

It's possible to do complex where queries with multiple levels of nested AND, OR and NOT conditions. In order to do that you can use `$or`, `$and` or `$not`:
It's possible to do complex where queries with multiple levels of nested AND, OR and NOT conditions. In order to do that you can use `or`, `and` or `not` `Operators`:

```js
Project.findOne({
where: {
name: 'a project',
$or: [
[Op.or]: [
{ id: [1,2,3] },
{ id: { $gt: 10 } }
{ id: { [Op.gt]: 10 } }
]
}
})
Expand All @@ -214,9 +214,9 @@ Project.findOne({
where: {
name: 'a project',
id: {
$or: [
[Op.or]: [
[1,2,3],
{ $gt: 10 }
{ [Op.gt]: 10 }
]
}
}
Expand All @@ -235,15 +235,15 @@ WHERE (
LIMIT 1;
```

`$not` example:
`not` example:

```js
Project.findOne({
where: {
name: 'a project',
$not: [
[Op.not]: [
{ id: [1,2,3] },
{ array: { $contains: [3,4,5] } }
{ array: { [Op.contains]: [3,4,5] } }
]
}
});
Expand Down Expand Up @@ -338,7 +338,7 @@ Project.count().then(c => {
console.log("There are " + c + " projects!")
})

Project.count({ where: {'id': {$gt: 25}} }).then(c => {
Project.count({ where: {'id': {[Op.gt]: 25}} }).then(c => {
console.log("There are " + c + " projects with an id greater than 25.")
})
```
Expand All @@ -358,7 +358,7 @@ Project.max('age').then(max => {
// this will return 40
})

Project.max('age', { where: { age: { lt: 20 } } }).then(max => {
Project.max('age', { where: { age: { [Op.lt]: 20 } } }).then(max => {
// will be 10
})
```
Expand All @@ -378,7 +378,7 @@ Project.min('age').then(min => {
// this will return 5
})

Project.min('age', { where: { age: { $gt: 5 } } }).then(min => {
Project.min('age', { where: { age: { [Op.gt]: 5 } } }).then(min => {
// will be 10
})
```
Expand All @@ -399,7 +399,7 @@ Project.sum('age').then(sum => {
// this will return 55
})

Project.sum('age', { where: { age: { $gt: 5 } } }).then(sum => {
Project.sum('age', { where: { age: { [Op.gt]: 5 } } }).then(sum => {
// will be 50
})
```
Expand Down Expand Up @@ -550,7 +550,7 @@ User.findAll({
include: [{
model: Tool,
as: 'Instruments',
where: { name: { $like: '%ooth%' } }
where: { name: { [Op.like]: '%ooth%' } }
}]
}).then(users => {
console.log(JSON.stringify(users))
Expand Down Expand Up @@ -597,7 +597,7 @@ To move the where conditions from an included model from the `ON` condition to t
```js
User.findAll({
where: {
'$Instruments.name$': { $iLike: '%ooth%' }
'$Instruments.name$': { [Op.iLike]: '%ooth%' }
},
include: [{
model: Tool,
Expand Down Expand Up @@ -653,7 +653,7 @@ In case you want to eager load soft deleted records you can do that by setting `
User.findAll({
include: [{
model: Tool,
where: { name: { $like: '%ooth%' } },
where: { name: { [Op.like]: '%ooth%' } },
paranoid: false // query and loads the soft deleted records
}]
});
Expand Down
Loading

0 comments on commit ccb99da

Please sign in to comment.