# CRUD API Resource Metadata Extension

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

Source: lingodata/crud@bc225ba

## 1. Resource metadata

The **Metadata** extension defines Resource-level metadata: version, content hash, events, status and provenance. 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 [ECSON](https://github.com/lingodata/ecson), including its canonical mode and its References type set.

> **Tip (non-normative): Example**
>
> ```json
> {
>   "_id": "0190a5f2-7c3e-7b1a-9f4e-2d8c6b1e4a90",
>   "name": "Ana",
>   "_meta": {
>     "version": 4,
>     "hash": "5fWJ6A",
>     "status": "published",
>     "events": {
>       "created":   { "timestamp": { "$date": "2026-11-01T10:00:00.000Z" }, "author": "gustavo@example.com" },
>       "updated":   { "timestamp": { "$date": "2026-11-04T15:30:00.000Z" }, "author": "thamirys@example.com", "comments": "Fix name" },
>       "published": { "timestamp": { "$date": "2026-11-02T09:00:00.000Z" }, "author": "gustavo@example.com" }
>     },
>     "ancestor": { "$rel": { "collection": "customers", "id": "0190a5f2-0000-7000-8000-00000000000a" } },
>     "origin": { "$rel": "https://pipedrive.com/contacts/123" }
>   }
> }
> ```

### 1.1 The `_meta` Field

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

Resource metadata MUST be represented in a reserved Field named `_meta` in each Resource, as allowed by [Base `FIELDS_EXTENSION_RESERVED`](./base.md#FIELDS_EXTENSION_RESERVED).

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

The Server MUST manage `_meta`. On write operations, the Server MUST ignore values sent by the Client for `_meta`, except for the members this extension declares as writable (`status`, `ancestor`, `origin`, and `reason` and `comments` of the current event), and MUST report each ignored member as a [`META_IGNORED`](#META_IGNORED) Warning, with its `field`.

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

Protocol specifications MAY also carry members of `_meta` in Protocol metadata, such as the HTTP `ETag` and `Last-Modified` headers for operations on a single Resource.

> **Note (non-normative): Rationale**
>
> Operation metadata (for example, HTTP headers) cannot describe each Resource of a `list`. A reserved Field travels with each Resource in every operation and Protocol.

### 1.2 Version and content hash

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

`_meta.version` MUST be an integer that is `1` when the Resource is created and is incremented by 1 on every write that changes the Resource.

<a id="META_DOC_HASH"></a>**Rule `META_DOC_HASH`**: SHOULD

`_meta.hash` SHOULD be a hash of the content of the Resource: the CRC-32 (the polynomial used by ISO-HDLC, zlib and gzip) of the ECSON canonical serialization ([ECSON `CANONICAL_JCS`](../ecson.md#CANONICAL_JCS)), computed by the Server whatever the negotiated Resource Representation, of the Resource without the `_id` and `_meta` Fields, encoded as the 4 bytes in big-endian order in base64url without padding.

> **Tip (non-normative): Example**
>
> The Resource `{ "_id": "...", "name": "Ana", "total": 10 }` has the canonical content `{"name":"Ana","total":10}`, whose CRC-32 is `3858074088`, and the hash `5fWJ6A`.

> **Note (non-normative): Rationale**
>
> The hash is meant to compare contents: two Resources, or two versions of one Resource, have the same hash when they have the same content, regardless of their Resource IDs, which also makes clones comparable with their ancestors. The canonical form makes the hash identical across implementations and languages. CRC-32 is fast and enough to detect changes, but it is not a cryptographic hash and must not be used to detect deliberate tampering.

<a id="META_DOC_CONDITIONAL"></a>**Rule `META_DOC_CONDITIONAL`**: SHOULD

The Server SHOULD accept an expected hash or version in write operations. If it does not match the current Resource, the Server MUST fail the operation with the [`PRECONDITION_FAILED`](#PRECONDITION_FAILED) error, without writing.

### 1.3 Events

Events record when and by whom something happened to a Resource. `_meta.events` is an object whose keys are event names and whose values have the following structure:

| Field | Type | Requirement | Description |
|---|---|---|---|
| `timestamp` | a date (ECSON `$date`) | MUST | When the event occurred |
| `author` | string | SHOULD | Who performed it, such as an e-mail or user ID |
| `reason` | string | MAY | Context provided by the system |
| `comments` | string | MAY | Notes provided by the user |

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

The Server MUST record a `created` event when the Resource is created, and MUST NOT change it afterwards.

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

The Server MUST record an `updated` event on every write that changes the Resource, replacing the previous `updated` event.

<a id="META_EVENT_AUTHOR"></a>**Rule `META_EVENT_AUTHOR`**: SHOULD

The `author` of an event SHOULD default to the identity of the authenticated Client.

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

The Client MAY send `reason` and `comments` for the event produced by a write operation.

### 1.4 Status

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

A Resource MAY have a status in `_meta.status`: a string whose possible values are defined by each application or by other extensions. The Client MAY set it on `create` and change it with `patch`.

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

When `_meta.status` changes, the Server MUST record an event named after the new status (for example `published`), with the structure of [section 1.3](#13-events), replacing any previous event with that name.

> **Note (non-normative)**
>
> This extension does not filter operations by status: a `list` returns Resources in every status, and Clients filter with a Search Query on `_meta.status`. Status sets and their transitions (for example `draft`, `published`, `archived`) belong to each domain, and may be defined by a future extension based on an open ontology.

> **Note (non-normative): Rationale**
>
> Statuses are domain-specific, so this extension only defines where the status is stored and how its changes are recorded.

### 1.5 Provenance

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

When a Resource is created by the `clone` operation, the Server MUST set `_meta.ancestor` to a reference ([ECSON collection object](../ecson.md#42-references-type-set)) to the source Resource. On `create`, the Client MAY set `_meta.ancestor` itself.

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

On `create`, the Client MAY set `_meta.origin` to a reference ([ECSON `$rel`](../ecson.md#42-references-type-set)) (a URI, Link header or link object) identifying the external source the Resource was imported or copied from.

### 1.6 Errors and Warnings

This extension adds the following error code to those of the Base Specification ([Base `ERROR_EXTENSION_CODES`](./base.md#ERROR_EXTENSION_CODES)):

| Code | Condition |
|---|---|
| <a id="PRECONDITION_FAILED"></a>`PRECONDITION_FAILED` | The expected hash or version sent by the Client does not match the current Resource ([`META_DOC_CONDITIONAL`](#META_DOC_CONDITIONAL)) |

It also defines the following Warning code ([Base `WARN_EXTENSION_CODES`](./base.md#WARN_EXTENSION_CODES)):

| Code | Condition | Members |
|---|---|---|
| <a id="META_IGNORED"></a>`META_IGNORED` | A member of `_meta` sent by the Client was ignored ([`META_SERVER_MANAGED`](#META_SERVER_MANAGED)) | `field` |
