Check if IndexedDB objectStore already contains key
google-chrome, indexeddb
Solution
So far none of the browsers have the sync API implemented so you're going to have to do it async. An `objectStore` exposes a `get` method which you provide it with a key and it'll return you the object (or null) that matches the key.
There's a really good tutorial on MDN that covers using IDB, and getting a record is covered too, but the inlined code is:
db.transaction("customers").objectStore("customers").get("444-44-4444").onsuccess = function(event) {
alert("Name for SSN 444-44-4444 is " + event.target.result.name);
};
If you don't want retrieve the record then you can always used the `count` method on an index as explained here in the spec. Based on the result of that you can either use `add` or `put` to modify the record, it'll save extracting the record if you don't need to.
Problem
Adding an object to an IndexedDB objectStore will fail if the key already exists. How can I check for the existence of an object with a given key – preferably synchronously (no reason for another layer of callbacks) and without pulling the object. I know how to do get requests asynchronously via transactions, but it seems a bit of an ordeal to go through every time I want to add an object. note Solution only has to work in Chrome (if that helps)