Yes, key
is like a primary key in SQL. But others seem to be missing an example explaining the main part of your question, and that is the distinction between indexName
and keyPath
. Per the Mozilla page on creating an index,
indexName
The name of the index to create. Note that it is possible to create an index with an empty name.
keyPath
The key path for the index to use. Note that it is possible to create an index with an empty keyPath, and also to pass in a sequence (array) as a keyPath.
The indexName
is what you use to access that index. Indexes are used to search that "column" in the database.
The keyPath
is the actual name of the "column." See other questions and answers for what forms a keyPath
may take.
Note that "column" is not technically correct, but I'm using it because that's what you used.
For example, suppose your data has the column hours
and you want to be able to search your database on that column. When creating your database you would create an index for that column:
objectStore.createIndex(indexName, keyPath, { unique: false });
where indexName
can be anything you want, let's say hoursColumn, and keyPath
would be hours.
objectStore.createIndex("hoursColumn", "hours", { unique: false });
unique: false
just means that other rows of data may have the same value for hours.
I can write data to the objectStore
as follows:
db.transaction(storeName, "readwrite").objectStore(storeName).add({hours: 20, minutes: 30});
So to search your data on the column hours, you could write:
var data = db.transaction(storeName).objectStore(storeName).index("hoursColumn").get(20)
and the result would be the first row of data where hours = 20, e.g. {hours: 20, minutes: 30}
So to summarize, indexName is just what you call the index you created that you want to search, and keyPath is the actual name of the stored data on which you want to search.