From c72959841fb2106acfdae660031e332ac883e07f Mon Sep 17 00:00:00 2001 From: Bauke Date: Tue, 25 Oct 2022 12:04:11 +0200 Subject: [PATCH] Add a sortIndex to items. --- source/settings/migrations.ts | 17 ++++++++++++++++ source/settings/settings.ts | 2 ++ source/types.d.ts | 1 + tests/migrations.test.ts | 2 +- tests/snapshots/tests/migrations.test.ts.md | 19 ++++++++++++++++++ tests/snapshots/tests/migrations.test.ts.snap | Bin 523 -> 578 bytes 6 files changed, 40 insertions(+), 1 deletion(-) 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 a673a361874dd3cdc99f214d0daa702ad73bcebb..16a19a32483de4ad932bce870bef587c05c801ab 100644 GIT binary patch literal 578 zcmV-I0=@k~RzV7j_M2kULN zdG1b)Nt~I;ws>l<{Sz%HhzGBpJqTVsd+<*P-t;Bwv};?{t-&>&Sk2G=OuZMNWg$ir}k&&}}gaeID&b9w{L`P1?gwIyK? z+&>1L0Ata`INdLcUrJ%Cuye*$moygF~=iD(DdmxOC7j;sPSpCyrz#^gpY);gdZoGEPow+*v;1aXZxJx;2G+^ zZuP~;s@EP>?+&e@J+JpbDX~$AH?$g!27`;Gc9#GaZIYMLniF;iZwX&cI*Na?b3BUU QgXYBOH=eemokRx!003$k82|tP literal 523 zcmV+m0`&bsRzV^M=xG&sdJ2e^u1&?P& zdO=!zEgy>r00000000BkQ!!85P!N7+$Dt~sNd!_sEJi||8uMZhR8bcs76yc>Fc7vE zd`%uTv9X^~Qgvy%_D?htEd2wWyLIi%-$3ftUL0PMIxUSlRI24qcW2+z-F@G^4=)>j zaQZ5K*GDClkDV1yC~+X0kx(*d$97AEVb>0YYRR}Q!nhTv_-)@CWYwW*vN<+*-~pH} z*=K|U!Xo5LkUwmk;i&=`xy53U!2$z=!6Sf(5|_YaV3okUkQD|6I%w!mbC(#!)Ps&K z?o^zeK`HBsPJSeOCfpFdXG2gW5W;d$s~iW<^O-5fyXPflS+=^$Sd=b?|8 z6f=o@?Ju^J3gha=26oa^Cy1I19#gtmEWxtNUAN3P%iKMxY*#s_*WsL>q*LVfghDXy z9GU=q(Lg^K$zrEeXvtPHk|phXL_4leXl6KMxBjeGmWy9YbAOj>HH@V2`XWE;eMjvjCj()4-zg(*!?`N7{ z^fY^wN%o$Sb@jZ~ZWM{`fV`nsC=?hxOu3r}c&-mw0ojD`o^Vb0GV4YA#{T1r=8u;X Ny&t{5sPG~M003(B@2mg-