Skip to content

Instantly share code, notes, and snippets.

@axemclion
Created September 17, 2012 04:08
Show Gist options
  • Save axemclion/3735483 to your computer and use it in GitHub Desktop.
Save axemclion/3735483 to your computer and use it in GitHub Desktop.
IndexedDB Put - Test cases to check how put function works
<html>
<head>
<title>Sample title</title>
</head>
<body>
<h3><a href = "javascript:schema()">Create Schema - Run this first</a></h3>
<h3><a href = "javascript:list()">List data in DB</a></h3>
<h1>Test cases</h1>
<ul>
<li>
<a href = "javascript:withKey()">With key</a>
</li>
<li>
<a href = "javascript:withoutKey()">Without Key</a>
</li>
<li>
<a href = "javascript:noKeyInObj()">No Key on Object</a>
</li>
<li>
<a href = "javascript:noKeyInObjButInArg()">No Key on Object, but was specified in put call</a>
</li>
</ul>
<script type = "text/javascript" src = "http://nparashuram.com/IndexedDBShim/dist/IndexedDBShim.min.js">
</script>
<script type = "text/javascript">
if (window.shimIndexedDB) {
console.log("IndexedDB Shim started");
window.shimIndexedDB.__useShim();
}
</script>
<script type = "text/javascript" src = "http://nparashuram.com/jquery-indexeddb/lib/jquery.min.js">
</script>
<script type = "text/javascript" src = "http://nparashuram.com/jquery-indexeddb/jquery.indexeddb.js">
</script>
<script type = "text/javascript">
function schema(){
$.indexedDB('helloworld', {
version: 1,
upgrade: function(transaction){
var items = transaction.createObjectStore('items', {
autoIncrement: false,
keypath: 'id',
keyPath: 'id'
});
}
});
}
function withKey(){
// For firefox, have to explicitly provide the key for callback to be called
$.indexedDB('helloworld').objectStore('items').put({
id: 'theKey',
from: 'firefox'
}, 'theKey').then(function(){
console.log('NOT RIGHT: This should not have happened - we did specify a key though there was a key path. This is incorrect');
}, function(){
console.log("EXPECTED: An error ocured when key is specified");
});
}
function withoutKey(){
$.indexedDB('helloworld').objectStore('items').put({
id: 'theKey',
from: 'safari'
}).then(function(){
console.log('EXPECTED: Key was not explicit and hence it was taken from the object itself');
}, function(){
console.log("NOT RIGHT: This should not have happened, no key was specified and ");
});
}
function noKeyInObj(){
$.indexedDB('helloworld').objectStore('items').put({
from: 'safari'
}).then(function(){
console.log('NOT RIGHT : No key in the object');
}, function(){
console.log("EXPECTED: Key was not in the object and not specified, so error");
});
}
function noKeyInObjButInArg(){
$.indexedDB('helloworld').objectStore('items').put({
from: 'safari'
}, 'theKey').then(function(){
console.log('NOT RIGHT : No key in the object');
}, function(){
console.log("EXPECTED: Key was not in the object and not specified, so error");
});
}
function list(){
$.indexedDB('helloworld').objectStore('items').each(function(){
console.log(arguments);
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment