Huong Vu [Zi]: Embracing IndexedDB
Huong Vu [Zi] (@hungryzi) was talking about IndexdDB at Scotland.js, these are my notes from her talk.
For an offline app use JS and in-browser storage with a server side back end. When the clients go off line the changes are stored then merged back to the server and update with new state.
- WebSQL - relational database, widely supported but now depreciated
- IndexDB - NoSQL database, new and evolving
Coming from a relational database background the switch to NoSQL can be hard to adjust to.
Database is named and versioned. Has an object store, key-value pairs - no rigid structure. Fast lookup and filter on arbitrary values. Has very granular locks (object store level). Asynchronous so callback hell.
Missing:
- Joins
AND
/OR
LIKE
or full text search.
There are some libraries to make your life easier. ydn-db is a good one. Neo have released a Backbone.js adapter for IndexdDB
It's still evolving so browser updates can break things - always test in developer preview version. Phantom.js doesn't support it. Hard to debug as it happens in the user's browser - use a logging system. Different browsers have different amounts of storage space available.
Managing versions: need to manage the versions, the server and client may be out of sync.
Minimise number of transactions: beware of transaction lock, share transactions between operations.
Use index as much as possible: fast and different types of index help different types of pages.
Model your data: try and avoid complex or slow queries. Try and use compound indexes if possible. Denormalise to save doing joints.
Denormalisation has tradeoffs: overhead in insert/update and indexes take up space.