getDb.ts 818 B

123456789101112131415161718192021222324252627282930313233343536
  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", 1, {
  16. upgrade(db) {
  17. const historyStore = db.createObjectStore("history", {
  18. keyPath: "uri",
  19. });
  20. historyStore.createIndex("historyUpdateTime", "readTime", {
  21. unique: false,
  22. });
  23. historyStore.createIndex("historyIsReading", ["isReading", "readTime"], {
  24. unique: false,
  25. });
  26. historyStore.createIndex("historyIsFavorite", ["isFavorite"], {
  27. unique: false,
  28. });
  29. },
  30. });
  31. }