.get
Method
The .get
method is used to retrieve values from the Storage Key. There are six different ways to get, retrieve, or restore data from the Store.
IMPORTANT
Before using the .get
method, make sure to import the Store using import { StoreName } from 'path/store-name.ts'
.
NOTE
All returned data is wrapped in a Promise and follows the async/await function signature.
Retrieve All Entire Data
This method returns all the data stored in the Store.
const data = await userSettings.get();
The returned data will look like this:
{
"theme": "dark",
"language": "en-US",
"primaryColor": "blue"
}
signature
.get(): Promise<Readonly<Data>>;
Sepcific Alias
.getByEntire(): Promise<Readonly<Data>>;
Get Single Key/Value
You can retrieve a specific key's value using the .get method.
userSettings.get('theme');
The returned value will be:
"dark"
You can also map the value to a custom output format using a function:
userSettings.get('theme', (theme) => {
return {
value: theme,
message: `the current theme is: ${theme}`,
};
});
The output will be:
{
"value": "dar",
"message": "we current theme is: dark"
}
signature
The generic type is automatically inferred, so you don't need to pass it explicitly.
.get<Key extends keyof Data>(key: Key): Promise<Data[Key]>;
In case you want to map the value to a custom output format, you can use:
.get<Key extends keyof Data, Mapper extends (data: Data[Key] | never) => (Data[Key] | unknown)>(key: Key, mapper: Mapper): Promise<ReturnType<Mapper> | Data[Key] | unknown>;
Specific Alias
.getByKey<Key extends keyof Data>(key: Key): Promise<Data[Key]>;
In case you want to map the value to a custom output format, you can use:
.getByKeyMapper<Key extends keyof Data, Mapper extends (data: Data[Key] | never) => (Data[Key] | unknown)>(key: Key, mapper: Mapper): Promise<ReturnType<Mapper> | Data[Key] | unknown>;
IMPORTANT
Avoiding to change data, rendering, update state or stores in mapper
callback function.
Otherwise, using .then
chain of returned Promise
Filter By Key List
This method allows you to retrieve data for a list of selected keys.
userSettings.get(['theme', 'primaryColor']);
The returned data will be:
{
"theme": "dark",
"primaryColor": "blue"
}
signature
The generic type is automatically inferred, so you don't need to pass it explicitly.
.get<KeyList extends Array<keyof Data>>(keys: Partial<KeyList>): Promise<Readonly<{ [SelectedKey in KeyList extends Partial<Array<infer Key>> ? Key : never]: Data[SelectedKey] }>>;
Specific Alias
The generic type is automatically inferred, so you don't need to pass it explicitly.
.getByKeys<KeyList extends Array<keyof Data>>(keys: Partial<KeyList>): Promise<Readonly<{ [SelectedKey in KeyList extends Partial<Array<infer Key>> ? Key : never]: Data[SelectedKey] }>>;
Filter By Key Object
This method allows you to retrieve data for selected keys based on a boolean
-like value.
userSettings.get({ theme: true, primaryColor: false });
{
"theme": "dark"
}
signature
The generic type is automatically inferred, so you don't need to pass it explicitly.
.get<KeyObject extends Record<keyof Data, boolean>>(keys: Partial<KeyObject>): Promise<TruthyKeysReturnType<Data, KeyObject>>;
Specific Alias
The generic type is automatically inferred, so you don't need to pass it explicitly.
.getByTruthy<KeyObject extends Record<keyof Data, boolean>>(keys: Partial<KeyObject>): Promise<TruthyKeysReturnType<Data, KeyObject>>;
Custom Data Mapping
This method allows you to transform the input data into a custom output format.
userSettings.get((store) => {
return {
value: store.theme,
message: `the current theme is: ${store.theme}`,
};
});
The returned data will be:
{
"value": "dark",
"message": "the current theme is: dark"
}
You can also use this method as a Selector option:
userSettings.get((store) => store.theme);
The returned data will be:
"dark"
signature
The generic type is automatically inferred, so you don't need to pass it explicitly.
.get<Mapper extends (data: Readonly<Data> | never) => (Data | unknown)>(mapper: Mapper): Promise<ReturnType<Mapper> | Data | unknown>;
Specific Alias
The generic type is automatically inferred, so you don't need to pass it explicitly.
.getByMapper<Mapper extends (data: Readonly<Data> | never) => (Data | unknown)>(mapper: Mapper): Promise<ReturnType<Mapper> | Data | unknown>;
IMPORTANT
Avoiding to change data, rendering, update state or stores in mapper
callback function.
Otherwise, using .then
chain of returned Promise
Using Utilities
There is also @biruni/utility
library to contain some magical modules to be helper for more DX!
- Import
import { get } from '@biruni/utility';
- Use
get(userSettings);
get(userSettings, ['theme']);
get(userSettings, 'theme');
get(userSettings, 'theme', (theme) => `${theme} mode`);
get(userSettings, { theme: true });
get(userSettings, (store) => store.theme);