# Stores

> Reference for defineStore, which declares state for an adapter or a page, and how persistence decides whether state is shared between requests.

A store holds local state, such as the warehouse that someone selected, the filters on a page, or the text in an editor. Data from the service belongs in [resources](/docs/adapter-sdk/resources). A store holds everything else.

## Define a store

```ts
import { defineStore } from "@northgraindata/dsui-adapter-sdk";

export const sessionStore = defineStore({
  id: "session",
  scope: "adapter",
  state: { warehouse: null as string | null },
  actions: ({ set }) => ({
    setWarehouse: (warehouse: string | null) => set({ warehouse }),
  }),
});
```

| Option | Meaning |
| --- | --- |
| `id` | Unique within its scope |
| `scope` | `adapter` or `page`. See below |
| `state` | The initial state. Every instance starts as a copy of it |
| `actions` | A function that receives `get`, `set`, and `reset` and returns your typed actions |
| `persistence` | `{ type: "memory" }` (default) or `{ type: "persistent" }`. See below |

`set(patch)` merges a partial update into the state and tells subscribers. `reset()` restores the initial state. `get()` returns a copy of the state. Register adapter stores in `stores` of `defineAdapter`.

## Choose a scope

- **`adapter`**: one instance for each adapter instance, for state like a selected warehouse
- **`page`**: one instance for each page that someone opens, for state like filters or editor content. List the store in the `stores` option of `definePage`. DSUI destroys the instance when the page closes

## Choose persistence

By default, a store keeps its state in memory. DSUI runs actions and jobs in separate, short-lived processes, apart from the process that serves reads. A write that an action makes to a memory store therefore never reaches a later resource read.

Use a persistent store for state that more than one request must see:

```ts
export const notesStore = defineStore({
  id: "notes",
  scope: "adapter",
  persistence: { type: "persistent", key: "notes", version: 1 },
  state: { text: "" },
  actions: ({ set }) => ({ setText: (text: string) => set({ text }) }),
});
```

DSUI saves the state of a persistent store in its own database, separately for each service, and loads it the next time the store is used.

- **`key`**: names the saved state. It defaults to the store `id`
- **`version`**: a number that DSUI saves with the state. It defaults to `1`. DSUI doesn't migrate saved state for you

When DSUI loads saved state, it merges it over the initial state. A field that you add later gets its default value.

Persistent state must be plain JSON.

## Use a store

- **In a page**: `stores.use(store)` and `stores.get(store)`. See [pages](/docs/adapter-sdk/pages#use-stores-in-a-page)
- **In an action or a resource**: `ctx.stores.get(store)` returns the live instance. Call its actions with `.actions`, or read it with `.get()`

```ts
run: async ({ text }, ctx: MyContext & ActionRuntimeContext) => {
  ctx.stores.get(notesStore).actions.setText(text);
  return { saved: true };
},
```

A resource reads the state the same way, with `ctx.stores.get(notesStore).get().text`. A resource that reads a persistent store sees what an action saved.
