Map before write and after read? #1456
-
Hi, is there a possibility to automatically map objects? I'm looking for something you can register once and then you can just use the default methods just as usual but internally the data will be mapped before every read/write. This would be pretty useful, since the data stored doesn't always match the object you want to consume later on. Currently I'm calling a helper function every time before storing and after reading data. It would be super elegant to just let dexie handle the map automatically without having to worry about mapping manually. I suspect it should be possible with hooks/ Dexie.use() but I couldn't get it to work. Could someone maybe provide a quick breakdown of what is required to achieve this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's possible both via hooks and middleware. Read mapping is easiest implemented via hook('reading'). Writes via a middleware that overrides mutate. It's a bit verbose to implement but you only need to copy the example from https://dexie.org/docs/Dexie/Dexie.use(), check if req.type === 'put' || req.type === 'add' and if the table is the target one, then replace myRequest.values = req.values.map(yourMapper) |
Beta Was this translation helpful? Give feedback.
It's possible both via hooks and middleware.
Read mapping is easiest implemented via hook('reading'). Writes via a middleware that overrides mutate. It's a bit verbose to implement but you only need to copy the example from https://dexie.org/docs/Dexie/Dexie.use(), check if req.type === 'put' || req.type === 'add' and if the table is the target one, then replace myRequest.values = req.values.map(yourMapper)