IndexedDB: Unique keys

createObjectStore() as you can see in case 1 accepts a second parameter that indicates the index key of the database. This is very useful when you store objects: put() calls don’t need a second parameter, but can just take the value (an object) and the key will be mapped to the object property that has that name.

The index gives you a way to retrieve a value later by that specific key, and it must be unique (every item must have a different key)

A key can be set to auto increment, so you don’t need to keep track of it on the client code:

db.createObjectStore('notes', { autoIncrement: true })

Use auto increment if your values do not contain a unique key already (for example, if you collect email addresses without an associated name).

Lessons in this unit:

0: Introduction
1: Loading the idb library
2: Creating a database and a store
3: Adding data into a store
4: Retriving data from a store
5: Deleting data
6: Migrations
7: ▶︎ Unique keys