# @salmonui/data/server API reference

> 10 exported symbols of @salmonui/data/server: 2 functions, 7 interfaces and 1 type.

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

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

## Functions

### createJsonQueryHandler

Kind: function

Page: https://salmonui.dev/docs/reference/data/server/createJsonQueryHandler

```ts
function createJsonQueryHandler(initial: readonly Obj[], options: JsonQueryHandlerOptions): JsonQueryHandler;
```

### createPivotQueryHandler

Kind: function

Page: https://salmonui.dev/docs/reference/data/server/createPivotQueryHandler

```ts
function createPivotQueryHandler(initial: readonly Obj[], options: PivotQueryHandlerOptions): PivotQueryHandler;
```

## Interfaces

### JsonQueryHandler

Kind: interface

Page: https://salmonui.dev/docs/reference/data/server/JsonQueryHandler

```ts
interface JsonQueryHandler {
    handle(message: unknown): unknown;
    rows(): readonly Obj[];
    transport(): JsonTransport;
}
```

Members (3):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `handle` | `handle(message: unknown): unknown;` |  | Handles one request, a batch, or a state-storage call. |
| `rows` | `rows(): readonly Obj[];` |  | Current rows (after writes). |
| `transport` | `transport(): JsonTransport;` |  | In-process transport (no HTTP) for tests and SSR. |

### JsonQueryHandlerOptions

Kind: interface

Page: https://salmonui.dev/docs/reference/data/server/JsonQueryHandlerOptions

```ts
interface JsonQueryHandlerOptions {
    readonly key: string;
    readonly fields?: readonly string[];
    readonly types?: Readonly<Record<string, WireType>>;
    readonly dict?: readonly string[] | 'auto';
    readonly parentField?: string;
    readonly versionField?: string;
    readonly validate?: (row: Obj) => readonly {
        readonly field?: string;
        readonly code: string;
        readonly message: string;
    }[];
    readonly nextKey?: () => Json;
    readonly facetLimit?: number;
    readonly lookups?: Readonly<Record<string, readonly Obj[]>>;
    readonly lookupFields?: Readonly<Record<string, {
        readonly lookup: string;
        readonly keyField: string;
        readonly displayField: string;
    }>>;
}
```

Members (11):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `key` | `readonly key: string;` |  |  |
| `fields` | `readonly fields?: readonly string[];` | optional | Full field list (default: keys of the first row). |
| `types` | `readonly types?: Readonly<Record<string, WireType>>;` | optional | Wire types per field (default: inferred from the first row; `Date` values are `date`). |
| `dict` | `readonly dict?: readonly string[] \| 'auto';` | optional | Dictionary-encode these string fields, or `'auto'`. Default none. |
| `parentField` | `readonly parentField?: string;` | optional | Tree data: field holding the parent key (blank for roots). Tree reads (`parent`, `null` for the root level) return that level's children with `childCounts`; with a filter or search they return every match with its ancestor chain (`filterMode: 'ancestors'`) in pre-order, with `parents`, `depths`, and `childCounts` of the kept children, paged by top-level row. Aggregates cover the kept rows of every depth. Writes: `parents` sets the parent of added rows, deleting a row deletes its subtree, `moved` sets the parent of moved rows and places them next to `target`. |
| `versionField` | `readonly versionField?: string;` | optional | Row version field; incremented on every change and checked against `version`. |
| `validate` | `readonly validate?: (row: Obj) => readonly {     readonly field?: string;     readonly code: string;     readonly message: string; }[];` | optional | Validation for added and changed rows. |
| `nextKey` | `readonly nextKey?: () => Json;` | optional | Key generator for added rows whose key is null. |
| `facetLimit` | `readonly facetLimit?: number;` | optional | Max facet entries. Default 1000. |
| `lookups` | `readonly lookups?: Readonly<Record<string, readonly Obj[]>>;` | optional | Lookup tables answered for `{ v: 1, lookup: { name, keys, keyField } }`. |
| `lookupFields` | `readonly lookupFields?: Readonly<Record<string, {     readonly lookup: string;     readonly keyField: string;     readonly displayField: string; }>>;` | optional | Fields holding a lookup key (lookup / foreign-key columns): sorting and grouping by such a field order rows and groups by the `displayField` of the matching row of `lookups[lookup]` (a server-side join), then by the key. Group keys, filters and the returned rows keep the key. |

### PivotEngineLike

Kind: interface

Page: https://salmonui.dev/docs/reference/data/server/PivotEngineLike

```ts
interface PivotEngineLike {
    setModel(model: unknown): PivotResultLike;
    destroy?(): void;
}
```

The part of `PivotEngine` the handler drives.

Members (2):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `setModel` | `setModel(model: unknown): PivotResultLike;` |  |  |
| `destroy` | `destroy?(): void;` | optional |  |

### PivotHeaderLike

Kind: interface

Page: https://salmonui.dev/docs/reference/data/server/PivotHeaderLike

```ts
interface PivotHeaderLike {
    readonly key: string;
    readonly kind: 'member' | 'subtotal' | 'grandTotal' | 'value';
    readonly path: readonly Json[];
    readonly field: string | null;
    readonly measure: string | null;
    readonly label: string;
    readonly depth: number;
    readonly position: number;
    readonly span: number;
    readonly expandable: boolean;
    readonly expanded: boolean;
    readonly children?: readonly PivotHeaderLike[];
}
```

The structural header node both sides read (`PivotHeaderNode`).

