queue/source/settings/migrations.ts

63 lines
1.4 KiB
TypeScript

import type {Migration} from '@holllo/migration-helper';
export const dataMigrations: Array<Migration<string>> = [
{
version: '0.1.7',
async migrate(data: Record<string, any>) {
const migrated: Record<string, any> = {
version: '0.1.7',
};
const items = (data.queue as Queue.Item[]) ?? [];
for (const item of items) {
const key = `qi${item.id}`;
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;
},
},
];
export function deserializeQueue(data: Record<string, any>): Queue.Item[] {
const deserialized: Queue.Item[] = [];
for (const [key, item] of Object.entries(data)) {
if (/^qi\d+$/.test(key)) {
item.added = new Date(item.added);
deserialized.push(item);
}
}
return deserialized;
}
export function serializeQueue(queue: Queue.Item[]): Record<string, any> {
const serialized: Record<string, any> = {};
for (const item of queue) {
const key = `qi${item.id}`;
serialized[key] = {...item};
serialized[key].added = item.added.toISOString();
}
return serialized;
}