Skip to content

Commit

Permalink
Merge pull request #447 from Titozzz/patch-1
Browse files Browse the repository at this point in the history
feat(MMKVWrapper): Added new storage wrapper
  • Loading branch information
wodCZ authored Sep 17, 2021
2 parents 44db77d + e3dbe4c commit 65512c4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/storage-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
| [`localForage`](https://github.com/localForage/localForage) | web | `LocalForageWrapper` |
| [`Ionic storage`](https://ionicframework.com/docs/building/storage) | web and mobile | `IonicStorageWrapper` |
| [`MMKV Storage`](https://github.com/ammarahm-ed/react-native-mmkv-storage) | React Native | `MMKVStorageWrapper` |
| [`MMKV`](https://github.com/mrousavy/react-native-mmkv) | React Native | `MMKVWrapper` |


## Redux Persist Providers
Expand Down
46 changes: 46 additions & 0 deletions src/storageWrappers/MMKVWrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { PersistentStorage } from '../types';

/**
* Wrapper for react-native-mmkv.
* See [https://github.com/mrousavy/react-native-mmkv](https://github.com/mrousavy/react-native-mmkv) for installation instructions.
*
* @example
* const storage = new MMKV();
* const persistor = new CachePersistor({
* cache,
* storage: new MMKVWrapper(storage),
* });
*
*/
export class MMKVWrapper implements PersistentStorage<string | null> {
private storage;

constructor(storage: MMKVInterface) {
this.storage = storage;
}

getItem(key: string): string | null {
return this.storage.getString(key) || null;
}

removeItem(key: string): void {
return this.storage.delete(key);
}

setItem(key: string, value: string | null): void {
if (value !== null) {
return this.storage.set(key, value);
}
return this.removeItem(key);
}
}

interface MMKVInterface {
set: (key: string, value: boolean | string | number) => void;
getBoolean: (key: string) => boolean;
getString: (key: string) => string | undefined;
getNumber: (key: string) => number;
delete: (key: string) => void;
getAllKeys: () => string[];
clearAll: () => void;
}
1 change: 1 addition & 0 deletions src/storageWrappers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from './IonicStorageWrapper';
export * from './LocalForageWrapper';
export * from './LocalStorageWrapper';
export * from './MMKVStorageWrapper';
export * from './MMKVWrapper';
export * from './SessionStorageWrapper';

0 comments on commit 65512c4

Please sign in to comment.