Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added fallback if onupgradeneeded is not available (UNTESTED) #1

Closed
wants to merge 5 commits into from

2 participants

@juzerali

I have put comments wherever I have made changes but didn't get a chance to test. Please check and confirm.

@juzerali juzerali closed this
@axemclion axemclion reopened this
@axemclion
Owner

I was working on something similar, but wanted to put all the set version code in one place
Reopening the pull request as it is still not merged. Thnx for the request, will make sure that the tests run with this and pull it into the repo over the weekend.

@juzerali
@juzerali

Hey what's the update on this? Did you test it?

@axemclion
Owner

Tried testing it, but for some reason, the database remains open, causing other transactiosn to fail. Looks like this solution does not seem to gel with a page open on multiple tabs. Still working on a way to get around that.

@juzerali

I had made an erronous variable reference, corrected it. Still not sure if it is working.

@axemclion
Owner

Fixed it in SHA:99ee4ff8a210f57085b3fc3dfa0b63c81e05bdd0

@axemclion axemclion closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 43 additions and 34 deletions.
  1. +43 −34 jquery.indexeddb.new.js
View
77 jquery.indexeddb.new.js
@@ -9,13 +9,13 @@
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
-
+
if (config) {
// Parse the config argument
if (typeof config === "number") config = {
"version": config
};
-
+
var version = config.version;
if (config.schema && !version) {
var max = -1;
@@ -25,7 +25,18 @@
version = config.version || max;
}
}
-
+ var doUpgrade = function(oldV, newV){ //This will be called from either onupgradeneeded or onsuccess, whichever is available first
+ if (config && config.schema) {
+ // Assuming that version is always an integer
+ //console.log("Upgrading DB to ", db.version);
+ for (var i = oldV; i <= newV; i++) { //I think the problem was here. Earlier it was e.oldVersion and e.newVersion.
+ typeof config.schema[i] === "function" && config.schema[i].call(this, wrap.transaction(this.transaction));
+ }
+ }
+ if (config && typeof config.upgrade === "function") {
+ config.upgrade.call(this, wrap.transaction(this.transaction));
+ }
+ }
var wrap = {
"request": function(req, args){
return $.Deferred(function(dfd){
@@ -89,7 +100,7 @@
},
"objectStore": function(idbObjectStore){
var result = {};
-
+
// Define CRUD operations
var crudOps = ["add", "put", "get", "delete", "clear", "count"];
for (var i = 0; i < crudOps.length; i++) {
@@ -101,19 +112,19 @@
}
})(crudOps[i]);
}
-
+
result.each = function(callback, range, direction){
return wrap.cursor(function(){
return idbObjectStore.openCursor(wrap.range(range), direction);
}, callback);
};
-
+
result.index = function(name){
return wrap.index(function(){
return idbObjectStore.index(name);
});
};
-
+
result.createIndex = function(prop, options, indexName){
if (arguments.length === 2 && typeof options === "string") {
indexName = arguments[1]
@@ -126,14 +137,14 @@
return idbObjectStore.createIndex(indexName, prop, options);
});
};
-
+
result.deleteIndex = function(indexName){
return idbObjectStore.deleteIndex(indexName);
}
-
+
return result;
},
-
+
"range": function(r){
if ($.isArray(r)) {
if (r.length === 1) {
@@ -145,7 +156,7 @@
return r;
}
},
-
+
"cursor": function(idbCursor, callback){
return $.Deferred(function(dfd){
try {
@@ -196,7 +207,7 @@
}
});
},
-
+
"index": function(index){
try {
var idbIndex = (typeof index === "function" ? index() : index);
@@ -218,13 +229,20 @@
};
}
}
-
+
// Start with opening the database
var dbPromise = wrap.request(function(){
//console.log("Trying to open DB with", version);
return version ? indexedDB.open(dbName, version) : indexedDB.open(dbName);
});
- dbPromise.then(function(db, e){
+ dbPromise.then(function(db, e){
+ var oldVersion = Number(db.version); // Checking if the onupgradeneeded has handled the version change
+ if(oldVersion !== version){ // If not
+ if(db.setVerion) { // If setVersion also not available then throw error
+ var setV = db.setVersion(version); //Set the version, I would have wrapped this call under 'wrap' but then there is no way of passing the oldversion in argument
+ setV.onsuccess(doUpgrade(oldVersion, version)); // Handle version change manually
+ }
+ }
//console.log("DB opened at", db.version);
db.onversionchange = function(){
// Try to automatically close the database if there is a version change request
@@ -236,19 +254,10 @@
// Nothing much to do if an error occurs
}, function(db, e){
if (e && e.type === "upgradeneeded") {
- if (config && config.schema) {
- // Assuming that version is always an integer
- //console.log("Upgrading DB to ", db.version);
- for (var i = e.oldVersion; i <= e.newVersion; i++) {
- typeof config.schema[i] === "function" && config.schema[i].call(this, wrap.transaction(this.transaction));
- }
- }
- if (config && typeof config.upgrade === "function") {
- config.upgrade.call(this, wrap.transaction(this.transaction));
- }
+ doUpgrade(e.oldVersion,e.newVersion);
}
});
-
+
return $.extend(dbPromise, {
"cmp": function(key1, key2){
return indexedDB.cmp(key1, key2);
@@ -301,12 +310,12 @@
}, function(err, e){
dfd.rejectWith(this, [e, err]);
});
-
+
});
},
"objectStore": function(storeName, mode){
var me = this, result = {};
-
+
function op(callback){
return $.Deferred(function(dfd){
function onTransactionProgress(trans, callback){
@@ -321,7 +330,7 @@
dfd.rejectWith(trans, [e, e]);
}
}
-
+
me.transaction(storeName, typeof mode === "number" ? mode : 1).then(function(){
// Nothing to do when transaction is complete
}, function(err, e){
@@ -361,20 +370,20 @@
});
});
};
-
+
function crudOp(opName, args){
return op(function(wrappedObjectStore){
return wrappedObjectStore[opName].apply(wrappedObjectStore, args);
});
}
-
+
function indexOp(opName, indexName, args){
return op(function(wrappedObjectStore){
var index = wrappedObjectStore.index(indexName);
return index[opName].apply(index[opName], args);
});
}
-
+
var crud = ["add", "delete", "get", "put", "clear", "count", "each"];
for (var i = 0; i < crud.length; i++) {
result[crud[i]] = (function(op){
@@ -383,7 +392,7 @@
}
})(crud[i]);
}
-
+
result.index = function(indexName){
return {
"each": function(callback, range){
@@ -394,10 +403,10 @@
}
};
}
-
+
return result;
}
});
}
});
-})(jQuery);
+})(jQuery);
Something went wrong with that request. Please try again.