Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple keyPath index (array index) should be explained, because of it's common usage but is not. #21598

Open
nathnolt opened this issue Oct 17, 2022 · 0 comments
Labels
area: IndexedDB Content:WebAPI Web API docs effort: medium This task is a medium effort. help wanted If you know something about this topic, we would love your help!

Comments

@nathnolt
Copy link

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB

What specific section or headline is this issue about?

In general (the text and examples)

What information was incorrect, unhelpful, or incomplete?

The information about the index is correct, but in my opinion incomplete.

with indexedDB, you can create an index, which adds a constraint on 2 properties at the same time, and this also allows you to find something using both of these 2 properties together in a get request on the index. you can do this by passing in an array on the .createIndex and .get requests

Doing this is extremely common for when you want to do slightly more advanced things. but it's not explained.

I personally did not know that you could do this, so I think this would be beneficial.

What did you expect to see?

An explanation with examples, ideally integrated in the other example on the page.

A simple example I came up with. In the example. The idea of the example, is that we have a band store as well. and at some point: you have the id of the band, and the name of the album, but not the id. And you need to update or remove the album.

Creation Example

const store = db.createObjectStore('album', {keyPath: 'id', autoIncrement: true})
const bandId_name = store.createIndex('bandId_name', ['bandId', 'name'], {unique: true})

Getting Example

const bandId = 1
const albumName = 'Thriller'

const store = tx.objectStore('album')
const index = store.index('bandId_name')
const getRequest = index.get([bandId, albumName])
getRequest.onsuccess = function(event) {
    console.log(getRequest.result) // {id: 42, bandId: 1, name: 'Thriller'}
})

In my example I use unique: true, but not having the unique flag also has a lot of use cases.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

No response

MDN metadata

Page report details
@github-actions github-actions bot added Content:WebAPI Web API docs needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Oct 17, 2022
@sideshowbarker sideshowbarker added help wanted If you know something about this topic, we would love your help! effort: medium This task is a medium effort. and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Oct 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: IndexedDB Content:WebAPI Web API docs effort: medium This task is a medium effort. help wanted If you know something about this topic, we would love your help!
Projects
None yet
Development

No branches or pull requests

3 participants