# CRUD API Relationships Extension

Version: 0.1.0 · Status: draft · License: Apache-2.0

Source: lingodata/crud@bc225ba

## 1. Relationships

The **Relationships** extension defines how Resources reference other Resources and external resources. It is optional: Servers that do not implement it are not required to follow any rule in this document.

This extension requires the Base Specification `^0.2.0` and the References type set of [ECSON](https://github.com/lingodata/ecson) ([ECSON section 4.2](../ecson.md#42-references-type-set)), which defines the `$rel` typed value.

> **Tip (non-normative): Example**
>
> ```json
> {
>   "_id": "0190a5f2-7c3e-7b1a-9f4e-2d8c6b1e4a90",
>   "title": "Hello World",
>   "author": { "$rel": { "collection": "users", "id": "0190a5f2-1111-7000-8000-000000000001" } },
>   "tags": { "$rel": { "collection": "tags", "ids": ["promo", "delivery"] } },
>   "customer": { "$rel": "https://api.example.com/customers/42" },
>   "reviewers": { "$rel": "<https://example.com/u/42>; rel=\"author\", <https://example.com/u/7>; rel=\"editor\"" },
>   "related": { "$rel": [{ "collection": "products", "id": "p1" }, { "href": "https://example.com/b", "rel": "related" }] }
> }
> ```

### 1.1 Relationship Fields

<a id="REL_FIELD"></a>**Rule `REL_FIELD`**: MUST

A relationship is a Field whose value is a reference ([ECSON `$rel`](../ecson.md#42-references-type-set)). Servers MUST store references in the native form of the Data source ([Base `REPR_NATIVE_STORAGE`](./base.md#REPR_NATIVE_STORAGE)), return them as references, and MUST NOT convert other values into relationships.

> **Note (non-normative)**
>
> In the plain JSON Representation, a reference travels in its plain JSON form ([ECSON section 5.3](../ecson.md#53-plain-json-form)), such as a plain collection object or a URI string, and a value read as plain JSON is never a reference ([Base `REPR_NO_CONVERSION`](./base.md#REPR_NO_CONVERSION)). Clients that create relationships use ECSON, or a schema with `refCollection` (Schema extension).

<a id="REL_WRITE_REFERENCE"></a>**Rule `REL_WRITE_REFERENCE`**: MUST

On write operations, Servers MUST store only the reference: the `data` member of collection objects ([ECSON `REF_COLLECTION_DATA`](../ecson.md#REF_COLLECTION_DATA)) MUST be ignored.

> **Note (non-normative): Rationale**
>
> Recognizing relationships by the shape of a Field would require knowing which Fields are relationships, which is a schema. With the ECSON `$rel` type, a relationship is recognized by its value alone, and denormalized data, when needed, goes in regular Fields (for example `authorName`).

### 1.2 Targets

<a id="REL_INTERNAL"></a>**Rule `REL_INTERNAL`**: MUST

A relationship to Resources in the same Data source MUST use the [ECSON collection object](../ecson.md#42-references-type-set) form, with the Collection Slug in `collection`.

<a id="REL_EXTERNAL"></a>**Rule `REL_EXTERNAL`**: MUST

A relationship to anything else, including Resources of another Data source in the same API, MUST use the URI, Link header ([RFC 8288](https://www.rfc-editor.org/rfc/rfc8288)) or link object ([RFC 9264](https://www.rfc-editor.org/rfc/rfc9264)) forms, alone or combined in arrays.

<a id="REL_EXTERNAL_NO_RESOLVE"></a>**Rule `REL_EXTERNAL_NO_RESOLVE`**: MUST NOT

Servers MUST NOT resolve or validate external relationships: they are opaque references for Clients to resolve.

### 1.3 Cardinality

| Cardinality | Form | Empty |
|---|---|---|
| One | collection object with `id`, a URI, a link object, or a Link header with one link | `{ "$rel": null }` |
| Many | collection object with `ids`, a Link header with several links, or an array | `{ "$rel": { "collection": "tags", "ids": [] } }` or `{ "$rel": [] }` |

<a id="REL_CARDINALITY"></a>**Rule `REL_CARDINALITY`**: MUST

Servers MUST preserve the cardinality chosen by the Client: a relationship written with a single reference MUST be returned with a single reference, and one written with several references (`ids`, an array or a Link header with several links) MUST be returned in the same form, in the same order.

### 1.4 Integrity

<a id="REL_VALIDATE"></a>**Rule `REL_VALIDATE`**: MAY

Servers MAY check that internal relationships refer to existing Resources, and fail write operations with the [`INVALID_INPUT`](./base.md#INVALID_INPUT) error when they do not.

> **Note (non-normative)**
>
> This extension does not define what happens to relationships when a referenced Resource is deleted (cascade, nullify or nothing); each Server decides and SHOULD document it.

### 1.5 Expansion

The **Expand Parameter** asks the Server to include the referenced Resources in the Result, inside the relationship values.

<a id="REL_EXPAND"></a>**Rule `REL_EXPAND`**: MAY

Servers MAY support an Expand Parameter in read operations: a field list ([Base section 3.3.1](./base.md#331-field-lists)) of relationship Fields. For each listed Field with an internal relationship, the Server MUST include the referenced Resources in the `data` member of the collection object.

<a id="REL_EXPAND_MISSING"></a>**Rule `REL_EXPAND_MISSING`**: MUST

When an expanded reference does not exist, its `data` MUST be `null` (or a `null` element, for `ids`), and the Server MUST report the [`REFERENCE_NOT_FOUND`](#REFERENCE_NOT_FOUND) Warning, with the `field` and the `id`.

<a id="REL_EXPAND_EXTERNAL"></a>**Rule `REL_EXPAND_EXTERNAL`**: MUST

External relationships MUST NOT be expanded; listing them in the Expand Parameter MUST be reported as an [`EXPAND_IGNORED`](#EXPAND_IGNORED) Warning, with the `field`.

<a id="REL_EXPAND_NESTED"></a>**Rule `REL_EXPAND_NESTED`**: MAY

Servers MAY support nested expansion with field paths through expanded Resources (for example `author.company`), and MAY limit its depth, reporting the [`EXPAND_DEPTH_LIMITED`](#EXPAND_DEPTH_LIMITED) Warning, with the `field`.

> **Tip (non-normative): Example**
>
> `list` on `posts` with the Expand Parameter `author` returns:
>
> ```json
> {
>   "_id": "0190a5f2-7c3e-7b1a-9f4e-2d8c6b1e4a90",
>   "title": "Hello World",
>   "author": { "$rel": {
>     "collection": "users",
>     "id": "0190a5f2-1111-7000-8000-000000000001",
>     "data": { "_id": "0190a5f2-1111-7000-8000-000000000001", "name": "Ana" }
>   } }
> }
> ```
> The relationship keeps its reference, so the Resource can be written back unchanged: `data` is ignored on writes ([`REL_WRITE_REFERENCE`](#REL_WRITE_REFERENCE)).

> **Note (non-normative): Rationale**
>
> The Base Specification forbids envelopes ([Base `REPR_NO_ENVELOPES`](./base.md#REPR_NO_ENVELOPES)), so expanded Resources cannot go in a separate `included` list, as in [JSON:API](https://jsonapi.org/format/#fetching-includes). Placing them in `data`, inside the reference, keeps the relationship explicit and the Resource round-trippable.

### 1.6 Filtering

> **Note (non-normative)**
>
> Relationships can be filtered with UnQuery, whose operands are read as ECSON: `{ "author": { "$rel": { "collection": "users", "id": "0190a5f2-..." } } }` matches Resources whose `author` is that reference. No additional rule is needed.

### 1.7 Warnings

This extension defines the following Warning codes ([Base `WARN_EXTENSION_CODES`](./base.md#WARN_EXTENSION_CODES)):

| Code | Condition | Members |
|---|---|---|
| <a id="REFERENCE_NOT_FOUND"></a>`REFERENCE_NOT_FOUND` | An expanded reference points to a Resource that does not exist ([`REL_EXPAND_MISSING`](#REL_EXPAND_MISSING)) | `field`, `id` |
| <a id="EXPAND_IGNORED"></a>`EXPAND_IGNORED` | An external relationship was listed in the Expand Parameter ([`REL_EXPAND_EXTERNAL`](#REL_EXPAND_EXTERNAL)) | `field` |
| <a id="EXPAND_DEPTH_LIMITED"></a>`EXPAND_DEPTH_LIMITED` | Nested expansion stopped at the depth limit of the Server ([`REL_EXPAND_NESTED`](#REL_EXPAND_NESTED)) | `field` |
