# Entity (https://effetch-query.monarcode.dev/docs/reference/entity)

`Entity` describes a record that can sit in many queries at once, and writes every cached copy of it. Import it from the core entry point:

```ts
import { Entity } from "@monarcode/effetch-query";
```

Its exports follow, grouped by the guide that covers them, each with its
docs. Your editor shows the same docs, with an example, when you hover the
name.

## Entities

Guide: [Entities](https://effetch-query.monarcode.dev/docs/cache/entities).

### Entity.make

Defines an entity: a schema, and how to get one's id. Define it once, beside the schema, and use that same schema in your queries: a query is searched for the schema itself (or a copy with annotations or checks added), not for another schema with the same fields.

A query is reached through its `schema` (`Query.MakeOptions.schema`), or an Effetch endpoint's `output`; a query without one is skipped. The entity is found in arrays, tuples, records, struct fields (optional and `NullOr` ones too), unions, nested and recursive (`Schema.suspend`) schemas, `Schema.Class` instances, infinite queries' pages, and the decoded side of schemas that transform. It isn't found inside `Option`, `Map` or other declarations, nor after a tuple's rest element, nor inside another copy of itself. A value hydrated from the server is reached once a query reads it: until then it's JSON with no schema to search.

### Entity.get

Reads the entity whose id is `id` from the cached queries that hold it, or `Option.none()` if none does. Where copies differ, the one from the query that fetched or was written last wins. Use it for a detail query's `placeholder`, to show the list's copy while the detail loads.

### Entity.isEntity

Checks whether a value is an [`Entity`](https://effetch-query.monarcode.dev/docs/reference/entity#entity). Its value and id types can't be known from the value, so it narrows to `Entity<unknown, unknown>`.

### Entity.remove

Drops the entity whose id is `id` from every cached query that holds it, as [`update`](https://effetch-query.monarcode.dev/docs/reference/entity#update) writes: from arrays and records, from an `optionalKey` field, which loses its key, and from a field that takes `null` or `undefined` (`NullOr`, `optional`), which becomes that. Where the schema has no way to leave it out, such as a required field, a tuple's element, a non-empty array's last item or a detail query's whole value, it stays. Totals aren't changed, so invalidate the lists too, and have an infinite query's `nextPageParam` count from the page's params, not its length.

### Entity.set

Writes an entity over every cached copy with the same id, as [`update`](https://effetch-query.monarcode.dev/docs/reference/entity#update) does: use it with a mutation's result, in `onSuccess`. It only replaces copies: a list that doesn't hold the entity yet doesn't get it, so invalidate lists it should join.

### Entity.TypeId

The property that marks an [`Entity`](https://effetch-query.monarcode.dev/docs/reference/entity#entity).

### Entity.update

Writes a value computed from the entity whose id is `id` into every cached query that holds it: each list, each loaded page, its detail query. In a mutation's `optimistic`, a failed run puts every one back, as it does `Query.update`'s writes. Unlike `Query.update`, a query keeps its age: the write doesn't make it fresh, and a query that's invalidated or failed, or has no value or no schema, is left alone, to its next fetch. A query whose value doesn't change isn't written. If `f` throws, nothing is written and the Effect dies with the defect.

### Entity.Entity

A record that many queries hold, such as a todo that's in a list, in a page of a search and in its own detail query, made with [`make`](https://effetch-query.monarcode.dev/docs/reference/entity#make). [`update`](https://effetch-query.monarcode.dev/docs/reference/entity#update), [`set`](https://effetch-query.monarcode.dev/docs/reference/entity#set) and [`remove`](https://effetch-query.monarcode.dev/docs/reference/entity#remove) write it in every cached query whose schema holds it, and [`get`](https://effetch-query.monarcode.dev/docs/reference/entity#get) reads it from any of them.

Nothing is normalized: each query keeps its own value, and an entity write maps it, as `Query.update` does.

| Prop                               | Type       | Description                                                                  |
| ---------------------------------- | ---------- | ---------------------------------------------------------------------------- |
| `~@monarcode/effetch-query/Entity` | `object`   | Marks an entity, and carries its types.                                      |
| `schema`                           | `object`   | The entity's schema, which queries' schemas are searched for.                |
| `id`                               | `function` | Gets an entity's id, which update, remove and get match with `Equal.equals`. |

### Entity.MakeOptions

Options for [`make`](https://effetch-query.monarcode.dev/docs/reference/entity#make).

| Prop | Type       | Description                                                                                                                                                                               |
| ---- | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | `function` | Gets an entity's id. Ids are compared with `Equal.equals`, so a number doesn't match the same digits as a string: convert route params, such as `Number(params.id)`, before passing them. |