1 // META: global=window,worker
2 // META: title=IDBIndex.getKey()
3 // META: script=resources/support.js
4 // @author Microsoft <https://www.microsoft.com>
5 // @author Intel <http://www.intel.com>
11 const record = { key: 1, indexedProperty: "data" };
13 const open_rq = createdb(t);
14 open_rq.onupgradeneeded = function(e) {
16 const objStore = db.createObjectStore("test", { keyPath: "key" });
17 objStore.createIndex("index", "indexedProperty");
22 open_rq.onsuccess = function(e) {
23 let rq = db.transaction("test", "readonly", { durability: 'relaxed' })
26 rq = rq.index("index");
28 rq = rq.getKey("data");
30 rq.onsuccess = t.step_func(function(e) {
31 assert_equals(e.target.result, record.key);
35 }, 'getKey() returns the record\'s primary key');
40 { key: 1, indexedProperty: "data" },
41 { key: 2, indexedProperty: "data" },
42 { key: 3, indexedProperty: "data" }
45 const open_rq = createdb(t);
46 open_rq.onupgradeneeded = function(e) {
48 var objStore = db.createObjectStore("test", { keyPath: "key" });
49 objStore.createIndex("index", "indexedProperty");
51 for (let i = 0; i < records.length; i++)
52 objStore.add(records[i]);
55 open_rq.onsuccess = function(e) {
56 const rq = db.transaction("test", "readonly", { durability: 'relaxed' })
61 rq.onsuccess = t.step_func(function(e) {
62 assert_equals(e.target.result, records[0].key);
66 }, 'getKey() returns the record\'s primary key where the index contains duplicate values');
70 const open_rq = createdb(t);
72 open_rq.onupgradeneeded = function(e) {
74 const rq = db.createObjectStore("test", { keyPath: "key" })
75 .createIndex("index", "indexedProperty")
78 rq.onsuccess = t.step_func(function(e) {
79 assert_equals(e.target.result, undefined);
83 }, 'getKey() attempt to retrieve the primary key of a record that doesn\'t exist');
88 const open_rq = createdb(t);
90 open_rq.onupgradeneeded = function(e) {
92 const store = db.createObjectStore("store", { keyPath: "key" });
93 store.createIndex("index", "indexedProperty");
95 for (let i = 0; i < 10; i++) {
96 store.add({ key: i, indexedProperty: "data" + i });
100 open_rq.onsuccess = function(e) {
101 const rq = db.transaction("store", "readonly", { durability: 'relaxed' })
102 .objectStore("store")
104 .getKey(IDBKeyRange.bound('data4', 'data7'));
106 rq.onsuccess = t.step_func(function(e) {
107 assert_equals(e.target.result, 4);
109 step_timeout(function () { t.done(); }, 4)
112 }, 'getKey() returns the key of the first record within the range');
116 const open_rq = createdb(t);
118 open_rq.onupgradeneeded = function(e) {
119 db = e.target.result;
121 const index = db.createObjectStore("test", { keyPath: "key" })
122 .createIndex("index", "indexedProperty");
124 assert_throws_dom("DataError", function () {
129 }, 'getKey() throws DataError when using invalid key');
133 const open_rq = createdb(t);
135 open_rq.onupgradeneeded = function(e) {
136 db = e.target.result;
137 const store = db.createObjectStore("store", { keyPath: "key" });
138 const index = store.createIndex("index", "indexedProperty");
140 store.add({ key: 1, indexedProperty: "data" });
141 store.deleteIndex("index");
143 assert_throws_dom("InvalidStateError", function () {
144 index.getKey("data");
148 }, 'getKey() throws InvalidStateError when the index is deleted');
153 const open_rq = createdb(t);
154 open_rq.onupgradeneeded = function(e) {
155 db = e.target.result;
156 const store = db.createObjectStore("store", { keyPath: "key" });
157 const index = store.createIndex("index", "indexedProperty");
158 store.add({ key: 1, indexedProperty: "data" });
161 open_rq.onsuccess = function(e) {
162 db = e.target.result;
163 const tx = db.transaction('store', 'readonly', { durability: 'relaxed' });
164 const index = tx.objectStore('store').index('index');
167 assert_throws_dom("TransactionInactiveError", function () {
168 index.getKey("data");
172 }, 'getKey() throws TransactionInactiveError on aborted transaction');
177 const open_rq = createdb(t);
178 open_rq.onupgradeneeded = function(e) {
179 db = e.target.result;
180 const store = db.createObjectStore("store", { keyPath: "key" });
181 const index = store.createIndex("index", "indexedProperty");
182 store.add({ key: 1, indexedProperty: "data" });
184 e.target.transaction.abort();
186 assert_throws_dom("InvalidStateError", function () {
187 index.getKey("data");
191 }, 'getKey() throws InvalidStateError on index deleted by aborted upgrade');