The continuePrimaryKey()
method of the IDBCursor
interface advances the cursor to the to the item whose key matches the key parameter as well as whose primary key matches the primary key parameter.
A typical use case, is to resume the iteration where a previous cursor has been closed, without having to compare the keys one by one.
Calling this method more than once before new cursor data has been loaded - for example, calling continuePrimaryKey()
twice from the same onsuccess handler - results in an InvalidStateError
being thrown on the second call because the cursor’s got value flag has been unset.
This method is only valid for cursors coming from an index. Using it for cursors coming from an object store will throw an error.
Note: This feature is available in Web Workers.
Syntax
cursor.continuePrimaryKey(key, primaryKey);
Parameters
key
- The key to position the cursor at.
primaryKey
- The primary key to position the cursor at.
Exceptions
This method may raise a DOMException
of one of the following types:
Exception | Description |
---|---|
TransactionInactiveError
|
This IDBCursor's transaction is inactive. |
DataError
|
The key parameter may have any of the following conditions:
|
InvalidStateError
|
The cursor is currently being iterated or has iterated past its end. |
InvalidAccessError
|
The cursor's direction is not prev or next .
|
Example
here’s how you can resume an iteration of all articles tagged with "javascript"
since your last visit:
let request = articleStore.index("tag").openCursor();
let count = 0;
let unreadList = [];
request.onsuccess = (event) => {
let cursor = event.target.result;
if (!cursor) { return; }
let lastPrimaryKey = getLastIteratedArticleId();
if (lastPrimaryKey > cursor.primaryKey) {
cursor.continuePrimaryKey("javascript", lastPrimaryKey);
return;
}
// update lastIteratedArticleId
setLastIteratedArticleId(cursor.primaryKey);
// preload 5 articles into the unread list;
unreadList.push(cursor.value);
if (++count < 5) {
cursor.continue();
}
};
Specifications
Specification | Status | Comment |
Indexed Database API DraftThe definition of 'continuePrimaryKey()' in that specification. | Recommendation |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
continuePrimaryKey
|
Chrome
Full support 58 |
Edge
Full support 79 |
Firefox Full support 51 Full support 51 No support 10 — 51 Prefixed' Implemented with the vendor prefix: moz |
IE
No support No |
Opera
Full support 45 |
Safari
Full support 10.1 |
WebView Android
Full support 58 |
Chrome Android
Full support 58 |
Firefox Android
Full support 51 |
Opera Android
Full support 43 |
Safari iOS
Full support 10.3 |
Samsung Internet Android
Full support 7.0 |
Legend
- Full support
- Full support
- No support
- No support
- Requires a vendor prefix or different name for use.'
- Requires a vendor prefix or different name for use.
See also
- Using IndexedDB
- Starting transactions:
IDBDatabase
- Using transactions:
IDBTransaction
- Setting a range of keys:
IDBKeyRange
- Retrieving and making changes to your data:
IDBObjectStore
- Using cursors:
IDBCursor
- Reference example: To-do Notifications ([[../../../../../../../mdn.github.io/to-do-notifications/index|view example live]].)
IDBCursor.continuePrimaryKey() by Mozilla Contributors is licensed under CC-BY-SA 2.5.