Browse documentation

createNativeStore

Creates a named store with the selected storage type, or returns it when reuse is enabled.

Table of contents

Category

Value

Import

js
import { createNativeStore } from "valyrian.js/native-store";

Public signature

ts
createNativeStore<T>(id: string, definition?: Record<string, unknown>, storageType?: StorageType, reuseIfExist?: boolean): NativeStorageInterface & T

API form

createNativeStore is a factory. It creates a named object backed by local storage by default or session storage when StorageType.Session is selected.

Returned surface

ts
state: Record<string, unknown>
set(key: string, value: unknown): void
get(key: string): unknown
delete(key: string): void
load(): void
clear(): void
cleanup(): void

Reuse

A duplicate id throws unless reuseIfExist is true. With reuse enabled, the factory returns the existing named store.

Errors

Throws when a store with the same identifier already exists and reuse is not enabled. In Node.js, import valyrian.js/node before creating a store so localStorage and sessionStorage exist.