Skip to content

Commit

Permalink
this in migration scripts now refers to IDBOpenDBRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
1999 committed Oct 21, 2014
1 parent 142bb88 commit 7a9fa8d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 1.2.0

* new: `this` in migration scripts now refers to IDBOpenDBRequest so that you can create indexes on top of already created object stores. Check out [docs](https://github.com/1999/sklad/blob/master/docs/README_sklad_open.md) for more info.

## 1.1.0

* new: `sklad.deleteDatabase()` method
Expand Down
4 changes: 4 additions & 0 deletions docs/README_sklad_open.md
Expand Up @@ -25,6 +25,10 @@ sklad.open('dbName', {
'2': function (database) {
// This migration part starts when your database migrates from "1" to "2" version
var objStore = database.createObjectStore('users_likes', {keyPath: 'date'});

// The `this` keyword refers to current IDBOpenDBRequest
var usersObjStore = this.transaction.objectStore('users');
usersObjStore.createIndex('github_search', 'github_login', {unique: true});
}
}
}, function (err, conn) {
Expand Down
1 change: 1 addition & 0 deletions karma.conf.js
Expand Up @@ -8,6 +8,7 @@ module.exports = function (config) {
'tests/interface.js',
'tests/open.js',
'tests/open_blocked_evt.js',
'tests/migration_context.js',
'tests/delete_database.js',
'tests/close.js',
'tests/insert.js',
Expand Down
2 changes: 1 addition & 1 deletion sklad.js
Expand Up @@ -662,7 +662,7 @@
if (!options.migration[i])
continue;

options.migration[i](this.result);
options.migration[i].call(this, this.result);
}
};

Expand Down
3 changes: 3 additions & 0 deletions tests/interface.js
Expand Up @@ -57,6 +57,9 @@ describe('API interface tests', function () {
migrationsRun.push('current database version migration');
},
'2': function (database) {
expect(this instanceof IDBOpenDBRequest).toEqual(true);
expect(this.result).toBe(database);

migrationsRun.push('new database version migration');
expect(database instanceof window.IDBDatabase).toBe(true);

Expand Down
42 changes: 42 additions & 0 deletions tests/migration_context.js
@@ -0,0 +1,42 @@
describe('Migration scripts context tests', function () {
var dbName = 'dbName' + Math.random();

function closeConnection(cb) {
if (conn) {
conn.close();
conn = null;

cb();
}
}

it('should create index during first migration', function (done) {
openBaseConnection(dbName, function (connection) {
connection.close();
done();
});
});

it('should add index to existing object store', function (done) {
sklad.open(dbName, {
version: 2,
migration: {
'2': function () {
var objectStore = this.transaction.objectStore('keypath_true__keygen_false_0');
objectStore.createIndex("foo", "bar");

expect(objectStore.indexNames.contains("sort_login")).toBe(true);
expect(objectStore.indexNames.contains("sort_name")).toBe(true);
expect(objectStore.indexNames.contains("foo")).toBe(true);
}
}
}, function (err, connection) {
if (err) {
throw new Error(err.name + ': ' + err.message);
}

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

0 comments on commit 7a9fa8d

Please sign in to comment.