https://github.com/mswjs/storage
Persistence and live synchronization layer for testing JavaScript applications.
https://github.com/mswjs/storage
Last synced: 4 months ago
JSON representation
Persistence and live synchronization layer for testing JavaScript applications.
- Host: GitHub
- URL: https://github.com/mswjs/storage
- Owner: mswjs
- Created: 2020-11-04T10:08:38.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-08-09T10:48:26.000Z (over 4 years ago)
- Last Synced: 2025-04-25T03:37:17.447Z (9 months ago)
- Language: TypeScript
- Homepage:
- Size: 163 KB
- Stars: 18
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-list - storage
README
> **Please note that this library is about to be discontinued. Please prefer using the [@mswjs/data](https://github.com/mswjs/data) package that provides data modeling capabilities, querying client, and data persistency. Thank you.**
# Storage
Data storage and persistency layer for testing JavaScript applications.
## Features
### Persistency
The values of a live storage are persisted in the session. In a browser that is achieved by [`sessionStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage).
### Real-time synchronization
Updates to the storage are synchronized between all active clients in real time. In a browser that is achieved by using a [`BroadcastChannel`](https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel) to signal updates.
## When to use?
- When writing CRUD operations in tests;
- When conducting local in-browser testing/debugging;
- In combination with API mocking tools (i.e. [MSW](https://github.com/mswjs/msw))
## Get started
### Install
```bash
$ npm install @mswjs/storage --save-dev
```
### Create storage
```js
import { LiveStorage } from '@mswjs/storage'
// Instantiate a new storage with a unique string key
// and initial value.
const posts = new LiveStorage('posts', [])
```
### Update values
```js
// Storage update is a function that derives the next value
// from the previous storage value.
posts.update((prevPosts) => prevPosts.concat({ title: 'Brave new world' });
```
### Get value
```js
posts.getValue() // [{ title: 'Brave new world' }]
```