Skip to content

Add Capacitor Preferences storage provider #476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions docs/storage-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@

`apollo3-cache-persist` provides wrappers for the following storage providers, with no additional dependencies:

| Storage provider | Platform | Wrapper class |
| ------------------------------------------------------------------------------- | -------------- | ----------------------- |
| [`AsyncStorage`](https://github.com/react-native-async-storage/async-storage)\* | React Native | `AsyncStorageWrapper` |
| `window.localStorage` | web | `LocalStorageWrapper` |
| `window.sessionStorage` | web | `SessionStorageWrapper` |
| [`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` |
| Storage provider | Platform | Wrapper class |
|---------------------------------------------------------------------------------|----------------|-------------------------------|
| [`AsyncStorage`](https://github.com/react-native-async-storage/async-storage)\* | React Native | `AsyncStorageWrapper` |
| `window.localStorage` | web | `LocalStorageWrapper` |
| `window.sessionStorage` | web | `SessionStorageWrapper` |
| [`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` |
| [`Capacitor Preferences`](https://capacitorjs.com/docs/apis/preferences) | Capacitor | `CapacitorPreferencesWrapper` |


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

export class CapacitorPreferencesWrapper implements PersistentStorage<string | null> {
private storage;

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

getItem(key: string): Promise<string | null> {
return this.storage.get({key: key}).then(r => r.value);
}

removeItem(key: string): Promise<void> {
return this.storage.remove({key: key});
}

setItem(key: string, value: string | null): Promise<void> {
if (value) {
// Capacitor Preferences does not support nullable values
return this.storage.set({key: key, value: value});
} else {
return this.removeItem(key);
}
}
}

interface CapacitorPreferencesInterface {
// Actual type definition: https://capacitorjs.com/docs/apis/preferences#api
get(options: { key: string }): Promise<{ value: string | null }>;

set(options: { key: string, value: string }): Promise<void>;

remove(options: { key: string }): Promise<void>;
}
1 change: 1 addition & 0 deletions src/storageWrappers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './LocalStorageWrapper';
export * from './MMKVStorageWrapper';
export * from './MMKVWrapper';
export * from './SessionStorageWrapper';
export * from './CapacitorPreferencesWrapper';