diff --git a/source/settings/migrations.ts b/source/settings/migrations.ts index df63d96..4cea947 100644 --- a/source/settings/migrations.ts +++ b/source/settings/migrations.ts @@ -14,6 +14,23 @@ export const dataMigrations: Array> = [ migrated[key] = item; } + return migrated; + }, + }, + { + version: '0.3.0', + async migrate(data: Record) { + const migrated: Record = { + version: '0.3.0', + }; + + for (const [key, value] of Object.entries(data)) { + if (key.startsWith('qi')) { + migrated[key] = value; + migrated[key].sortIndex = value.id; + } + } + return migrated; }, }, diff --git a/source/settings/settings.ts b/source/settings/settings.ts index 027cb07..39fa720 100644 --- a/source/settings/settings.ts +++ b/source/settings/settings.ts @@ -42,6 +42,7 @@ export class Settings { const item: Queue.Item = { added: new Date(), id, + sortIndex: id, text, url, }; @@ -51,6 +52,7 @@ export class Settings { [`qi${id}`]: { added: item.added.toISOString(), id, + sortIndex: id, text, url, }, diff --git a/source/types.d.ts b/source/types.d.ts index 989fb56..bbd8aa6 100644 --- a/source/types.d.ts +++ b/source/types.d.ts @@ -22,6 +22,7 @@ declare global { type Item = { added: Date; id: number; + sortIndex: number; text: string; url: string; }; diff --git a/tests/migrations.test.ts b/tests/migrations.test.ts index 41bec03..f768eb7 100644 --- a/tests/migrations.test.ts +++ b/tests/migrations.test.ts @@ -13,7 +13,7 @@ const queueItemSample: Queue.Item = { id: 1, text: 'Sample', url: 'https://example.org', -}; +} as unknown as Queue.Item; test('dataMigrations happy path', async (t) => { let data: Record = { diff --git a/tests/snapshots/tests/migrations.test.ts.md b/tests/snapshots/tests/migrations.test.ts.md index 69c9ee7..3cb7b41 100644 --- a/tests/snapshots/tests/migrations.test.ts.md +++ b/tests/snapshots/tests/migrations.test.ts.md @@ -18,6 +18,19 @@ Generated by [AVA](https://avajs.dev). version: '0.1.7', } +> Migration 0.3.0 + + { + qi1: { + added: Date 2022-03-02 16:00:00 UTC {}, + id: 1, + sortIndex: 1, + text: 'Sample', + url: 'https://example.org', + }, + version: '0.3.0', + } + ## dataMigrations unhappy path > Migration 0.1.7 @@ -26,6 +39,12 @@ Generated by [AVA](https://avajs.dev). version: '0.1.7', } +> Migration 0.3.0 + + { + version: '0.3.0', + } + ## Serializing & Deserializing Queue > Serialized diff --git a/tests/snapshots/tests/migrations.test.ts.snap b/tests/snapshots/tests/migrations.test.ts.snap index a673a36..16a19a3 100644 Binary files a/tests/snapshots/tests/migrations.test.ts.snap and b/tests/snapshots/tests/migrations.test.ts.snap differ