WICG / kv-storage

[On hold] A proposal for an async key/value storage API for the web
Other
550 stars 18 forks source link

A couple more libraries to investigate/ask for feedback from #20

Open domenic opened 6 years ago

domenic commented 6 years ago

Mentioned to me by a colleague over lunch:

domenic commented 6 years ago

Oh, and I found https://github.com/w3c/IndexedDB/issues/91.

Right now I consider this blocked on #18.

cyrilletuzi commented 5 years ago

As the author of @ngx-pwa/local-storage, which main goal is the same as this proposal, and is currently the main Angular lib used for this purpose, I'm interested to stay in touch about this.

rbiggs commented 5 years ago

I also wrote a wrapper for IndexedDB for the same reasons as this proposal. Similar API and promise-based so you can use it in an async function with await. I'll gladly deprecate it once this ships. https://github.com/composi/idb

morrys commented 4 years ago

Hi all, I wanted to introduce you to the library I created wora/cache-persist. I used to persist Relay & Apollo GraphQL caches:

In short, wora/cache-persist is an object that allows you to manage interfacing with storage (localStorage, sessionStorage, indexedDB, React-Native AsyncStorage & any custom storage) in "synchronous" mode and with the possibility of serializing and deserialization of keys and values ​​in storage (used to filter keys, encription, compression ...)

This result is achieved by combining:

If you need to be sure that the data is written into the storage you can use the flush function.

While to handle any errors that may occur during asynchronous writing, there is a callback errorHandling function as a Cache configuration parameter.

export interface ICache {
    isRehydrated(): boolean;
    restore(): Promise<DataCache>;
    flush(): Promise<void>;
    getState(): Readonly<{ [key: string]: any }>;
    get(key: string): any;
    getAllKeys(): Array<string>;
    has(key: string): boolean;
    set(key: string, value: any): void;
    delete(key: string): void;
    remove(key: string): void;
    purge(): void;
    replace(data: any): void;
    subscribe(callback: (state: any, action: any) => void): () => void;
    notify(payload?: { state?: any; action?: any }): void;
    getStorage(): ICacheStorage;
}

export type CacheOptions = {
    serialize?: boolean;
    prefix?: string | undefined | null;
    initialState?: DataCache;
    mergeState?: (restoredState?: DataCache, initialState?: DataCache) => Promise<DataCache> | DataCache;
    mutateKeys?: Array<IMutateKey>;
    mutateValues?: Array<IMutateValue>;
    storage?: ICacheStorage;
    webStorage?: 'local' | 'session';
    disablePersist?: boolean;
    errorHandling?: (cache: ICache, error: any) => boolean;
    throttle?: number;
};

The size of the library is about 2kb. (can be improved)

I would like to have your feedback Thanks