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;
}
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;
},
},

View File

@ -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,
},

1
source/types.d.ts vendored
View File

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

View File

@ -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<string, any> = {

View File

@ -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