Skip to content

Collection.primaryKeys()

David Fahlander edited this page May 10, 2016 · 9 revisions

Since 1.4.0

Syntax

collection.primaryKeys(callback)

Parameters

callback: Function function (keysArray) { } optional

Callback Parameters

keysArray: Array Array of keys

Return Value

Promise

Performance Notice

Similar to Collection.keys(), this operation is faster than Collection.toArray() of several reasons. First because entire objects does not need to be instanciated (less data processsing). Secondly because the underlying database engine need not to do follow the primaryKey reference for each found item and load it (less disk IO).

This method will use IDBObjectStore.getAllKeys() / IDBIndex.getAllKeys() when available in the IndexedDB API to read all the primary keys. However, this optimization will only be used when collection is a NOT reversed, has no offset and is only filtered using a vanilla key range (below(), above(), between(), equals() or Table.orderBy), optionally combined with a limit.

Remarks

Selects primary keys of all items in the collection.

Given callback / returned Promise, will recieve an array containing all primary keys of the index being indexed in the collection.

If callback is omitted and operation succeeds, returned Promise will resolve with the result of the operation, calling any Promise.then() callback.

If callback is specified and operation succeeds, given callback will be called and the returned Promise will resolve with the return value of given callback.

If operation fails, returned promise will reject, calling any Promise.catch() callback.

Sample

db.transaction('r', db.friends, function*(){
    //
    // A simple logical AND operation based on multiple indices. Note: This sample is just
    // for showing an example using primaryKeys() method. In real life, it would be better
    // to use a compound index of [firstName+lastName] to resolve this kind of query!
    //

    // Search for friends where firstName = "hillary" and lastName = "clinton"
    var keys = yield Dexie.Promise.all([
        db.friends.where('firstName').equalsIgnoreCase('hillary').primaryKeys(),
        db.friends.where('lastName').equalsIgnoreCase('clinton').primaryKeys()]);

    // Find all common primary keys
    var intersection = keys[0].filter(key => keys[1].indexOf(key) !== -1);

    // At last look up the actual objects from these primary keys:
    return yield db.friends.where(':id').anyOf(intersection).toArray();
});
Clone this wiki locally