# @salmonui/data/views API reference

> 6 exported symbols of @salmonui/data/views: 1 function, 4 interfaces and 1 constant.

Canonical: https://salmonui.dev/docs/reference/data/views

```ts
import { … } from '@salmonui/data/views';
```

## Functions

### createSavedViews

Kind: function

Page: https://salmonui.dev/docs/reference/data/views/createSavedViews

```ts
function createSavedViews<State extends DataGridState = DataGridState>(store: KeyValueStore, options: SavedViewsOptions<State>): SavedViews<State>;
```

Named saved views of one grid over an injected key-value store
(`localStorage`, IndexedDB wrappers, a server-backed store, `createMemoryStore()`).
Writes through one object are serialized, so concurrent `save` calls never
lose each other's views; unreadable or invalid records are skipped.

## Interfaces

### SavedView

Kind: interface

Page: https://salmonui.dev/docs/reference/data/views/SavedView

```ts
interface SavedView<State extends DataGridState = DataGridState> {
    readonly id: string;
    readonly name: string;
    readonly description?: string;
    readonly state: State;
    readonly createdAt: number;
    readonly updatedAt: number;
    readonly schemaVersion: number;
}
```

Members (7):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `id` | `readonly id: string;` |  |  |
| `name` | `readonly name: string;` |  |  |
| `description` | `readonly description?: string;` | optional |  |
| `state` | `readonly state: State;` |  |  |
| `createdAt` | `readonly createdAt: number;` |  | Epoch milliseconds. |
| `updatedAt` | `readonly updatedAt: number;` |  | Epoch milliseconds. |
| `schemaVersion` | `readonly schemaVersion: number;` |  | `SAVED_VIEW_SCHEMA_VERSION` the record was written with. |

### SavedViewInput

Kind: interface

Page: https://salmonui.dev/docs/reference/data/views/SavedViewInput

```ts
interface SavedViewInput<State extends DataGridState = DataGridState> {
    readonly id?: string;
    readonly name: string;
    readonly description?: string;
    readonly state: State;
}
```

`save()` input: without `id` a view is created, with an existing `id` it is replaced (keeping `createdAt`).

Members (4):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `id` | `readonly id?: string;` | optional |  |
| `name` | `readonly name: string;` |  |  |
| `description` | `readonly description?: string;` | optional |  |
| `state` | `readonly state: State;` |  |  |

### SavedViews

Kind: interface

Page: https://salmonui.dev/docs/reference/data/views/SavedViews

```ts
interface SavedViews<State extends DataGridState = DataGridState> {
    list(): Promise<readonly SavedView<State>[]>;
    get(id: string): Promise<SavedView<State> | null>;
    save(input: SavedViewInput<State>): Promise<SavedView<State>>;
    rename(id: string, name: string, description?: string): Promise<SavedView<State> | null>;
    remove(id: string): Promise<boolean>;
    clear(): Promise<void>;
    subscribe(listener: (views: readonly SavedView<State>[]) => void): () => void;
}
```

Members (7):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `list` | `list(): Promise<readonly SavedView<State>[]>;` |  | Every view, oldest first (creation order). |
| `get` | `get(id: string): Promise<SavedView<State> \| null>;` |  |  |
| `save` | `save(input: SavedViewInput<State>): Promise<SavedView<State>>;` |  | Creates or replaces a view and returns the stored record. |
| `rename` | `rename(id: string, name: string, description?: string): Promise<SavedView<State> \| null>;` |  | Changes the name (and, when given, the description) of a view; null when it does not exist. |
| `remove` | `remove(id: string): Promise<boolean>;` |  | Removes a view; false when it did not exist. |
| `clear` | `clear(): Promise<void>;` |  | Removes every view of the grid. |
| `subscribe` | `subscribe(listener: (views: readonly SavedView<State>[]) => void): () => void;` |  | Called after every change made through this object. Returns the unsubscribe function. |

### SavedViewsOptions

Kind: interface

Page: https://salmonui.dev/docs/reference/data/views/SavedViewsOptions

```ts
interface SavedViewsOptions<State extends DataGridState = DataGridState> {
    readonly gridId: string;
    readonly prefix?: string;
    readonly now?: () => number;
    readonly createId?: () => string;
    readonly migrate?: (record: Readonly<Record<string, unknown>>, schemaVersion: number) => SavedView<State> | null;
}
```

Members (5):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `gridId` | `readonly gridId: string;` |  | The grid whose views these are (`DataGrid` `id`). |
| `prefix` | `readonly prefix?: string;` | optional | Storage key prefix. Default `salmonui:grid-views:`. |
| `now` | `readonly now?: () => number;` | optional | Clock in epoch milliseconds. Default `Date.now`. |
| `createId` | `readonly createId?: () => string;` | optional | Id for a new view. Default `crypto.randomUUID()` when available, else a time-based counter. |
| `migrate` | `readonly migrate?: (record: Readonly<Record<string, unknown>>, schemaVersion: number) => SavedView<State> \| null;` | optional | Upgrades a stored record of an older (or unknown) record schema, or whose grid state needs upgrading; return null to drop it. Records of the current schema are passed through unchanged without calling it. |

## Constants

### SAVED_VIEW_SCHEMA_VERSION

Kind: const

Page: https://salmonui.dev/docs/reference/data/views/SAVED_VIEW_SCHEMA_VERSION

```ts
const SAVED_VIEW_SCHEMA_VERSION = 1;
```

Schema version of the stored view record (not of the grid state, which carries its own `version`).
