localStorageCollectionOptions

Function: localStorageCollectionOptions()

ts
function localStorageCollectionOptions<TExplicit, TSchema, TFallback>(config): object
function localStorageCollectionOptions<TExplicit, TSchema, TFallback>(config): object

Defined in: packages/db/src/local-storage.ts:205

Creates localStorage collection options for use with a standard Collection

This function creates a collection that persists data to localStorage/sessionStorage and synchronizes changes across browser tabs using storage events.

Type Parameters

TExplicit = unknown

The explicit type of items in the collection (highest priority)

TSchema extends StandardSchemaV1<unknown, unknown> = never

The schema type for validation and type inference (second priority)

TFallback extends object = Record<string, unknown>

The fallback type if no explicit or schema type is provided

Parameters

config

LocalStorageCollectionConfig<TExplicit, TSchema, TFallback>

Configuration options for the localStorage collection

Returns

object

Collection options with utilities including clearStorage and getStorageSize

getKey()

ts
getKey: (item) => string | number;
getKey: (item) => string | number;

Parameters

item

ResolveType

Returns

string | number

id

ts
id: string = collectionId;
id: string = collectionId;

onDelete()

ts
onDelete: (params) => Promise<any> = wrappedOnDelete;
onDelete: (params) => Promise<any> = wrappedOnDelete;

Parameters

params

DeleteMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>

Returns

Promise<any>

onInsert()

ts
onInsert: (params) => Promise<any> = wrappedOnInsert;
onInsert: (params) => Promise<any> = wrappedOnInsert;

Parameters

params

InsertMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>

Returns

Promise<any>

onUpdate()

ts
onUpdate: (params) => Promise<any> = wrappedOnUpdate;
onUpdate: (params) => Promise<any> = wrappedOnUpdate;

Parameters

params

UpdateMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>

Returns

Promise<any>

schema?

ts
optional schema: TSchema;
optional schema: TSchema;

sync

ts
sync: SyncConfig<ResolveType<TExplicit, TSchema, TFallback>, string | number> & object;
sync: SyncConfig<ResolveType<TExplicit, TSchema, TFallback>, string | number> & object;

Type declaration

manualTrigger()?
ts
optional manualTrigger: () => void;
optional manualTrigger: () => void;
Returns

void

utils

ts
utils: object;
utils: object;

utils.clearStorage

ts
clearStorage: ClearStorageFn;
clearStorage: ClearStorageFn;

utils.getStorageSize

ts
getStorageSize: GetStorageSizeFn;
getStorageSize: GetStorageSizeFn;

Examples

ts
// Basic localStorage collection
const collection = createCollection(
  localStorageCollectionOptions({
    storageKey: 'todos',
    getKey: (item) => item.id,
  })
)
// Basic localStorage collection
const collection = createCollection(
  localStorageCollectionOptions({
    storageKey: 'todos',
    getKey: (item) => item.id,
  })
)
ts
// localStorage collection with custom storage
const collection = createCollection(
  localStorageCollectionOptions({
    storageKey: 'todos',
    storage: window.sessionStorage, // Use sessionStorage instead
    getKey: (item) => item.id,
  })
)
// localStorage collection with custom storage
const collection = createCollection(
  localStorageCollectionOptions({
    storageKey: 'todos',
    storage: window.sessionStorage, // Use sessionStorage instead
    getKey: (item) => item.id,
  })
)
ts
// localStorage collection with mutation handlers
const collection = createCollection(
  localStorageCollectionOptions({
    storageKey: 'todos',
    getKey: (item) => item.id,
    onInsert: async ({ transaction }) => {
      console.log('Item inserted:', transaction.mutations[0].modified)
    },
  })
)
// localStorage collection with mutation handlers
const collection = createCollection(
  localStorageCollectionOptions({
    storageKey: 'todos',
    getKey: (item) => item.id,
    onInsert: async ({ transaction }) => {
      console.log('Item inserted:', transaction.mutations[0].modified)
    },
  })
)
Our Partners
Electric
Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.