# Entity (https://0758e0c7.effetch-query.pages.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 one has
the first paragraph of its docs, and what it needs and how it fails; your
editor shows the rest, with an example, when you hover the name.

## Entities

Guide: [Entities](https://0758e0c7.effetch-query.pages.dev/docs/mutations/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.

### 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://0758e0c7.effetch-query.pages.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://0758e0c7.effetch-query.pages.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://0758e0c7.effetch-query.pages.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://0758e0c7.effetch-query.pages.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://0758e0c7.effetch-query.pages.dev/docs/reference/entity#make). [`update`](https://0758e0c7.effetch-query.pages.dev/docs/reference/entity#update), [`set`](https://0758e0c7.effetch-query.pages.dev/docs/reference/entity#set) and [`remove`](https://0758e0c7.effetch-query.pages.dev/docs/reference/entity#remove) write it in every cached query whose schema holds it, and [`get`](https://0758e0c7.effetch-query.pages.dev/docs/reference/entity#get) reads it from any of them.

### Entity.MakeOptions

Options for [`make`](https://0758e0c7.effetch-query.pages.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. |

## Related pages

- [Entry points](https://0758e0c7.effetch-query.pages.dev/docs/reference): what each entry point exports.
- [`@monarcode/effetch-query`](https://0758e0c7.effetch-query.pages.dev/docs/reference/core): the core entry point's namespaces and top-level names.
- [`Query`](https://0758e0c7.effetch-query.pages.dev/docs/reference/query): defining queries, keys and targets, and the cache from Effect.
- [`InfiniteQuery`](https://0758e0c7.effetch-query.pages.dev/docs/reference/infinite-query): queries that load a list one page at a time.
- [`Mutation`](https://0758e0c7.effetch-query.pages.dev/docs/reference/mutation): defining and running mutations.
- [`QueryClient`](https://0758e0c7.effetch-query.pages.dev/docs/reference/query-client): the client, its options and hooks, and server rendering.
- [`Signals`](https://0758e0c7.effetch-query.pages.dev/docs/reference/signals): where a client hears about focus and reconnects.
- [`@monarcode/effetch-query/react`](https://0758e0c7.effetch-query.pages.dev/docs/reference/react): the provider, the hooks and the boundaries.
- [`@monarcode/effetch-query/effetch`](https://0758e0c7.effetch-query.pages.dev/docs/reference/effetch): queries and mutations from Effetch endpoints.
- [`@monarcode/effetch-query/react-native`](https://0758e0c7.effetch-query.pages.dev/docs/reference/react-native): focus and online signals for React Native.
- [`@monarcode/effetch-query/react/tanstack-router`](https://0758e0c7.effetch-query.pages.dev/docs/reference/tanstack-router): connecting a client to TanStack Router and Start.