localstorage migration

This commit is contained in:
adrifcastr 2023-06-22 10:47:14 +02:00
parent f892a3037f
commit 9fbba7ea55
3 changed files with 101 additions and 0 deletions

View File

@ -2,6 +2,7 @@ import { createVersionedStore } from "@/utils/storage";
import { BookmarkStoreData } from "./types";
import { OldBookmarks, migrateV1Bookmarks } from "../watched/migrations/v2";
import { migrateV2Bookmarks } from "../watched/migrations/v3";
export const BookmarkStore = createVersionedStore<BookmarkStoreData>()
.setKey("mw-bookmarks")
@ -13,6 +14,12 @@ export const BookmarkStore = createVersionedStore<BookmarkStoreData>()
})
.addVersion({
version: 1,
migrate(old: OldBookmarks) {
return migrateV2Bookmarks(old);
},
})
.addVersion({
version: 2,
create() {
return {
bookmarks: [],

View File

@ -0,0 +1,87 @@
import { getLegacyMetaFromId } from "@/backend/metadata/getmeta";
import { getMovieFromExternalId } from "@/backend/metadata/tmdb";
import { MWMediaType } from "@/backend/metadata/types/mw";
import { WatchedStoreData } from "../types";
async function migrateId(
id: number,
type: MWMediaType
): Promise<string | undefined> {
console.log("migrating id", id, type);
const meta = await getLegacyMetaFromId(type, id.toString());
console.log("migrating id", meta);
if (!meta) return undefined;
const { tmdbId, imdbId } = meta;
if (!tmdbId && !imdbId) return undefined;
// movies always have an imdb id on tmdb
if (imdbId && type === MWMediaType.MOVIE) {
const movieId = await getMovieFromExternalId(imdbId);
if (movieId) return movieId;
}
if (tmdbId) {
return tmdbId;
}
}
export async function migrateV2Bookmarks(old: any) {
const oldData = old;
if (!oldData) return;
const updatedBookmarks = oldData.bookmarks.map(
async (item: { id: number; type: MWMediaType }) => ({
...item,
mediaId: await migrateId(item.id, item.type),
})
);
return {
bookmarks: (await Promise.all(updatedBookmarks)).filter(
(item) => item.mediaId
),
};
}
export async function migrateV3Videos(old: any) {
console.log("migrating watched");
const oldData = old;
if (!oldData) return;
console.log(oldData);
const updatedItems = await Promise.all(
oldData.items.map(async (item: any) => {
const migratedId = await migrateId(
item.item.meta.id,
item.item.meta.type
);
const migratedItem = {
...item,
item: {
...item.item,
meta: {
...item.item.meta,
id: migratedId,
},
},
};
return {
...item,
item: migratedId ? migratedItem : item.item,
};
})
);
const newData: WatchedStoreData = {
items: updatedItems.map((item) => item.item), // Extract the "item" object
};
return {
...oldData,
items: newData.items,
};
}

View File

@ -1,6 +1,7 @@
import { createVersionedStore } from "@/utils/storage";
import { OldData, migrateV2Videos } from "./migrations/v2";
import { migrateV3Videos } from "./migrations/v3";
import { WatchedStoreData } from "./types";
export const VideoProgressStore = createVersionedStore<WatchedStoreData>()
@ -21,6 +22,12 @@ export const VideoProgressStore = createVersionedStore<WatchedStoreData>()
})
.addVersion({
version: 2,
migrate(old: OldData) {
return migrateV3Videos(old);
},
})
.addVersion({
version: 3,
create() {
return {
items: [],