|
1 | 1 | # Authoring Storage Backend |
2 | 2 |
|
3 | | -To create custom storage, one must create a class that implements `IStorageBackend` interface. |
4 | | -This contract makes sure that Core knows how to use it. |
| 3 | +Async Storage is a [facade](https://en.wikipedia.org/wiki/Facade_pattern) over the underlying Storage solution. |
| 4 | +In order for the new storage to be compatible, it has to implement `IStorageBackend` and its methods. |
| 5 | + |
| 6 | + |
| 7 | +## Table of Content |
| 8 | + |
| 9 | +1. [Creating a storage](#creating-storage-backend) |
| 10 | +2. [Adding extra functionality](#going-being-default-api) |
| 11 | +3. [Example](#example) |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +## Creating Storage Backend |
| 16 | + |
| 17 | +To create storage compatible with Async Storage, one must create a class that implements `IStorageBackend`. It contains a handful of methods, |
| 18 | +that simplifies access to the storage features. Those methods are: |
| 19 | + |
| 20 | +- `getSingle` - retrieves a single element, using provided `key`. |
| 21 | +- `setSingle` - sets a `value` under provided `key` |
| 22 | +- `removeSingle` - removes an entry for provided `key` |
| 23 | +- `getMany` - returns an array of `values`, for a provided array of `keys` |
| 24 | +- `setMany` - provided an array of `key-value` pairs, saves them to the storage |
| 25 | +- `removeMany` - removes a bunch of values, for a provided array of `keys` |
| 26 | +- `getKeys` - returns an array of `keys` that were used to store values |
| 27 | +- `dropStorage` - purges the storage |
| 28 | + |
| 29 | + |
| 30 | +Few points to keep in mind while developing new storage: |
| 31 | + |
| 32 | +- Every public method should be asynchronous (returns a promise) - even if access to the storage is not. This helps to keep API consistent. |
| 33 | +- Each method accepts additional `opts` argument, which can be used to modify the call (i. e. decide if the underlying value should be overridden, if already exists) |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +## Going being default API |
| 38 | + |
| 39 | +Unified API can be limiting - storages differ from each other and contain features that others do not. Async Storage comes with an extension property, that lets you extend its standard API. |
| 40 | + |
| 41 | +The `ext` property is a custom getter that exposes publicly available methods from your Storage. |
| 42 | +Let's say that you have a feature that removes entries older than 30 days and you call it `purge`. |
| 43 | + |
| 44 | +#### Notes |
| 45 | + |
| 46 | +In order for a property to be exposed: |
| 47 | + |
| 48 | +- It has to be a function |
| 49 | +- It has to have `public` property access (for type safety) |
| 50 | +- Does not start with _underscore_ character - AsyncStorage consider those private |
| 51 | + |
| 52 | + |
| 53 | +#### Example: |
| 54 | + |
| 55 | +Simply add a public method to expose it for Async Storage's extension. |
| 56 | + |
| 57 | +```typescript |
| 58 | +import { IStorageBackend } from '@react-native-community/async-storage'; |
| 59 | + |
| 60 | + |
| 61 | +class MyStorage implements IStorageBackend<MyModel> { |
| 62 | + // overridden methods here |
| 63 | + |
| 64 | + public async purgeEntries() { |
| 65 | + // implementation |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +Now your method is exposed through `ext` property: |
| 71 | + |
| 72 | + |
| 73 | +```typescript |
| 74 | + |
| 75 | +import MyStorage from 'my-awesome-storage' |
| 76 | +import ASFactory from '@react-native-community/async-storage' |
| 77 | + |
| 78 | +const storage = ASFactory.create(new MyStorage(), {}); |
| 79 | + |
| 80 | +// somewhere in the codebase |
| 81 | +async function removeOldEntries() { |
| 82 | + await storage.ext.purgeEntries(); |
| 83 | + console.log('Done!'); |
| 84 | +} |
| 85 | +``` |
5 | 86 |
|
6 | 87 | ## Example |
7 | 88 |
|
@@ -82,32 +163,3 @@ class WebStorage<T extends EmptyStorageModel = EmptyStorageModel> implements ISt |
82 | 163 |
|
83 | 164 | export default WebStorage; |
84 | 165 | ``` |
85 | | - |
86 | | -### Notes |
87 | | - |
88 | | -- Each function should be asynchronous - even if access to storage is not. |
89 | | -- In `localStorage`, remember that __keys__ and __values__ are always `string` - it's up to you if you're going to stringify it or accept stringified arguments. |
90 | | -- `opts` argument can be used to 'enhance' each call, for example, one could use it to decide if the stored value should be replaced: |
91 | | - |
92 | | -```typescript |
93 | | - |
94 | | -// in a class |
95 | | - |
96 | | -async setSingle<K extends keyof T>( |
97 | | - key: K, |
98 | | - value: T[K], |
99 | | - opts?: StorageOptions, |
100 | | -): Promise<void> { |
101 | | - |
102 | | - if(!opts.replaceCurrent) { |
103 | | - const current = this.storage.getItem(key); |
104 | | - if(!current){ |
105 | | - this.storage.setItem(key, value); |
106 | | - } |
107 | | - return; |
108 | | - } |
109 | | - |
110 | | - return this.storage.setItem(key, value); |
111 | | -} |
112 | | - |
113 | | -``` |
|
0 commit comments