⚠ Project Discontinuation
We regret to inform you that this project is no longer maintained. For an alternative solution, consider using React Query
It provides CRUD methods for objects stored in localStorage
or sessionStorage
.
Create a new provider using the LocalStorage
or SessionStorage
classes.
options
(Object): Apart of common data-provider options, this plugin also accept next options:id
(String): Id of the provider, will be used also as thekey
where the provider data is stored inlocalStorage
orsessionStorage
.storageFallback
(Boolean): Defaulttrue
. If there is an error trying to access towindow.localStorage
orwindow.sessionStorage
, a mock will be used instead, and data will be persisted in memory. This could happen iflocalStorage
is disabled by the browser, for example. If you want to handle exceptions by yourself, you can disable this behavior setting this option tofalse
, and then all calls toread
,update
ordelete
methods will be rejected with the correspondent error, which will be stored also in theerror
property of the provider state.initialState
(Object): Same option than the one described in the data-provider API docs, except thedata
property, which in this case has no effect, as the initial data set in the state will be the data retrieved synchronously from the reallocalStorage
orsessionStorage
.
import { LocalStorage } from "@data-provider/browser-storage";
const userPreferences = new LocalStorage({
id: "user-preferences",
storageFallback: false
});
// userPreferences object will be stored in localStorage `user-preferences` key.
// If localStorage is disabled, no in-memory mock will be used instead
const sessionData = new SessionStorage({
id: "user-session"
});
// sessionData object will be stored in sessionStorate `user-session` key.
Read the Data Provider docs for further info about how to use addons.
When querying providers created with this addon, the query object can have one of the next properties:
prop
(String): Specific property of the object from localStorage or sessionStorage to be accessed.
import { LocalStorage } from "@data-provider/browser-storage";
const userPreferences = new LocalStorage({
id: "user-preferences"
});
userPreferences.query({ prop: "cookiesAccepted" }).update(true);
userPreferences.query({ prop: "cookiesAccepted" }).read().then(result => {
console.log("Cookies accepted", result);
// true
});
Apart of the common Data Provider methods, next ones are available:
Updates an specific property of the stored object when the provider is queried, or the full object when not. When the object is modified, it will automatically cleans the cache of the provider and also the cache of the parent provider when it is queried (as modifying a property also modifies the full object).
data
(Any): New data to be set. (Take into account that provided data will be stringified when saved to localStorage)
A promise that will be resolved when the localStorage is updated, or will be rejected with an error in case the operation fails.
// modify an specific property
userPreferences.query({ prop: "cookiesAccepted" }).update(true)
.then(() => {
console.log("Local storage updated!");
})
.catch(error => {
console.log("Error updating local storage", error);
});
// Overwrite full object
userPreferences.update({
cookiesAccepted: true
});
Removes an specific property of the stored object when the provider is queried, or sets the full object as empty when not. When the object is modified, it will automatically cleans the cache of the provider and also the cache of the parent provider when it is queried (as deleting a property also modifies the full object).
A promise that will be resolved when the localStorage is updated, or will be rejected with an error in case the operation fails.
// removes an specific property
userPreferences.query({ prop: "cookiesAccepted" }).delete()
.then(() => {
console.log("Local storage updated");
})
.catch(error => {
console.log("Error updating local storage", error);
});
// Sets the full object as {}
userPreferences.delete();
Providers created with this addon will have automatically the browser-storage
tag, so you can select all of them together using the providers
methods as in:
import { providers } from "@data-provider/core";
providers.getByTag("browser-storage").cleanCache();
Apart of this common tag, each different type of browser-storage
origin also has next tags:
LocalStorage
: "local-storage"SessionStorage
: "session-storage"
Contributors are welcome. Please read the contributing guidelines and code of conduct.