queue/source/settings/migrations.ts

46 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-10-03 16:44:28 +00:00
import type {Migration} from '@holllo/migration-helper';
2022-03-05 13:10:45 +00:00
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;
},
},
];
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();
2022-03-05 13:10:45 +00:00
}
return serialized;
}