Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

localstorage-migrator

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

localstorage-migrator

A library for applying migrations to localstorage.

  • 1.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
182
decreased by-36.81%
Maintainers
1
Weekly downloads
 
Created
Source

LocalStorage Migrator

This is a library for handling migrations of local storage. It is similar to DbUp. It uses rstolsmark-json-migrator for handling the migration logic.

Install

npm i localstorage-migrator

Usage

A migration is an object that consist of two items:

  • name: This has to be unique
  • up: a method that is called when applying the migration.

Example:


import * as migrator from "localstorage-migrator";
const migrations = [
  {
    name: "delta1",
    up: () => {
      localStorage.setItem(
        "hei",
        JSON.stringify({
          hei: "1"
        })
      );
    }
  },
  {
    name: "delta2",
    up: () => {
      var object = JSON.parse(localStorage.getItem("hei") as string);
      object.num = 2;
      localStorage.setItem("hei", JSON.stringify(object));
    }
  },
  {
    name: "delta3",
    up: () => {
      var object = JSON.parse(localStorage.getItem("hei") as string);
      object.num++;
      localStorage.setItem("hei", JSON.stringify(object));
    }
  }
];

/* This would typically be called in some startup method 
   before local storage is accessed by other code. */
export function runMigrations() : void {
    migrator.migrate(migrations);
}

An example project can be found on GitHub: https://github.com/ragnarstolsmark/migratortest

migrate method

The migrate method takes a an array of migrations, filters them by those who have already been applied and executes them in sequence. The applied migrations is then stored in local storage under a key called: "appliedMigrations".

other methods

getAppliedMigrations

This returns an array of those appliedMigrations that have been stored in local storage. They are returned in the order they was applied. An appliedMigration consist of:

  • name: The name of the migration that has been applied
  • dateApplied: The date the migration was applied
storeAppliedMigrations

This takes an array of appliedMigration objects and stores them in local storage. This can work like a reset method when calling it with an empty array.

Keywords

FAQs

Package last updated on 28 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc