|
| 1 | +# Async Storage Core |
| 2 | + |
| 3 | +Main, public-facing components of Async Storage. The core module contains a factory to create an `AsyncStorage` instance |
| 4 | +and `IStorageBackend`, that needs to be implemented by any Backend Storage in order to be compatible. |
| 5 | + |
| 6 | +## Install |
| 7 | + |
| 8 | +```bash |
| 9 | +$ yarn install @react-native-community/async-storage |
| 10 | +``` |
| 11 | + |
| 12 | +## API |
| 13 | + |
| 14 | + |
| 15 | +### `AsyncStorageFactory` |
| 16 | + |
| 17 | +A factory module for `AsyncStorage` instance with selected storage backend attached. |
| 18 | + |
| 19 | + |
| 20 | +```typescript |
| 21 | +// storage.js |
| 22 | + |
| 23 | +import ASFactory from '@react-native-community/async-storage' |
| 24 | + |
| 25 | +// use any available Storage Backend |
| 26 | +const storageBackend = new StorageBackend(); |
| 27 | + |
| 28 | +const mobileStorage = ASFactory.create(storageBackend, options); |
| 29 | + |
| 30 | +export default mobileStorage; |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | +**Factory options** |
| 35 | + |
| 36 | +`AsyncStorageFactory.create` accepts an options object, that enables additional features. |
| 37 | + |
| 38 | + |
| 39 | +- *logger* |
| 40 | + |
| 41 | +```typescript |
| 42 | +type logger = ((action: LoggerAction) => void) | boolean; |
| 43 | +``` |
| 44 | + |
| 45 | +Used to log `AsyncStorage` method calls and used arguments. |
| 46 | +You can provide your own implementation or use provided default logger (enabled by default in DEV mode). |
| 47 | + |
| 48 | + |
| 49 | +- *errorHandler* |
| 50 | + |
| 51 | +```typescript |
| 52 | +type errorHandler = ((error: Error | string) => void) | boolean; |
| 53 | +```` |
| 54 | + |
| 55 | +Used to report any errors thrown. |
| 56 | +You can provide your own implementation or use provided default error handler (enabled by default in DEV mode). |
| 57 | + |
| 58 | + |
| 59 | +**Providing a storage model** |
| 60 | + |
| 61 | +If you know the structure of the stored data upfront, you can use the full potential of the type system, by providing a type argument to `AsyncStorageFactory.create<T>` method. |
| 62 | + |
| 63 | + |
| 64 | +```typescript |
| 65 | +import ASFactory from '@react-native-community/async-storage' |
| 66 | +
|
| 67 | +type StorageModel = { |
| 68 | + user: { |
| 69 | + name: string; |
| 70 | + age: number; |
| 71 | + }; |
| 72 | + preferences: { |
| 73 | + darkModeEnabled: boolean; |
| 74 | + }; |
| 75 | + subscribedChannels: Array<string>; |
| 76 | + onboardingCompleted: boolean; |
| 77 | +}; |
| 78 | +
|
| 79 | +// use any available Storage Backend |
| 80 | +const storageBackend = new StorageBackend(); |
| 81 | +
|
| 82 | +
|
| 83 | +const storage = ASFactory.create<StorageModel>(storageBackend); |
| 84 | +
|
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | +### `IStorageBackend` |
| 89 | + |
| 90 | +In order to let `AsyncStorage` use a storage backend, it has to implement this interface. |
| 91 | +Contains basic set method of methods to get, set and remove data, return already used keys or drop the whole storage. |
| 92 | +
|
| 93 | +
|
| 94 | +```typescript |
| 95 | + |
| 96 | +import { |
| 97 | + IStorageBackend, |
| 98 | +} from '@react-native-community/async-storage'; |
| 99 | + |
| 100 | +type Model = { |
| 101 | + count: number |
| 102 | + user: { |
| 103 | + name: string, |
| 104 | + rating: number |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +class MyStorageSolution implements IStorageBackend<Model> { |
| 109 | + // implement necessary methods |
| 110 | +} |
| 111 | + |
| 112 | +``` |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +## License |
| 117 | + |
| 118 | +MIT |
| 119 | + |
0 commit comments