Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capability to detect whether a database already exists without creating it #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 53 additions & 24 deletions idbstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
onStoreReady: function () {
},
onError: defaultErrorHandler,
indexes: []
indexes: [],
cancelIfNew: false,
cancelMsg: ""
};

/**
Expand Down Expand Up @@ -240,6 +242,22 @@
_insertIdCount: 0,

/**
* If database does not exist yet, abort transaction if cancelIfNew is true
*
* @type Number
* @private
*/
cancelIfNew: false,

/**
* If cancelIfNew is true and database does not exist, call onError with this msg
*
* @type Number
* @private
*/
cancelMsg: "",

/**
* Opens an IndexedDB; called by the constructor.
*
* Will check if versions match and compare provided index configuration
Expand Down Expand Up @@ -300,35 +318,38 @@
var emptyTransaction = this.db.transaction([this.storeName], this.consts.READ_ONLY);
this.store = emptyTransaction.objectStore(this.storeName);

// check indexes
var existingIndexes = Array.prototype.slice.call(this.getIndexList());
this.indexes.forEach(function(indexData){
var indexName = indexData.name;
// check indexes only if not checking for existing database
var existingIndexes = [];
if (!this.cancelIfNew) {
existingIndexes = Array.prototype.slice.call(this.getIndexList());
this.indexes.forEach(function(indexData){
var indexName = indexData.name;

if(!indexName){
preventSuccessCallback = true;
this.onError(new Error('Cannot create index: No index name given.'));
return;
}
if(!indexName){
preventSuccessCallback = true;
this.onError(new Error('Cannot create index: No index name given.'));
return;
}

this.normalizeIndexData(indexData);
this.normalizeIndexData(indexData);

if(this.hasIndex(indexName)){
// check if it complies
var actualIndex = this.store.index(indexName);
var complies = this.indexComplies(actualIndex, indexData);
if(!complies){
if(this.hasIndex(indexName)){
// check if it complies
var actualIndex = this.store.index(indexName);
var complies = this.indexComplies(actualIndex, indexData);
if(!complies){
preventSuccessCallback = true;
this.onError(new Error('Cannot modify index "' + indexName + '" for current version. Please bump version number to ' + ( this.dbVersion + 1 ) + '.'));
}

existingIndexes.splice(existingIndexes.indexOf(indexName), 1);
} else {
preventSuccessCallback = true;
this.onError(new Error('Cannot modify index "' + indexName + '" for current version. Please bump version number to ' + ( this.dbVersion + 1 ) + '.'));
this.onError(new Error('Cannot create new index "' + indexName + '" for current version. Please bump version number to ' + ( this.dbVersion + 1 ) + '.'));
}

existingIndexes.splice(existingIndexes.indexOf(indexName), 1);
} else {
preventSuccessCallback = true;
this.onError(new Error('Cannot create new index "' + indexName + '" for current version. Please bump version number to ' + ( this.dbVersion + 1 ) + '.'));
}

}, this);
}, this);
}

if (existingIndexes.length) {
preventSuccessCallback = true;
Expand All @@ -340,6 +361,14 @@

openRequest.onupgradeneeded = function(/* IDBVersionChangeEvent */ event){

// when this method is called, requested database version does not exist
// yet - abort if so requested and call error handler with configured msg
if (this.cancelIfNew) {
event.target.transaction.abort();
this.onError(this.cancelMsg);
return;
}

this.db = event.target.result;

if(this.db.objectStoreNames.contains(this.storeName)){
Expand Down
21 changes: 21 additions & 0 deletions test/idbwrapper_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
describe('IDBWrapper', function(){

describe('check for existing database', function(){

it('should report the database does not exist', function(done){
var cancelMsg = "Database does not exist";
var count = 0;
var onError = function(err) {
if (count++ === 0) {
expect(err).to.equal(cancelMsg);
} else {
done();
}
}
store = new IDBStore({
storeName: 'spec-store-simple',
onError: onError,
cancelIfNew: true,
cancelMsg: cancelMsg
}, done);
});
});

describe('basic CRUD, in-line keys', function(){

var store;
Expand Down