@@ -34,12 +34,16 @@ import Database.IndexedDB.IDBTransaction as IDBTransaction
3434
3535infixr 7 Tuple as :+:
3636
37+launchAff' :: forall a e. Aff e a -> Eff (exception :: EXCEPTION | e) Unit
38+launchAff' aff =
39+ pure unit <* (launchAff aff)
40+
3741main :: forall eff. Eff (now :: NOW, mocha :: MOCHA, idb :: IDB, exception :: EXCEPTION, avar :: AVAR | eff) Unit
3842main = runMocha do
3943 describe "IDBFactory" do
4044 let
4145 tearDown name version db = do
42− liftEff $ IDBDatabase.close db
46+ IDBDatabase.close db
4347 version' <- IDBFactory.deleteDatabase name
4448 version' `shouldEqual` version
4549
@@ -73,7 +77,7 @@ main = runMocha do
7377 , onBlocked : Nothing
7478 }
7579 IDBDatabase.name db `shouldEqual` name
76− liftEff $ IDBDatabase.close db
80+ IDBDatabase.close db
7781 db <- IDBFactory.open name Nothing
7882 { onUpgradeNeeded : Nothing
7983 , onBlocked : Nothing
@@ -111,7 +115,7 @@ main = runMocha do
111115 }
112116 _ <- forkAff do
113117 delay (Milliseconds 100.0)
114− liftEff $ IDBDatabase.close db01
118+ IDBDatabase.close db01
115119
116120 db02 <- IDBFactory.open name (Just version)
117121 { onUpgradeNeeded : Nothing
@@ -277,14 +281,14 @@ main = runMocha do
277281 describe "IDBDatabase" do
278282 let
279283 tearDown db = do
280− liftEff $ IDBDatabase.close db
284+ IDBDatabase.close db
281285 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
282286 pure unit
283287
284288 setup storeParams = do
285− let onUpgradeNeeded var db _ = pure unit <* do
289+ let onUpgradeNeeded var db _ = launchAff' do
286290 store <- IDBDatabase.createObjectStore db "store" storeParams
287− _ <- (launchAff $ putVar var { db, store })
291+ _ <- putVar var { db, store }
288292 pure unit
289293
290294 var <- makeVar
@@ -320,14 +324,13 @@ main = runMocha do
320324 tearDown db
321325
322326 it "deleteObjectStore" do
323− let onUpgradeNeeded var db _ = do
327+ let onUpgradeNeeded var db _ = launchAff' do
324328 _ <- IDBDatabase.deleteObjectStore db "store"
325− _ <- (launchAff $ putVar var true)
326− pure unit
329+ putVar var true
327330
328331 var <- makeVar
329332 { db, store } <- setup IDBObjectStore.defaultParameters
330− liftEff $ IDBDatabase.close db
333+ IDBDatabase.close db
331334 db <- IDBFactory.open "db" (Just 999) { onUpgradeNeeded : Just (onUpgradeNeeded var)
332335 , onBlocked : Nothing
333336 }
@@ -339,16 +342,15 @@ main = runMocha do
339342 describe "IDBObjectStore" do
340343 let
341344 tearDown db = do
342− liftEff $ IDBDatabase.close db
345+ IDBDatabase.close db
343346 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
344347 pure unit
345348
346349 setup { storeParams, onUpgradeNeeded } = do
347− let onUpgradeNeeded' var db _ = pure unit <* do
350+ let onUpgradeNeeded' var db _ = launchAff' do
348351 store <- IDBDatabase.createObjectStore db "store" storeParams
349− launchAff do
350− liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store)
351− putVar var { db, store }
352+ liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store)
353+ putVar var { db, store }
352354
353355 var <- makeVar
354356 db <- IDBFactory.open "db" Nothing
@@ -362,7 +364,7 @@ main = runMocha do
362364 date <- liftEff $ toDateTime <$> now
363365 { db } <- setup
364366 { storeParams: { autoIncrement: true, keyPath: [] }
365− , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do
367+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
366368 -- no key
367369 key <- IDBObjectStore.add store "patate" (Nothing :: Maybe Key)
368370 (toKey 1) `shouldEqual` key
@@ -386,48 +388,48 @@ main = runMocha do
386388 -- array key
387389 key <- IDBObjectStore.add store "patate" (Just $ toKey [14, 42])
388390 (toKey [14, 42]) `shouldEqual` key
389− )}
391+ }
390392 tearDown db
391393
392394 it "clear()" do
393395 { db } <- setup
394396 { storeParams: IDBObjectStore.defaultParameters
395− , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do
397+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
396398 key <- IDBObjectStore.add store "patate" (Just 14)
397399 _ <- IDBObjectStore.clear store
398400 val <- IDBObjectStore.get store (IDBKeyRange.only key)
399401 val `shouldEqual` (Nothing :: Maybe String)
400− )}
402+ }
401403 tearDown db
402404
403405 it "count()" do
404406 { db } <- setup
405407 { storeParams: IDBObjectStore.defaultParameters
406− , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do
408+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
407409 key <- IDBObjectStore.add store "patate" (Just 14)
408410 key <- IDBObjectStore.add store "autruche" (Just 42)
409411 n <- IDBObjectStore.count store Nothing
410412 n `shouldEqual` 2
411− )}
413+ }
412414 tearDown db
413415
414416 it "getKey()" do
415417 { db } <- setup
416418 { storeParams: IDBObjectStore.defaultParameters
417− , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do
419+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
418420 key <- IDBObjectStore.add store "patate" (Just 14)
419421 mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14)
420422 mkey `shouldEqual` (Just key)
421423
422424 mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
423425 mkey `shouldEqual` (Nothing :: Maybe Key)
424− )}
426+ }
425427 tearDown db
426428
427429 it "getAllKeys()" do
428430 { db } <- setup
429431 { storeParams: IDBObjectStore.defaultParameters
430− , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do
432+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
431433 key1 <- IDBObjectStore.add store "patate" (Just 14)
432434 key2 <- IDBObjectStore.add store "autruche" (Just 42)
433435 key3 <- IDBObjectStore.add store 14 (Just 1337)
@@ -447,39 +449,39 @@ main = runMocha do
447449 -- count
448450 keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
449451 keys `shouldEqual` [key1, key2]
450− )}
452+ }
451453 tearDown db
452454
453455 it "openCursor()" do
454456 { db } <- setup
455457 { storeParams: IDBObjectStore.defaultParameters
456− , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do
458+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
457459 _ <- IDBObjectStore.openCursor store Nothing Next
458460 _ <- IDBObjectStore.openCursor store Nothing NextUnique
459461 _ <- IDBObjectStore.openCursor store Nothing Prev
460462 _ <- IDBObjectStore.openCursor store Nothing PrevUnique
461463 _ <- IDBObjectStore.openCursor store (Just $ IDBKeyRange.upperBound 1 true) Next
462464 pure unit
463− )}
465+ }
464466 tearDown db
465467
466468 it "openKeyCursor()" do
467469 { db } <- setup
468470 { storeParams: IDBObjectStore.defaultParameters
469− , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do
471+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
470472 _ <- IDBObjectStore.openKeyCursor store Nothing Next
471473 _ <- IDBObjectStore.openKeyCursor store Nothing NextUnique
472474 _ <- IDBObjectStore.openKeyCursor store Nothing Prev
473475 _ <- IDBObjectStore.openKeyCursor store Nothing PrevUnique
474476 _ <- IDBObjectStore.openKeyCursor store (Just $ IDBKeyRange.lowerBound 1 true) Next
475477 pure unit
476− )}
478+ }
477479 tearDown db
478480
479481 describe "IDBIndex" do
480482 let
481483 tearDown db = do
482− liftEff $ IDBDatabase.close db
484+ IDBDatabase.close db
483485 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
484486 pure unit
485487
@@ -491,13 +493,12 @@ main = runMocha do
491493 , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
492494 } -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
493495 setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
494− let onUpgradeNeeded' var db tx = pure unit <* do
496+ let onUpgradeNeeded' var db tx = launchAff' do
495497 store <- IDBDatabase.createObjectStore db "store" storeParams
496− launchAff do
497− _ <- traverse (uncurry (IDBObjectStore.add store)) values
498− index <- liftEff $ IDBObjectStore.createIndex store "index" keyPath indexParams
499− liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
500− putVar var { db, index, store }
498+ _ <- traverse (uncurry (IDBObjectStore.add store)) values
499+ index <- IDBObjectStore.createIndex store "index" keyPath indexParams
500+ liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
501+ putVar var { db, index, store }
501502
502503 var <- makeVar
503504 db <- IDBFactory.open "db" Nothing
@@ -522,7 +523,7 @@ main = runMocha do
522523 tearDown db
523524
524525 it "attempt to create an index that requires unique values on an object store already contains duplicates" do
525− let onAbort var = launchAff (putVar var true) *> pure unit
526+ let onAbort var = launchAff' (putVar var true)
526527 txVar <- makeVar
527528 dbVar <- makeVar
528529 res <- attempt $ setup
@@ -534,7 +535,7 @@ main = runMocha do
534535 , values : [ { indexedProperty: "bar" } :+: (Just $ toKey 1)
535536 , { indexedProperty: "bar" } :+: (Just $ toKey 2)
536537 ]
537− , onUpgradeNeeded : Just $ \db tx _ -> do
538+ , onUpgradeNeeded : Just $ \db tx _ -> launchAff' do
538539 IDBTransaction.onAbort tx (onAbort txVar)
539540 IDBDatabase.onAbort db (onAbort dbVar)
540541 }
@@ -557,7 +558,7 @@ main = runMocha do
557558 , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing
558559 , { key: "key3", indexedProperty: "indexed_3" } :+: Nothing
559560 ]
560− , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do
561+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
561562 val <- IDBIndex.get index (IDBKeyRange.only "indexed_2")
562563 ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2")
563564 }
@@ -572,9 +573,9 @@ main = runMocha do
572573 , "object_2" :+: (Just $ toKey 2)
573574 , "object_3" :+: (Just $ toKey 3)
574575 ]
575− , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do
576− val <- IDBIndex.get index (IDBKeyRange.only "object_3")
577− val `shouldEqual` (Just "object_3")
576+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
577+ val <- IDBIndex.get index (IDBKeyRange.only "object_3")
578+ val `shouldEqual` (Just "object_3")
578579 }
579580 tearDown db
580581
@@ -588,9 +589,9 @@ main = runMocha do
588589 , keyPath : ["i"]
589590 , values : [ { key: "date", i: (toKey date) } :+: Nothing
590591 ]
591− , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do
592− val <- IDBIndex.get index (IDBKeyRange.only date)
593− ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
592+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
593+ val <- IDBIndex.get index (IDBKeyRange.only date)
594+ ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
594595 }
595596 tearDown db
596597
@@ -604,9 +605,9 @@ main = runMocha do
604605 , keyPath : ["i"]
605606 , values : [ { key: "num", i: (toKey num) } :+: Nothing
606607 ]
607− , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do
608− val <- IDBIndex.get index (IDBKeyRange.only num)
609− ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
608+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
609+ val <- IDBIndex.get index (IDBKeyRange.only num)
610+ ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
610611 }
611612 tearDown db
612613
@@ -620,8 +621,28 @@ main = runMocha do
620621 , keyPath : ["i"]
621622 , values : [ { key: "array", i: (toKey array) } :+: Nothing
622623 ]
623− , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do
624− val <- IDBIndex.get index (IDBKeyRange.only array)
625− ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
624+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
625+ val <- IDBIndex.get index (IDBKeyRange.only array)
626+ ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
627+ }
628+ tearDown db
629+
630+ it "multiEntry - adding keys" do
631+ { db } <- setup
632+ { storeParams : IDBObjectStore.defaultParameters
633+ , indexParams : { unique: false, multiEntry: true }
634+ , keyPath : ["name"]
635+ , values : [ { name: ["patate", "autruche"] } :+: (Just $ toKey 1)
636+ , { name: ["bob"] } :+: (Just $ toKey 2)
637+ ]
638+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
639+ key <- IDBIndex.getKey index (IDBKeyRange.only "patate")
640+ key `shouldEqual` (Just $ toKey 1)
641+
642+ key <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
643+ key `shouldEqual` (Just $ toKey 1)
644+
645+ key <- IDBIndex.getKey index (IDBKeyRange.only "bob")
646+ key `shouldEqual` (Just $ toKey 2)
626647 }
627648 tearDown db