Skip to content

@statesync/pinia


@statesync/pinia / PiniaSnapshotApplierOptions

Type Alias: PiniaSnapshotApplierOptions<State, Data>

ts
type PiniaSnapshotApplierOptions<State, Data> = 
  | {
  mode?: "patch";
  omitKeys?: ReadonlyArray<keyof State>;
  pickKeys?: ReadonlyArray<keyof State>;
  strict?: boolean;
  toState?: (data, ctx) => Partial<State>;
}
  | {
  mode: "replace";
  omitKeys?: ReadonlyArray<keyof State>;
  pickKeys?: ReadonlyArray<keyof State>;
  strict?: boolean;
  toState?: (data, ctx) => State;
};

Defined in: pinia.ts:30

Type Parameters

Type Parameter
State extends Record<string, unknown>
Data

Type Declaration

ts
{
  mode?: "patch";
  omitKeys?: ReadonlyArray<keyof State>;
  pickKeys?: ReadonlyArray<keyof State>;
  strict?: boolean;
  toState?: (data, ctx) => Partial<State>;
}

mode?

ts
optional mode: "patch";

Default: 'patch'

  • 'patch': calls store.$patch(partial) (non-destructive)
  • 'replace': applies a top-level replace using $patch((state) => ...):
    • deletes keys not present in nextState
    • assigns keys present in nextState

Why not store.$state = nextState? Pinia documents that assigning $state internally calls $patch(), so it does not reliably remove stale keys on its own.

omitKeys?

ts
optional omitKeys: ReadonlyArray<keyof State>;

pickKeys?

ts
optional pickKeys: ReadonlyArray<keyof State>;

Limit which top-level keys are allowed to be updated by snapshots.

Use this to keep ephemeral/local-only fields (like UI flags) isolated.

strict?

ts
optional strict: boolean;

If true, throws when toState returns a non-object value. Default: true

toState()?

ts
optional toState: (data, ctx) => Partial<State>;

Maps snapshot data to a state patch.

Default: identity cast (treats data as Partial<State>).

Parameters

ParameterType
dataData
ctx{ store: PiniaStoreLike<State>; }
ctx.storePiniaStoreLike<State>

Returns

Partial<State>

ts
{
  mode: "replace";
  omitKeys?: ReadonlyArray<keyof State>;
  pickKeys?: ReadonlyArray<keyof State>;
  strict?: boolean;
  toState?: (data, ctx) => State;
}

mode

ts
mode: "replace";

omitKeys?

ts
optional omitKeys: ReadonlyArray<keyof State>;

pickKeys?

ts
optional pickKeys: ReadonlyArray<keyof State>;

strict?

ts
optional strict: boolean;

toState()?

ts
optional toState: (data, ctx) => State;

Maps snapshot data to a full next state.

When using 'replace', prefer returning the full state to avoid leaving stale keys.

Parameters

ParameterType
dataData
ctx{ store: PiniaStoreLike<State>; }
ctx.storePiniaStoreLike<State>

Returns

State

Released under the MIT License.