I use creation of indexedDB like this
var getIndexedDB = function() {
if ( !indexedDB ) {
indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB || ((window.indexedDB === null && window.shimIndexedDB) ? window.shimIndexedDB : undefined);
if ( !indexedDB ) {
throw 'IndexedDB required';
}
}
return indexedDB;
};
On deployment changing the version of database. But if look into size of files (where saving browser indexedDB) they add a new data and not deleting old. Need by js check if indexedDB has databases with old version and drop (by js!) only old versions (not all database).
I use creation of indexedDB like this
var getIndexedDB = function() {
if ( !indexedDB ) {
indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB || ((window.indexedDB === null && window.shimIndexedDB) ? window.shimIndexedDB : undefined);
if ( !indexedDB ) {
throw 'IndexedDB required';
}
}
return indexedDB;
};
On deployment changing the version of database. But if look into size of files (where saving browser indexedDB) they add a new data and not deleting old. Need by js check if indexedDB has databases with old version and drop (by js!) only old versions (not all database).
Share Improve this question asked Sep 20, 2015 at 8:57 moledetmoledet 93911 silver badges19 bronze badges2 Answers
Reset to default 5Some starting bytes:
Using the getIndexedDB()
you mentioned, you will only get the holder of IDB, it will not open a database for storage. You need to use below code for opening a IDB instance:
var request = window.indexedDB.open(DB_NAME, DB_VERSION, {storage: "temporary"});
Also, personally I would NOT remend using IDB prefixes like webkitIndexedDB
, mozIndexedDB
, until you have some real good reason for using prefixes. Read more about IDB prefix from Mozilla
Beware that implementations that use a prefix may be buggy, or inplete, or following an old version of the specification.
Deleting IDB instance:
Once you have opened a database, you can delete the database using IDBFactory.deleteDatabase()
, read more here.
But please note that if there are 2 tabs opened for same application which means 2 IDB instance of same database, then you cannot delete database because db instance will go into blocked state. You can try using deleteDatabase()
and you will see that database delete operation will not work until you close other tab. Try using below code and you will database goes in blocked state, also below is the how to delete IDB database using JS code.
var DBDeleteRequest = window.indexedDB.deleteDatabase("toDoList");
DBDeleteRequest.onblocked = function(event) {
alert("Error message: Database in blocked state. ");
};
DBDeleteRequest.onerror = function(event) {
console.log("Error deleting database.");
};
DBDeleteRequest.onsuccess = function(event) {
console.log("Database deleted successfully");
console.log(request.result); // should be null
};
IDB versions:
I would personally not remend changing IDB version each time you do a new deployment otherwise user browser will end up having hell lot of IDB instance.
Versions in IDB is mainly to support the runtime time manipulation of IDB schema. Suppose you want to add some new column/store, remove some index from existing store etc. then you should increase the version by one and do db schema manipulation.
Clear browser cache and IDBFactory.deleteDatabase()
are 2 mechanism to delete IDB instance.
Below is how you can handle open and delete IDB but we aware that you should/can use localStorage
in conjunction with it to check when you just need to open a database and when delete + open.
var dbDeleteRequest = window.indexedDB.deleteDatabase("toDoList");
dbDeleteRequest.onerror = function(event) {
console.log("Error while deleting database.", true);
};
dbDeleteRequest.onsuccess = function(event) {
console.log("Success while deleting database.", true);
// Let us open our database
var DBOpenRequest = window.indexedDB.open("toDoList", 5);
DBOpenRequest.onsuccess = function(event) {
console.log('<li>Database initialised.</li>');
};
DBOpenRequest.onupgradeneeded = function(event) {
console.log('<li>DBOpenRequest.onupgradeneeded</li>');
};
};
In my case solution before open a new connection for work check if existing version is actual. If not actual delete all database before open a new connection.
function deleteOldIndexedDB(dbName, actualVersion){
if(!indexedDB){
return false;
}
var req = indexedDB.open(dbName);
req.onsuccess = function(event)
{
var version;
if(event.srcElement != undefined){
version = event.srcElement.result.version;//chrome
}else{
version = event.target.result.version;//firefox
}
event.target.result.close();
if(version != actualVersion && version != 1){
indexedDB.deleteDatabase(dbName);
}
};
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745356079a4624124.html
评论列表(0条)