Skip to content

Commit

Permalink
fix!: add missing storage type to prepare for cookies management
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
- Add `storageType` to all operations
- Many types are renamed
  • Loading branch information
ivangabriele committed May 31, 2024
1 parent 637adc0 commit c08ee28
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 30 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![License][img-license]][lnk-license] [![NPM Version][img-npm]][lnk-npm]
[![Unit Tests][img-unit-tests]][lnk-unit-tests] [![Coverage][img-coverage]][lnk-coverage]

Manage your Local Storage migrations like you would with a database.
Manage your Local Storage & cookies migrations like you would with a database.

---

Expand All @@ -21,6 +21,8 @@ Manage your Local Storage migrations like you would with a database.

- [x] JS library
- [x] React hook
- [ ] Cookies
- [x] LocalStorage

## Usage

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "storage-migrator",
"description": "Manage your Local Storage migrations like you would with a database.",
"description": "Manage your Local Storage & cookies migrations like you would with a database.",
"version": "1.0.0",
"license": "MIT",
"type": "module",
Expand All @@ -15,7 +15,7 @@
"clean": "yarn build:clean && rm -Rf ./node_modules/.cache",
"prepare": "husky",
"test": "yarn test:lint && yarn test:type",
"test:lint": "biome check --apply --files-ignore-unknown=true --no-errors-on-unmatched *",
"test:lint": "biome check --apply --files-ignore-unknown=true --no-errors-on-unmatched .",
"test:type": "tsc",
"test:unit": "jest --config=./config/jest.config.js",
"test:unit:watch": "yarn test:unit --watch"
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export { useLocalStorageMigrator } from "./hooks/useLocalStorageMigrator"

