Collection.primaryKeys()

Since 1.4.0

Syntax

collection.primaryKeys(callback)

Parameters

callback: Functionfunction (keysArray) { }optional

Callback Parameters

keysArray: ArrayArray 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 instantiated (less data processing). 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(), startsWith() or Table.orderBy), optionally combined with a limit.

Remarks

Selects primary keys of all items in the collection.

Given callback / returned Promise, will receive 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, async () => {
  //
  // A simple logical AND operation based on multiple indexes.
  // This sample assumes your primary keys (ids) are strings or numbers
  // as Set won't work for arrays or Dates.
  //
  // The last criterion will define the order. To change which criterion will define the order,
  // swap the criteria to have the ordering criteria last.
  //
  // To order by an index that is not part of the criteria, use [Table.orderBy()](https://dexie.org/docs/Table/Table.orderBy())
  // as if it was a criterion (for example `db.friends.orderBy('shoeSize').primaryKeys()`) and put it last.
  //

  // Search for friends where firstName starts with "Ali", lastName
  // starts with "Svens" and age is in the range 18..65.
  const results = await Promise.all([
  
    db.friends
      .where('firstName')
      .startsWith('Ali')
      .primaryKeys(),
      
    db.friends
      .where('lastName')
      .startsWith('Svens')
      .primaryKeys(),
      
    db.friends
      .where('age')
      .between(18, 65)
      .primaryKeys()
      
    //... (more operands to AND with)
  ]);
  
  // Ok, so now we have all primary keys from all queries stored in separate results

  // Find all common primary keys
  const intersection = results.reduce((keysInCommon, keysFromPromise) => {
    const set = new Set(keysInCommon);
    return keysFromPromise.filter(id => set.has(id));
  });

  // At last look up the actual objects from these primary keys:
  return await db.friends.bulkGet(intersection);
});

Table of Contents