From 0b0579aa12f77257f258e437b6085420cddde197 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Thu, 22 Jun 2017 16:19:21 +0200 Subject: [PATCH] add deleteObjectStore, createObjectStore and close methods to IDBDatabase --- .eslintrc | 3 ++- src/Core.js | 11 +++++++- src/Core.purs | 18 ++++++------- src/IDBDatabase.js | 61 +++++++++++++++++++++++++++++++++++--------- src/IDBDatabase.purs | 42 +++++++++++++++++++++--------- 5 files changed, 99 insertions(+), 36 deletions(-) diff --git a/.eslintrc b/.eslintrc index 847cff4..f06a079 100644 --- a/.eslintrc +++ b/.eslintrc @@ -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 } } diff --git a/src/Core.js b/src/Core.js index 1468483..44fc22a 100644 --- a/src/Core.js +++ b/src/Core.js @@ -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 + + ' })'; +}; diff --git a/src/Core.purs b/src/Core.purs index 0d47873..f4af5f9 100644 --- a/src/Core.purs +++ b/src/Core.purs @@ -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 diff --git a/src/IDBDatabase.js b/src/IDBDatabase.js index 9b60213..4ffb261 100644 --- a/src/IDBDatabase.js +++ b/src/IDBDatabase.js @@ -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; }; diff --git a/src/IDBDatabase.purs b/src/IDBDatabase.purs index d4177f7..ea2617a 100644 --- a/src/IDBDatabase.purs +++ b/src/IDBDatabase.purs @@ -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