scottohara / tvmanager

PWA for tracking recorded, watched & upcoming TV shows
MIT License
3 stars 0 forks source link

IDBPObjectStore type can't be (easily) specified #91

Closed scottohara closed 4 years ago

scottohara commented 4 years ago

Each store contains an upgradeTo array for handling version upgrades.

The first entry in the array performs the initial creation of the IndexedDb store:

// Version 1
(db: IDBPDatabase<TVManagerDB>): void => {
  const store = db.createObjectStore("episodes", { keyPath: "id" });

  /* create indexes etc. */
}

The type of the store constant is implicitly inferred, but can't be explicitly specified, because the signature of the return type of createObjectStore() is:

IDBPObjectStore<DBTypes, StoreNames<DBTypes>[], Name>

...and StoreNames (resolves to an array of all valid store names) is not exported by idb.

One workaround would be to explicitly list each store, e.g.

// Version 1
(db: IDBPDatabase<TVManagerDB>): void => {
  const store: IDBPObjectStore<TVManager, ("programs" | "series" | "episodes" | "settings" | "syncs")[], "episodes"> = db.createObjectStore("episodes", { keyPath: "id" });
  ...
}

...but that would mean adding a new store in future would break all of these references.