git.iko.soy/purescript-indexed-DB
on master help sign in
add some sugar (defaultParameters) and use type synonym to improve readability
commitfdbbd1703e0b649f0ad8d2cc13a4c1f50573856c authorKtorZ <matthias.benkort@gmail.com> dateFri 30 Jun 2017, 13:49 (9 years ago) parentf583308
7 files changed+81−23
src/Database/IndexedDB/Core.purs+4 src/Database/IndexedDB/IDBDatabase.purs+13−8 src/Database/IndexedDB/IDBIndex.purs+2 src/Database/IndexedDB/IDBIndex/Internal.js+18−4 src/Database/IndexedDB/IDBIndex/Internal.purs+17−3 src/Database/IndexedDB/IDBObjectStore.js+3−3 src/Database/IndexedDB/IDBObjectStore.purs+24−5
src/Database/IndexedDB/Core.purs+4
@@ -7,6 +7,7 @@ module Database.IndexedDB.Core
77 , Index
88 , KeyCursor
99 , KeyRange
10+ , KeyPath
1011 , ObjectStore
1112 , Transaction
1213 , TransactionMode(..)
@@ -35,6 +36,9 @@ data CursorSource = ObjectStore ObjectStore | Index Index
3536data TransactionMode = ReadOnly | ReadWrite | VersionChange
3637
3738
39+type KeyPath = Array String
40+
41+
3842foreign import data INDEXED_DB :: Effect
3943
4044
src/Database/IndexedDB/IDBDatabase.purs+13−8
@@ -1,18 +1,20 @@
11module Database.IndexedDB.IDBDatabase
22 ( class IDBDatabase, close, createObjectStore, deleteObjectStore, transaction
3+ , StoreName
34 , name
45 , objectStoreNames
56 , version
67 ) where
78
8−import Prelude (Unit, show)
9+import Prelude (Unit, show)
910
10−import Control.Monad.Eff (Eff)
11−import Control.Monad.Eff.Exception (EXCEPTION)
12−import Data.Function.Uncurried as Fn
13−import Data.Function.Uncurried (Fn2, Fn3, Fn4)
11+import Control.Monad.Eff (Eff)
12+import Control.Monad.Eff.Exception (EXCEPTION)
13+import Data.Function.Uncurried as Fn
14+import Data.Function.Uncurried (Fn2, Fn3, Fn4)
1415
1516import Database.IndexedDB.Core
17+import Database.IndexedDB.IDBObjectStore (IDBObjectStoreParameters)
1618
1719
1820--------------------
@@ -20,9 +22,12 @@ import Database.IndexedDB.Core
2022--
2123class IDBDatabase db where
2224 close :: forall eff. db -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit
23− createObjectStore :: forall eff. db -> String -> { keyPath :: Array String, autoIncrement :: Boolean } -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) ObjectStore
24− deleteObjectStore :: forall eff . db -> String -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) ObjectStore
25− transaction :: forall eff. db -> Array String -> TransactionMode -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Transaction
25+ createObjectStore :: forall eff. db -> StoreName -> IDBObjectStoreParameters -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) ObjectStore
26+ deleteObjectStore :: forall eff . db -> StoreName -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) ObjectStore
27+ transaction :: forall eff. db -> KeyPath -> TransactionMode -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Transaction
28+
29+
30+type StoreName = String
2631
2732
2833--------------------
src/Database/IndexedDB/IDBIndex.purs+2
@@ -4,9 +4,11 @@ module Database.IndexedDB.IDBIndex
44
55import Database.IndexedDB.IDBIndex.Internal
66 ( class IDBIndex, count, get, getAllKeys, getKey, openCursor, openKeyCursor
7+ , IDBIndexParameters
78 , keyPath
89 , multiEntry
910 , name
1011 , objectStore
1112 , unique
13+ , defaultParameters
1214 )
src/Database/IndexedDB/IDBIndex/Internal.js+18−4
@@ -12,7 +12,17 @@ const successHandler = function successHandler(cb) {
1212
1313
1414exports._keyPath = function _keyPath(index) {
15− return index.keyPath;
15+ const path = index.keyPath;
16+
17+ if (Array.isArray(path)) {
18+ return path;
19+ }
20+
21+ if (typeof path === 'string' && path !== '') {
22+ return [path];
23+ }
24+
25+ return [];
1626};
1727
1828exports._multiEntry = function _multiEntry(index) {
@@ -33,9 +43,13 @@ exports._unique = function _unique(index) {
3343
3444exports._count = function _count(index, query) {
3545 return function aff(success, error) {
36− const request = index.count(query);
37− request.onsuccess = successHandler(success);
38− request.onerror = errorHandler(error);
46+ try {
47+ const request = index.count(query);
48+ request.onsuccess = successHandler(success);
49+ request.onerror = errorHandler(error);
50+ } catch (e) {
51+ error(new Error(e.name));
52+ }
3953 };
4054};
4155
src/Database/IndexedDB/IDBIndex/Internal.purs+17−3
@@ -10,7 +10,8 @@ import Data.Function.Uncurried (Fn2, Fn3)
1010import Data.Maybe (Maybe)
1111import Data.Nullable (Nullable, toMaybe, toNullable)
1212
13−import Database.IndexedDB.Core (INDEXED_DB, CursorDirection, Index, Key, KeyCursor, KeyRange, ObjectStore, ValueCursor)
13+import Database.IndexedDB.Core (INDEXED_DB, CursorDirection, Index, Key, KeyCursor, KeyRange,
14+ KeyPath, ObjectStore, ValueCursor)
1415
1516
1617--------------------
@@ -25,10 +26,16 @@ class IDBIndex index where
2526 openKeyCursor :: forall eff. index -> Maybe KeyRange -> CursorDirection -> Aff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) KeyCursor
2627
2728
29+type IDBIndexParameters =
30+ { unique :: Boolean
31+ , multiEntry :: Boolean
32+ }
33+
34+
2835--------------------
2936-- ATTRIBUTES
3037--
31−keyPath :: Index -> String
38+keyPath :: Index -> KeyPath
3239keyPath =
3340 _keyPath
3441
@@ -96,10 +103,17 @@ instance idbIndexObjectStore :: IDBIndex ObjectStore where
96103 Fn.runFn3 _openKeyCursor store (toNullable range) (show dir)
97104
98105
106+defaultParameters :: IDBIndexParameters
107+defaultParameters =
108+ { unique : false
109+ , multiEntry : false
110+ }
111+
112+
99113--------------------
100114-- FFI
101115--
102−foreign import _keyPath :: Index -> String
116+foreign import _keyPath :: Index -> Array String
103117
104118
105119foreign import _multiEntry :: Index -> Boolean
src/Database/IndexedDB/IDBObjectStore.js+3−3
@@ -44,7 +44,7 @@ exports._createIndex = function _createIndex(store, name, path, params) {
4444 // a string hasn't the same meaning as a sequence of strings
4545 switch (path.length) {
4646 case 0:
47− keyPath = null;
47+ keyPath = '';
4848 break;
4949 case 1:
5050 keyPath = path[0];
@@ -53,7 +53,7 @@ exports._createIndex = function _createIndex(store, name, path, params) {
5353 keyPath = path;
5454 }
5555
56− store.createIndex(name, keyPath, params);
56+ return store.createIndex(name, keyPath, params);
5757 } catch (e) {
5858 throw new Error(e.name);
5959 }
@@ -100,7 +100,7 @@ exports._keyPath = function _keyPath(store) {
100100 }
101101
102102 if (typeof path === 'string') {
103− return path.split('.');
103+ return [path];
104104 }
105105
106106 return [];
src/Database/IndexedDB/IDBObjectStore.purs+24−5
@@ -1,10 +1,13 @@
11module Database.IndexedDB.IDBObjectStore
22 ( class IDBObjectStore, add, clear, createIndex, delete, deleteIndex, index, put
33 , module Database.IndexedDB.IDBIndex.Internal
4+ , IDBObjectStoreParameters
5+ , IndexName
46 , autoIncrement
57 , indexNames
68 , keyPath
79 , name
10+ , defaultParameters
811 ) where
912
1013import Prelude (Unit, ($), (<$>))
@@ -18,8 +21,8 @@ import Data.Function.Uncurried (Fn2, Fn3, Fn4)
1821import Data.Maybe (Maybe)
1922import Data.Nullable (Nullable, toNullable)
2023
21−import Database.IndexedDB.Core (INDEXED_DB, Index, KeyRange, ObjectStore)
22−import Database.IndexedDB.IDBIndex.Internal (class IDBIndex, count, get, getAllKeys, getKey, openCursor, openKeyCursor)
24+import Database.IndexedDB.Core (INDEXED_DB, Index, KeyRange, KeyPath, ObjectStore)
25+import Database.IndexedDB.IDBIndex.Internal (class IDBIndex, IDBIndexParameters, count, get, getAllKeys, getKey, openCursor, openKeyCursor)
2326import Database.IndexedDB.IDBKey.Internal (Key(Key), extractForeign)
2427
2528
@@ -29,13 +32,22 @@ import Database.IndexedDB.IDBKey.Internal (Key(Key), extractForeign)
2932class IDBObjectStore store where
3033 add :: forall value eff. store -> value -> Maybe Key -> Aff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Key
3134 clear :: forall eff. store -> Aff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit
32− createIndex :: forall eff. store -> String -> (Array String) -> { unique :: Boolean, multiEntry :: Boolean } -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Index
35+ createIndex :: forall eff. store -> IndexName -> KeyPath -> IDBIndexParameters -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Index
3336 delete :: forall eff. store -> KeyRange -> Aff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit
34− deleteIndex :: forall eff. store -> String -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit
35− index :: forall eff. store -> String -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Index
37+ deleteIndex :: forall eff. store -> IndexName -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit
38+ index :: forall eff. store -> IndexName -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Index
3639 put :: forall value eff. store -> value -> Maybe Key -> Aff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Key
3740
3841
42+type IndexName = String
43+
44+
45+type IDBObjectStoreParameters =
46+ { keyPath :: KeyPath
47+ , autoIncrement :: Boolean
48+ }
49+
50+
3951--------------------
4052-- ATTRIBUTES
4153--
@@ -85,6 +97,13 @@ instance idbObjectStoreObjectStore :: IDBObjectStore ObjectStore where
8597 Key <$> Fn.runFn3 _put store value (toNullable $ extractForeign <$> key)
8698
8799
100+defaultParameters :: IDBObjectStoreParameters
101+defaultParameters =
102+ { keyPath : []
103+ , autoIncrement : false
104+ }
105+
106+
88107--------------------
89108-- FFI
90109--