From 915987cbfe46f45773779f7e9b1f29fdce817907 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Tue, 15 Oct 2024 16:45:04 +0800 Subject: [PATCH] refac: @/indexed/upgrade --- src/app.tsx | 72 +++------------------------------------- src/indexedDB/upgrade.ts | 24 ++++++++++++++ src/indexedDB/v1.ts | 38 +++++++++++++++++++++ src/indexedDB/v11.ts | 25 ++++++++++++++ 4 files changed, 91 insertions(+), 68 deletions(-) create mode 100644 src/indexedDB/upgrade.ts create mode 100644 src/indexedDB/v1.ts create mode 100644 src/indexedDB/v11.ts diff --git a/src/app.tsx b/src/app.tsx index 1e50c51..d357f70 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -1,4 +1,4 @@ -import { IDBPDatabase, openDB } from "idb"; +import { openDB } from "idb"; import { useEffect, useState } from "preact/hooks"; import "@/global.css"; @@ -9,12 +9,9 @@ import { DefaultModel } from "@/const"; import { Tr, langCodeContext, LANG_OPTIONS } from "@/translate"; import { ChatStore } from "@/types/chatstore"; import { newChatStore } from "@/types/newChatstore"; -import { - STORAGE_NAME, - STORAGE_NAME_INDEXES, - STORAGE_NAME_SELECTED, -} from "@/const"; +import { STORAGE_NAME, STORAGE_NAME_SELECTED } from "@/const"; import { BuildFiledForSearch } from "@/utils/buildForSearch"; +import { upgrade } from "@/indexedDB/upgrade"; export function App() { // init selected index @@ -28,68 +25,7 @@ export function App() { }, [selectedChatIndex]); const db = openDB(STORAGE_NAME, 11, { - async upgrade(db, oldVersion, newVersion, transaction) { - if (oldVersion < 1) { - const store = db.createObjectStore(STORAGE_NAME, { - autoIncrement: true, - }); - - // copy from localStorage to indexedDB - const allChatStoreIndexes: number[] = JSON.parse( - localStorage.getItem(STORAGE_NAME_INDEXES) ?? "[]", - ); - let keyCount = 0; - for (const i of allChatStoreIndexes) { - console.log("importing chatStore from localStorage", i); - const key = `${STORAGE_NAME}-${i}`; - const val = localStorage.getItem(key); - if (val === null) continue; - store.add(JSON.parse(val)); - keyCount += 1; - } - setSelectedChatIndex(keyCount); - if (keyCount > 0) { - alert( - "v2.0.0 Update: Imported chat history from localStorage to indexedDB. 🎉", - ); - } - } - - if (oldVersion < 11) { - if (oldVersion < 11 && oldVersion >= 1) { - alert( - "Start upgrading storage, just a sec... (Click OK to continue)", - ); - } - if ( - transaction - .objectStore(STORAGE_NAME) - .indexNames.contains("contents_for_index") - ) { - transaction - .objectStore(STORAGE_NAME) - .deleteIndex("contents_for_index"); - } - transaction.objectStore(STORAGE_NAME).createIndex( - "contents_for_index", // name - "contents_for_index", // keyPath - { - multiEntry: true, - unique: false, - }, - ); - - // iter through all chatStore and update contents_for_index - const store = transaction.objectStore(STORAGE_NAME); - const allChatStoreIndexes = await store.getAllKeys(); - for (const i of allChatStoreIndexes) { - const chatStore: ChatStore = await store.get(i); - - chatStore.contents_for_index = BuildFiledForSearch(chatStore); - await store.put(chatStore, i); - } - } - }, + upgrade, }); const getChatStoreByIndex = async (index: number): Promise => { diff --git a/src/indexedDB/upgrade.ts b/src/indexedDB/upgrade.ts new file mode 100644 index 0000000..38d2ed4 --- /dev/null +++ b/src/indexedDB/upgrade.ts @@ -0,0 +1,24 @@ +import { STORAGE_NAME } from "@/const"; +import { ChatStore } from "@/types/chatstore"; +import { IDBPDatabase, IDBPTransaction, StoreNames } from "idb"; +import { upgradeV1 } from "@/indexedDB/v1"; +import { upgradeV11 } from "./v11"; + +export async function upgrade( + db: IDBPDatabase, + oldVersion: number, + newVersion: number, + transaction: IDBPTransaction< + ChatStore, + StoreNames[], + "versionchange" + >, +) { + if (oldVersion < 1) { + upgradeV1(db, oldVersion, newVersion, transaction); + } + + if (oldVersion < 11) { + upgradeV11(db, oldVersion, newVersion, transaction); + } +} diff --git a/src/indexedDB/v1.ts b/src/indexedDB/v1.ts new file mode 100644 index 0000000..aa7aa3f --- /dev/null +++ b/src/indexedDB/v1.ts @@ -0,0 +1,38 @@ +import { STORAGE_NAME, STORAGE_NAME_INDEXES } from "@/const"; +import { ChatStore } from "@/types/chatstore"; +import { IDBPDatabase, IDBPTransaction, StoreNames } from "idb"; + +export async function upgradeV1( + db: IDBPDatabase, + oldVersion: number, + newVersion: number, + transaction: IDBPTransaction< + ChatStore, + StoreNames[], + "versionchange" + >, +) { + const store = db.createObjectStore(STORAGE_NAME, { + autoIncrement: true, + }); + + // copy from localStorage to indexedDB + const allChatStoreIndexes: number[] = JSON.parse( + localStorage.getItem(STORAGE_NAME_INDEXES) ?? "[]", + ); + let keyCount = 0; + for (const i of allChatStoreIndexes) { + console.log("importing chatStore from localStorage", i); + const key = `${STORAGE_NAME}-${i}`; + const val = localStorage.getItem(key); + if (val === null) continue; + store.add(JSON.parse(val)); + keyCount += 1; + } + // setSelectedChatIndex(keyCount); + if (keyCount > 0) { + alert( + "v2.0.0 Update: Imported chat history from localStorage to indexedDB. 🎉", + ); + } +} diff --git a/src/indexedDB/v11.ts b/src/indexedDB/v11.ts new file mode 100644 index 0000000..e6ed946 --- /dev/null +++ b/src/indexedDB/v11.ts @@ -0,0 +1,25 @@ +import { ChatStore } from "@/types/chatstore"; +import { STORAGE_NAME } from "@/const"; +import { IDBPDatabase, IDBPTransaction, StoreNames } from "idb"; + +export async function upgradeV11( + db: IDBPDatabase, + oldVersion: number, + newVersion: number, + transaction: IDBPTransaction< + ChatStore, + StoreNames[], + "versionchange" + >, +) { + if (oldVersion < 11 && oldVersion >= 1) { + alert("Start upgrading storage, just a sec... (Click OK to continue)"); + } + if ( + transaction + .objectStore(STORAGE_NAME) + .indexNames.contains("contents_for_index") + ) { + transaction.objectStore(STORAGE_NAME).deleteIndex("contents_for_index"); + } +}