Add JS query example in relation documentation

This commit is contained in:
Jim LAURIE 2018-12-26 12:30:52 +01:00
parent e4dc27be54
commit 24db8ef868

View File

@ -148,6 +148,17 @@ module.exports = {
}
```
**Example**
```js
// Create a pet
var xhr = new XMLHttpRequest();
xhr.open("POST", "/pets", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
owner: '5c151d9d5b1d55194d3209be' // The id of the user you want to link
}));
```
### One-to-one
Refer to the [one-to-one concept](../concepts/concepts.md#one-to-one) for informations.
@ -211,6 +222,17 @@ module.exports = {
}
```
**Example**
```js
// Create an address
var xhr = new XMLHttpRequest();
xhr.open("POST", "/addresses", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
user: '5c151d9d5b1d55194d3209be' // The id of the user you want to link
}));
```
### One-to-many
Refer to the [one-to-many concept](../concepts/concepts.md#one-to-many) for more informations.
@ -274,6 +296,25 @@ module.exports = {
}
```
**Examples**
```js
// Create an article
var xhr = new XMLHttpRequest();
xhr.open("POST", "/articles", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
author: '5c151d9d5b1d55194d3209be' // The id of the user you want to link
}));
// Update an article
var xhr = new XMLHttpRequest();
xhr.open("PUT", "/users/5c151d9d5b1d55194d3209be", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
articles: ['5c151d51eb28fd19457189f6', '5c151d51eb28fd19457189f8'] // Set of ALL articles linked to the user (existing articles + new article or - removed article)
}));
```
### Many-to-many
Refer to the [many-to-many concept](../concepts/concepts.md#many-to-many).
@ -343,6 +384,17 @@ module.exports = {
}
```
**Example**
```js
// Update a product
var xhr = new XMLHttpRequest();
xhr.open("PUT", "/products/5c151d9d5b1d55194d3209be", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
categories: ['5c151d51eb28fd19457189f6', '5c151d51eb28fd19457189f8'] // Set of ALL categories linked to the product (existing categories + new category or - removed category)
}));
```
### Polymorphic
The polymorphic relationships are the solution when you don't know which kind of model will be associated to your entry. A common use case is an `Image` model that can be associated to many others kind of models (Article, Product, User, etc).