# repositoryBuilder

Takes a reducer and an adapter and returns a repository. A repository is the layer of our Onion Architecture (see [Introduction](/master.md#event-sourcing-es)) which handles data persistence.

The `write-model` repository has a single method called `getById`. When invoked, it uses the adapter to load all events for given ID and runs them through the reducer to calculate the current state. It returns an object that contains `state` and also a `save` method, which is used to append new events.

## Methods

### build

`build({ adapter, reducer })`&#x20;

builds a repository&#x20;

#### Parameters

| attribute | type       | description                                                                                      |
| --------- | ---------- | ------------------------------------------------------------------------------------------------ |
| `adapter` | `object`   | Any object which implements the write-model [Adapter Interface](/advanced/repository.md).        |
| `reducer` | `function` | a function which, given a list of events, knows how to calculate the current state of an object. |

#### Returns

`{ getById }` - an object with the `getById` function.

### getById

`getById(id)`&#x20;

Loads and returns the current state of an entity. Also returns a `save` function for appending new events to the data store.

#### Parameters

| attribute | type     | description          |
| --------- | -------- | -------------------- |
| `id`      | `string` | the id of the entity |

#### Returns

`{ state, save }` - an object containing an arbitrary `state` object, and a `save` function.

### save

`save(events)`&#x20;

Appends new events to the datastore

#### Parameters

| attribute | type    | description                         |
| --------- | ------- | ----------------------------------- |
| `events`  | `array` | an array of arbitrary event objects |

#### Returns

`null`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.serverless-cqrs.com/components/write-model/repositorybuilder.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
