mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1# `#/storage` 2 3## Usage 4 5Import the correctly scoped store from `#/storage`. Each instance of `Storage` 6(the base class, not to be used directly), has the following interface: 7 8- `set([...scope, key], value)` 9- `get([...scope, key])` 10- `remove([...scope, key])` 11- `removeMany([...scope], [...keys])` 12 13For example, using our `device` store looks like this, since it's scoped to the 14device (the most base level scope): 15 16```typescript 17import { device } from '#/storage'; 18 19device.set(['foobar'], true); 20device.get(['foobar']); 21device.remove(['foobar']); 22device.removeMany([], ['foobar']); 23``` 24 25## TypeScript 26 27Stores are strongly typed, and when setting a given value, it will need to 28conform to the schemas defined in `#/storage/schema`. When getting a value, it 29will be returned to you as the type defined in its schema. 30 31## Scoped Stores 32 33Some stores are (or might be) scoped to an account or other identifier. In this 34case, storage instances are created with type-guards, like this: 35 36```typescript 37type AccountSchema = { 38 language: `${string}-${string}`; 39}; 40 41type DID = `did:${string}`; 42 43const account = new Storage< 44 [DID], 45 AccountSchema 46>({ 47 id: 'account', 48}); 49 50account.set( 51 ['did:plc:abc', 'language'], 52 'en-US', 53); 54 55const language = account.get([ 56 'did:plc:abc', 57 'language', 58]); 59``` 60 61Here, if `['did:plc:abc']` is not supplied along with the key of 62`language`, the `get` will return undefined (and TS will yell at you).