# `Scrypath.Schema`
[🔗](https://github.com/szTheory/scrypath/blob/v0.3.21/lib/scrypath/schema.ex#L1)

Metadata-only schema declaration support for `use Scrypath`.

`Scrypath.Schema` validates declaration options, stores normalized metadata on the
schema module, and exposes that metadata through `__scrypath__/1`.

The initial declaration surface supports these `__scrypath__/1` keys:

- `:config`
- `:fields`
- `:filterable`
- `:faceting`
- `:sortable`
- `:settings`
- `:document_id`
- `:document_source`
- `:backend`
- `:tenant_field`
- `:fan_outs`
- `:preload`

The macro does not generate runtime APIs such as `search/2` or `reindex/1`.

## Hooks

`use Scrypath` makes the schema module implement the `Scrypath.Schema` behaviour.
Define these callbacks on the schema module to change how Scrypath builds and loads
documents:

- `c:search_document/1`: build the document yourself, for computed or nested values
  that `fields:` cannot express.
- `c:search_document_id/1`: use a document id other than the `document_id:` field.
- `c:search_records/2`: load records by document id yourself, for custom ids,
  soft deletes or preloads that a plain Ecto query cannot express.

All three are optional. When a hook is absent, Scrypath projects the declared
`fields:`, reads the `document_id:` field and loads records with an Ecto query.

A hook defined at the wrong arity, such as `search_records/1`, triggers a compile
warning, because Scrypath would otherwise ignore it at runtime.

If the schema module uses `@impl` for any other behaviour, Elixir expects `@impl`
on every callback, so mark the hooks with `@impl Scrypath.Schema`.

# `search_document`
*optional* 

```elixir
@callback search_document(record :: struct() | map()) :: map()
```

Builds the search document for `record`.

When defined, Scrypath uses the returned map instead of projecting the declared
`fields:`. The callback must return a map. An `:id` (or `"id"`) key in the map
overrides the document id. When the schema declares `tenant_field:`, Scrypath
still adds the tenant value to the document.

# `search_document_id`
*optional* 

```elixir
@callback search_document_id(record :: struct() | map()) :: term()
```

Returns the search document id for `record`.

When defined, Scrypath uses the returned value instead of reading the
`document_id:` field. If you sync through Oban, also define
`c:search_records/2`, because the job cannot map a custom id back to a row:
without it, enqueueing an upsert raises `ArgumentError` before any job is
inserted.

# `search_records`
*optional* 

```elixir
@callback search_records(document_ids :: [term()], context :: %{repo: module() | nil}) ::
  [struct() | map()]
```

Loads the current records for `document_ids`.

Scrypath calls this during Oban sync and backfill with at most 1000 deduplicated
ids per call and a context map `%{repo: repo}`, where `repo` may be `nil`. The
callback must return a list. Ids whose records are not returned are treated as
missing, so their documents are deleted from the index.

When the callback is absent, Scrypath loads records with an Ecto query on the
`document_id:` field through the configured repo.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
