# @rschubkegel/datacache

> A utility for manually caching data in a browser.

Latest version **0.3.2** (published 2023-04-17) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @rschubkegel/datacache
pnpm add @rschubkegel/datacache
yarn add @rschubkegel/datacache
bun add @rschubkegel/datacache
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2023-04-17 |
| First published | 2023-04-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Rylan Schubkegel |
| Maintainers | rschubkegel |

## Links

- npm: https://www.npmjs.com/package/@rschubkegel/datacache
- npm.io page: https://npm.io/package/@rschubkegel/datacache

## Recent versions

- 0.3.2 (latest) — 2023-04-17
- 0.3.1 — 2023-04-17
- 0.3.0 — 2023-04-17
- 0.2.1 — 2023-04-13
- 0.1.1 — 2023-04-13
- 0.1.0 — 2023-04-11

## README

# datacache

This package is intended for use with web apps that do the following:

1. Fetch a significant amount of data from APIs
2. Perform multiple modifications to that data before saving
3. Require data to be persistent between page navigation (or browser sessions)

This package uses _chunks_ to split data into collections. This reduces read/write overhead when only modifying small portions of a large collection of data.

For example, you may have a book writing web app where each chapter is saved in the cache as its own _chunk_.

## Installation

Install via npm:

```bash
npm i @rschubkegel/datacache
```

Install via yarn:

```bash
yarn add @rschubkegel/datacache
```

## Usage

### Import & setup

```js
import DataCache from '@rschubkegel/datacache';

const myDataCache = new DataCache(
  getData,    // typically GET request
  putData,    // typically PUT request
  deleteData, // typically DELETE
  'my-data',  // prefix for keys in storage where data saved
  window.sessionStorage // can by any object with Storage interface
);
```

### Fetch data

```js
// first call fetches data from API
await myDataCache
  .getChunk('first-item')
  .then(data => console.log('Data received:', data));

// second call (and subsequent calls) gets data from cache
let data = await myDataCache.getChunk('first-item');
```

### Modify & save data

```js
// data can be saved to local cache to reduce API calls
myDataCache.saveLocalChunk('first-item', {...data, newProperty: 'Hello world!'});

// user can freely navigate to & from other pages
// without losing the changes saved locally
window.location.replace('/other-page');
window.location.replace('/original-page');

// when all modifications have completed, the data can be
// saved back to the server (only requires one API call!)
myDataCache.saveChunk('first-item', data);
```

---
_Source: https://npm.io/package/@rschubkegel/datacache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
