localstorage-migrator
Advanced tools
Comparing version 1.0.7 to 1.0.8
{ | ||
"name": "localstorage-migrator", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "A library for applying migrations to localstorage.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -15,8 +15,13 @@ # LocalStorage Migrator | ||
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. | ||
- name: This has to be unique | ||
- up: a method that is called when applying the migration. | ||
Example: | ||
```typescript | ||
import * as migrator from "localstorage-migrator"; | ||
const objectKey = "someKey"; | ||
const migrations = [ | ||
@@ -27,5 +32,5 @@ { | ||
localStorage.setItem( | ||
"hei", | ||
objectKey, | ||
JSON.stringify({ | ||
hei: "1" | ||
name: "John Doe" | ||
}) | ||
@@ -38,5 +43,5 @@ ); | ||
up: () => { | ||
var object = JSON.parse(localStorage.getItem("hei") as string); | ||
object.num = 2; | ||
localStorage.setItem("hei", JSON.stringify(object)); | ||
var object = JSON.parse(localStorage.getItem(objectKey) as string); | ||
object.age = 2; | ||
localStorage.setItem(objectKey, JSON.stringify(object)); | ||
} | ||
@@ -47,5 +52,5 @@ }, | ||
up: () => { | ||
var object = JSON.parse(localStorage.getItem("hei") as string); | ||
object.num++; | ||
localStorage.setItem("hei", JSON.stringify(object)); | ||
var object = JSON.parse(localStorage.getItem(objectKey) as string); | ||
object.age++; | ||
localStorage.setItem(objectKey, JSON.stringify(object)); | ||
} | ||
@@ -57,8 +62,8 @@ } | ||
before local storage is accessed by other code. */ | ||
export function runMigrations() : void { | ||
migrator.migrate(migrations); | ||
export function runMigrations(): void { | ||
migrator.migrate(migrations); | ||
} | ||
``` | ||
An example project can be found on GitHub: | ||
An example project can be found on GitHub: | ||
https://github.com/ragnarstolsmark/migratortest | ||
@@ -69,2 +74,3 @@ | ||
### migrate | ||
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". | ||
@@ -75,8 +81,8 @@ | ||
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 | ||
- 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. | ||
5996
82