Add a sortIndex to items.

This commit is contained in:
Bauke 2022-10-25 12:04:11 +02:00
parent ab6ef97d91
commit c72959841f
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
6 changed files with 40 additions and 1 deletions

View File

@ -14,6 +14,23 @@ export const dataMigrations: Array<Migration<string>> = [
migrated[key] = item; migrated[key] = item;
} }
return migrated;
},
},
{
version: '0.3.0',
async migrate(data: Record<string, any>) {
const migrated: Record<string, any> = {
version: '0.3.0',
};
for (const [key, value] of Object.entries<Queue.Item>(data)) {
if (key.startsWith('qi')) {
migrated[key] = value;
migrated[key].sortIndex = value.id;
}
}
return migrated; return migrated;
}, },
}, },

View File

@ -42,6 +42,7 @@ export class Settings {
const item: Queue.Item = { const item: Queue.Item = {
added: new Date(), added: new Date(),
id, id,
sortIndex: id,
text, text,
url, url,
}; };
@ -51,6 +52,7 @@ export class Settings {
[`qi${id}`]: { [`qi${id}`]: {
added: item.added.toISOString(), added: item.added.toISOString(),
id, id,
sortIndex: id,
text, text,
url, url,
}, },

1
source/types.d.ts vendored
View File

@ -22,6 +22,7 @@ declare global {
type Item = { type Item = {
added: Date; added: Date;
id: number; id: number;
sortIndex: number;
text: string; text: string;
url: string; url: string;
}; };

View File

@ -13,7 +13,7 @@ const queueItemSample: Queue.Item = {
id: 1, id: 1,
text: 'Sample', text: 'Sample',
url: 'https://example.org', url: 'https://example.org',
}; } as unknown as Queue.Item;
test('dataMigrations happy path', async (t) => { test('dataMigrations happy path', async (t) => {
let data: Record<string, any> = { let data: Record<string, any> = {

View File

@ -18,6 +18,19 @@ Generated by [AVA](https://avajs.dev).
version: '0.1.7', 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 ## dataMigrations unhappy path
> Migration 0.1.7 > Migration 0.1.7
@ -26,6 +39,12 @@ Generated by [AVA](https://avajs.dev).
version: '0.1.7', version: '0.1.7',
} }
> Migration 0.3.0
{
version: '0.3.0',
}
## Serializing & Deserializing Queue ## Serializing & Deserializing Queue
> Serialized > Serialized