-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
144 changed files
with
4,872 additions
and
1,292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
--- | ||
title: Join Field | ||
label: Join | ||
order: 140 | ||
desc: The Join field provides the ability to work on related documents. Learn how to use Join field, see examples and options. | ||
keywords: join, relationship, junction, fields, config, configuration, documentation, Content Management System, cms, headless, javascript, node, react, nextjs | ||
--- | ||
|
||
The Join Field is used to make Relationship fields in the opposite direction. It is used to show the relationship from | ||
the other side. The field itself acts as a virtual field, in that no new data is stored on the collection with a Join | ||
field. Instead, the Admin UI surfaces the related documents for a better editing experience and is surfaced by Payload's APIs. | ||
|
||
The Join field is useful in scenarios including: | ||
|
||
- To see related `Products` on an `Order` | ||
- To view and edit `Posts` belonging to a `Category` | ||
- To work with any bi-directional relationship data | ||
|
||
For the Join field to work, you must have an existing [relationship](./relationship) field in the collection you are | ||
joining. This will reference the collection and path of the field of the related documents. | ||
To add a Relationship Field, set the `type` to `join` in your [Field Config](./overview): | ||
|
||
```ts | ||
import type { Field } from 'payload/types' | ||
|
||
export const MyJoinField: Field = { | ||
// highlight-start | ||
name: 'relatedPosts', | ||
type: 'join', | ||
collection: 'posts', | ||
on: 'category', | ||
// highlight-end | ||
} | ||
|
||
// relationship field in another collection: | ||
export const MyRelationshipField: Field = { | ||
name: 'category', | ||
type: 'relationship', | ||
relationTo: 'categories', | ||
} | ||
``` | ||
|
||
In this example, the field is defined to show the related `posts` when added to a `category` collection. The `on` property is used to | ||
specify the relationship field name of the field that relates to the collection document. | ||
|
||
## Config Options | ||
|
||
| Option | Description | | ||
|------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ||
| **`name`** \* | To be used as the property name when retrieved from the database. [More](/docs/fields/overview#field-names) | | ||
| **`collection`** \* | The `slug`s having the relationship field. | | ||
| **`on`** \* | The relationship field name of the field that relates to collection document. Use dot notation for nested paths, like 'myGroup.relationName'. | | ||
| **`maxDepth`** | Default is 1, Sets a maximum population depth for this field, regardless of the remaining depth when this field is reached. [Max Depth](/docs/getting-started/concepts#field-level-max-depth) | | ||
| **`label`** | Text used as a field label in the Admin Panel or an object with keys for each language. | | ||
| **`hooks`** | Provide Field Hooks to control logic for this field. [More details](../hooks/fields). | | ||
| **`access`** | Provide Field Access Control to denote what users can see and do with this field's data. [More details](../access-control/fields). | | ||
| **`localized`** | Enable localization for this field. Requires [localization to be enabled](/docs/configuration/localization) in the Base config. | | ||
| **`required`** | Require this field to have a value. | | ||
| **`admin`** | Admin-specific configuration. | | ||
| **`custom`** | Extension point for adding custom data (e.g. for plugins) | | ||
| **`typescriptSchema`** | Override field type generation with providing a JSON schema | | ||
|
||
_\* An asterisk denotes that a property is required._ | ||
|
||
## Join Field Data | ||
|
||
When a document is returned that for a Join field is populated with related documents. The structure returned is an object with: | ||
|
||
- `docs` an array of related documents or only IDs if the depth is reached | ||
- `hasNextPage` a boolean indicating if there are additional documents | ||
|
||
```json | ||
{ | ||
"id": "66e3431a3f23e684075aae9c", | ||
"relatedPosts": { | ||
"docs": [ | ||
{ | ||
"id": "66e3431a3f23e684075aaeb9", | ||
// other fields... | ||
"category": "66e3431a3f23e684075aae9c", | ||
}, | ||
// { ... } | ||
], | ||
"hasNextPage": false | ||
}, | ||
// other fields... | ||
} | ||
``` | ||
|
||
## Query Options | ||
|
||
The Join Field supports custom queries to filter, sort, and limit the related documents that will be returned. In addition to the specific query options for each Join Field, you can pass `joins: false` to disable all Join Field from returning. This is useful for performance reasons when you don't need the related documents. | ||
|
||
The following query options are supported: | ||
|
||
| Property | Description | | ||
|-------------|--------------------------------------------------------------| | ||
| **`limit`** | The maximum related documents to be returned, default is 10. | | ||
| **`where`** | An optional `Where` query to filter joined documents. | | ||
| **`sort`** | A string used to order related results | | ||
|
||
These can be applied to the local API, GraphQL, and REST API. | ||
|
||
### Local API | ||
|
||
By adding `joins` to the local API you can customize the request for each join field by the `name` of the field. | ||
|
||
```js | ||
const result = await db.findOne('categories', { | ||
where: { | ||
title: { | ||
equals: 'My Category' | ||
} | ||
}, | ||
joins: { | ||
relatedPosts: { | ||
limit: 5, | ||
where: { | ||
title: { | ||
equals: 'My Post' | ||
} | ||
}, | ||
sort: 'title' | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
### Rest API | ||
|
||
The rest API supports the same query options as the local API. You can use the `joins` query parameter to customize the request for each join field by the `name` of the field. For example, an API call to get a document with the related posts limited to 5 and sorted by title: | ||
|
||
`/api/categories/${id}?joins[relatedPosts][limit]=5&joins[relatedPosts][sort]=title` | ||
|
||
You can specify as many `joins` parameters as needed for the same or different join fields for a single request. | ||
|
||
### GraphQL | ||
|
||
Coming soon. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.