Search code examples
typescriptindexeddbdexie

Is it possible to ask Dexie to exclude one or more table properties to be saved in IndexedDB?


Is it possible to exclude one or more properties of a table when created in the store or an entry is saved in the indexeddb?

E.g. the following table description (Typescript) has four properties where only the first three shall be used for creation and adding new entries:

interface IContact {
  id?: number,
  first: string,
  last: string,
  notInDb: string
}

The property notInDb shall neither be considered when the table is created nor when an instance is added. notInDb is only used inside the actual class implementing the interface.


Solution

  • You cannot select only certain properties from an object to be stored. I am guessing the reason you want to exclude a property is because it is too large or is not structured-cloneable.

    indexedDB uses a structured-cloning algorithm that only accepts certain objects. This is basically serialization, but not to a string. So, you need to design your own serialization methods. Copy the properties you want into a basic object. This sounds like overhead, but it really is not that bad. Instead of trying to store the class itself, create a serialize method that creates a basic object and store that instead. Create a corresponding deserialize method that accepts a basic object and from it creates an instance of the class.

    I suggest trying this approach first. If you later find that performance is an issue, and want to continue using indexedDB, then you probably will need to rethink your design, and design around indexedDB's requirements.