localStorageCollectionOptions

函数:localStorageCollectionOptions()

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

定义于:packages/db/src/local-storage.ts:205

为标准 Collection 创建 localStorage collection options

此函数创建一个 collection,该 collection 将数据持久化到 localStorage/sessionStorage,并通过 storage 事件在浏览器标签页之间同步更改。

类型参数

TExplicit = 未知

集合中项目的显式类型(最高优先级)

TSchema extends StandardSchemaV1<unknown, unknown> = never

用于验证和类型推断的模式类型(第二优先级)

TFallback extends object = Record<string, unknown>

如果没有提供显式类型或模式类型,则回退类型

参数

config

LocalStorageCollectionConfig<TExplicit, TSchema, TFallback>

localStorage collection 的配置选项

Returns (返回)

object

包含 clearStorage 和 getStorageSize 工具的 collection 选项

getKey()

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

参数

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;

参数

params

DeleteMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>

Returns (返回)

Promise<any>

onInsert()

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

参数

params

InsertMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>

Returns (返回)

Promise<any>

onUpdate()

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

参数

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;

类型声明

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;

示例

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)
    },
  })
)
我们的合作伙伴
Code Rabbit
Electric
Prisma
订阅 Bytes

您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。

Bytes

无垃圾邮件。您可以随时取消订阅。

订阅 Bytes

您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。

Bytes

无垃圾邮件。您可以随时取消订阅。