-
-
Notifications
You must be signed in to change notification settings - Fork 641
Compound Index
A compound (or composite) index is an index based on several keypaths. It can be used to efficiently index multiple properties in one index to easily find the existance of the combination of two keys and their values.
In Dexie, a compound index must be specified when defining the schema:
var db = new Dexie('dbname');
db.version(1).stores({
people: 'id, [firstName+lastName]'
});
In the above sample, records containing valid keys in the firstName and lastName properties will be indexed. If having stored an object with properties {firstName: 'foo', lastName: 'bar'}, it can be efficiently looked up using db.people.where('[firstName+lastName]').equals(['foo', 'bar'])`
db.put({
id: 1,
firstName: 'foo',
lastName: 'bar'
}).then(() => {
return db.people.where('[firstName+lastName]').equals(['foo', 'bar']).first();
}).then(fooBar) {
console.log ("Foobar: ", fooBar.id, fooBar.firstName, fooBar.lastName);
});
A primary key can also be compound in the same manner as indexes. However, a compound primary key cannot be auto-incremented of natural reasons.
var db = new Dexie('dbname');
db.version(1).stores({
people: '[date+firstName+lastName], ...'
});
The above sample uses a compound primary key containing four properties: date, firstName and lastName.
IE 8, IE 9, Edge <= 38 and Safari <= 9 does not support compound indexes or primary keys.
If an index is compound using "[firstName+lastName]", Table.orderBy() will sort the contents based on firstName first and lastname secondary. Reverse the order of the compound entries to sort by lastName first. The sorting does not only apply when using "orderBy()" but also when using a WhereClause targeting the compound index, such as:
db.people.where('[lastName+firstName']).between(['A', ''], ['B','').toArray();
The above query will list all people who has a lastName starting with letter 'A'. Result will be sorted by lastName first and firstName secondly when people have the same lastName but different firstName. Notice that you cannot use that compound key to sort by firstName first. Must add another index with a reversed order of the properties to accomplish that. Notice also that if any person lacks either of the properties, it will not be contained in the result. Compound indexes will only index objects that has valid keys for all contained keypaths.
Dexie.js - minimalistic and bullet proof indexedDB library