@@ -1,125 +1,172 @@
11-- | An object store is the primary storage mechanism for storing data in a database.
22module Database.IndexedDB.IDBObjectStore
3− ( class IDBObjectStore, add, clear, createIndex, delete, deleteIndex, index, put
4− , module Database.IndexedDB.IDBIndex.Internal
5− , IDBObjectStoreParameters
6− , IndexName
3+ -- * Types
4+ ( IndexName
5+ , IndexParameters
6+ , defaultParameters
7+
8+ -- * Interface
9+ , add
10+ , clear
11+ , createIndex
12+ , delete
13+ , deleteIndex
14+ , index
15+ , put
16+
17+ -- * Attributes
718 , autoIncrement
819 , indexNames
920 , keyPath
1021 , name
1122 , transaction
12− , defaultParameters
13− ) where
1423
15−import Prelude (Unit, ($), (<$>), (>>>))
24+ -- * Re-Exports
25+ , module Database.IndexedDB.IDBIndex
26+ ) where
1627
17−import Control.Monad.Aff (Aff)
18−import Data.Foreign (Foreign)
19−import Data.Function.Uncurried as Fn
20−import Data.Function.Uncurried (Fn2, Fn3, Fn4)
21−import Data.Maybe (Maybe)
22−import Data.Nullable (Nullable, toNullable)
28+import Prelude (Unit, ($), (<$>), (>>>))
2329
24−import Database.IndexedDB.Core (IDB, Index, KeyRange, KeyPath, ObjectStore, Transaction)
25−import Database.IndexedDB.IDBIndex.Internal (class IDBIndex, IDBIndexParameters, count, get, getAllKeys, getKey, openCursor, openKeyCursor)
26−import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key(Key), extractForeign, toKey)
30+import Control.Monad.Aff (Aff)
31+import Data.Foreign (Foreign)
32+import Data.Function.Uncurried as Fn
33+import Data.Function.Uncurried (Fn2, Fn3, Fn4)
34+import Data.Maybe (Maybe)
35+import Data.Nullable (Nullable, toNullable)
2736
37+import Database.IndexedDB.Core
38+import Database.IndexedDB.IDBIndex (count, get, getAllKeys, getKey, openCursor, openKeyCursor)
39+import Database.IndexedDB.IDBKey.Internal (class IDBKey, toForeign, toKey)
2840
2941--------------------
30−-- INTERFACES
42+-- TYPES
3143--
32−-- | The IDBObjectStore interface represents an object store handle.
33−class IDBObjectStore store where
34− -- | Adds or updates a record in store with the given value and key.
35− -- |
36− -- | If the store uses in-line keys and key is specified a "DataError" DOMException
37− -- | will be thrown.
38− -- |
39− -- | If add() is used, and if a record with the key already exists the request will fail,
40− -- | with a "ConstraintError" DOMException.
41− add
42− :: forall v k e. (IDBKey k)
43− => store
44− -> v
45− -> Maybe k
46− -> Aff (idb :: IDB | e) Key
47−
48− -- | Deletes all records in store.
49− clear
50− :: forall e
51− . store
52− -> Aff (idb :: IDB | e) Unit
53−
54− -- | Creates a new index in store with the given name, keyPath and options and
55− -- | returns a new IDBIndex. If the keyPath and options define constraints that
56− -- | cannot be satisfied with the data already in store the upgrade transaction
57− -- | will abort with a "ConstraintError" DOMException.
58− -- |
59− -- | Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
60− createIndex
61− :: forall e
62− . store
63− -> IndexName
64− -> KeyPath
65− -> IDBIndexParameters
66− -> Aff (idb :: IDB | e) Index
67−
68− -- | Deletes records in store with the given key or in the given key range in query.
69− delete
70− :: forall e
71− . store
72− -> KeyRange
73− -> Aff (idb :: IDB | e) Unit
74−
75− -- | Deletes the index in store with the given name.
76− -- |
77− -- | Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
78− deleteIndex
79− :: forall e
80− . store
81− -> IndexName
82− -> Aff (idb :: IDB | e) Unit
83−
84− -- | Returns an IDBIndex for the index named name in store.
85− index
86− :: forall e
87− . store
88− -> IndexName
89− -> Aff (idb :: IDB | e) Index
90−
91− -- | Adds or updates a record in store with the given value and key.
92− -- |
93− -- | If the store uses in-line keys and key is specified a "DataError" DOMException
94− -- | will be thrown.
95− -- |
96− -- | If put() is used, any existing record with the key will be replaced.
97− put
98− :: forall v k e. (IDBKey k)
99− => store
100− -> v
101− -> Maybe k
102− -> Aff (idb :: IDB | e) Key
103−
10444
10545-- | Type alias for IndexName
10646type IndexName = String
10747
10848
109−-- | Options provided when creating an object store.
110−type IDBObjectStoreParameters =
111− { keyPath :: KeyPath
112− , autoIncrement :: Boolean
49+-- | Flags to set on the index.
50+-- |
51+-- | An index has a `unique` flag. When this flag is set, the index enforces that no
52+-- | two records in the index has the same key. If a record in the index’s referenced
53+-- | object store is attempted to be inserted or modified such that evaluating the index’s
54+-- | key path on the records new value yields a result which already exists in the index,
55+-- | then the attempted modification to the object store fails.
56+-- |
57+-- | An index has a `multiEntry` flag. This flag affects how the index behaves when the
58+-- | result of evaluating the index’s key path yields an array key. If the `multiEntry` flag
59+-- | is unset, then a single record whose key is an array key is added to the index.
60+-- | If the `multiEntry` flag is true, then the one record is added to the index for each
61+-- | of the subkeys.
62+type IndexParameters =
63+ { unique :: Boolean
64+ , multiEntry :: Boolean
11365 }
11466
11567
116−defaultParameters :: IDBObjectStoreParameters
68+defaultParameters :: IndexParameters
11769defaultParameters =
118− { keyPath : []
119− , autoIncrement : false
70+ { unique : false
71+ , multiEntry : false
12072 }
12173
12274
75+--------------------
76+-- INTERFACES
77+--
78+
79+-- | Adds or updates a record in store with the given value and key.
80+-- |
81+-- | If the store uses in-line keys and key is specified a "DataError" DOMException
82+-- | will be thrown.
83+-- |
84+-- | If add() is used, and if a record with the key already exists the request will fail,
85+-- | with a "ConstraintError" DOMException.
86+add
87+ :: forall e key val store. (IDBKey key) => (IDBObjectStore store)
88+ => store
89+ -> val
90+ -> Maybe key
91+ -> Aff (idb :: IDB | e) key
92+add store value key =
93+ Fn.runFn3 _add store value (toNullable $ (toKey >>> toForeign) <$> key)
94+
95+
96+-- | Deletes all records in store.
97+clear
98+ :: forall e store. (IDBObjectStore store)
99+ => store
100+ -> Aff (idb :: IDB | e) Unit
101+clear =
102+ _clear
103+
104+
105+-- | Creates a new index in store with the given name, keyPath and options and
106+-- | returns a new IDBIndex. If the keyPath and options define constraints that
107+-- | cannot be satisfied with the data already in store the upgrade transaction
108+-- | will abort with a "ConstraintError" DOMException.
109+-- |
110+-- | Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
111+createIndex
112+ :: forall e store. (IDBObjectStore store)
113+ => store
114+ -> IndexName
115+ -> KeyPath
116+ -> IndexParameters
117+ -> Aff (idb :: IDB | e) Index
118+createIndex store name' path params =
119+ Fn.runFn4 _createIndex store name' path params
120+
121+
122+-- | Deletes records in store with the given key or in the given key range in query.
123+delete
124+ :: forall e store. (IDBObjectStore store)
125+ => store
126+ -> KeyRange
127+ -> Aff (idb :: IDB | e) Unit
128+delete store range =
129+ Fn.runFn2 _delete store range
130+
131+
132+-- | Deletes the index in store with the given name.
133+-- |
134+-- | Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
135+deleteIndex
136+ :: forall e store. (IDBObjectStore store)
137+ => store
138+ -> IndexName
139+ -> Aff (idb :: IDB | e) Unit
140+deleteIndex store name' =
141+ Fn.runFn2 _deleteIndex store name'
142+
143+
144+-- | Returns an IDBIndex for the index named name in store.
145+index
146+ :: forall e store. (IDBObjectStore store)
147+ => store
148+ -> IndexName
149+ -> Aff (idb :: IDB | e) Index
150+index store name' =
151+ Fn.runFn2 _index store name'
152+
153+
154+-- | Adds or updates a record in store with the given value and key.
155+-- |
156+-- | If the store uses in-line keys and key is specified a "DataError" DOMException
157+-- | will be thrown.
158+-- |
159+-- | If put() is used, any existing record with the key will be replaced.
160+put
161+ :: forall e val key store. (IDBKey key) => (IDBObjectStore store)
162+ => store
163+ -> val
164+ -> Maybe key
165+ -> Aff (idb :: IDB | e) key
166+put store value key =
167+ Fn.runFn3 _put store value (toNullable $ (toKey >>> toForeign) <$> key)
168+
169+
123170--------------------
124171-- ATTRIBUTES
125172--
@@ -163,38 +210,12 @@ transaction =
163210 _transaction
164211
165212
166−--------------------
167−-- INSTANCES
168−--
169−instance idbObjectStoreObjectStore :: IDBObjectStore ObjectStore where
170− add store value key =
171− Key <$> Fn.runFn3 _add store value (toNullable $ (toKey >>> extractForeign) <$> key)
172−
173− clear =
174− _clear
175−
176− createIndex store name' path params =
177− Fn.runFn4 _createIndex store name' path params
178−
179− delete store range =
180− Fn.runFn2 _delete store range
181−
182− deleteIndex store name' =
183− Fn.runFn2 _deleteIndex store name'
184−
185− index store name' =
186− Fn.runFn2 _index store name'
187−
188− put store value key =
189− Key <$> Fn.runFn3 _put store value (toNullable $ (toKey >>> extractForeign) <$> key)
190−
191−
192213--------------------
193214-- FFI
194215--
195216foreign import _add
196− :: forall value e
197− . Fn3 ObjectStore value (Nullable Foreign) (Aff (idb :: IDB | e) Foreign)
217+ :: forall e key val store. (IDBKey key)
218+ => Fn3 store val (Nullable Foreign) (Aff (idb :: IDB | e) key)
198219
199220
200221foreign import _autoIncrement
@@ -203,29 +224,29 @@ foreign import _autoIncrement
203224
204225
205226foreign import _clear
206− :: forall e
207− . ObjectStore
227+ :: forall e store
228+ . store
208229 -> Aff (idb :: IDB | e) Unit
209230
210231
211232foreign import _createIndex
212− :: forall e
213− . Fn4 ObjectStore String (Array String) { unique :: Boolean, multiEntry :: Boolean } (Aff (idb :: IDB | e) Index)
233+ :: forall e store
234+ . Fn4 store String (Array String) { unique :: Boolean, multiEntry :: Boolean } (Aff (idb :: IDB | e) Index)
214235
215236
216237foreign import _delete
217− :: forall e
218− . Fn2 ObjectStore KeyRange (Aff (idb :: IDB | e) Unit)
238+ :: forall e store
239+ . Fn2 store KeyRange (Aff (idb :: IDB | e) Unit)
219240
220241
221242foreign import _deleteIndex
222− :: forall e
223− . Fn2 ObjectStore String (Aff (idb :: IDB | e) Unit)
243+ :: forall e store
244+ . Fn2 store String (Aff (idb :: IDB | e) Unit)
224245
225246
226247foreign import _index
227− :: forall e
228− . Fn2 ObjectStore String (Aff (idb :: IDB | e) Index)
248+ :: forall e store
249+ . Fn2 store String (Aff (idb :: IDB | e) Index)
229250
230251
231252foreign import _indexNames
@@ -244,8 +265,8 @@ foreign import _name
244265
245266
246267foreign import _put
247− :: forall value e
248− . Fn3 ObjectStore value (Nullable Foreign) (Aff (idb :: IDB | e) Foreign)
268+ :: forall e key val store. (IDBKey key)
269+ => Fn3 store val (Nullable Foreign) (Aff (idb :: IDB | e) key)
249270
250271
251272foreign import _transaction