Skip to content

Collection.primaryKeys()

David Fahlander edited this page May 9, 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

Remarks

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