https://github.com/steveswork/universal-storage
https://github.com/steveswork/universal-storage
Last synced: 13 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/steveswork/universal-storage
- Owner: steveswork
- Created: 2024-03-07T07:06:55.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-14T00:45:31.000Z (over 2 years ago)
- Last Synced: 2026-06-13T01:32:39.697Z (13 days ago)
- Language: JavaScript
- Size: 420 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# Universal Storage
A persistent client-server coordinated storage based on client domain cookies with a local-storage fallback.
### Name:
universal-storage
# Installation
npm install --save universal-storage
# Usage
### In client-side code.
```jsx
import { discardClientStorage, getClientStorage } from 'universal-storage';
let storage = getClientStorage(); // establishes a client storage singleton
storage.current.setItem( 'key', 'value' );
storage.current.getItem( 'key' ); // 'value'
storage.current.removeItem( 'key' );
storage.current.getItem( 'key' ); // undefined
storage.current.setItem( 'key', 'value2' );
storage.current.getItem( 'key' ); // 'value2'
discardClientStorage(); // destroys the current client storage singleton
storage.current.getItem( 'key' ); // error! cannot read getItem of undefined; reading 'storage.current'
storage = getClientStorage(); // reestablishes a client storage singleton
storage.current.getItem( 'key' ); // 'value2'
```
### In server-side code.
```jsx
import { discardServerStorage, getServerStorage } from 'universal-storage';
let storage = getServerStorage(); // establishes a client storage singleton
storage.current.setItem( 'key', 'value', e.response );
storage.current.getItem( 'key', e.request ); // 'value'
storage.current.removeItem( 'key', 'value', e.response );
storage.current.getItem( 'key', e.request ); // undefined
storage.current.setItem( 'key', 'value2', e.response );
storage.current.getItem( 'key', e.request ); // 'value2'
discardServerStorage(); // destroys the current client storage singleton
storage.current.getItem( 'key', e.request ); // error! cannot read getItem of undefined; reading 'storage.current'
storage = getServerStorage(); // reestablishes a client storage singleton
storage.current.getItem( 'key', e.request ); // 'value2'
```
# License
MIT