Use named, versioned and typed (TypeScript and Flow) JSON storage through localStorage
!
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 TypeScript 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 }); // 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.
import { Storage } from '@catchen/versioned-storage';
The API is very simple. There are only 4 functions you need to know.
Storage
classCreate 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
methodWrite data to a named storage by calling write
method on its instance.
settingsStorage.write({ brightness: 80, volume: 100, });
Storage.prototype.read
methodRead data from a named storage by calling read
method on its instance.
const { brightness, volume, } = settingsStorage.read();
Storage.reset
static methodClear all data across all named storages by calling the static method reset
.
Storage.reset();
Add Package
deno add jsr:@catchen/versioned-storage
Import symbol
import * as versioned_storage from "@catchen/versioned-storage";
---- OR ----
Import directly with a jsr specifier
import * as versioned_storage from "jsr:@catchen/versioned-storage";
Add Package
npx jsr add @catchen/versioned-storage
Import symbol
import * as versioned_storage from "@catchen/versioned-storage";
Add Package
yarn dlx jsr add @catchen/versioned-storage
Import symbol
import * as versioned_storage from "@catchen/versioned-storage";
Add Package
pnpm dlx jsr add @catchen/versioned-storage
Import symbol
import * as versioned_storage from "@catchen/versioned-storage";
Add Package
bunx jsr add @catchen/versioned-storage
Import symbol
import * as versioned_storage from "@catchen/versioned-storage";