idbindex_getKey2.htm
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
<title> IDBIndex.getKey() - returns the record's primary key where the index contains duplicate values </title>
<script type="text/javascript" src="support.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script type="text/javascript">
var objectStoreName = "objectstore";
var indexName = "index";
var records = [{key:1, indexedProperty:"data"},
{key:2, indexedProperty:"data"},
{key:3, indexedProperty:"data"}];
var db = null;
var t = async_test();
function RunTest() {
var rqOpen = window.indexedDB.open(databaseName, databaseVersion);
rqOpen.onupgradeneeded = t.step_func(function(event) {
db = event.target.result;
db.onerror = t.step_func(assert_database_error);
var objStore = db.createObjectStore(objectStoreName, {keyPath:"key"});
var index = objStore.createIndex(indexName, "indexedProperty");
for (var i = 0; i < records.length; i++) {
objStore.add(records[i]);
}
});
rqOpen.onerror = t.step_func(assert_open_request_error);
rqOpen.onsuccess = t.step_func(GetRecordFromIndex);
}
function GetRecordFromIndex() {
var txn = db.transaction(objectStoreName, "readonly");
var objStore = txn.objectStore(objectStoreName);
var index = objStore.index(indexName);
var rqGet = index.getKey(records[0].indexedProperty);
rqGet.onsuccess = t.step_func(function(event) {
assert_equals(event.target.result, records[0].key, 'event.target.result');
t.done();
});
}
add_completion_callback(function() { if(db) db.close(); });
setup(function() {
var rqDelete = window.indexedDB.deleteDatabase(databaseName);
rqDelete.onsuccess = t.step_func( RunTest );
});
</script>
</head>
<body>
<div id="log"></div>
</body>
</html>