Add feature to allow nested include and filtering relationships/associations #36

Open
labibramadhan wants to merge 8 commits from labibramadhan/include-with-filter into master
Showing only changes of commit 1977304287 - Show all commits

View File

@ -92,24 +92,14 @@ It's easy to restrict your requests using Sequelize's `where` query option. Just
Team.findOne({ where: { city: 'windsor' }}) Team.findOne({ where: { city: 'windsor' }})
``` ```
You can also do more complex queries by setting the value of a key to JSON.
```js
// returns only teams that have a `address.city` property of "windsor"
// GET /team?city={"address": "windsor"}
// or
// GET /team?city[address]=windsor
// results in the Sequelize query:
Team.findOne({ where: { address: { city: 'windsor' }}})
```
## `include` queries ## `include` queries
Getting related models is easy, just use a query parameter `include`. Getting related models is easy, just use a query parameter `include`.
```js ```js
// returns all teams with their related City model // returns all teams with their related City model
// GET /teams?include=city // GET /teams?include=city or
// GET /teams?include={"include": {"model": "City"}}}
// results in a Sequelize query: // results in a Sequelize query:
Team.findAll({include: City}) Team.findAll({include: City})
@ -133,6 +123,15 @@ For models that have a many-to-many relationship, you can also pass the plural v
Team.findAll({include: [Player]}) Team.findAll({include: [Player]})
``` ```
Filtering by related models property, you can pass **where** paremeter inside each **include** item(s) object.
```js
// returns all team with their related City where City property name equals Healdsburg
// GET /teams?include={"include": {"model": "City", "where": {"name": "Healdsburg"}}}
// results in a Sequelize query:
Team.findAll({include: {model: City, where: {name: 'Healdsburg'}}})
```
## `limit` and `offset` queries ## `limit` and `offset` queries
Restricting list (`GET`) and scope queries to a restricted count can be done by passing `limit=<number>` and/or `offset=<number>`. Restricting list (`GET`) and scope queries to a restricted count can be done by passing `limit=<number>` and/or `offset=<number>`.