When interacting with Dexie/Indexeddb, I am passing in data as JS classes. Some properties on these classes are handled through getters/setters to allow me the opportunity to manipulate the data prior to storing on the object.
Dexie is saving the internal property name (_when
) to the db "as is", rather than reading the data through it's getter (when
), using the column name defined during database initialisation:
plans: "++id, contactId, when, sooner, later",
Dexie IndexedDB table column names
How can I instruct Dexie to use the class getters, rather than the object properties directly?
You may see the code/problem I am working on in this Stackblitz environment.
IndexedDB will store own enumerable properties and let all prototype based properties be. Class getters and setters are prototype based so IndexedDB will not use them. To workaround this you would need to map the entities through a function when putting them and map them back using another function before returning them.
However, I would argue that the internal properties is normally what you would want to store for the same reason they are stored in memory. In your case, _when contains Date object with zeroed out time. This would easily become another date if time zone changes so it would probably be better to store it as a date string and let that internal format be the format to store in indexedDB as well.