'use strict';

var OBJECT_STORE_DATA = 'syncdb-data';

function IDBDriver(dbName) {
    if (typeof dbName !== 'string')
        throw new TypeError('dbName argument must be a string');

    this._db = null;
    this._name = dbName;

    var self = this;
    this._init = new Promise(function (resolve, reject) {
        var openRequest = indexedDB.open(self._name, 1);
        openRequest.onerror = reject;
        openRequest.onsuccess = function () {
            self._db = openRequest.result;
            resolve();
        };
        openRequest.onupgradeneeded = function (event) {
            var db = event.target.result;
            var objectStore = db.createObjectStore(OBJECT_STORE_DATA, {keyPath: 'id'});
            objectStore.createIndex('seqid', 'seqid');
        };
    });
}

IDBDriver.prototype.insert = function (obj) {
    var self = this;
    return this._init.then(function () {
        return new Promise(function (resolve, reject) {
            var transaction = self._db.transaction(OBJECT_STORE_DATA, 'readwrite');
            transaction.oncomplete = resolve;
            transaction.onerror = function () {
                reject(transaction.error);
            };
            var objectStore = transaction.objectStore(OBJECT_STORE_DATA);
            objectStore.put(obj);
        });
    });
};

IDBDriver.prototype.get = function (id) {
    var self = this;
    return this._init.then(function () {
        return new Promise(function (resolve, reject) {
            var transaction = self._db.transaction(OBJECT_STORE_DATA, 'readonly');
            transaction.onerror = function () {
                reject(transaction.error);
            };
            var objectStore = transaction.objectStore(OBJECT_STORE_DATA);
            var request = objectStore.get(id);
            request.onsuccess = function (event) {
                resolve(event.target.result);
            };
        });
    });
};

IDBDriver.prototype.getData = function () {
    var self = this;
    return this._init.then(function () {
        return new Promise(function (resolve, reject) {
            var result = [];
            var transaction = self._db.transaction(OBJECT_STORE_DATA, 'readonly');
            transaction.oncomplete = function () {
                resolve(result);
            };
            transaction.onerror = function () {
                reject(transaction.error);
            };
            var objectStore = transaction.objectStore(OBJECT_STORE_DATA);
            var request = objectStore.openCursor();
            request.onsuccess = function (event) {
                var cursor = event.target.result;
                if (cursor) {
                    result.push(cursor.value);
                    cursor.continue();
                }
            };
        });
    });
};

IDBDriver.prototype.getLastSeq = function () {
    var self = this;
    return this._init.then(function () {
        return new Promise(function (resolve, reject) {
            var transaction = self._db.transaction(OBJECT_STORE_DATA, 'readonly');
            transaction.onerror = function () {
                reject(transaction.error);
            };
            var objectStore = transaction.objectStore(OBJECT_STORE_DATA);
            var index = objectStore.index('seqid');
            index.openCursor(null, 'prev').onsuccess = function (event) {
                var cursor = event.target.result;
                if (cursor) {
                    resolve(cursor.value.seqid);
                } else {
                    resolve(0);
                }
            };
        });
    });
};

module.exports = IDBDriver;

@@ -226,16 +226,83 @@
 bName;%0A%0A
+    this._doInit();%0A%7D%0A%0AIDBDriver.prototype._doInit = function () %7B%0A
     var 
@@ -848,24 +848,25 @@
 %7D;%0A    %7D);%0A%7D
+;
 %0A%0AIDBDriver.
@@ -3669,16 +3669,349 @@
 %7D);%0A%7D;%0A%0A
+IDBDriver.prototype.clearDatabase = function () %7B%0A    var self = this;%0A    return new Promise(function (resolve, reject) %7B%0A        var request = indexedDB.deleteDatabase(self._name);%0A        request.onerror = reject;%0A        request.onsuccess = function () %7B%0A            self._doInit();%0A            resolve();%0A        %7D;%0A    %7D);%0A%7D;%0A%0A
 module.e

add clearDatabase