IndexedDB - What is Key, keyPath and indexName?

旧时模样 提交于 2019-12-04 12:54:23

Indexes are a way to make it possible to query data in the indexeddb database. As you know objects are stored into the objectstores. These objectstore don't have a schema like you have in an normal SQL database.

An index exists out of 3 important properties:

indexName: The indexname is just a name you provide to the index. You will need this name if you want to use the index to query data.

keyPath: This defines which property of the object you want to address in your index. For example: you have an object

{ foo: "bar" } 

and you want to query on the foo property, "foo" will be your keypath. The keypath can even go further. You can access nested properties

{ foo: { bar: "bla" } }

If you want to query the bar property the keypath will be "foo.bar"

key: The keys are the values inside the keypath. As you mentioned this key is unique for the index, but this doens't mean this value must be unique over all your objects in the objectstore.

The indexes in the indexeddb work like this: When you create an index, it creates a new objectstore in which the object will be stored. Instead of storing these object bosed on primary key they are stored based on the values present in the keypath. This means that for a single key in an index you can have multiple objects. So if you start querying an index, it will filter on the keys and return the values that are present in these keys.

Hope this makes indexes clear for you.

For some more info I have written some blogpost about it: info about using keys and Indexeddb basics

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.

To contribute here - there is a very interesting explanation here:

https://golb.hplar.ch/2017/09/A-closer-look-at-IndexedDB.html#primary-keys

Basically it explains that there are 4 types of keys within IndexedDB:

  1. out-of-line : you provide the key yourself on each insert
  2. out-of-line auto generated : primary key (i.e. AutoIncrement) number generated automatically
  3. inline : the contents of field form the index (this is typical to say a MySQL database)
  4. inline auto generated: hidden field (visible in console) added and is accessible through code but not related directly to the object data

To do (3) you still need an unique keypath even if you don't use it - otherwise the db will error when you then add a row of data (The object store uses out-of-line keys and has no key generator and the key parameter was not provided. as it is expecting you to provide a separate/specific key)

Be aware that multiple fields in a keypath are a compound index as opposed to indexes over multiple fields.

On an aside I believe its called a Keypath because you can call field.subproperty which is quite nice. Please also make sure your keys are spelt correctly! Obvious mistake.

Key       = Row Item
indexName = Column name
keyPath   = Column name
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!