# versioned-storage

> Local storage with versioning.

Latest version **4.0.23** (published 2026-08-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install versioned-storage
pnpm add versioned-storage
yarn add versioned-storage
bun add versioned-storage
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.0.23 |
| Published | 2026-08-16 |
| First published | 2019-11-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=20 |
| Dependencies | 0 |
| Unpacked size | 485.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 16 |
| Author | catchen |
| Maintainers | catchen |
| Keywords | versioned, storage |

## Links

- npm: https://www.npmjs.com/package/versioned-storage
- Repository: https://github.com/CatChen/versioned-storage
- Homepage: https://github.com/CatChen/versioned-storage#readme
- Issues: https://github.com/CatChen/versioned-storage/issues
- Funding: https://github.com/CatChen/versioned-storage?sponsor=1
- npm.io page: https://npm.io/package/versioned-storage

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 4.0.23 (latest) — 2026-08-16
- 4.0.22 — 2026-08-09
- 4.0.21 — 2026-08-02
- 4.0.20 — 2026-07-26
- 4.0.19 — 2026-07-12
- 4.0.18 — 2026-06-28
- 4.0.17 — 2026-06-21
- 4.0.16 — 2026-06-14
- 4.0.15 — 2026-06-07
- 4.0.14 — 2026-05-31
- 4.0.13 — 2026-05-17
- 4.0.12 — 2026-05-10
- 4.0.11 — 2026-05-03
- 4.0.10 — 2026-04-26
- 4.0.9 — 2026-04-12
- … 147 more at https://npm.io/package/versioned-storage/versions

## README

# versioned-storage [![Node](https://github.com/CatChen/versioned-storage/actions/workflows/node.yml/badge.svg)](https://github.com/CatChen/versioned-storage/actions/workflows/node.yml) [![Deno](https://github.com/CatChen/versioned-storage/actions/workflows/deno.yml/badge.svg)](https://github.com/CatChen/versioned-storage/actions/workflows/deno.yml) [![codecov](https://codecov.io/gh/CatChen/versioned-storage/branch/main/graph/badge.svg)](https://codecov.io/gh/CatChen/versioned-storage)

Use named, versioned and typed ([TypeScript](https://www.typescriptlang.org/) and [Flow](https://flow.org/)) JSON storage through `localStorage`!

## Examples

Create a named and versioned storage by creating a `Storage` object. Use `Storage.prototype.write` and `Storage.prototype.read` to operate.

```JavaScript
import { Storage } from 'versioned-storage';

const userStorage = new Storage('user', 1);
userStorage.write({
  id: 42,
  name: 'Cat',
});
console.log(userStorage.read()); // { id: 42, name: 'Cat' }
```

Add typing information with TypeScript to make each storage type safe.

```TypeScript
import { Storage } from 'versioned-storage';

type User = {
  id: number;
  name: string;
}

const userStorage = new Storage<User>('user', 1);

userStorage.write({ id: 42 });
// TypeScript error because userStorage.write is typed as (value: User) => void.

const user = userStorage.read();
// TypeScript infers user being User because userStorage.read is typed as () => User.
```

Use package name with scope when used in Deno with JSR.

```TypeScript
import { Storage } from '@catchen/versioned-storage';
```

## Features

1. Read and write values as JSON instead of string.
2. TypeScript and Flow typing support for JSON structure being read and written.

## API

The API is very simple. There are only 4 functions you need to know.

### `Storage` class

Create a storage by calling `Storage` class constructor with a name and a version.

```JavaScript
const settingsStorage = new Storage('settings', 1);
```

When a storage's schema is changed and no longer compatible, bump the version number and old data will be purged automatically.

```JavaScript
const settingsStorage = new Storage('settings', 2); // Erase all data from version 1
```

Migrate data from previous storage schema before purging if necessary.

```JavaScript
let settingsStorage;
try {
  settingsStorage = new Storage('settings'); // Get the storage of existing version
  switch (settingsStorage.version) {
    case 1:
      // Read v1 settings, migrate it to v3 schema and store it
      break;
    case 2:
      // Read v2 settings, migrate it to v3 schema and store it
      break;
    default:
      throw new Error('Incompatible legacy storage schema');
  }
} catch (_error) {
  settingsStorage = new Storage('settings', 3); // Start from scratch if not migratable
}
```

### `Storage.prototype.write` method

Write data to a named storage by calling `write` method on its instance.

```JavaScript
settingsStorage.write({
  brightness: 80,
  volume: 100,
});
```

### `Storage.prototype.read` method

Read data from a named storage by calling `read` method on its instance.

```JavaScript
const {
  brightness,
  volume,
} = settingsStorage.read();
```

### `Storage.reset` static method

Clear all data across all named storages by calling the static method `reset`.

```JavaScript
Storage.reset();
```

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