export {
LocalStorageMigrator,
LocalStorageMigrationOperationType,
OperationType,
LAST_MIGRATION_VERSION_KEY,
type LocalStorageMigration,
type LocalStorageMigrationOperation,
type Operation,
} from "./libs/LocalStorageMigrator"
6 changes: 5 additions & 1 deletion src/libs/LocalStorageMigrator/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
export enum LocalStorageMigrationOperationType {
export enum OperationType {
DeleteKey = "DeleteKey",
RenameJsonValuePropertyKey = "RenameJsonValuePropertyKey",
RenameKey = "RenameKey",
UpdateJsonPropertyValue = "UpdateJsonPropertyValue",
}

export enum StorageType {
LocalStorage = "LocalStorage",
}

export const LAST_MIGRATION_VERSION_KEY =
"localStorageMigrator:lastMigrationVersion"
22 changes: 12 additions & 10 deletions src/libs/LocalStorageMigrator/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { assertNotNullish } from "@utils/assertNotNullish"
import { isPlainObject, omit } from "lodash/fp"

import {
LAST_MIGRATION_VERSION_KEY,
LocalStorageMigrationOperationType,
} from "./constants"
import { LAST_MIGRATION_VERSION_KEY, OperationType } from "./constants"

import type { AnyObject } from "@typings"
import type {
Expand Down Expand Up @@ -58,22 +55,22 @@ export class LocalStorageMigrator {
#applyMigration(migration: LocalStorageMigration): void {
for (const operation of migration.operations) {
switch (operation.type) {
case LocalStorageMigrationOperationType.RenameKey: {
case OperationType.RenameKey: {
this.#renameLocalStorageKey(operation.from, operation.to)
break
}

case LocalStorageMigrationOperationType.DeleteKey: {
case OperationType.DeleteKey: {
this.#deleteLocalStorageKey(operation.key)
break
}

case LocalStorageMigrationOperationType.UpdateJsonPropertyValue: {
case OperationType.UpdateJsonPropertyValue: {
this.#updateLocalStorageJsonValuePropertyValue(operation)
break
}

case LocalStorageMigrationOperationType.RenameJsonValuePropertyKey: {
case OperationType.RenameJsonValuePropertyKey: {
this.#updateLocalStorageJsonValuePropertyKey(operation)
break
}
Expand Down Expand Up @@ -188,9 +185,14 @@ export class LocalStorageMigrator {
}
}

export { LocalStorageMigrationOperationType }
// biome-ignore lint/performance/noBarrelFile: <explanation>
export {
LAST_MIGRATION_VERSION_KEY,
OperationType,
StorageType,
} from "./constants"

export type {
LocalStorageMigration,
LocalStorageMigrationOperation,
Operation,
} from "./types"
18 changes: 11 additions & 7 deletions src/libs/LocalStorageMigrator/types.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
import type { LocalStorageMigrationOperationType } from "./constants"
import type { OperationType, StorageType } from "./constants"

export type LocalStorageMigration = {
description: string
operations: LocalStorageMigrationOperation[]
operations: Operation[]
}

export type LocalStorageMigrationOperation =
export type Operation =
| DeleteKey
| RenameKey
| RenameJsonValuePropertyKey
| UpdateJsonValuePropertyValue

export type DeleteKey = {
key: string
type: LocalStorageMigrationOperationType.DeleteKey
type: OperationType.DeleteKey
storageType: StorageType
}

export type RenameKey = {
from: string
to: string
type: LocalStorageMigrationOperationType.RenameKey
storageType: StorageType
type: OperationType.RenameKey
}

export type RenameJsonValuePropertyKey = {
key: string
newJsonKey: string
oldJsonKey: string
type: LocalStorageMigrationOperationType.RenameJsonValuePropertyKey
storageType: StorageType
type: OperationType.RenameJsonValuePropertyKey
}

export type UpdateJsonValuePropertyValue = {
jsonKey: string
key: string
newJsonValue: string | number | boolean | null
oldJsonValue: string | number | boolean | null
type: LocalStorageMigrationOperationType.UpdateJsonPropertyValue
storageType: StorageType
type: OperationType.UpdateJsonPropertyValue
}
18 changes: 11 additions & 7 deletions src/libs/__tests__/LocalStorageMigrator.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { beforeEach, describe, expect, it } from "@jest/globals"
import { LAST_MIGRATION_VERSION_KEY } from "@libs/LocalStorageMigrator/constants"

import {
LAST_MIGRATION_VERSION_KEY,
type LocalStorageMigration,
LocalStorageMigrationOperationType,
LocalStorageMigrator,
OperationType,
StorageType,
} from "../LocalStorageMigrator"

import type {
Expand All @@ -21,7 +22,8 @@ const TEST_MIGRATIONS: LocalStorageMigration[] = [
{
from: "oldKey",
to: "newKey",
type: LocalStorageMigrationOperationType.RenameKey,
type: OperationType.RenameKey,
storageType: StorageType.LocalStorage,
} as RenameKey,
],
},
Expand All @@ -30,7 +32,8 @@ const TEST_MIGRATIONS: LocalStorageMigration[] = [
operations: [
{
key: "deprecatedKey",
type: LocalStorageMigrationOperationType.DeleteKey,
storageType: StorageType.LocalStorage,
type: OperationType.DeleteKey,
} as DeleteKey,
],
},
Expand All @@ -41,7 +44,8 @@ const TEST_MIGRATIONS: LocalStorageMigration[] = [
key: "jsonKey1",
newJsonKey: "newJsonKey",
oldJsonKey: "oldJsonKey",
type: LocalStorageMigrationOperationType.RenameJsonValuePropertyKey,
storageType: StorageType.LocalStorage,
type: OperationType.RenameJsonValuePropertyKey,
} as RenameJsonValuePropertyKey,
],
},
Expand All @@ -53,7 +57,8 @@ const TEST_MIGRATIONS: LocalStorageMigration[] = [
key: "jsonKey2",
newJsonValue: "NEW_VALUE",
oldJsonValue: "OLD_VALUE",
type: LocalStorageMigrationOperationType.UpdateJsonPropertyValue,
storageType: StorageType.LocalStorage,
type: OperationType.UpdateJsonPropertyValue,
} as UpdateJsonValuePropertyValue,
],
},
Expand Down Expand Up @@ -144,7 +149,6 @@ describe("libs/LocalStorageMigrator", () => {
description: "Invalid operation type",
operations: [
{
key: "invalidKey",
// @ts-expect-error Invalid operation type.
type: "INVALID_TYPE",
},
Expand Down

0 comments on commit c08ee28

Please sign in to comment.