getDb.ts 1002 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { openDB, DBSchema, StoreNames } from "idb";
  2. export interface NovelDb extends DBSchema {
  3. history: {
  4. value: HistoryItem;
  5. key: string;
  6. indexes: {
  7. uri: string;
  8. historyUpdateTime: Date;
  9. historyIsReading: number;
  10. historyIsFavorite: number;
  11. };
  12. };
  13. }
  14. export default function getDb() {
  15. return openDB<NovelDb>("NovelDit", 3, {
  16. upgrade(db, oldVersion, newVersion) {
  17. console.log(db, oldVersion, newVersion);
  18. if (oldVersion && newVersion && newVersion > oldVersion) {
  19. db.deleteObjectStore("history");
  20. }
  21. const historyStore = db.createObjectStore("history", {
  22. keyPath: "uri",
  23. });
  24. historyStore.createIndex("historyUpdateTime", "readTime", {
  25. unique: false,
  26. });
  27. historyStore.createIndex("historyIsReading", ["isReading", "readTime"], {
  28. unique: false,
  29. });
  30. historyStore.createIndex("historyIsFavorite", "isFavorite", {
  31. unique: false,
  32. });
  33. },
  34. });
  35. }