Leo il y a 3 ans
Parent
commit
e0c0d4e187
2 fichiers modifiés avec 35 ajouts et 18 suppressions
  1. 1 1
      components/library/LibrayList/index.tsx
  2. 34 17
      libs/db/getDb.ts

+ 1 - 1
components/library/LibrayList/index.tsx

@@ -21,7 +21,7 @@ export default function LibrayList({ type }: LibrayListProps) {
           [1, 0],
           [1, new Date()],
         ]
-      : [[1], [1]]
+      : [1, 1]
   );
 
   const handlePageChange = (

+ 34 - 17
libs/db/getDb.ts

@@ -14,23 +14,40 @@ export interface NovelDb extends DBSchema {
 }
 
 export default function getDb() {
-  return openDB<NovelDb>("NovelDit", 1, {
-    upgrade(db) {
-      const historyStore = db.createObjectStore("history", {
-        keyPath: "uri",
-      });
-
-      historyStore.createIndex("historyUpdateTime", "readTime", {
-        unique: false,
-      });
-
-      historyStore.createIndex("historyIsReading", ["isReading", "readTime"], {
-        unique: false,
-      });
-
-      historyStore.createIndex("historyIsFavorite", ["isFavorite"], {
-        unique: false,
-      });
+  return openDB<NovelDb>("NovelDit", 2, {
+    upgrade(db, oldVersion, newVersion) {
+      console.log(oldVersion, newVersion);
+      if (oldVersion === 0) {
+        const historyStore = db.createObjectStore("history", {
+          keyPath: "uri",
+        });
+
+        historyStore.createIndex("historyUpdateTime", "readTime", {
+          unique: false,
+        });
+
+        historyStore.createIndex(
+          "historyIsReading",
+          ["isReading", "readTime"],
+          {
+            unique: false,
+          }
+        );
+
+        historyStore.createIndex("historyIsFavorite", "isFavorite", {
+          unique: false,
+        });
+      } else if (oldVersion === 1) {
+        const historyStore = db.transaction("history", "versionchange");
+
+        const store = historyStore.store;
+
+        store.deleteIndex("historyIsFavorite");
+
+        store.createIndex("historyIsFavorite", "isFavorite", {
+          unique: false,
+        });
+      }
     },
   });
 }