Skip to content

Instantly share code, notes, and snippets.

@rmehner
Last active March 3, 2020 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmehner/ebfa37db2b27e3f0431c3dc97d385f3b to your computer and use it in GitHub Desktop.
Save rmehner/ebfa37db2b27e3f0431c3dc97d385f3b to your computer and use it in GitHub Desktop.
Using unique compound indizes with Dexie.js
const db = new Dexie('friends')
db.version(1).stores({
friends: '++id,&[name+age]',
})
async function main() {
await db.friends.add({ name: 'Robin', age: 1337 })
// this will fail with:
// ConstraintError: A mutation operation in the transaction failed because a constraint was not satisfied.
await db.friends.add({ name: 'Robin', age: 1337 })
// query like this:
const allMyFriendsAreDead = await db.friends.where({
name: 'Robin',
age: 1337,
})
console.log(allMyFriendsAreDead)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment