From 1b16bdb00e896f62ee379777f7224a156e16004f Mon Sep 17 00:00:00 2001 From: Thibault Malbranche Date: Wed, 15 Sep 2021 17:45:47 +0200 Subject: [PATCH 1/3] feat(MMKVWrapper): Added new storage wrapper Added storage wrapper for react-native-mmkv. Storage wrapper already existed for another mmvk lib, but they don't have the same interface --- src/storageWrappers/MMKVWrapper.ts | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/storageWrappers/MMKVWrapper.ts diff --git a/src/storageWrappers/MMKVWrapper.ts b/src/storageWrappers/MMKVWrapper.ts new file mode 100644 index 00000000..79429659 --- /dev/null +++ b/src/storageWrappers/MMKVWrapper.ts @@ -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 { + 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; +} From aaac616a4549ddda88f37996e336add7d57b04bf Mon Sep 17 00:00:00 2001 From: Thibault Malbranche Date: Wed, 15 Sep 2021 17:49:35 +0200 Subject: [PATCH 2/3] Update storage-providers.md --- docs/storage-providers.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/storage-providers.md b/docs/storage-providers.md index 17cd4c2b..d29e2ad1 100644 --- a/docs/storage-providers.md +++ b/docs/storage-providers.md @@ -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 From e3dbe4c4f76f44f0931349553ea715d6750372fd Mon Sep 17 00:00:00 2001 From: Thibault Malbranche Date: Fri, 17 Sep 2021 10:03:38 +0200 Subject: [PATCH 3/3] Update index.ts --- src/storageWrappers/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/storageWrappers/index.ts b/src/storageWrappers/index.ts index 55e686c2..38383427 100644 --- a/src/storageWrappers/index.ts +++ b/src/storageWrappers/index.ts @@ -3,4 +3,5 @@ export * from './IonicStorageWrapper'; export * from './LocalForageWrapper'; export * from './LocalStorageWrapper'; export * from './MMKVStorageWrapper'; +export * from './MMKVWrapper'; export * from './SessionStorageWrapper';