Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <button>Kill database</button>
</body>
</html>
 
window.indexedDB = window.indexedDB || window.webkitIndexedDB ||
window.mozIndexedDB;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
var req = indexedDB.open('test', 1);
var server;
req.onupgradeneeded = function (e) {
  var db = e.target.result;
  
  console.log('creating schema');
  
  var store = db.createObjectStore('store', { keyPath: 'id', autoIncrement: true });
  
  store.createIndex('a', 'a', { unique: false });
  
  store.add({ a: 'a' });
  store.add({ a: 'b' });
  store.add({ a: 'a' });
};
req.onsuccess = function (e) {
  server = e.target.result;
  
  console.log('success');
  
  var transaction = server.transaction('store');
  var store = transaction.objectStore('store');
  
  var keyRange = IDBKeyRange.only('a');
  
  var index = store.index('a');
  
  index.openCursor(keyRange, 'nextunique').onsuccess = function (e) {
    var cursor = e.target.result;
    
    if(cursor) {
        console.log(cursor.value.id, 'record found from prevunique cursor');
        cursor['continue']();
    }
  };
  
  index.openCursor(keyRange, 'next').onsuccess = function (e) {
    var cursor = e.target.result;
    
    if(cursor) {
        console.log(cursor.value.id, 'record found from prev cursor');
        cursor['continue']();
    }
  };
};
$('button').click(function () {
  console.log('closing database');
  
  server.close();
  
  indexedDB.deleteDatabase('test');
  
  console.log('db should be deleted');
});
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers