add deleteObjectStore, createObjectStore and close methods to IDBDatabase

This commit is contained in:
KtorZ 2017-06-22 16:19:21 +02:00
parent 02e2a5832e
commit 0b0579aa12
No known key found for this signature in database
GPG Key ID: 3F72E8BC2894C015
5 changed files with 99 additions and 36 deletions

View File

@ -15,6 +15,7 @@
"no-param-reassign": 0,
"import/no-unresolved": 0,
"no-underscore-dangle": 0,
"prefer-template": 0
"prefer-template": 0,
"object-shorthand": 0
}
}

View File

@ -27,7 +27,16 @@ exports.eventHandler = function eventHandler(cb) {
exports._showIDBDatabase = function _showIDBDatabase(db) {
return '(IDBDatabase ' +
'{ name: ' + db.name +
', objectStoreNames: [' + exports.toArray(db.objectStoreNames).join(',') + ']' +
', objectStoreNames: [' + exports.toArray(db.objectStoreNames).join(', ') + ']' +
', version: ' + db.version +
' })';
};
exports._showObjectStore = function _showObjectStore(store) {
return '(IDBObjectStore ' +
'{ autoIncrement: ' + store.autoIncrement +
', indexNames: [' + exports.toArray(store.indexNames).join(', ') + ']' +
', keyPath: ' + store.keyPath +
', name: ' + store.name +
' })';
};

View File

@ -12,16 +12,14 @@ foreign import data INDEXED_DB :: Effect
foreign import data IDBDatabase :: Type
foreign import _showIDBDatabase
:: forall eff
. IDBDatabase
-> String
foreign import _showIDBDatabase :: forall eff. IDBDatabase -> String
instance showIDBDatabase :: Show IDBDatabase where
show db = _showIDBDatabase db
show = _showIDBDatabase
type IDBOpenRequest eff =
{ onBlocked :: Maybe (IDBDatabase -> Eff eff Unit)
, onUpgradeNeeded :: Maybe (IDBDatabase -> Eff eff Unit)
}
foreign import data IDBObjectStore :: Type
foreign import _showObjectStore :: forall eff. IDBObjectStore -> String
instance showIDBObjectStore :: Show IDBObjectStore where
show = _showObjectStore

View File

@ -6,6 +6,55 @@ const noOp2 = Core.noOp2;
const errorHandler = Core.errorHandler;
const eventHandler = Core.eventHandler;
exports.close = function close(db) {
return function effects() {
try {
db.close();
} catch (e) {
throw new Error(e.name);
}
};
};
exports._createObjectStore = function _createObjectStore(db, name, opts) {
const keyPath = Maybe.fromMaybe(undefined)(opts.keyPath);
const autoIncrement = opts.autoIncrement;
return function effects() {
try {
return db.createObjectStore(name, {
keyPath: keyPath,
autoIncrement: autoIncrement,
});
} catch (e) {
throw new Error(e.name);
}
};
};
exports._deleteObjectStore = function _deleteObjectStore(db, name) {
return function effects() {
try {
db.deleteObjectStore(name);
} catch (e) {
throw new Error(e.name);
}
};
};
exports.deleteDatabase = function deleteDatabase(name) {
return function callback(success, error) {
const request = indexedDB.deleteDatabase(name);
request.onsuccess = function onSuccess(e) {
success(e.oldVersion);
};
request.onerror = errorHandler(error);
};
};
exports._open = function _open(name, mver, req) {
const ver = Maybe.fromMaybe(undefined)(mver);
@ -21,18 +70,6 @@ exports._open = function _open(name, mver, req) {
};
};
exports.deleteDatabase = function _deleteDatabase(name) {
return function callback(success, error) {
const request = indexedDB.deleteDatabase(name);
request.onsuccess = function onSuccess(e) {
success(e.oldVersion);
};
request.onerror = errorHandler(error);
};
};
exports.name = function name(db) {
return db.name;
};

View File

@ -4,23 +4,34 @@ import Prelude
import Control.Monad.Aff(Aff)
import Control.Monad.Eff(kind Effect, Eff)
import Control.Monad.Eff.Exception(EXCEPTION)
import Control.Monad.Eff.Console as Console
import Data.Function.Uncurried as Fn
import Data.Function.Uncurried(Fn3)
import Data.Function.Uncurried(Fn2, Fn3)
import Data.Maybe(Maybe)
import Core
foreign import _open
:: forall eff. Fn3
String
(Maybe Int)
(IDBOpenRequest eff)
(Aff (idb :: INDEXED_DB | eff) IDBDatabase)
open :: forall eff. String -> Maybe Int -> (IDBOpenRequest eff) -> Aff (idb :: INDEXED_DB | eff) IDBDatabase
open name mver req =
Fn.runFn3 _open name mver req
type IDBOpenRequest eff =
{ onBlocked :: Maybe (IDBDatabase -> Eff (idb :: INDEXED_DB | eff) Unit)
, onUpgradeNeeded :: Maybe (IDBDatabase -> Eff (idb :: INDEXED_DB | eff) Unit)
}
foreign import close :: forall eff. IDBDatabase -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit
foreign import _createObjectStore :: forall eff. Fn3 IDBDatabase String { keyPath :: Maybe String, autoIncrement :: Boolean } (Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) IDBObjectStore)
createObjectStore :: forall eff . IDBDatabase -> String -> { keyPath :: Maybe String, autoIncrement :: Boolean } -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) IDBObjectStore
createObjectStore db name opts =
Fn.runFn3 _createObjectStore db name opts
foreign import _deleteObjectStore :: forall eff. Fn2 IDBDatabase String (Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) IDBObjectStore)
deleteObjectStore :: forall eff . IDBDatabase -> String -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) IDBObjectStore
deleteObjectStore db name =
Fn.runFn2 _deleteObjectStore db name
foreign import deleteDatabase :: forall eff. String -> Aff (idb :: INDEXED_DB | eff) Int
@ -29,6 +40,13 @@ foreign import deleteDatabase :: forall eff. String -> Aff (idb :: INDEXED_DB |
foreign import name :: IDBDatabase -> String
foreign import version :: IDBDatabase -> Int
foreign import objectStoreNames :: IDBDatabase -> Array String
foreign import _open :: forall eff. Fn3 String (Maybe Int) (IDBOpenRequest eff) (Aff (idb :: INDEXED_DB | eff) IDBDatabase)
open :: forall eff . String -> Maybe Int -> IDBOpenRequest eff -> Aff (idb :: INDEXED_DB | eff) IDBDatabase
open name mver req =
Fn.runFn3 _open name mver req
foreign import version :: IDBDatabase -> Int