From 7a9fa8d15258cddb2b9f5812d483ed51d69d0853 Mon Sep 17 00:00:00 2001 From: Dmitry Sorin Date: Tue, 21 Oct 2014 22:41:10 +0400 Subject: [PATCH] `this` in migration scripts now refers to IDBOpenDBRequest --- CHANGELOG.md | 4 ++++ docs/README_sklad_open.md | 4 ++++ karma.conf.js | 1 + sklad.js | 2 +- tests/interface.js | 3 +++ tests/migration_context.js | 42 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/migration_context.js diff --git a/CHANGELOG.md b/CHANGELOG.md index b525120..8b659aa 100644 --- a/CHANGELOG.md +++ b/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 diff --git a/docs/README_sklad_open.md b/docs/README_sklad_open.md index 9e9f361..7e50e6f 100644 --- a/docs/README_sklad_open.md +++ b/docs/README_sklad_open.md @@ -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) { diff --git a/karma.conf.js b/karma.conf.js index f17635b..15b637f 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -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', diff --git a/sklad.js b/sklad.js index d7604dd..5dcc661 100644 --- a/sklad.js +++ b/sklad.js @@ -662,7 +662,7 @@ if (!options.migration[i]) continue; - options.migration[i](this.result); + options.migration[i].call(this, this.result); } }; diff --git a/tests/interface.js b/tests/interface.js index 3addab0..f538c06 100644 --- a/tests/interface.js +++ b/tests/interface.js @@ -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); diff --git a/tests/migration_context.js b/tests/migration_context.js new file mode 100644 index 0000000..1814968 --- /dev/null +++ b/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(); + }); + }); +});