npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

versioned-storage

v2.0.61

Published

Local storage with versioning.

Downloads

3,252

Readme

versioned-storage Node.js CI codecov

Use named, versioned and typed (TypeScript and Flow) 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.

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 Flow to make each storage type safe.

import Storage from 'versioned-storage';

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

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

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

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

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.

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.

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

Migrate data from previous storage schema before purging if necessary.

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.

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

Storage.prototype.read method

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

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

Storage.reset static method

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

Storage.reset();