Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add metadata in relationships doc #1389

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions docs/relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Cozy-Client [predefines](https://github.com/cozy/cozy-client/blob/master/package

#### Files relations

The files implements a special type of relation: `'io.cozy.files:has-many'`. This relation name is `referenced_by`, which is an array of references, just like the `'has-many'` relation.
The files implements a special type of relation: `'io.cozy.files:has-many'`. This relation name is `referenced_by`, which is an array of references, just like the `'has-many'` relation.

The stack implements routes to handle this kind of relation on the `/files` endpoint. See the [stack documentation](https://docs.cozy.io/en/cozy-stack/references-docs-in-vfs/).

Expand All @@ -77,7 +77,7 @@ You are free to create your own relation type if you need a special behaviour.

For instance, [Banks doctypes](https://github.com/cozy/cozy-banks/blob/master/src/doctypes.js) implements `HasManyBills` or `HasManyReimbursements`.

#### Old relation types
#### Old relation types

Note there are two others basic relations, that are here for backward compatibility:

Expand Down Expand Up @@ -110,6 +110,25 @@ const book = {
}
```

### Metadata

It is possible to assign metadatas to relationships. You need to modify the content of the document relationships somehow and save the modified document.

```javascript
const doc = {
_id: "mobydick",
relationships: {
authors: {
data: [{
_id: "hermanmelville",
_type: "io.cozy.contacts"
}]
}
}
}
doc.relationships.authors.data[0].metadata = { addressId: "123" }
await client.save(doc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems kind of tedious :/ Using doc.relationships.authors.add({_id: ..., metadata: ...}) does not work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paultranvan non ça ne fonctionne pas, l'argument est le doc en question à lier, alors que concernant les metadata on ne veut pas les mettre sur le doc mais sur la relationships elle-même.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's make an issue then: #1393

```

## Usage

Expand All @@ -123,9 +142,9 @@ const query = Q('io.cozy.books')
.limitBy(20)
```

You will then find your relations under the `data` attribute:
You will then find your relations under the `data` attribute:

```javascript
```javascript
const response = await client.query(query)
const docs = response.data
const firstDoc = docs[0]
Expand Down
Loading