Skip to content

Commit

Permalink
Add deleteDatabase API
Browse files Browse the repository at this point in the history
  • Loading branch information
1999 committed Oct 16, 2014
1 parent 7bf2777 commit 142bb88
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 49 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 1.1.0

* new: `sklad.deleteDatabase()` method

## 1.0.0

* new: full tests coverage with Travis CI: [Travis CI](https://travis-ci.org/1999/sklad), [tests](https://github.com/1999/sklad/tree/master/tests)
Expand Down
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -159,6 +159,16 @@ sklad.open(dbName, options, function (err, conn) {
});
```

## Delete database
```javascript
/**
* @param {String} dbName
* @param {Function} callback invokes:
* @param {DOMError|Null} err
*/
sklad.deleteDatabase(dbName, callback);
```

# Important notes
There's an [unclear and scaring table](https://developer.mozilla.org/en-US/docs/IndexedDB/Using_IndexedDB#Structuring_the_database) on MDN called "Structuring the database". It means that there are 4 ways of storing your data in the object stores and you should choose which of them fits your needs best.

Expand Down
4 changes: 3 additions & 1 deletion karma.conf.js
Expand Up @@ -5,8 +5,10 @@ module.exports = function (config) {
files: [
'sklad.js',
'tests/test_utils.js',
'tests/interface.js',
'tests/open.js',
'tests/blocked.js',
'tests/open_blocked_evt.js',
'tests/delete_database.js',
'tests/close.js',
'tests/insert.js',
'tests/upsert.js',
Expand Down
36 changes: 36 additions & 0 deletions sklad.js
Expand Up @@ -732,6 +732,42 @@
};
};

/**
* Deletes database
*
* @param {String} dbName
* @param {Function} callback invokes:
* @param {DOMError|Null} err
*/
skladAPI.deleteDatabase = function sklad_deleteDatabase(dbName, callback) {
if (!window.indexedDB) {
var err = new DOMError('NotSupportedError', 'Your browser doesn\'t support IndexedDB');
callback(err);

return;
}

var openDbRequest = window.indexedDB.deleteDatabase(dbName);
var callbackRun = false;

openDbRequest.onsuccess = openDbRequest.onerror = openDbRequest.onblocked = function sklad_deleteDatabase_onFinish(evt) {
if (callbackRun) {
return;
}

var err = (evt.type === 'blocked')
? new DOMError('InvalidStateError', 'Database ' + dbName + ' is blocked')
: evt.target.error;

callback(err || null);
callbackRun = true;

if (evt.type !== 'success') {
evt.preventDefault();
}
};
},

skladAPI.keyValue = function sklad_keyValue(key, value) {
return Object.create(skladKeyValueContainer, {
key: {value: key, configurable: false, writable: false},
Expand Down
25 changes: 25 additions & 0 deletions tests/delete_database.js
@@ -0,0 +1,25 @@
describe('Delete database tests', function () {
it('should delete database', function (done) {
var dbName = 'dbName' + Math.random();

openBaseConnection(dbName, function (conn) {
conn.close();

sklad.deleteDatabase(dbName, function (err) {
expect(err).toBeFalsy();
done();
});
});
});

it('should produce DOMError.InvalidStateError if database is blocked', function (done) {
var dbName = 'dbName' + Math.random();

openBaseConnection(dbName, function (conn) {
sklad.deleteDatabase(dbName, function (err) {
expect(err.name).toBe('InvalidStateError');
done();
});
});
});
});
88 changes: 88 additions & 0 deletions tests/interface.js
@@ -0,0 +1,88 @@
describe('API interface tests', function () {
var dbName = 'dbName' + Math.random();
var conn;

it('global variables should exist', function () {
expect(window.indexedDB).toBeDefined();
expect(window.IDBTransaction).toBeDefined();
expect(window.IDBKeyRange).toBeDefined();
expect(window.IDBCursor).toBeDefined();
expect(typeof window.sklad).toBe('object');
expect(typeof window.sklad.open).toBe('function');
expect(typeof window.sklad.deleteDatabase).toBe('function');
expect(typeof window.sklad.keyValue).toBe('function');

expect(window.sklad.ASC).toBeDefined();
expect(window.sklad.ASC_UNIQUE).toBeDefined();
expect(window.sklad.DESC).toBeDefined();
expect(window.sklad.DESC_UNIQUE).toBeDefined();
});

it('should contain all needed methods in connection', function (done) {
sklad.open(dbName, function (err, connection) {
expect(err).toBeFalsy();

expect(typeof connection.insert).toBe('function');
expect(typeof connection.upsert).toBe('function');
expect(typeof connection.delete).toBe('function');
expect(typeof connection.clear).toBe('function');
expect(typeof connection.get).toBe('function');
expect(typeof connection.count).toBe('function');
expect(typeof connection.close).toBe('function');

expect(connection.database instanceof window.IDBDatabase).toBe(true);
expect(Object.getOwnPropertyDescriptor(connection, 'database')).toEqual({
value: connection.database,
enumerable: false,
configurable: true,
writable: false
});

// close existing connection
conn = connection;

done();
});
});

it('should run migration code if database upgrades', function (done) {
var migrationsRun = [];

sklad.open(dbName, {
version: 2,
migration: {
'1': function (database) {
// this migration part shoud not run at all
// because previous spec has already created 1st version of databse
migrationsRun.push('current database version migration');
},
'2': function (database) {
migrationsRun.push('new database version migration');
expect(database instanceof window.IDBDatabase).toBe(true);

var objStore = database.createObjectStore('some_object_store', {keyPath: 'date'});
expect(objStore instanceof window.IDBObjectStore).toBe(true);
}
}
}, function (err, connection) {
expect(err).toBeFalsy();

expect(migrationsRun).not.toContain('current database version migration');
expect(migrationsRun).toContain('new database version migration');

// close existing connection
conn = connection;

done();
});
});

afterEach(function () {
if (conn) {
// otherwise 'blocked' event will be caught
// FIXME: handle it
conn.close();
conn = null;
}
});
});
51 changes: 4 additions & 47 deletions tests/open.js
@@ -1,42 +1,10 @@
describe('Basic tests', function () {
describe('Basic open tests', function () {
var dbName = 'dbName' + Math.random();
var conn;

it('global variables should exist', function () {
expect(window.indexedDB).toBeDefined();
expect(window.IDBTransaction).toBeDefined();
expect(window.IDBKeyRange).toBeDefined();
expect(window.IDBCursor).toBeDefined();
expect(window.sklad).toBeDefined();

expect(window.sklad.ASC).toBeDefined();
expect(window.sklad.ASC_UNIQUE).toBeDefined();
expect(window.sklad.DESC).toBeDefined();
expect(window.sklad.DESC_UNIQUE).toBeDefined();
});

it('should connect to database', function (done) {
sklad.open(dbName, function (err, connection) {
expect(err).toBeFalsy();

expect(typeof connection.insert).toBe('function');
expect(typeof connection.upsert).toBe('function');
expect(typeof connection.delete).toBe('function');
expect(typeof connection.clear).toBe('function');
expect(typeof connection.get).toBe('function');
expect(typeof connection.count).toBe('function');
expect(typeof connection.close).toBe('function');

expect(connection.database instanceof window.IDBDatabase).toBe(true);
expect(Object.getOwnPropertyDescriptor(connection, 'database')).toEqual({
value: connection.database,
enumerable: false,
configurable: true,
writable: false
});

// close existing connection
conn = connection;
connection.close();

done();
});
Expand All @@ -49,7 +17,7 @@ describe('Basic tests', function () {
version: 2,
migration: {
'1': function (database) {
// this migration part shoud not run at all
// this migration part should not run at all
// because previous spec has already created 1st version of databse
migrationsRun.push('current database version migration');
},
Expand All @@ -67,19 +35,8 @@ describe('Basic tests', function () {
expect(migrationsRun).not.toContain('current database version migration');
expect(migrationsRun).toContain('new database version migration');

// close existing connection
conn = connection;

connection.close();
done();
});
});

afterEach(function () {
if (conn) {
// otherwise 'blocked' event will be caught
// FIXME: handle it
conn.close();
conn = null;
}
});
});
2 changes: 1 addition & 1 deletion tests/blocked.js → tests/open_blocked_evt.js
Expand Up @@ -30,4 +30,4 @@ describe('Database block tests', function () {
done();
});
});
});
});

0 comments on commit 142bb88

Please sign in to comment.