@@ -31,7 +31,8 @@ import Effect.Exception (name)
31import Effect.Now (now)
31import Effect.Now (now)
32import Test.Spec (describe, it)
32import Test.Spec (describe, it)
33import Test.Spec.Assertions (shouldEqual, fail)
33import Test.Spec.Assertions (shouldEqual, fail)
34−import Test.Spec.Mocha (runMocha)
34+import Test.Spec.Reporter (consoleReporter)
35+import Test.Spec.Runner (runSpec)
36infixr 7 Tuple as :+:
37infixr 7 Tuple as :+:
@@ -45,1096 +46,1096 @@ modifyAVar fn v = do
45 AVar.put (fn val) v
46 AVar.put (fn val) v
47main :: Effect Unit
48main :: Effect Unit
49− runMocha
50+ launchAff_ $ runSpec [ consoleReporter ] $ do
50− do
51+ describe "IDBFactory" do
51− describe "IDBFactory" do
53− tearDown name version db = do
53+ tearDown name version db = do
54− IDBDatabase.close db
54+ IDBDatabase.close db
55− version' <- IDBFactory.deleteDatabase name
55+ version' <- IDBFactory.deleteDatabase name
56− liftEffect $ log $ show version'
56+ liftEffect $ log $ show version'
57− version' `shouldEqual` version
62− version = 1
57+ version' `shouldEqual` version
63− db <- IDBFactory.open name Nothing
64− { onUpgradeNeeded: Nothing
67− IDBDatabase.name db `shouldEqual` name
68− tearDown name version db
63+ db <- IDBFactory.open name Nothing
64+ { onUpgradeNeeded: Nothing
67+ IDBDatabase.name db `shouldEqual` name
68+ tearDown name version db
70− it "open specific version" do
70+ it "open specific version" do
72− name = "db-specific"
72+ name = "db-specific"
73− version = 14
73+ version = 14
74+ db <- IDBFactory.open name (Just version)
75+ { onUpgradeNeeded: Nothing
78+ IDBDatabase.name db `shouldEqual` name
79+ tearDown name version db
81+ it "open specific version -> close -> open latest" do
74− db <- IDBFactory.open name (Just version)
85+ db01 <- IDBFactory.open name (Just version)
75− { onUpgradeNeeded: Nothing
86+ { onUpgradeNeeded: Nothing
76− , onBlocked: Nothing
87+ , onBlocked: Nothing
78− IDBDatabase.name db `shouldEqual` name
89+ IDBDatabase.name db01 `shouldEqual` name
90+ IDBDatabase.close db01
91+ db02 <- IDBFactory.open name Nothing
92+ { onUpgradeNeeded: Nothing
79− tearDown name version db
95+ tearDown name version db02
97+ it "open + onUpgradeNeed" do
99+ name = "db-upgrade-needed"
101+ callback (Tuple varName varVersion) db _ { oldVersion } = do
102+ _ <- launchAff $ modifyAVar (const $ IDBDatabase.name db) varName
103+ _ <- launchAff $ modifyAVar (const $ oldVersion) varVersion
105+ varName <- AVar.new "-"
106+ varVersion <- AVar.new (-1)
107+ db <- IDBFactory.open name Nothing
108+ { onUpgradeNeeded: Just (callback (Tuple varName varVersion))
109+ , onBlocked: Nothing
111+ name' <- AVar.read varName
112+ version' <- AVar.read varVersion
113+ name' `shouldEqual` name
114+ version' `shouldEqual` 0
115+ tearDown name version db
117+ it "open + onBlocked" do
122+ _ <- launchAff $ modifyAVar (const $ "db-blocked") var
81− it "open specific version -> close -> open latest" do
125+ var <- AVar.new "-"
85− db01 <- IDBFactory.open name (Just version)
126+ db01 <- IDBFactory.open name Nothing
86− { onUpgradeNeeded: Nothing
127+ { onUpgradeNeeded: Nothing
87− , onBlocked: Nothing
128+ , onBlocked: Nothing
89− IDBDatabase.name db01 `shouldEqual` name
130+ _ <- forkAff do
131+ delay (Milliseconds 100.0)
90 IDBDatabase.close db01
132 IDBDatabase.close db01
91− db02 <- IDBFactory.open name Nothing
92− { onUpgradeNeeded: Nothing
95− tearDown name version db02
97− it "open + onUpgradeNeed" do
99− name = "db-upgrade-needed"
101− callback (Tuple varName varVersion) db _ { oldVersion } = do
102− _ <- launchAff $ modifyAVar (const $ IDBDatabase.name db) varName
103− _ <- launchAff $ modifyAVar (const $ oldVersion) varVersion
105− varName <- AVar.new "-"
106− varVersion <- AVar.new (-1)
107− db <- IDBFactory.open name Nothing
108− { onUpgradeNeeded: Just (callback (Tuple varName varVersion))
109− , onBlocked: Nothing
111− name' <- AVar.read varName
112− version' <- AVar.read varVersion
113− name' `shouldEqual` name
114− version' `shouldEqual` 0
115− tearDown name version db
117− it "open + onBlocked" do
134+ db02 <- IDBFactory.open name (Just version)
118− let
135+ { onUpgradeNeeded: Nothing
121− callback var = do
136+ , onBlocked: Just (callback var)
122− _ <- launchAff $ modifyAVar (const $ "db-blocked") var
137+ }
123− pure unit
138+ name' <- AVar.read var
139+ name' `shouldEqual` name
140+ tearDown name version db02
125− var <- AVar.new "-"
142+ describe "IDBKeyRange" do
126− db01 <- IDBFactory.open name Nothing
127− { onUpgradeNeeded: Nothing
128− , onBlocked: Nothing
131− delay (Milliseconds 100.0)
132− IDBDatabase.close db01
134− db02 <- IDBFactory.open name (Just version)
135− { onUpgradeNeeded: Nothing
136− , onBlocked: Just (callback var)
138− name' <- AVar.read var
139− name' `shouldEqual` name
140− tearDown name version db02
142− describe "IDBKeyRange" do
143− it "only(int)" do
143+ it "only(int)" do
145− key = 14
145+ key = 14
146− range = IDBKeyRange.only key
147− IDBKeyRange.includes range (toKey key) `shouldEqual` true
149− it "only(string)" do
152− range = IDBKeyRange.only key
146+ range = IDBKeyRange.only key
153− IDBKeyRange.includes range (toKey key) `shouldEqual` true
147+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
155− it "only(float)" do
149+ it "only(string)" do
157− key = 14.42
151+ key = "patate"
158− range = IDBKeyRange.only key
152+ range = IDBKeyRange.only key
159− IDBKeyRange.includes range (toKey key) `shouldEqual` true
153+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
161− it "only(date)" do
155+ it "only(float)" do
164− <$> (Date.canonicalDate <$> toEnum 2017 <*> toEnum 6 <*> toEnum 23)
165− <*> (Time <$> toEnum 17 <*> toEnum 59 <*> toEnum 34 <*> toEnum 42)
169− fail "unable to create datetime"
171− let range = IDBKeyRange.only key
172− IDBKeyRange.includes range (toKey key) `shouldEqual` true
176− key = [ 14, 42 ]
157+ key = 14.42
177− range = IDBKeyRange.only key
158+ range = IDBKeyRange.only key
178− IDBKeyRange.includes range (toKey key) `shouldEqual` true
159+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
164+ <$> (Date.canonicalDate <$> toEnum 2017 <*> toEnum 6 <*> toEnum 23)
165+ <*> (Time <$> toEnum 17 <*> toEnum 59 <*> toEnum 34 <*> toEnum 42)
169+ fail "unable to create datetime"
171+ let range = IDBKeyRange.only key
172+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
180− it "only([string])" do
174+ it "only([int])" do
182− key = [ "patate", "autruche" ]
176+ key = [ 14, 42 ]
183− range = IDBKeyRange.only key
177+ range = IDBKeyRange.only key
184− IDBKeyRange.includes range (toKey key) `shouldEqual` true
178+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
186− it "lowerBound(14, open)" do
180+ it "only([string])" do
190− range = IDBKeyRange.lowerBound key open
191− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
192− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
193− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
195− it "lowerBound(14, close)" do
199− range = IDBKeyRange.lowerBound key open
200− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
201− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
202− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
204− it "upperBound(14, open)" do
208− range = IDBKeyRange.upperBound key open
209− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
210− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
211− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
213− it "upperBound(14, close)" do
217− range = IDBKeyRange.upperBound key open
218− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
219− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
220− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
222− it "bound(42, 14, open, open) => Nothing" do
228− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
229− isNothing mrange `shouldEqual` true
231− it "bound(14, 42, open, open)" do
237− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
240− fail "invalid range provided"
242− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
243− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
244− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
245− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
246− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
247− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
249− it "bound(14, 42, open, close)" do
255− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
258− fail "invalid range provided"
260− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
261− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
262− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
263− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
264− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
265− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
267− it "bound(14, 42, close, open)" do
273− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
276− fail "invalid range provided"
278− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
279− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
280− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
281− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
282− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
283− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
285− it "bound(14, 42, close, close)" do
291− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
294− fail "invalid range provided"
296− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
297− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
298− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
299− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
300− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
301− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
303− it "can access attributes of a range" do
304− let range = IDBKeyRange.lowerBound 14 false
305− IDBKeyRange.lower range `shouldEqual` (Just $ toKey 14)
306− IDBKeyRange.upper range `shouldEqual` none
307− IDBKeyRange.lowerOpen range `shouldEqual` false
308− IDBKeyRange.upperOpen range `shouldEqual` true
310− describe "IDBDatabase" do
312− tearDown db = do
182+ key = [ "patate", "autruche" ]
313− IDBDatabase.close db
183+ range = IDBKeyRange.only key
314− _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
184+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
317− setup storeParams = do
186+ it "lowerBound(14, open)" do
319− onUpgradeNeeded var db _ _ = launchAff' do
188+ key = 14
320− store <- IDBDatabase.createObjectStore db "store" storeParams
189+ open = true
321− _ <- AVar.put { db, store } var
190+ range = IDBKeyRange.lowerBound key open
191+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
192+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
193+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
195+ it "lowerBound(14, close)" do
199+ range = IDBKeyRange.lowerBound key open
200+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
201+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
202+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
204+ it "upperBound(14, open)" do
208+ range = IDBKeyRange.upperBound key open
209+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
210+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
211+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
213+ it "upperBound(14, close)" do
217+ range = IDBKeyRange.upperBound key open
218+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
219+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
220+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
222+ it "bound(42, 14, open, open) => Nothing" do
228+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
229+ isNothing mrange `shouldEqual` true
231+ it "bound(14, 42, open, open)" do
237+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
240+ fail "invalid range provided"
242+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
243+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
244+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
245+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
246+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
247+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
249+ it "bound(14, 42, open, close)" do
255+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
258+ fail "invalid range provided"
260+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
261+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
262+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
263+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
264+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
265+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
267+ it "bound(14, 42, close, open)" do
273+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
276+ fail "invalid range provided"
278+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
279+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
280+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
281+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
282+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
283+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
285+ it "bound(14, 42, close, close)" do
291+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
294+ fail "invalid range provided"
296+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
297+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
298+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
299+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
300+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
301+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
303+ it "can access attributes of a range" do
304+ let range = IDBKeyRange.lowerBound 14 false
305+ IDBKeyRange.lower range `shouldEqual` (Just $ toKey 14)
306+ IDBKeyRange.upper range `shouldEqual` none
307+ IDBKeyRange.lowerOpen range `shouldEqual` false
308+ IDBKeyRange.upperOpen range `shouldEqual` true
310+ describe "IDBDatabase" do
313+ IDBDatabase.close db
314+ _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
322− pure unit
315+ pure unit
325− _ <- IDBFactory.open "db" Nothing
326− { onUpgradeNeeded: Just (onUpgradeNeeded var)
327− , onBlocked: Nothing
330− AVar.take var
317+ setup storeParams = do
332− it "createObjectStore (keyPath: [], autoIncrement: true)" do
333− { db, store } <- setup { keyPath: [], autoIncrement: true }
334− IDBObjectStore.name store `shouldEqual` "store"
335− IDBObjectStore.keyPath store `shouldEqual` []
336− IDBObjectStore.autoIncrement store `shouldEqual` true
337− IDBObjectStore.indexNames store `shouldEqual` []
340− it "createObjectStore (keyPath: [\"patate\"], autoIncrement: true)" do
341− { db, store } <- setup { keyPath: [ "patate" ], autoIncrement: true }
342− IDBObjectStore.name store `shouldEqual` "store"
343− IDBObjectStore.keyPath store `shouldEqual` [ "patate" ]
344− IDBObjectStore.autoIncrement store `shouldEqual` true
345− IDBObjectStore.indexNames store `shouldEqual` []
348− it "createObjectStore (keyPath: [\"a\", \"b\"], autoIncrement: false)" do
349− { db, store } <- setup { keyPath: [ "patate", "autruche" ], autoIncrement: false }
350− IDBObjectStore.name store `shouldEqual` "store"
351− IDBObjectStore.keyPath store `shouldEqual` [ "patate", "autruche" ]
352− IDBObjectStore.autoIncrement store `shouldEqual` false
353− IDBObjectStore.indexNames store `shouldEqual` []
356− it "deleteObjectStore" do
358 onUpgradeNeeded var db _ _ = launchAff' do
319 onUpgradeNeeded var db _ _ = launchAff' do
359− _ <- IDBDatabase.deleteObjectStore db "store"
320+ store <- IDBDatabase.createObjectStore db "store" storeParams
360− AVar.put true var
321+ _ <- AVar.put { db, store } var
362 var <- AVar.empty
324 var <- AVar.empty
363− { db } <- setup IDBDatabase.defaultParameters
364− IDBDatabase.close db
365− db' <- IDBFactory.open "db" (Just 999)
325+ _ <- IDBFactory.open "db" Nothing
366 { onUpgradeNeeded: Just (onUpgradeNeeded var)
326 { onUpgradeNeeded: Just (onUpgradeNeeded var)
367 , onBlocked: Nothing
327 , onBlocked: Nothing
369− deleted <- AVar.take var
370− deleted `shouldEqual` true
332+ it "createObjectStore (keyPath: [], autoIncrement: true)" do
333+ { db, store } <- setup { keyPath: [], autoIncrement: true }
334+ IDBObjectStore.name store `shouldEqual` "store"
335+ IDBObjectStore.keyPath store `shouldEqual` []
336+ IDBObjectStore.autoIncrement store `shouldEqual` true
337+ IDBObjectStore.indexNames store `shouldEqual` []
340+ it "createObjectStore (keyPath: [\"patate\"], autoIncrement: true)" do
341+ { db, store } <- setup { keyPath: [ "patate" ], autoIncrement: true }
342+ IDBObjectStore.name store `shouldEqual` "store"
343+ IDBObjectStore.keyPath store `shouldEqual` [ "patate" ]
344+ IDBObjectStore.autoIncrement store `shouldEqual` true
345+ IDBObjectStore.indexNames store `shouldEqual` []
348+ it "createObjectStore (keyPath: [\"a\", \"b\"], autoIncrement: false)" do
349+ { db, store } <- setup { keyPath: [ "patate", "autruche" ], autoIncrement: false }
350+ IDBObjectStore.name store `shouldEqual` "store"
351+ IDBObjectStore.keyPath store `shouldEqual` [ "patate", "autruche" ]
352+ IDBObjectStore.autoIncrement store `shouldEqual` false
373− describe "IDBObjectStore" do
353+ IDBObjectStore.indexNames store `shouldEqual` []
356+ it "deleteObjectStore" do
376− IDBDatabase.close db
377− _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
380− setup { storeParams, onUpgradeNeeded } = do
382− onUpgradeNeeded' var db _ _ = launchAff' do
358+ onUpgradeNeeded var db _ _ = launchAff' do
383− store <- IDBDatabase.createObjectStore db "store" storeParams
359+ _ <- IDBDatabase.deleteObjectStore db "store"
384− liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure store)
385− AVar.put { db, store } var
360+ AVar.put true var
387− var <- AVar.empty
362+ var <- AVar.empty
363+ { db } <- setup IDBDatabase.defaultParameters
364+ IDBDatabase.close db
388− _ <- IDBFactory.open "db" Nothing
365+ db' <- IDBFactory.open "db" (Just 999)
389− { onUpgradeNeeded: Just (onUpgradeNeeded' var)
366+ { onUpgradeNeeded: Just (onUpgradeNeeded var)
390− , onBlocked: Nothing
367+ , onBlocked: Nothing
393− AVar.take var
369+ deleted <- AVar.take var
394−
370+ deleted `shouldEqual` true
395− it "add()" do
371+ tearDown db'
396− date <- liftEffect $ toDateTime <$> now
372+
398− { storeParams: { autoIncrement: true, keyPath: [] }
399− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
401− key <- IDBObjectStore.add store "patate" none
402− (toKey 1) `shouldEqual` key
405− key' <- IDBObjectStore.add store "patate" (Just 14)
373+ describe "IDBObjectStore" do
406− (toKey 14) `shouldEqual` key'
374+ let
407−
375+ tearDown db = do
408− -- number key
376+ IDBDatabase.close db
409− key'' <- IDBObjectStore.add store "patate" (Just 14.42)
377+ _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
410− (toKey 14.42) `shouldEqual` key''
378+ pure unit
413− key''' <- IDBObjectStore.add store "patate" (Just "key")
414− (toKey "key") `shouldEqual` key'''
416− -- date key
380+ setup { storeParams, onUpgradeNeeded } = do
417− key'''' <- IDBObjectStore.add store "patate" (Just date)
381+ let
418− (toKey date) `shouldEqual` key''''
382+ onUpgradeNeeded' var db _ _ = launchAff' do
383+ store <- IDBDatabase.createObjectStore db "store" storeParams
384+ liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure store)
385+ AVar.put { db, store } var
420− -- array key
387+ var <- AVar.empty
421− key''''' <- IDBObjectStore.add store "patate" (Just $ toKey [ 14, 42 ])
388+ _ <- IDBFactory.open "db" Nothing
422− (toKey [ 14, 42 ]) `shouldEqual` key'''''
428− { storeParams: IDBDatabase.defaultParameters
429− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
430− key <- IDBObjectStore.add store "patate" (Just 14)
431− _ <- IDBObjectStore.clear store
432− val <- IDBObjectStore.get store (IDBKeyRange.only key)
433− val `shouldEqual` (Nothing :: Maybe String)
439− { storeParams: IDBDatabase.defaultParameters
440− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
389+ { onUpgradeNeeded: Just (onUpgradeNeeded' var)
441− _ <- IDBObjectStore.add store "patate" (Just 14)
390+ , onBlocked: Nothing
442− _ <- IDBObjectStore.add store "autruche" (Just 42)
443− n <- IDBObjectStore.count store Nothing
450− { storeParams: IDBDatabase.defaultParameters
451− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
452− key <- IDBObjectStore.add store "patate" (Just 14)
453− mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14)
454− mkey `shouldEqual` (Just key)
456− mkey' <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
457− mkey' `shouldEqual` none
461− it "getAllKeys()" do
463− { storeParams: IDBDatabase.defaultParameters
464− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
465− key1 <- IDBObjectStore.add store "patate" (Just 14)
466− key2 <- IDBObjectStore.add store "autruche" (Just 42)
467− key3 <- IDBObjectStore.add store 14 (Just 1337)
470− keys <- IDBObjectStore.getAllKeys store Nothing Nothing
471− keys `shouldEqual` [ key1, key2, key3 ]
474− keys' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 14 true) Nothing
475− keys' `shouldEqual` [ key2, key3 ]
478− keys'' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.upperBound 42 false) Nothing
479− keys'' `shouldEqual` [ key1, key2 ]
482− keys''' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
483− keys''' `shouldEqual` [ key1, key2 ]
487− it "openCursor()" do
393+ AVar.take var
396+ date <- liftEffect $ toDateTime <$> now
398+ { storeParams: { autoIncrement: true, keyPath: [] }
399+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
401+ key <- IDBObjectStore.add store "patate" none
402+ (toKey 1) `shouldEqual` key
405+ key' <- IDBObjectStore.add store "patate" (Just 14)
406+ (toKey 14) `shouldEqual` key'
409+ key'' <- IDBObjectStore.add store "patate" (Just 14.42)
410+ (toKey 14.42) `shouldEqual` key''
413+ key''' <- IDBObjectStore.add store "patate" (Just "key")
414+ (toKey "key") `shouldEqual` key'''
417+ key'''' <- IDBObjectStore.add store "patate" (Just date)
418+ (toKey date) `shouldEqual` key''''
421+ key''''' <- IDBObjectStore.add store "patate" (Just $ toKey [ 14, 42 ])
422+ (toKey [ 14, 42 ]) `shouldEqual` key'''''
428+ { storeParams: IDBDatabase.defaultParameters
429+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
430+ key <- IDBObjectStore.add store "patate" (Just 14)
431+ _ <- IDBObjectStore.clear store
432+ val <- IDBObjectStore.get store (IDBKeyRange.only key)
433+ val `shouldEqual` (Nothing :: Maybe String)
439+ { storeParams: IDBDatabase.defaultParameters
440+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
441+ _ <- IDBObjectStore.add store "patate" (Just 14)
442+ _ <- IDBObjectStore.add store "autruche" (Just 42)
443+ n <- IDBObjectStore.count store Nothing
450+ { storeParams: IDBDatabase.defaultParameters
451+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
452+ key <- IDBObjectStore.add store "patate" (Just 14)
453+ mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14)
454+ mkey `shouldEqual` (Just key)
456+ mkey' <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
457+ mkey' `shouldEqual` none
461+ it "getAllKeys()" do
463+ { storeParams: IDBDatabase.defaultParameters
464+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
465+ key1 <- IDBObjectStore.add store "patate" (Just 14)
466+ key2 <- IDBObjectStore.add store "autruche" (Just 42)
467+ key3 <- IDBObjectStore.add store 14 (Just 1337)
470+ keys <- IDBObjectStore.getAllKeys store Nothing Nothing
471+ keys `shouldEqual` [ key1, key2, key3 ]
474+ keys' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 14 true) Nothing
475+ keys' `shouldEqual` [ key2, key3 ]
478+ keys'' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.upperBound 42 false) Nothing
479+ keys'' `shouldEqual` [ key1, key2 ]
482+ keys''' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
483+ keys''' `shouldEqual` [ key1, key2 ]
487+ it "openCursor()" do
490− { onSuccess: const $ pure unit
490+ { onSuccess: const $ pure unit
491− , onError: show >>> fail >>> launchAff'
491+ , onError: show >>> fail >>> launchAff'
492− , onComplete: pure unit
492+ , onComplete: pure unit
495− { storeParams: IDBDatabase.defaultParameters
496− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
497− _ <- IDBObjectStore.openCursor store Nothing Next cb
498− _ <- IDBObjectStore.openCursor store Nothing NextUnique cb
499− _ <- IDBObjectStore.openCursor store Nothing Prev cb
500− _ <- IDBObjectStore.openCursor store Nothing PrevUnique cb
501− _ <- IDBObjectStore.openCursor store (Just $ IDBKeyRange.upperBound 1 true) Next cb
495+ { storeParams: IDBDatabase.defaultParameters
496+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
497+ _ <- IDBObjectStore.openCursor store Nothing Next cb
498+ _ <- IDBObjectStore.openCursor store Nothing NextUnique cb
499+ _ <- IDBObjectStore.openCursor store Nothing Prev cb
500+ _ <- IDBObjectStore.openCursor store Nothing PrevUnique cb
501+ _ <- IDBObjectStore.openCursor store (Just $ IDBKeyRange.upperBound 1 true) Next cb
504− tearDown db
504+ tearDown db
506− it "openKeyCursor()" do
506+ it "openKeyCursor()" do
509− { onSuccess: const $ pure unit
509+ { onSuccess: const $ pure unit
510− , onError: show >>> fail >>> launchAff'
510+ , onError: show >>> fail >>> launchAff'
511− , onComplete: pure unit
511+ , onComplete: pure unit
514− { storeParams: IDBDatabase.defaultParameters
515− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
516− _ <- IDBObjectStore.openKeyCursor store Nothing Next cb
517− _ <- IDBObjectStore.openKeyCursor store Nothing NextUnique cb
518− _ <- IDBObjectStore.openKeyCursor store Nothing Prev cb
519− _ <- IDBObjectStore.openKeyCursor store Nothing PrevUnique cb
520− _ <- IDBObjectStore.openKeyCursor store (Just $ IDBKeyRange.lowerBound 1 true) Next cb
514+ { storeParams: IDBDatabase.defaultParameters
515+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
516+ _ <- IDBObjectStore.openKeyCursor store Nothing Next cb
517+ _ <- IDBObjectStore.openKeyCursor store Nothing NextUnique cb
518+ _ <- IDBObjectStore.openKeyCursor store Nothing Prev cb
519+ _ <- IDBObjectStore.openKeyCursor store Nothing PrevUnique cb
520+ _ <- IDBObjectStore.openKeyCursor store (Just $ IDBKeyRange.lowerBound 1 true) Next cb
523− tearDown db
523+ tearDown db
525− describe "IDBIndex" do
525+ describe "IDBIndex" do
527− tearDown db = do
527+ tearDown db = do
528− IDBDatabase.close db
528+ IDBDatabase.close db
529− _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
529+ _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
530− pure unit
530+ pure unit
534+ . { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
535+ , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
536+ , values :: Array (Tuple value (Maybe Key))
537+ , keyPath :: Array String
538+ , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Effect Unit)
540+ -> Aff { db :: Database, index :: Index, store :: ObjectStore }
541+ setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
543+ onUpgradeNeeded' var db tx _ = launchAff' do
544+ store <- IDBDatabase.createObjectStore db "store" storeParams
545+ _ <- traverse (uncurry (IDBObjectStore.add store)) values
546+ index <- IDBObjectStore.createIndex store "index" keyPath indexParams
547+ liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
548+ AVar.put { db, index, store } var
534− . { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
535− , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
536− , values :: Array (Tuple value (Maybe Key))
537− , keyPath :: Array String
538− , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Effect Unit)
540− -> Aff { db :: Database, index :: Index, store :: ObjectStore }
541− setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
543− onUpgradeNeeded' var db tx _ = launchAff' do
544− store <- IDBDatabase.createObjectStore db "store" storeParams
545− _ <- traverse (uncurry (IDBObjectStore.add store)) values
546− index <- IDBObjectStore.createIndex store "index" keyPath indexParams
547− liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
548− AVar.put { db, index, store } var
550− var <- AVar.empty
550+ var <- AVar.empty
551− _ <- IDBFactory.open "db" Nothing
551+ _ <- IDBFactory.open "db" Nothing
552− { onUpgradeNeeded: Just (onUpgradeNeeded' var)
552+ { onUpgradeNeeded: Just (onUpgradeNeeded' var)
553− , onBlocked: Nothing
553+ , onBlocked: Nothing
557− it "returns an IDBIndex and the properties are set correctly" do
558− { db, index } <- setup
559− { storeParams: IDBDatabase.defaultParameters
560− , indexParams: IDBObjectStore.defaultParameters
561− , onUpgradeNeeded: Nothing
565− IDBIndex.name index `shouldEqual` "index"
566− IDBIndex.keyPath index `shouldEqual` []
567− IDBIndex.unique index `shouldEqual` IDBObjectStore.defaultParameters.unique
568− IDBIndex.multiEntry index `shouldEqual` IDBObjectStore.defaultParameters.multiEntry
571− it "attempt to create an index that requires unique values on an object store already contains duplicates" do
572− let onAbort var = launchAff' (AVar.put true var)
575− res <- attempt $ setup
576− { storeParams: IDBDatabase.defaultParameters
581− , keyPath: [ "indexedProperty" ]
583− [ { indexedProperty: "bar" } :+: (Just $ toKey 1)
584− , { indexedProperty: "bar" } :+: (Just $ toKey 2)
586− , onUpgradeNeeded: Just $ \db tx _ -> launchAff' do
587− IDBTransaction.onAbort tx (onAbort txVar)
588− IDBDatabase.onAbort db (onAbort dbVar)
592− fail "expected abort"
594− shouldEqual true =<< AVar.take txVar
595− shouldEqual true =<< AVar.take dbVar
597− it "the index is usable right after being made" do
600− { keyPath: [ "key" ]
601− , autoIncrement: false
603− , indexParams: IDBObjectStore.defaultParameters
604− , keyPath: [ "indexedProperty" ]
606− [ { key: "key1", indexedProperty: "indexed_1" } :+: Nothing
607− , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing
608− , { key: "key3", indexedProperty: "indexed_3" } :+: Nothing
610− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
611− val <- IDBIndex.get index (IDBKeyRange.only "indexed_2")
612− ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2")
616− it "empty keyPath" do
618− { storeParams: IDBDatabase.defaultParameters
619− , indexParams: IDBObjectStore.defaultParameters
622− [ "object_1" :+: (Just $ toKey 1)
623− , "object_2" :+: (Just $ toKey 2)
624− , "object_3" :+: (Just $ toKey 3)
626− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
627− val <- IDBIndex.get index (IDBKeyRange.only "object_3")
628− val `shouldEqual` (Just "object_3")
632− it "index can be valid keys [date]" do
633− date <- liftEffect $ toDateTime <$> now
636− { keyPath: [ "key" ]
637− , autoIncrement: false
639− , indexParams: IDBObjectStore.defaultParameters
642− [ { key: "date", i: (toKey date) } :+: Nothing
644− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
645− val <- IDBIndex.get index (IDBKeyRange.only date)
646− ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
650− it "index can be valid keys [num]" do
654− { keyPath: [ "key" ]
655− , autoIncrement: false
657− , indexParams: IDBObjectStore.defaultParameters
660− [ { key: "num", i: (toKey num) } :+: Nothing
662− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
663− val <- IDBIndex.get index (IDBKeyRange.only num)
664− ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
668− it "index can be valid keys [array]" do
669− let array = [ "patate", "autruche" ]
672− { keyPath: [ "key" ]
673− , autoIncrement: false
675− , indexParams: IDBObjectStore.defaultParameters
678− [ { key: "array", i: (toKey array) } :+: Nothing
680− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
681− val <- IDBIndex.get index (IDBKeyRange.only array)
682− ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
686− it "openKeyCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
687− res <- attempt $ setup
688− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
689− , indexParams: IDBObjectStore.defaultParameters
690− , keyPath: [ "indexedProperty" ]
692− [ { key: 14, indexedProperty: "patate" } :+: Nothing
694− , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
697− { onSuccess: const $ pure unit
698− , onError: show >>> fail >>> launchAff'
699− , onComplete: pure unit
701− IDBTransaction.onAbort tx (pure unit)
702− IDBDatabase.onAbort db (pure unit)
703− IDBTransaction.abort tx
704− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
706− Right _ -> fail "expected InvalidStateError"
707− Left err -> name err `shouldEqual` "InvalidStateError"
710− Right _ -> fail "expected InvalidStateError"
713− it "openKeyCursor() - throw TransactionInactiveError on aborted transaction" do
715− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
716− , indexParams: IDBObjectStore.defaultParameters
717− , keyPath: [ "indexedProperty" ]
719− [ { key: 14, indexedProperty: "patate" } :+: Nothing
721− , onUpgradeNeeded: Nothing
723− let
555+ AVar.take var
725− { onSuccess: const $ pure unit
557+ it "returns an IDBIndex and the properties are set correctly" do
726− , onError: show >>> fail >>> launchAff'
558+ { db, index } <- setup
727− , onComplete: pure unit
559+ { storeParams: IDBDatabase.defaultParameters
560+ , indexParams: IDBObjectStore.defaultParameters
561+ , onUpgradeNeeded: Nothing
565+ IDBIndex.name index `shouldEqual` "index"
566+ IDBIndex.keyPath index `shouldEqual` []
567+ IDBIndex.unique index `shouldEqual` IDBObjectStore.defaultParameters.unique
568+ IDBIndex.multiEntry index `shouldEqual` IDBObjectStore.defaultParameters.multiEntry
571+ it "attempt to create an index that requires unique values on an object store already contains duplicates" do
572+ let onAbort var = launchAff' (AVar.put true var)
575+ res <- attempt $ setup
576+ { storeParams: IDBDatabase.defaultParameters
729− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
581+ , keyPath: [ "indexedProperty" ]
730− store <- IDBTransaction.objectStore tx "store"
582+ , values:
731− index <- IDBObjectStore.index store "index"
583+ [ { indexedProperty: "bar" } :+: (Just $ toKey 1)
732− IDBTransaction.onAbort tx (pure unit)
584+ , { indexedProperty: "bar" } :+: (Just $ toKey 2)
733− IDBTransaction.abort tx
585+ ]
734− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
586+ , onUpgradeNeeded: Just $ \db tx _ -> launchAff' do
587+ IDBTransaction.onAbort tx (onAbort txVar)
588+ IDBDatabase.onAbort db (onAbort dbVar)
735− case cursor of
590+ case res of
736− Right _ -> fail "expected TransactionInactiveError"
592+ fail "expected abort"
737− Left err -> name err `shouldEqual` "TransactionInactiveError"
593+ _ -> do
738−
594+ shouldEqual true =<< AVar.take txVar
739− tearDown db
595+ shouldEqual true =<< AVar.take dbVar
741− it "openKeyCursor() - throw InvalidStateError when the index is deleted" do
597+ it "the index is usable right after being made" do
742− { db } <- setup
598+ { db } <- setup
600+ { keyPath: [ "key" ]
743− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
601+ , autoIncrement: false
744− , indexParams: IDBObjectStore.defaultParameters
603+ , indexParams: IDBObjectStore.defaultParameters
745− , keyPath: [ "indexedProperty" ]
604+ , keyPath: [ "indexedProperty" ]
746− , values:
605+ , values:
747− [ { key: 14, indexedProperty: "patate" } :+: Nothing
606+ [ { key: "key1", indexedProperty: "indexed_1" } :+: Nothing
748− ]
607+ , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing
749− , onUpgradeNeeded: Just $ \_ tx index -> launchAff' do
608+ , { key: "key3", indexedProperty: "indexed_3" } :+: Nothing
751− cb =
610+ , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
752− { onSuccess: const $ pure unit
611+ val <- IDBIndex.get index (IDBKeyRange.only "indexed_2")
753− , onError: show >>> fail >>> launchAff'
612+ ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2")
754− , onComplete: pure unit
756− store <- IDBTransaction.objectStore tx "store"
757− IDBObjectStore.deleteIndex store "index"
758− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
760− Right _ -> fail "expected InvalidStateError"
761− Left err -> name err `shouldEqual` "InvalidStateError"
764− tearDown db
614+ tearDown db
616+ it "empty keyPath" do
618+ { storeParams: IDBDatabase.defaultParameters
619+ , indexParams: IDBObjectStore.defaultParameters
622+ [ "object_1" :+: (Just $ toKey 1)
623+ , "object_2" :+: (Just $ toKey 2)
624+ , "object_3" :+: (Just $ toKey 3)
626+ , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
627+ val <- IDBIndex.get index (IDBKeyRange.only "object_3")
628+ val `shouldEqual` (Just "object_3")
632+ it "index can be valid keys [date]" do
633+ date <- liftEffect $ toDateTime <$> now
636+ { keyPath: [ "key" ]
637+ , autoIncrement: false
639+ , indexParams: IDBObjectStore.defaultParameters
642+ [ { key: "date", i: (toKey date) } :+: Nothing
644+ , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
645+ val <- IDBIndex.get index (IDBKeyRange.only date)
646+ ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
650+ it "index can be valid keys [num]" do
654+ { keyPath: [ "key" ]
655+ , autoIncrement: false
657+ , indexParams: IDBObjectStore.defaultParameters
660+ [ { key: "num", i: (toKey num) } :+: Nothing
662+ , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
663+ val <- IDBIndex.get index (IDBKeyRange.only num)
664+ ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
668+ it "index can be valid keys [array]" do
669+ let array = [ "patate", "autruche" ]
672+ { keyPath: [ "key" ]
673+ , autoIncrement: false
675+ , indexParams: IDBObjectStore.defaultParameters
678+ [ { key: "array", i: (toKey array) } :+: Nothing
680+ , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
681+ val <- IDBIndex.get index (IDBKeyRange.only array)
682+ ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
766− it "openCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
686+ it "openKeyCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
767− res <- attempt $ setup
687+ res <- attempt $ setup
768− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
688+ { storeParams: { keyPath: [ "key" ], autoIncrement: false }
769− , indexParams: IDBObjectStore.defaultParameters
689+ , indexParams: IDBObjectStore.defaultParameters
770− , keyPath: [ "indexedProperty" ]
690+ , keyPath: [ "indexedProperty" ]
771− , values:
691+ , values:
772− [ { key: 14, indexedProperty: "patate" } :+: Nothing
692+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
774− , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
694+ , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
777− { onSuccess: const $ pure unit
697+ { onSuccess: const $ pure unit
778− , onError: show >>> fail >>> launchAff'
698+ , onError: show >>> fail >>> launchAff'
779− , onComplete: pure unit
699+ , onComplete: pure unit
781− IDBTransaction.onAbort tx (pure unit)
701+ IDBTransaction.onAbort tx (pure unit)
782− IDBDatabase.onAbort db (pure unit)
702+ IDBDatabase.onAbort db (pure unit)
783− IDBTransaction.abort tx
703+ IDBTransaction.abort tx
784− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
704+ cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
785− case cursor of
705+ case cursor of
786− Right _ -> fail "expected InvalidStateError"
706+ Right _ -> fail "expected InvalidStateError"
787− Left err -> name err `shouldEqual` "InvalidStateError"
707+ Left err -> name err `shouldEqual` "InvalidStateError"
710+ Right _ -> fail "expected InvalidStateError"
713+ it "openKeyCursor() - throw TransactionInactiveError on aborted transaction" do
715+ { storeParams: { keyPath: [ "key" ], autoIncrement: false }
716+ , indexParams: IDBObjectStore.defaultParameters
717+ , keyPath: [ "indexedProperty" ]
719+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
721+ , onUpgradeNeeded: Nothing
725+ { onSuccess: const $ pure unit
726+ , onError: show >>> fail >>> launchAff'
727+ , onComplete: pure unit
729+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
730+ store <- IDBTransaction.objectStore tx "store"
731+ index <- IDBObjectStore.index store "index"
732+ IDBTransaction.onAbort tx (pure unit)
733+ IDBTransaction.abort tx
734+ cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
736+ Right _ -> fail "expected TransactionInactiveError"
737+ Left err -> name err `shouldEqual` "TransactionInactiveError"
741+ it "openKeyCursor() - throw InvalidStateError when the index is deleted" do
743+ { storeParams: { keyPath: [ "key" ], autoIncrement: false }
744+ , indexParams: IDBObjectStore.defaultParameters
745+ , keyPath: [ "indexedProperty" ]
747+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
749+ , onUpgradeNeeded: Just $ \_ tx index -> launchAff' do
752+ { onSuccess: const $ pure unit
753+ , onError: show >>> fail >>> launchAff'
754+ , onComplete: pure unit
756+ store <- IDBTransaction.objectStore tx "store"
757+ IDBObjectStore.deleteIndex store "index"
758+ cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
760+ Right _ -> fail "expected InvalidStateError"
761+ Left err -> name err `shouldEqual` "InvalidStateError"
766+ it "openCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
767+ res <- attempt $ setup
768+ { storeParams: { keyPath: [ "key" ], autoIncrement: false }
769+ , indexParams: IDBObjectStore.defaultParameters
770+ , keyPath: [ "indexedProperty" ]
772+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
774+ , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
777+ { onSuccess: const $ pure unit
778+ , onError: show >>> fail >>> launchAff'
779+ , onComplete: pure unit
781+ IDBTransaction.onAbort tx (pure unit)
782+ IDBDatabase.onAbort db (pure unit)
783+ IDBTransaction.abort tx
784+ cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
786+ Right _ -> fail "expected InvalidStateError"
787+ Left err -> name err `shouldEqual` "InvalidStateError"
789− case res of
789+ case res of
790− Right _ -> fail "expected InvalidStateError"
790+ Right _ -> fail "expected InvalidStateError"
791− _ -> pure unit
791+ _ -> pure unit
793− it "openCursor() - throw TransactionInactiveError on aborted transaction" do
793+ it "openCursor() - throw TransactionInactiveError on aborted transaction" do
794− { db } <- setup
794+ { db } <- setup
795− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
795+ { storeParams: { keyPath: [ "key" ], autoIncrement: false }
796− , indexParams: IDBObjectStore.defaultParameters
796+ , indexParams: IDBObjectStore.defaultParameters
797− , keyPath: [ "indexedProperty" ]
797+ , keyPath: [ "indexedProperty" ]
798− , values:
798+ , values:
799− [ { key: 14, indexedProperty: "patate" } :+: Nothing
799+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
801− , onUpgradeNeeded: Nothing
801+ , onUpgradeNeeded: Nothing
805+ { onSuccess: const $ pure unit
806+ , onError: show >>> fail >>> launchAff'
807+ , onComplete: pure unit
809+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
810+ store <- IDBTransaction.objectStore tx "store"
811+ index <- IDBObjectStore.index store "index"
812+ IDBTransaction.onAbort tx (pure unit)
813+ IDBTransaction.abort tx
814+ cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
816+ Right _ -> fail "expected TransactionInactiveError"
817+ Left err -> name err `shouldEqual` "TransactionInactiveError"
821+ it "openCursor() - throw InvalidStateError when the index is deleted" do
823+ { storeParams: { keyPath: [ "key" ], autoIncrement: false }
824+ , indexParams: IDBObjectStore.defaultParameters
825+ , keyPath: [ "indexedProperty" ]
827+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
829+ , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
832+ { onSuccess: const $ pure unit
833+ , onError: show >>> fail >>> launchAff'
834+ , onComplete: pure unit
836+ store <- IDBTransaction.objectStore tx "store"
837+ IDBObjectStore.deleteIndex store "index"
838+ cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
840+ Right _ -> fail "expected InvalidStateError"
841+ Left err -> name err `shouldEqual` "InvalidStateError"
846+ it "getKey() - multiEntry - adding keys" do
848+ { storeParams: IDBDatabase.defaultParameters
849+ , indexParams: { unique: false, multiEntry: true }
850+ , keyPath: [ "name" ]
852+ [ { name: [ "patate", "autruche" ] } :+: (Just $ toKey 1)
853+ , { name: [ "bob" ] } :+: (Just $ toKey 2)
855+ , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
856+ key <- IDBIndex.getKey index (IDBKeyRange.only "patate")
857+ key `shouldEqual` (Just $ toKey 1)
859+ key' <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
860+ key' `shouldEqual` (Just $ toKey 1)
862+ key'' <- IDBIndex.getKey index (IDBKeyRange.only "bob")
863+ key'' `shouldEqual` (Just $ toKey 2)
867+ it "get() - returns the record with the first key in the range" do
869+ { storeParams: { keyPath: [ "key" ], autoIncrement: false }
870+ , indexParams: IDBObjectStore.defaultParameters
871+ , keyPath: [ "indexedProperty" ]
873+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
874+ , { key: 42, indexedProperty: "autruche" } :+: Nothing
875+ , { key: 1337, indexedProperty: "baguette" } :+: Nothing
877+ , onUpgradeNeeded: Nothing
880+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
881+ store <- IDBTransaction.objectStore tx "store"
882+ index <- IDBObjectStore.index store "index"
883+ val <- IDBIndex.get index (IDBKeyRange.lowerBound "autruche" false)
884+ ((\r -> r.key) <$> val) `shouldEqual` (Just 42)
888+ describe "IDBCursor" do
891+ IDBDatabase.close db
892+ _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
897+ . { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
898+ , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
899+ , values :: Array (Tuple value (Maybe Key))
900+ , keyPath :: Array String
901+ , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Effect Unit)
903+ -> Aff { db :: Database, index :: Index, store :: ObjectStore }
904+ setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
804− cb =
906+ onUpgradeNeeded' var db tx _ = launchAff' do
805− { onSuccess: const $ pure unit
907+ store <- IDBDatabase.createObjectStore db "store" storeParams
806− , onError: show >>> fail >>> launchAff'
908+ _ <- traverse (uncurry (IDBObjectStore.add store)) values
807− , onComplete: pure unit
909+ index <- IDBObjectStore.createIndex store "index" keyPath indexParams
808− }
910+ liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
809− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
911+ AVar.put { db, index, store } var
810− store <- IDBTransaction.objectStore tx "store"
811− index <- IDBObjectStore.index store "index"
812− IDBTransaction.onAbort tx (pure unit)
813− IDBTransaction.abort tx
814− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
816− Right _ -> fail "expected TransactionInactiveError"
817− Left err -> name err `shouldEqual` "TransactionInactiveError"
821− it "openCursor() - throw InvalidStateError when the index is deleted" do
823− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
824− , indexParams: IDBObjectStore.defaultParameters
825− , keyPath: [ "indexedProperty" ]
827− [ { key: 14, indexedProperty: "patate" } :+: Nothing
829− , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
832− { onSuccess: const $ pure unit
833− , onError: show >>> fail >>> launchAff'
834− , onComplete: pure unit
836− store <- IDBTransaction.objectStore tx "store"
837− IDBObjectStore.deleteIndex store "index"
838− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
840− Right _ -> fail "expected InvalidStateError"
841− Left err -> name err `shouldEqual` "InvalidStateError"
844− tearDown db
913+ var <- AVar.empty
845−
914+ db <- IDBFactory.open "db" Nothing
846− it "getKey() - multiEntry - adding keys" do
915+ { onUpgradeNeeded: Just (onUpgradeNeeded' var)
847− { db } <- setup
916+ , onBlocked: Nothing
848− { storeParams: IDBDatabase.defaultParameters
849− , indexParams: { unique: false, multiEntry: true }
850− , keyPath: [ "name" ]
852− [ { name: [ "patate", "autruche" ] } :+: (Just $ toKey 1)
853− , { name: [ "bob" ] } :+: (Just $ toKey 2)
855− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
856− key <- IDBIndex.getKey index (IDBKeyRange.only "patate")
857− key `shouldEqual` (Just $ toKey 1)
859− key' <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
860− key' `shouldEqual` (Just $ toKey 1)
862− key'' <- IDBIndex.getKey index (IDBKeyRange.only "bob")
863− key'' `shouldEqual` (Just $ toKey 2)
865− tearDown db
918+ AVar.take var
867− it "get() - returns the record with the first key in the range" do
920+ it "continue() - iterate to the next record" do
868− { db } <- setup
921+ { db } <- setup
869− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
922+ { storeParams: IDBDatabase.defaultParameters
870− , indexParams: IDBObjectStore.defaultParameters
923+ , indexParams: IDBObjectStore.defaultParameters
871− , keyPath: [ "indexedProperty" ]
924+ , keyPath: []
872− , values:
925+ , values:
873− [ { key: 14, indexedProperty: "patate" } :+: Nothing
926+ [ "cupcake" :+: (Just $ toKey 4)
874− , { key: 42, indexedProperty: "autruche" } :+: Nothing
927+ , "pancake" :+: (Just $ toKey 2)
875− , { key: 1337, indexedProperty: "baguette" } :+: Nothing
928+ , "pie" :+: (Just $ toKey 1)
929+ , "pie" :+: (Just $ toKey 3)
877− , onUpgradeNeeded: Nothing
931+ , onUpgradeNeeded: Nothing
935+ { onComplete: launchAff' do
938+ , onError: \error -> launchAff' do
939+ fail $ "unexpected error: " <> show error
941+ , onSuccess: \cursor -> launchAff' do
942+ vals <- AVar.take vvals
943+ pure (IDBCursor.value cursor) >>= shouldEqual (maybe "" _.v $ head vals)
944+ IDBCursor.primaryKey cursor >>= shouldEqual (maybe (toKey 0) _.k $ head vals)
945+ AVar.put (drop 1 vals) vvals
946+ IDBCursor.continue cursor none
879−
948+ vdone <- AVar.empty
950+ [ { v: "pie", k: toKey 1 }
951+ , { v: "pancake", k: toKey 2 }
952+ , { v: "pie", k: toKey 3 }
953+ , { v: "cupcake", k: toKey 4 }
880− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
955+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
881− store <- IDBTransaction.objectStore tx "store"
956+ store <- IDBTransaction.objectStore tx "store"
882− index <- IDBObjectStore.index store "index"
957+ IDBObjectStore.openCursor store Nothing Next (cb vdone vvals)
883− val <- IDBIndex.get index (IDBKeyRange.lowerBound "autruche" false)
958+ AVar.take vdone
884− ((\r -> r.key) <$> val) `shouldEqual` (Just 42)
886− tearDown db
959+ tearDown db
888− describe "IDBCursor" do
961+ it "continue() - attempt to iterate in the wrong direction" do
963+ { storeParams: IDBDatabase.defaultParameters
964+ , indexParams: IDBObjectStore.defaultParameters
967+ [ "cupcake" :+: (Just $ toKey 4)
968+ , "pancake" :+: (Just $ toKey 2)
969+ , "pie" :+: (Just $ toKey 1)
970+ , "pie" :+: (Just $ toKey 3)
972+ , onUpgradeNeeded: Nothing
890− tearDown db = do
975+ cb vdone =
891− IDBDatabase.close db
976+ { onComplete: launchAff' do
892− _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
977+ fail $ "shouldn't complete"
894−
979+ , onError: \error -> launchAff' do
895− setup
980+ fail $ "unexpected error: " <> show error
897− . { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
982+ , onSuccess: \cursor -> launchAff' do
898− , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
983+ res <- attempt $ IDBCursor.continue cursor (Just 1)
899− , values :: Array (Tuple value (Maybe Key))
984+ case res of
900− , keyPath :: Array String
985+ Left err -> do
901− , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Effect Unit)
986+ name err `shouldEqual` "DataError"
902− }
987+ AVar.put unit vdone
903− -> Aff { db :: Database, index :: Index, store :: ObjectStore }
988+ Right _ -> do
904− setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
989+ fail "expected continue to fail"
906− onUpgradeNeeded' var db tx _ = launchAff' do
907− store <- IDBDatabase.createObjectStore db "store" storeParams
908− _ <- traverse (uncurry (IDBObjectStore.add store)) values
909− index <- IDBObjectStore.createIndex store "index" keyPath indexParams
910− liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
911− AVar.put { db, index, store } var
914− db <- IDBFactory.open "db" Nothing
915− { onUpgradeNeeded: Just (onUpgradeNeeded' var)
916− , onBlocked: Nothing
920− it "continue() - iterate to the next record" do
922− { storeParams: IDBDatabase.defaultParameters
923− , indexParams: IDBObjectStore.defaultParameters
926− [ "cupcake" :+: (Just $ toKey 4)
927− , "pancake" :+: (Just $ toKey 2)
928− , "pie" :+: (Just $ toKey 1)
929− , "pie" :+: (Just $ toKey 3)
931− , onUpgradeNeeded: Nothing
992+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
993+ store <- IDBTransaction.objectStore tx "store"
994+ IDBObjectStore.openCursor store Nothing Next (cb vdone)
998+ it "advance() - iterate cursor number of times specified by count" do
1000+ { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1001+ , indexParams: IDBObjectStore.defaultParameters
1004+ [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1005+ , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
1006+ , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
1007+ , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
1009+ , onUpgradeNeeded: Nothing
934− cb vdone vvals =
1012+ cb vdone vjump =
935− { onComplete: launchAff' do
1013+ { onComplete: launchAff' do
936− AVar.put unit vdone
1014+ AVar.put unit vdone
938− , onError: \error -> launchAff' do
1016+ , onError: \error -> launchAff' do
939− fail $ "unexpected error: " <> show error
1017+ fail $ "unexpected error: " <> show error
941− , onSuccess: \cursor -> launchAff' do
1019+ , onSuccess: \cursor -> launchAff' do
942− vals <- AVar.take vvals
1020+ jump <- AVar.take vjump
943− pure (IDBCursor.value cursor) >>= shouldEqual (maybe "" _.v $ head vals)
1022+ IDBCursor.advance cursor 3
944− IDBCursor.primaryKey cursor >>= shouldEqual (maybe (toKey 0) _.k $ head vals)
1023+ else do
945− AVar.put (drop 1 vals) vvals
1024+ let value = IDBCursor.value cursor
1025+ value.pKey `shouldEqual` "pkey_3"
1026+ value.iKey `shouldEqual` "ikey_3"
946 IDBCursor.continue cursor none
1027 IDBCursor.continue cursor none
947− }
1028+ AVar.put false vjump
950− [ { v: "pie", k: toKey 1 }
951− , { v: "pancake", k: toKey 2 }
952− , { v: "pie", k: toKey 3 }
953− , { v: "cupcake", k: toKey 4 }
955− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
956− store <- IDBTransaction.objectStore tx "store"
957− IDBObjectStore.openCursor store Nothing Next (cb vdone vvals)
961− it "continue() - attempt to iterate in the wrong direction" do
963− { storeParams: IDBDatabase.defaultParameters
964− , indexParams: IDBObjectStore.defaultParameters
967− [ "cupcake" :+: (Just $ toKey 4)
968− , "pancake" :+: (Just $ toKey 2)
969− , "pie" :+: (Just $ toKey 1)
970− , "pie" :+: (Just $ toKey 3)
972− , onUpgradeNeeded: Nothing
976− { onComplete: launchAff' do
977− fail $ "shouldn't complete"
979− , onError: \error -> launchAff' do
980− fail $ "unexpected error: " <> show error
982− , onSuccess: \cursor -> launchAff' do
983− res <- attempt $ IDBCursor.continue cursor (Just 1)
986− name err `shouldEqual` "DataError"
989− fail "expected continue to fail"
991− vdone <- AVar.empty
1030+ vdone <- AVar.empty
1031+ vjump <- AVar.new true
992− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
1032+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
993− store <- IDBTransaction.objectStore tx "store"
1033+ store <- IDBTransaction.objectStore tx "store"
994− IDBObjectStore.openCursor store Nothing Next (cb vdone)
1034+ IDBObjectStore.openCursor store Nothing Next (cb vdone vjump)
995− AVar.take vdone
1035+ AVar.take vdone
996− tearDown db
1036+ tearDown db
998− it "advance() - iterate cursor number of times specified by count" do
1038+ it "delete() - remove a record from the object store" do
999− { db } <- setup
1039+ { db } <- setup
1000− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1040+ { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1001− , indexParams: IDBObjectStore.defaultParameters
1041+ , indexParams: IDBObjectStore.defaultParameters
1002− , keyPath: []
1042+ , keyPath: []
1003− , values:
1043+ , values:
1004− [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1044+ [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1005− , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
1045+ , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
1006− , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
1046+ , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
1007− , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
1047+ , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
1009− , onUpgradeNeeded: Nothing
1049+ , onUpgradeNeeded: Nothing
1053+ { onComplete: launchAff' do
1054+ mval <- map _.pKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
1055+ mval `shouldEqual` (Nothing :: Maybe String)
1056+ AVar.put unit vdone
1058+ , onError: \error -> launchAff' do
1059+ fail $ "unexpected error: " <> show error
1061+ , onSuccess: \cursor -> launchAff' do
1062+ let value = IDBCursor.value cursor
1063+ value.pKey `shouldEqual` "pkey_0"
1064+ IDBCursor.delete cursor
1065+ IDBCursor.advance cursor 4
1013− { onComplete: launchAff' do
1014− AVar.put unit vdone
1016− , onError: \error -> launchAff' do
1017− fail $ "unexpected error: " <> show error
1019− , onSuccess: \cursor -> launchAff' do
1020− jump <- AVar.take vjump
1022− IDBCursor.advance cursor 3
1024− let value = IDBCursor.value cursor
1025− value.pKey `shouldEqual` "pkey_3"
1026− value.iKey `shouldEqual` "ikey_3"
1027− IDBCursor.continue cursor none
1028− AVar.put false vjump
1030− vdone <- AVar.empty
1067+ vdone <- AVar.empty
1031− vjump <- AVar.new true
1032− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
1068+ tx <- IDBDatabase.transaction db [ "store" ] ReadWrite
1033− store <- IDBTransaction.objectStore tx "store"
1069+ store <- IDBTransaction.objectStore tx "store"
1034− IDBObjectStore.openCursor store Nothing Next (cb vdone vjump)
1070+ IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1035− AVar.take vdone
1071+ AVar.take vdone
1036− tearDown db
1072+ tearDown db
1038− it "delete() - remove a record from the object store" do
1074+ it "update() - modify a record in the object store" do
1039− { db } <- setup
1075+ { db } <- setup
1040− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1076+ { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1041− , indexParams: IDBObjectStore.defaultParameters
1077+ , indexParams: IDBObjectStore.defaultParameters
1042− , keyPath: []
1078+ , keyPath: []
1043− , values:
1079+ , values:
1044− [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1080+ [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1045− , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
1046− , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
1047− , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
1049− , onUpgradeNeeded: Nothing
1082+ , onUpgradeNeeded: Nothing
1086+ { onComplete: launchAff' do
1087+ mval <- map _.iKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
1088+ mval `shouldEqual` (Just "patate")
1089+ AVar.put unit vdone
1091+ , onError: \error -> launchAff' do
1092+ fail $ "unexpected error: " <> show error
1094+ , onSuccess: \cursor -> launchAff' do
1095+ let value = IDBCursor.value cursor
1096+ value.pKey `shouldEqual` "pkey_0"
1097+ key <- IDBCursor.update cursor { pKey: "pkey_0", iKey: "patate" }
1098+ key `shouldEqual` toKey "pkey_0"
1099+ IDBCursor.advance cursor 4
1053− { onComplete: launchAff' do
1054− mval <- map _.pKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
1055− mval `shouldEqual` (Nothing :: Maybe String)
1056− AVar.put unit vdone
1058− , onError: \error -> launchAff' do
1059− fail $ "unexpected error: " <> show error
1061− , onSuccess: \cursor -> launchAff' do
1062− let value = IDBCursor.value cursor
1063− value.pKey `shouldEqual` "pkey_0"
1064− IDBCursor.delete cursor
1065− IDBCursor.advance cursor 4
1067− vdone <- AVar.empty
1101+ vdone <- AVar.empty
1068− tx <- IDBDatabase.transaction db [ "store" ] ReadWrite
1102+ tx <- IDBDatabase.transaction db [ "store" ] ReadWrite
1069− store <- IDBTransaction.objectStore tx "store"
1103+ store <- IDBTransaction.objectStore tx "store"
1070− IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1104+ IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1071− AVar.take vdone
1105+ AVar.take vdone
1072− tearDown db
1106+ tearDown db
1074− it "update() - modify a record in the object store" do
1108+ it "update() - throw ReadOnlyError after update on ReadOnly transaction" do
1075− { db } <- setup
1109+ { db } <- setup
1076− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1110+ { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1077− , indexParams: IDBObjectStore.defaultParameters
1111+ , indexParams: IDBObjectStore.defaultParameters
1078− , keyPath: []
1112+ , keyPath: []
1079− , values:
1113+ , values:
1080− [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1114+ [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1082− , onUpgradeNeeded: Nothing
1116+ , onUpgradeNeeded: Nothing
1120+ { onComplete: launchAff' do
1121+ fail $ "shouldn't complete"
1123+ , onError: \error -> launchAff' do
1124+ fail $ "unexpected error: " <> show error
1126+ , onSuccess: \cursor -> launchAff' do
1127+ res <- attempt $ IDBCursor.update cursor "patate"
1130+ name err `shouldEqual` "ReadOnlyError"
1131+ AVar.put unit vdone
1133+ fail $ "expected ReadOnlyError"
1084− let
1135+ vdone <- AVar.empty
1085− cb vdone store =
1136+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
1086− { onComplete: launchAff' do
1137+ store <- IDBTransaction.objectStore tx "store"
1087− mval <- map _.iKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
1138+ IDBObjectStore.openCursor store Nothing Next (cb vdone)
1088− mval `shouldEqual` (Just "patate")
1139+ AVar.take vdone
1089− AVar.put unit vdone
1140+ tearDown db
1091− , onError: \error -> launchAff' do
1092− fail $ "unexpected error: " <> show error
1094− , onSuccess: \cursor -> launchAff' do
1095− let value = IDBCursor.value cursor
1096− value.pKey `shouldEqual` "pkey_0"
1097− key <- IDBCursor.update cursor { pKey: "pkey_0", iKey: "patate" }
1098− key `shouldEqual` toKey "pkey_0"
1099− IDBCursor.advance cursor 4
1101− vdone <- AVar.empty
1102− tx <- IDBDatabase.transaction db [ "store" ] ReadWrite
1103− store <- IDBTransaction.objectStore tx "store"
1104− IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1108− it "update() - throw ReadOnlyError after update on ReadOnly transaction" do
1110− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1111− , indexParams: IDBObjectStore.defaultParameters
1114− [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1116− , onUpgradeNeeded: Nothing
1120− { onComplete: launchAff' do
1121− fail $ "shouldn't complete"
1123− , onError: \error -> launchAff' do
1124− fail $ "unexpected error: " <> show error
1126− , onSuccess: \cursor -> launchAff' do
1127− res <- attempt $ IDBCursor.update cursor "patate"
1130− name err `shouldEqual` "ReadOnlyError"
1131− AVar.put unit vdone
1133− fail $ "expected ReadOnlyError"
1135− vdone <- AVar.empty
1136− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
1137− store <- IDBTransaction.objectStore tx "store"
1138− IDBObjectStore.openCursor store Nothing Next (cb vdone)