Turns a flat key-value store into a pseudo-document store.
Note that this package is in early stages of development and there may be breaking changes within semantically compatible versions. See change log.
yarn add deep-storage-adapter
The adapter does not store anything itself. It uses the specified storage in a way that allows setting and then retriving objects in their original form (as long as the values can be serialized).
Supports both async and sync storage conforming to:
export interface IKeyStorage {
getItem(key: string): Promise<any> | any;
setItem(key: string, val: string): Promise<void> | void;
removeItem(key: string): Promise<void> | void;
clear?: () => Promise<void> | void;
}
Note that even though sync stores are supported, the adapter's interface is async.
let deepStore = new DeepStorageAdapter({
store: AsyncStorage,
encoder: require('base62/lib/ascii'), // See https://github.com/base62/base62.js
});
deepStore
.setItem('item', {
foo: 'bar',
type: 2,
})
.then(() => deepStore.getItem('item'))
.then(item => {
// item matches original object
console.log(JSON.stringify(item));
});