Members (12):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `key` | `readonly key: string;` |  |  |
| `kind` | `readonly kind: 'member' \| 'subtotal' \| 'grandTotal' \| 'value';` |  |  |
| `path` | `readonly path: readonly Json[];` |  |  |
| `field` | `readonly field: string \| null;` |  |  |
| `measure` | `readonly measure: string \| null;` |  |  |
| `label` | `readonly label: string;` |  |  |
| `depth` | `readonly depth: number;` |  |  |
| `position` | `readonly position: number;` |  |  |
| `span` | `readonly span: number;` |  |  |
| `expandable` | `readonly expandable: boolean;` |  |  |
| `expanded` | `readonly expanded: boolean;` |  |  |
| `children` | `readonly children?: readonly PivotHeaderLike[];` | optional |  |

### PivotQueryHandler

Kind: interface

Page: https://salmonui.dev/docs/reference/data/server/PivotQueryHandler

```ts
interface PivotQueryHandler {
    handle(message: unknown): unknown;
    transport(): UniversalJsonTransport;
    compute(model: unknown, options?: {
        readonly where?: WirePredicate;
    }): PivotResultLike;
    rows(): readonly Obj[];
    setRows(rows: readonly Obj[]): void;
}
```

Members (5):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `handle` | `handle(message: unknown): unknown;` |  | Handles one request (`pivot`, `read`, `facet`, `write`) or a batch. |
| `transport` | `transport(): UniversalJsonTransport;` |  | In-process transport (no HTTP) for tests, SSR and same-process apps. |
| `compute` | `compute(model: unknown, options?: {     readonly where?: WirePredicate; }): PivotResultLike;` |  | The whole result of a model (server-side export). |
| `rows` | `rows(): readonly Obj[];` |  | Current rows (after writes). |
| `setRows` | `setRows(rows: readonly Obj[]): void;` |  | Replaces the rows; cached engines are dropped and `resultVersion` changes. |

### PivotQueryHandlerOptions

Kind: interface

Page: https://salmonui.dev/docs/reference/data/server/PivotQueryHandlerOptions

```ts
interface PivotQueryHandlerOptions {
    readonly engine: PivotEngineFactoryLike;
    readonly fields?: readonly unknown[];
    readonly locale?: string;
    readonly key?: string;
    readonly types?: Readonly<Record<string, WireType>>;
    readonly dict?: readonly string[] | 'auto';
    readonly facetLimit?: number;
    readonly maxCells?: number;
    readonly cacheSize?: number;
}
```

Members (9):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `engine` | `readonly engine: PivotEngineFactoryLike;` |  | The engine factory: `createPivotEngine` from `@salmonui/pivot-table/engine`. |
| `fields` | `readonly fields?: readonly unknown[];` | optional | Field definitions passed to the engine (server-side: `valueGetter` allowed). |
| `locale` | `readonly locale?: string;` | optional |  |
| `key` | `readonly key?: string;` | optional | Key field for `write` requests (drill-through editing). Default: the first field. |
| `types` | `readonly types?: Readonly<Record<string, WireType>>;` | optional | Wire types of drill-through and `read` rows (default: inferred). |
| `dict` | `readonly dict?: readonly string[] \| 'auto';` | optional | Dictionary-encode drill-through and `read` string fields. |
| `facetLimit` | `readonly facetLimit?: number;` | optional | Max facet entries. Default 1000. |
| `maxCells` | `readonly maxCells?: number;` | optional | Largest window a request may ask for, in cells. Default 250,000. |
| `cacheSize` | `readonly cacheSize?: number;` | optional | Engines kept per distinct `where`. Default 4. |

### PivotResultLike

Kind: interface

Page: https://salmonui.dev/docs/reference/data/server/PivotResultLike

```ts
interface PivotResultLike {
    readonly model: WirePivotModel;
    readonly rowCount: number;
    readonly columnCount: number;
    readonly rowDepth: number;
    readonly columnDepth: number;
    readonly rowTree: readonly PivotHeaderLike[];
    readonly columnTree: readonly PivotHeaderLike[];
    memberAt(axis: 'rows' | 'columns', index: number): PivotHeaderLike;
    valueAt(rowIndex: number, columnIndex: number): number | null;
    cellAt(rowIndex: number, columnIndex: number): PivotCellLike;
    rowsAt(rowIndex: number, columnIndex: number): readonly number[];
}
```

The part of `PivotResult` the handler reads (and `compute()` returns: `exportPivot()` accepts it).

Members (11):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `model` | `readonly model: WirePivotModel;` |  |  |
| `rowCount` | `readonly rowCount: number;` |  |  |
| `columnCount` | `readonly columnCount: number;` |  |  |
| `rowDepth` | `readonly rowDepth: number;` |  |  |
| `columnDepth` | `readonly columnDepth: number;` |  |  |
| `rowTree` | `readonly rowTree: readonly PivotHeaderLike[];` |  |  |
| `columnTree` | `readonly columnTree: readonly PivotHeaderLike[];` |  |  |
| `memberAt` | `memberAt(axis: 'rows' \| 'columns', index: number): PivotHeaderLike;` |  |  |
| `valueAt` | `valueAt(rowIndex: number, columnIndex: number): number \| null;` |  |  |
| `cellAt` | `cellAt(rowIndex: number, columnIndex: number): PivotCellLike;` |  |  |
| `rowsAt` | `rowsAt(rowIndex: number, columnIndex: number): readonly number[];` |  |  |

## Types

### PivotEngineFactoryLike

Kind: type

Page: https://salmonui.dev/docs/reference/data/server/PivotEngineFactoryLike

```ts
type PivotEngineFactoryLike = {
    bivariant(options: PivotEngineFactoryOptions): PivotEngineLike;
}['bivariant'];
```

`createPivotEngine` from `@salmonui/pivot-table/engine`, typed
structurally. Declared as a method type so its parameter is bivariant:
the engine's typed `fields` and `model` options accept it without `any`.
