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>
如果没有提供显式类型或模式类型,则回退类型
LocalStorageCollectionConfig<TExplicit, TSchema, TFallback>
localStorage collection 的配置选项
object
包含 clearStorage 和 getStorageSize 工具的 collection 选项
getKey: (item) => string | number;
getKey: (item) => string | number;
string | number
id: string = collectionId;
id: string = collectionId;
onDelete: (params) => Promise<any> = wrappedOnDelete;
onDelete: (params) => Promise<any> = wrappedOnDelete;
DeleteMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>
Promise<any>
onInsert: (params) => Promise<any> = wrappedOnInsert;
onInsert: (params) => Promise<any> = wrappedOnInsert;
InsertMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>
Promise<any>
onUpdate: (params) => Promise<any> = wrappedOnUpdate;
onUpdate: (params) => Promise<any> = wrappedOnUpdate;
UpdateMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>
Promise<any>
optional schema: TSchema;
optional schema: TSchema;
sync: SyncConfig<ResolveType<TExplicit, TSchema, TFallback>, string | number> & object;
sync: SyncConfig<ResolveType<TExplicit, TSchema, TFallback>, string | number> & object;
optional manualTrigger: () => void;
optional manualTrigger: () => void;
void
utils: object;
utils: object;
clearStorage: ClearStorageFn;
clearStorage: ClearStorageFn;
getStorageSize: GetStorageSizeFn;
getStorageSize: GetStorageSizeFn;
// 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,
})
)
// 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,
})
)
// 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)
},
})
)
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。