git.iko.soy/purescript-indexed-DB
on 564ff75 help sign in
33 files changed+2164−2842
since splitsnapshots
unifiedsplit
.gitignore−2 README.md+11−65 bower.json+38 deploy.sh+3 index.js−4 karma.browserstack.conf.js+91 karma.conf.js+25 package.json+36−9 packages.dhall−105 spago.dhall−42 src/Database/IndexedDB/Core.js+6−6 src/Database/IndexedDB/Core.purs+95−143 src/Database/IndexedDB/IDBCursor.js+27−65 src/Database/IndexedDB/IDBCursor.purs+142−112 src/Database/IndexedDB/IDBDatabase.js+28−62 src/Database/IndexedDB/IDBDatabase.purs+113−131 src/Database/IndexedDB/IDBFactory.js+8−21 src/Database/IndexedDB/IDBFactory.purs+32−36 src/Database/IndexedDB/IDBIndex.js−171 src/Database/IndexedDB/IDBIndex.purs+7−197 src/Database/IndexedDB/IDBIndex/Internal.js+129 src/Database/IndexedDB/IDBIndex/Internal.purs+217 src/Database/IndexedDB/IDBKey.purs+1−8 src/Database/IndexedDB/IDBKey/Internal.js+3−3 src/Database/IndexedDB/IDBKey/Internal.purs+110−99 src/Database/IndexedDB/IDBKeyRange.js+9−9 src/Database/IndexedDB/IDBKeyRange.purs+75−91 src/Database/IndexedDB/IDBObjectStore.js+28−55 src/Database/IndexedDB/IDBObjectStore.purs+125−160 src/Database/IndexedDB/IDBTransaction.js+18−38 src/Database/IndexedDB/IDBTransaction.purs+77−67 test/Main.purs+710−1060 yarn.lock−81
.gitignore−2
@@ -77,5 +77,3 @@ releases
7777.purs*
7878.psa*
7979dist
80−.spago
81−test.js
README.md+11−65
@@ -1,40 +1,13 @@
1−PureScript IndexedDB [![](https://img.shields.io/badge/doc-pursuit-60b5cc.svg)](http://pursuit.purescript.org/packages/purescript-indexeddb) [![Build Status](https://travis-ci.org/truqu/purescript-indexedDB.svg?branch=master)](https://travis-ci.org/truqu/purescript-indexedDB)
1+PureScript IndexedDB [![](https://img.shields.io/badge/doc-pursuit-60b5cc.svg)](http://pursuit.purescript.org/packages/purescript-indexeddb) [![Build Status](https://travis-ci.org/truqu/purescript-indexeddb.svg?branch=master)](https://travis-ci.org/truqu/purescript-indexeddb)
22=====
33
44This package offers complete bindings and type-safety upon the [IndexedDB API](https://w3c.github.io/IndexedDB).
55
66## Overview
77
8−The `IDBCore` and `IDBFactory` are the two entry points required to create and connect to an
9−indexed database. From there, modules are divided such that each of them covers a specific IDB
10−interface.
11−
12−They are designed to be used as qualified imports such that each method gets prefixed with a
13−menaingful namespace (e.g `IDBIndex.get`, `IDBObjectStore.openCursor` ...)
14−
15−Here's a quick example of what it look likes.
168```purescript
17−module Main where
18−
19−import Prelude
20−
21−import Control.Monad.Aff (Aff, launchAff_)
22−import Control.Monad.Aff.Console (CONSOLE, log)
23−import Control.Monad.Eff.Exception (EXCEPTION)
24−import Control.Monad.Eff (Eff)
25−import Data.Maybe (Maybe(..), maybe)
26−
27−import Database.IndexedDB.Core
28−import Database.IndexedDB.IDBFactory as IDBFactory
29−import Database.IndexedDB.IDBDatabase as IDBDatabase
30−import Database.IndexedDB.IDBObjectStore as IDBObjectStore
31−import Database.IndexedDB.IDBIndex as IDBIndex
32−import Database.IndexedDB.IDBTransaction as IDBTransaction
33−import Database.IndexedDB.IDBKeyRange as IDBKeyRange
34−
35−
369main :: Eff (idb :: IDB, exception :: EXCEPTION, console :: CONSOLE) Unit
37−main = launchAff_ do
10+main = launchAff' do
3811 db <- IDBFactory.open "db" Nothing { onBlocked : Nothing
3912 , onUpgradeNeeded : Just onUpgradeNeeded
4013 }
@@ -45,50 +18,23 @@ main = launchAff_ do
4518 log $ maybe "not found" id val
4619
4720
48−onUpgradeNeeded :: forall e. Database -> Transaction -> { oldVersion :: Int } -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit
49−onUpgradeNeeded db _ _ = launchAff_ do
50− store <- IDBDatabase.createObjectStore db "store" IDBDatabase.defaultParameters
21+onUpgradeNeeded :: forall e. Database -> Transaction -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit
22+onUpgradeNeeded db _ = launchAff' do
23+ store <- IDBDatabase.createObjectStore db "store" IDBObjectStore.defaultParameters
5124 _ <- IDBObjectStore.add store "patate" (Just 1)
5225 _ <- IDBObjectStore.add store { property: 42 } (Just 2)
53− _ <- IDBObjectStore.createIndex store "index" ["property"] IDBObjectStore.defaultParameters
26+ _ <- IDBObjectStore.createIndex store "index" ["property"] IDBIndex.defaultParameters
5427 pure unit
5528```
5629
57−## Notes
58−
59−### Errors
60−Errors normally thrown by the IDB\* interfaces are wrapped in the `Aff` Monad as `Error` where
61−the `name` corresponds to the error's name (e.g. "InvalidStateError"). Pattern matching can
62−therefore be done on any error to handle specific errors thrown by the API.
63−
64−### Examples
65−The `test` folder contains a great amount of examples showing practical usage of the IDB\*
66−interfaces. Do not hesitate to have a peek should you wonder how to use one of the module. The
67−wrapper tries to keep as much as possible an API consistent with the original IndexedDB API.
68−Hence, it should be quite straightforward to translate any JavaScript example to a PureScript
69−one.
70−
7130## Changelog
7231
73−#### v3.0.0
74−
75−- callback to `onUpgradeNeeded` event now provide a record with the database old version.
76−
77−#### v2.0.0
78−
79−- review interface implementation (use of opaque classes to improve readability without compromising
80− the reusability). The API doesn't really change from a user perspective though.
81−
82−- make the Key more opaque (by having an IDBKey instance for Foreign types)
83−
84−- Upgrade purescript-exceptions to 3.1.0 and leverage the new `name` accessor
85−
32+- Release incoming
8633
87−#### v1.0.0
34+#### TODO
8835
89−- [Indexed Database API 2.0](https://w3c.github.io/IndexedDB/) totally covered apart from
90− - `index.getAll` method (and the associated one for the IDBObjectStore)
91− - binary keys
36+- Add support for `index.getAll` method
37+- Complete the specifications with the [official tests list](https://github.com/w3c/web-platform-tests/blob/master/IndexedDB/README.md) provided by W3C
9238
9339## Documentation
9440
@@ -100,7 +46,7 @@ Tested in the cloud on multiple browsers and operating systems thanks to [Browse
10046
10147| IE / Edge | Chrome | Firefox | Safari | Opera | Android | iOS Safari |
10248| ----------| ------ | ------- | ------- | ----- | ------- | ---------- |
103−| - | >= 57 | >= 51 | - | >= 46 | - | - |
49+| - | >= 56 | >= 51 | >= 10.1 | >= 43 | >= 4.4 | >= 10.3 |
10450
10551<p align="center">
10652 <a href="https://www.browserstack.com"><img alt="browserstack" src=".github/browserstack.png" /></a>
bower.jsonnew file+38
@@ -0,0 +1,38 @@
1+{
2+ "name": "purescript-indexeddb",
3+ "description": "An API wrapper around IndexedDB",
4+ "version": "1.0.0",
5+ "authors": [
6+ "Matthias Benkort <matthias.benkort@truqu.com>"
7+ ],
8+ "ignore": [
9+ "**/.*",
10+ "node_modules",
11+ "bower_components",
12+ "output",
13+ "test",
14+ "dist",
15+ "releases",
16+ "package.json",
17+ "karma.conf.js",
18+ "karma.browserstack.conf.js",
19+ "deploy.sh"
20+ ],
21+ "license": "MIT",
22+ "homepage": "https://github.com/truqu/purescript-indexeddb",
23+ "repository": {
24+ "type": "git",
25+ "url": "git://github.com/KtorZ/purescript-ifrit.git"
26+ },
27+ "dependencies": {
28+ "purescript-maybe": "^3.0.0",
29+ "purescript-aff": "^3.1.0",
30+ "purescript-datetime": "^3.2.0"
31+ },
32+ "devDependencies": {
33+ "purescript-spec": "^1.0.0",
34+ "purescript-psci-support": "^3.0.0",
35+ "purescript-now": "^3.0.0",
36+ "purescript-spec-mocha": "https://github.com/truqu/purescript-spec-mocha.git#0.5.0"
37+ }
38+}
deploy.shnew file+3
@@ -0,0 +1,3 @@
1+#!/bin/sh
2+
3+test -n "$TRAVIS_TAG" && ( yes | pulp publish --no-push )
index.jsdeleted−4
@@ -1,4 +0,0 @@
1−import "fake-indexeddb/auto";
2−import * as Main from './output/Test.Main/index.js';
3−
4−Main.main();
karma.browserstack.conf.jsnew file+91
@@ -0,0 +1,91 @@
1+module.exports = (config) => {
2+ config.set({
3+ browsers: [
4+ 'FirefoxMAC',
5+ 'ChromeMAC',
6+ // 'SafariMAC',
7+ 'OperaMAC',
8+
9+ 'FirefoxWIN',
10+ 'ChromeWIN',
11+ 'OperaWIN',
12+ ],
13+ files: [
14+ 'dist/karma/index.js',
15+ ],
16+ frameworks: [
17+ 'mocha',
18+ ],
19+ plugins: [
20+ 'karma-browserstack-launcher',
21+ 'karma-mocha',
22+ ],
23+ reporters: [
24+ 'dots',
25+ 'BrowserStack',
26+ ],
27+ singleRun: true,
28+ client: {
29+ mocha: {
30+ timeout: 10000,
31+ },
32+ },
33+ browserStack: {
34+ username: process.env.BROWSERSTACK_USERNAME,
35+ accessKey: process.env.BROWSERSTACK_ACCESSKEY,
36+ timeout: 1500,
37+ captureTimeout: 500,
38+ },
39+ customLaunchers: {
40+ FirefoxMAC: {
41+ base: 'BrowserStack',
42+ browser: 'Firefox',
43+ browser_version: '51',
44+ os: 'OS X',
45+ os_version: 'Sierra',
46+ },
47+ ChromeMAC: {
48+ base: 'BrowserStack',
49+ browser: 'Chrome',
50+ browser_version: '58',
51+ os: 'OS X',
52+ os_version: 'Sierra',
53+ },
54+ SafariMAC: {
55+ base: 'BrowserStack',
56+ browser: 'Safari',
57+ browser_version: '10.1',
58+ os: 'OS X',
59+ os_version: 'Sierra',
60+ },
61+ OperaMAC: {
62+ base: 'BrowserStack',
63+ browser: 'Opera',
64+ browser_version: '46',
65+ os: 'OS X',
66+ os_version: 'Sierra',
67+ },
68+ FirefoxWIN: {
69+ base: 'BrowserStack',
70+ browser: 'Firefox',
71+ browser_version: '51',
72+ os: 'WINDOWS',
73+ os_version: '10',
74+ },
75+ ChromeWIN: {
76+ base: 'BrowserStack',
77+ browser: 'Chrome',
78+ browser_version: '58',
79+ os: 'WINDOWS',
80+ os_version: '10',
81+ },
82+ OperaWIN: {
83+ base: 'BrowserStack',
84+ browser: 'Opera',
85+ browser_version: '46',
86+ os: 'WINDOWS',
87+ os_version: '10',
88+ },
89+ },
90+ });
91+};
karma.conf.jsnew file+25
@@ -0,0 +1,25 @@
1+module.exports = config => {
2+ config.set({
3+ autoWatch: true,
4+ singleRun: true,
5+ browsers: ["Chrome"],
6+ files: [
7+ "dist/karma/index.js",
8+ ],
9+ frameworks: [
10+ "mocha",
11+ ],
12+ plugins: [
13+ "karma-chrome-launcher",
14+ "karma-firefox-launcher",
15+ "karma-spec-reporter",
16+ "karma-mocha",
17+ ],
18+ reporters: ["spec"],
19+ client: {
20+ mocha: {
21+ timeout: 10000
22+ }
23+ }
24+ });
25+};
package.json+36−9
@@ -1,15 +1,42 @@
11{
2− "name": "purescript-indexed-DB",
2+ "name": "purescript-indexeddb",
33 "version": "1.0.0",
4− "main": "index.js",
5− "repository": "https://github.com/ilyakooo0/purescript-indexed-DB.git",
6− "author": "iko <mail@iko.soy>",
7− "license": "MIT",
4+ "description": "A wrapper around the IndexedDB API",
85 "scripts": {
9− "test": "spago build && node index.js"
6+ "prepare:release": "mkdir -p releases/github && cp -r README.md LICENSE src bower.json releases/github",
7+ "test:build": "mkdir -p dist/karma && pulp browserify -j 4 --main \"Test.Main\" -I test --to dist/karma/index.js",
8+ "test:run": "karma start karma.conf.js",
9+ "test:run:browserstack": "karma start karma.browserstack.conf.js",
10+ "test:browserstack": "npm run test:build && npm run test:run:browserstack",
11+ "test": "npm run test:build && npm run test:run",
12+ "start": "find . -type f -regex '.*\\(src\\|test\\).*' ! -regex '.*bower_components.*\\|.*node_modules.*|.*\\.sw.*' | entr -s 'npm run test -s'"
13+ },
14+ "dependencies": {},
15+ "devDependencies": {
16+ "bower": "1.8.0",
17+ "eslint": "3.19.0",
18+ "eslint-config-airbnb": "15.0.1",
19+ "eslint-plugin-import": "2.5.0",
20+ "eslint-plugin-jsx-a11y": "5.0.3",
21+ "eslint-plugin-react": "7.1.0",
22+ "karma": "1.7.0",
23+ "karma-browserstack-launcher": "1.3.0",
24+ "karma-chrome-launcher": "2.1.1",
25+ "karma-firefox-launcher": "1.0.1",
26+ "karma-mocha": "1.3.0",
27+ "karma-spec-reporter": "0.0.31",
28+ "mocha": "3.4.2",
29+ "pulp": "11.0.0",
30+ "purescript": "0.11.5"
1031 },
11− "dependencies": {
12− "fake-indexeddb": "^4.0.1"
32+ "repository": {
33+ "type": "git",
34+ "url": "git+https://github.com/Truqu/purescript-indexeddb.git"
35+ },
36+ "author": "Matthias Benkort <matthias.benkort@truqu.com>",
37+ "license": "MIT",
38+ "bugs": {
39+ "url": "https://github.com/Truqu/purescript-indexeddb/issues"
1340 },
14− "type": "module"
41+ "homepage": "https://github.com/Truqu/purescript-indexeddb#readme"
1542}
packages.dhalldeleted−105
@@ -1,105 +0,0 @@
1−{-
2−Welcome to your new Dhall package-set!
3−
4−Below are instructions for how to edit this file for most use
5−cases, so that you don't need to know Dhall to use it.
6−
7−## Use Cases
8−
9−Most will want to do one or both of these options:
10−1. Override/Patch a package's dependency
11−2. Add a package not already in the default package set
12−
13−This file will continue to work whether you use one or both options.
14−Instructions for each option are explained below.
15−
16−### Overriding/Patching a package
17−
18−Purpose:
19−- Change a package's dependency to a newer/older release than the
20− default package set's release
21−- Use your own modified version of some dependency that may
22− include new API, changed API, removed API by
23− using your custom git repo of the library rather than
24− the package set's repo
25−
26−Syntax:
27−where `entityName` is one of the following:
28−- dependencies
29−- repo
30−- version
31−-------------------------------
32−let upstream = --
33−in upstream
34− with packageName.entityName = "new value"
35−-------------------------------
36−
37−Example:
38−-------------------------------
39−let upstream = --
40−in upstream
41− with halogen.version = "master"
42− with halogen.repo = "https://example.com/path/to/git/repo.git"
43−
44− with halogen-vdom.version = "v4.0.0"
45− with halogen-vdom.dependencies = [ "extra-dependency" ] # halogen-vdom.dependencies
46−-------------------------------
47−
48−### Additions
49−
50−Purpose:
51−- Add packages that aren't already included in the default package set
52−
53−Syntax:
54−where `<version>` is:
55−- a tag (i.e. "v4.0.0")
56−- a branch (i.e. "master")
57−- commit hash (i.e. "701f3e44aafb1a6459281714858fadf2c4c2a977")
58−-------------------------------
59−let upstream = --
60−in upstream
61− with new-package-name =
62− { dependencies =
63− [ "dependency1"
64− , "dependency2"
65− ]
66− , repo =
67− "https://example.com/path/to/git/repo.git"
68− , version =
69− "<version>"
70− }
71−-------------------------------
72−
73−Example:
74−-------------------------------
75−let upstream = --
76−in upstream
77− with benchotron =
78− { dependencies =
79− [ "arrays"
80− , "exists"
81− , "profunctor"
82− , "strings"
83− , "quickcheck"
84− , "lcg"
85− , "transformers"
86− , "foldable-traversable"
87− , "exceptions"
88− , "node-fs"
89− , "node-buffer"
90− , "node-readline"
91− , "datetime"
92− , "now"
93− ]
94− , repo =
95− "https://github.com/hdgarrood/purescript-benchotron.git"
96− , version =
97− "v7.0.0"
98− }
99−-------------------------------
100−-}
101−
102−let upstream =
103− https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20221124/packages.dhall
104− sha256:792255bbd8e2141468d967325f3e78246cb7e500bf3ab6d76d5b22dffbb09d49
105−in upstream
spago.dhalldeleted−42
@@ -1,42 +0,0 @@
1−{-
2−Welcome to a Spago project!
3−You can edit this file as you like.
4−
5−Need help? See the following resources:
6−- Spago documentation: https://github.com/purescript/spago
7−- Dhall language tour: https://docs.dhall-lang.org/tutorials/Language-Tour.html
8−
9−When creating a new Spago project, you can use
10−`spago init --no-comments` or `spago init -C`
11−to generate this file without the comments in this block.
12−-}
13−{ name = "indexed-db"
14−, dependencies =
15− [ "aff"
16− , "arrays"
17− , "avar"
18− , "console"
19− , "control"
20− , "datetime"
21− , "effect"
22− , "either"
23− , "enums"
24− , "exceptions"
25− , "foldable-traversable"
26− , "foreign"
27− , "functions"
28− , "identity"
29− , "lists"
30− , "maybe"
31− , "nonempty"
32− , "now"
33− , "nullable"
34− , "prelude"
35− , "read"
36− , "spec"
37− , "transformers"
38− , "tuples"
39− ]
40−, packages = ./packages.dhall
41−, sources = [ "src/**/*.purs", "test/**/*.purs" ]
42−}
src/Database/IndexedDB/Core.js+6−6
@@ -3,7 +3,7 @@ const toArray = function toArray(xs) {
33};
44
55
6−export function _showCursor(cursor) {
6+exports._showCursor = function _showCursor(cursor) {
77 return '(IDBCursor ' +
88 '{ direction: ' + cursor.direction +
99 ', key: ' + cursor.key +
@@ -11,7 +11,7 @@ export function _showCursor(cursor) {
1111 ' })';
1212};
1313
14−export function _showDatabase(db) {
14+exports._showDatabase = function _showDatabase(db) {
1515 return '(IDBDatabase ' +
1616 '{ name: ' + db.name +
1717 ', objectStoreNames: [' + toArray(db.objectStoreNames).join(', ') + ']' +
@@ -19,7 +19,7 @@ export function _showDatabase(db) {
1919 ' })';
2020};
2121
22−export function _showIndex(index) {
22+exports._showIndex = function _showIndex(index) {
2323 return '(IDBIndex ' +
2424 '{ name: ' + index.name +
2525 ', keyPath: ' + index.keyPath +
@@ -28,7 +28,7 @@ export function _showIndex(index) {
2828 ' })';
2929};
3030
31−export function _showKeyRange(range) {
31+exports._showKeyRange = function _showKeyRange(range) {
3232 return '(IDBKeyRange ' +
3333 '{ lower: ' + range.lower +
3434 ', upper: ' + range.upper +
@@ -37,7 +37,7 @@ export function _showKeyRange(range) {
3737 ' })';
3838};
3939
40−export function _showObjectStore(store) {
40+exports._showObjectStore = function _showObjectStore(store) {
4141 return '(IDBObjectStore ' +
4242 '{ autoIncrement: ' + store.autoIncrement +
4343 ', indexNames: [' + toArray(store.indexNames).join(', ') + ']' +
@@ -46,7 +46,7 @@ export function _showObjectStore(store) {
4646 ' })';
4747};
4848
49−export function _showTransaction(tx) {
49+exports._showTransaction = function _showTransaction(tx) {
5050 return '(IDBTransaction ' +
5151 '{ error: ' + tx.error +
5252 ', mode: ' + tx.mode +
src/Database/IndexedDB/Core.purs+95−143
@@ -1,72 +1,39 @@
11-- | The Core module gathers types used across the library and provides basic Show instances for
22-- | those types.
3−module Database.IndexedDB.Core
4− -- * Effects
5− (
6− -- IDB
3+-- |
4+-- | NOTE:
5+-- | Errors normally thrown by the IDB* interfaces are wrapped in the `Aff` Monad as `Error`
6+-- | where the `message` corresponds to the error's name (e.g. "InvalidStateError").
7+-- | Pattern matching can therefore be done on any error message to handle specific errors thrown
8+-- | by the API.
79
8− -- * Types
9− Database
10+module Database.IndexedDB.Core
11+ ( class FromString, parse
12+ , IDB
13+ , CursorDirection(..)
14+ , CursorSource(..)
15+ , Database
1016 , Index
1117 , KeyCursor
1218 , KeyRange
19+ , KeyPath
1320 , ObjectStore
1421 , Transaction
15− , ValueCursor
16− , CursorDirection(..)
17− , CursorSource(..)
18− , KeyPath
1922 , TransactionMode(..)
20−
21− -- * Classes
22− , class IDBConcreteCursor
23− , class IDBCursor
24− , class IDBDatabase
25− , class IDBIndex
26− , class IDBKeyRange
27− , class IDBTransaction
28− , class IDBObjectStore
29−
30− -- * Re-Exports
23+ , ValueCursor
3124 , module Database.IndexedDB.IDBKey
3225 ) where
3326
34−import Prelude (class Show)
27+import Prelude (class Show)
3528
36−import Data.Maybe (Maybe(..))
37−import Data.String.Read (class Read)
29+import Control.Monad.Eff (kind Effect)
30+import Data.Maybe (Maybe(..))
3831
3932import Database.IndexedDB.IDBKey
4033
41−--------------------
42−-- TYPES
43−--
44−
45−-- | Each origin has an associated set of databases. A database has zero or more object
46−-- | stores which hold the data stored in the database.
47−foreign import data Database :: Type
48−
49−-- | An index allows looking up records in an object store using properties of the values
50−-- | in the object stores records.
51−foreign import data Index :: Type
52−
53−-- | A key range is a continuous interval over some data type used for keys.
54−foreign import data KeyRange :: Type
55−
56−-- | A cursor is used to iterate over a range of records in an index or an object store
57−-- | in a specific direction. A KeyCursor doesn't hold any value.
58−foreign import data KeyCursor :: Type
34+-- | IDB Effects, manifestation that something happened with the IndexedDB
35+foreign import data IDB :: Effect
5936
60−-- | A Transaction is used to interact with the data in a database.
61−-- | Whenever data is read or written to the database it is done by using a transaction.
62−foreign import data Transaction :: Type
63−
64−-- | An object store is the primary storage mechanism for storing data in a database.
65−foreign import data ObjectStore :: Type
66−
67−-- | A cursor is used to iterate over a range of records in an index or an object store
68−-- | in a specific direction. A ValueCursor also holds the value corresponding to matching key.
69−foreign import data ValueCursor :: Type
7037
7138-- | A cursor has a direction that determines whether it moves in monotonically
7239-- | increasing or decreasing order of the record keys when iterated, and if it
@@ -79,6 +46,7 @@ data CursorDirection
7946 | Prev
8047 | PrevUnique
8148
49+
8250-- | If the source of a cursor is an object store, the effective object store of
8351-- | the cursor is that object store and the effective key of the cursor is the
8452-- | cursor’s position. If the source of a cursor is an index, the effective object
@@ -88,155 +56,139 @@ data CursorSource
8856 = ObjectStore ObjectStore
8957 | Index Index
9058
91−-- | A key path is a list of strings that defines how to extract a key from a value.
92−-- | A valid key path is one of:
93−-- |
94−-- | - An empty list.
95−-- | - An singleton identifier, which is a string matching the IdentifierName production from the ECMAScript Language Specification [ECMA-262].
96−-- | - A singleton string consisting of two or more identifiers separated by periods (U+002E FULL STOP).
97−-- | - A non-empty list containing only strings conforming to the above requirements.
98−type KeyPath = Array String
9959
10060-- | A transaction has a mode that determines which types of interactions can be performed
10161-- | upon that transaction. The mode is set when the transaction is created and remains
10262-- | fixed for the life of the transaction.
10363data TransactionMode
104− = ReadOnly -- ^ The transaction is only allowed to read data.
105− | ReadWrite -- ^ The transaction is allowed to read, modify and delete data from existing object stores
106− | VersionChange -- ^ The transaction is allowed to read, modify and delete data from existing object stores, and can also create and remove object stores and indexes.
64+ = ReadOnly -- ^ The transaction is only allowed to read data.
65+ | ReadWrite -- ^ The transaction is allowed to read, modify and delete data from existing object stores
66+ | VersionChange -- ^ The transaction is allowed to read, modify and delete data from existing object stores, and can also create and remove object stores and indexes.
10767
108−--------------------
109−-- Classes
110−--
11168
112−-- | A concrete cursor not only shares IDBCursor properties, but also some
113−-- | specific attributes (see KeyCursor or CursorWithValue).
114−class (IDBCursor cursor) <= IDBConcreteCursor cursor
11569
116−-- | Cursor objects implement the IDBCursor interface.
117−-- | There is only ever one IDBCursor instance representing a given cursor.
118−-- | There is no limit on how many cursors can be used at the same time.
119−class IDBCursor cursor
70+-- | A key path is a list of strings that defines how to extract a key from a value.
71+-- | A valid key path is one of:
72+-- |
73+-- | - An empty list.
74+-- | - An singleton identifier, which is a string matching the IdentifierName production from the ECMAScript Language Specification [ECMA-262].
75+-- | - A singleton string consisting of two or more identifiers separated by periods (U+002E FULL STOP).
76+-- | - A non-empty list containing only strings conforming to the above requirements.
77+type KeyPath = Array String
12078
121−-- | The IDBDatabase interface represents a connection to a database.
122−class IDBDatabase db
12379
124−-- | The IDBIndex interface represents an index handle.
125−-- | Any of these methods throw an "TransactionInactiveError" DOMException
126−-- | if called when the transaction is not active.
127−class IDBIndex index
80+-- | Each origin has an associated set of databases. A database has zero or more object
81+-- | stores which hold the data stored in the database.
82+foreign import data Database :: Type
12883
129−-- | The IDBKeyRange interface represents a key range.
130−class IDBKeyRange range
84+-- | An index allows looking up records in an object store using properties of the values
85+-- | in the object stores records.
86+foreign import data Index :: Type
13187
132−-- | The IDBtransaction interface.
133−class IDBTransaction tx
13488
135−-- | The IDBObjectStore interface represents an object store handle.
136−class IDBObjectStore store
89+-- | A cursor is used to iterate over a range of records in an index or an object store
90+-- | in a specific direction. A KeyCursor doesn't hold any value.
91+foreign import data KeyCursor :: Type
13792
138−--------------------
139−-- INSTANCES
140−--
14193
142−instance idbCursorKeyCursor :: IDBCursor KeyCursor
94+-- | A key range is a continuous interval over some data type used for keys.
95+foreign import data KeyRange :: Type
14396
144−instance idbCursorValueCursor :: IDBCursor ValueCursor
14597
146−instance idbConcreteCursorKeyCursor :: IDBConcreteCursor KeyCursor
98+-- | An object store is the primary storage mechanism for storing data in a database.
99+foreign import data ObjectStore :: Type
147100
148−instance idbConcreteCursorValueCursor :: IDBConcreteCursor ValueCursor
149101
150−instance idbDatabaseDatabase :: IDBDatabase Database
102+-- | A Transaction is used to interact with the data in a database.
103+-- | Whenever data is read or written to the database it is done by using a transaction.
104+foreign import data Transaction :: Type
151105
152−instance idbIndexIndex :: IDBIndex Index
153106
154−instance idbIndexObjectStore :: IDBIndex ObjectStore
107+-- | A cursor is used to iterate over a range of records in an index or an object store
108+-- | in a specific direction. A ValueCursor also holds the value corresponding to matching key.
109+foreign import data ValueCursor :: Type
155110
156−instance idbKeyRangeKeyRange :: IDBKeyRange KeyRange
157111
158−instance idbObjectStoreObjectStore :: IDBObjectStore ObjectStore
112+-- | FromString represents enumerations that can be represented as Strings.
113+class FromString a where
114+ parse :: String -> Maybe a
159115
160−instance idbTransactionTransaction :: IDBTransaction Transaction
161116
117+foreign import _showCursor :: forall cursor. cursor -> String
162118instance showKeyCursor :: Show KeyCursor where
163119 show = _showCursor
164120
121+
165122instance showValueCursor :: Show ValueCursor where
166123 show = _showCursor
167124
125+
126+foreign import _showDatabase
127+ :: Database
128+ -> String
129+instance showDatabase :: Show Database where
130+ show = _showDatabase
131+
132+
133+foreign import _showIndex
134+ :: Index
135+ -> String
168136instance showIndex :: Show Index where
169137 show = _showIndex
170138
139+
140+foreign import _showKeyRange
141+ :: KeyRange
142+ -> String
171143instance showKeyRange :: Show KeyRange where
172144 show = _showKeyRange
173145
146+
147+foreign import _showObjectStore
148+ :: ObjectStore
149+ -> String
174150instance showObjectStore :: Show ObjectStore where
175151 show = _showObjectStore
176152
153+
154+foreign import _showTransaction
155+ :: Transaction
156+ -> String
177157instance showTransaction :: Show Transaction where
178158 show = _showTransaction
179159
180−instance showDatabase :: Show Database where
181− show = _showDatabase
182160
183161instance showCursorDirection :: Show CursorDirection where
184162 show x =
185163 case x of
186− Next -> "next"
164+ Next -> "next"
187165 NextUnique -> "nextunique"
188− Prev -> "prev"
166+ Prev -> "prev"
189167 PrevUnique -> "prevunique"
190168
169+
191170instance showTransactionMode :: Show TransactionMode where
192171 show x =
193172 case x of
194− ReadOnly -> "readonly"
195− ReadWrite -> "readwrite"
173+ ReadOnly -> "readonly"
174+ ReadWrite -> "readwrite"
196175 VersionChange -> "versionchange"
197176
198−instance readCursorDirection :: Read CursorDirection where
199− read s =
177+
178+instance fromStringCursorDirection :: FromString CursorDirection where
179+ parse s =
200180 case s of
201− "next" -> Just Next
181+ "next" -> Just Next
202182 "nextunique" -> Just NextUnique
203− "prev" -> Just Prev
183+ "prev" -> Just Prev
204184 "prevunique" -> Just PrevUnique
205− _ -> Nothing
185+ _ -> Nothing
186+
206187
207−instance readTransactionMode :: Read TransactionMode where
208− read s =
188+instance fromStringTransactionMode :: FromString TransactionMode where
189+ parse s =
209190 case s of
210− "readonly" -> Just ReadOnly
211− "readwrite" -> Just ReadWrite
191+ "readonly" -> Just ReadOnly
192+ "readwrite" -> Just ReadWrite
212193 "versionchange" -> Just VersionChange
213− _ -> Nothing
214−
215−--------------------
216−-- FFI
217−--
218−
219−foreign import _showCursor
220− :: forall cursor
221− . cursor
222− -> String
223−
224−foreign import _showDatabase
225− :: Database
226− -> String
227−
228−foreign import _showIndex
229− :: Index
230− -> String
231−
232−foreign import _showKeyRange
233− :: KeyRange
234− -> String
235−
236−foreign import _showObjectStore
237− :: ObjectStore
238− -> String
239−
240−foreign import _showTransaction
241− :: Transaction
242− -> String
194+ _ -> Nothing
src/Database/IndexedDB/IDBCursor.js+27−65
@@ -1,6 +1,6 @@
11const errorHandler = function errorHandler(cb) {
22 return function _handler(e) {
3− cb(e.target.error);
3+ cb(new Error(e.target.error.name));
44 };
55};
66
@@ -10,135 +10,97 @@ const successHandler = function successHandler(cb) {
1010 };
1111};
1212
13−export function _advance(cursor, count) {
14− return function aff(error, success) {
13+
14+exports._advance = function _advance(cursor, count) {
15+ return function aff(success, error) {
1516 try {
1617 cursor.advance(count);
1718 success();
1819 } catch (e) {
19− error(e);
20+ error(new Error(e.name));
2021 }
21−
22− return function canceler(_, cancelerError) {
23− cancelerError(new Error("Can't cancel IDB Effects"));
24− };
2522 };
2623};
2724
28−export function _continue(cursor, key) {
29− return function aff(error, success) {
25+exports._continue = function _continue(cursor, key) {
26+ return function aff(success, error) {
3027 try {
3128 cursor.continue(key || undefined);
32− success();
3329 } catch (e) {
34− error(e);
30+ error(new Error(e.name));
3531 }
36−
37− return function canceler(_, cancelerError) {
38− cancelerError(new Error("Can't cancel IDB Effects"));
39− };
4032 };
4133};
4234
43−export function _continuePrimaryKey(cursor, key, primaryKey) {
44− return function aff(error, success) {
35+exports._continuePrimaryKey = function _continuePrimaryKey(cursor, key, primaryKey) {
36+ return function aff(success, error) {
4537 try {
4638 cursor.continuePrimaryKey(key, primaryKey);
47− success();
4839 } catch (e) {
49− error(e);
40+ error(new Error(e.name));
5041 }
51−
52− return function canceler(_, cancelerError) {
53− cancelerError(new Error("Can't cancel IDB Effects"));
54− };
5542 };
5643};
5744
58−export function _delete(cursor) {
59− return function aff(error, success) {
45+exports._delete = function _delete(cursor) {
46+ return function aff(success, error) {
6047 try {
6148 const request = cursor.delete();
6249 request.onsuccess = successHandler(success);
6350 request.onerror = errorHandler(error);
6451 } catch (e) {
65− error(e);
52+ error(new Error(e.name));
6653 }
67−
68− return function canceler(_, cancelerError) {
69− cancelerError(new Error("Can't cancel IDB Effects"));
70− };
7154 };
7255};
7356
74−export function _direction(fromString, cursor) {
57+exports._direction = function _direction(fromString, cursor) {
7558 return fromString(cursor.direction);
7659};
7760
78−export function _key(cursor) {
79− return function aff(error, success) {
61+exports._key = function _key(cursor) {
62+ return function aff(success, error) {
8063 try {
8164 success(cursor.key);
8265 } catch (e) {
83− error(e);
66+ error(new Error(e.name));
8467 }
85−
86− return function canceler(_, cancelerError) {
87− cancelerError(new Error("Can't cancel IDB Effects"));
88− };
8968 };
9069};
9170
92−export function _primaryKey(cursor) {
93− return function aff(error, success) {
71+exports._primaryKey = function _primaryKey(cursor) {
72+ return function aff(success, error) {
9473 try {
9574 success(cursor.primaryKey);
9675 } catch (e) {
97− error(e);
76+ error(new Error(e.name));
9877 }
99−
100− return function canceler(_, cancelerError) {
101− cancelerError(new Error("Can't cancel IDB Effects"));
102− };
10378 };
10479};
10580
106−export function _source(IDBObjectStore, IDBIndex, cursor) {
81+exports._source = function _source(IDBObjectStore, IDBIndex, cursor) {
10782 switch (cursor.source.constructor.name) {
10883 case 'IDBIndex':
10984 return IDBIndex(cursor.source);
11085 case 'IDBObjectStore':
11186 return IDBObjectStore(cursor.source);
11287 default:
113− throw Object.create(Error.prototype, {
114− message: {
115− enumerable: true,
116− value: 'Unable to retrieve the cursor\'s source constructor.',
117− },
118− name: {
119− enumerable: true,
120− value: 'UnexpectedCursorSource',
121− },
122− });
88+ throw new Error('UnexpectedCursorSource');
12389 }
12490};
12591
126−export function _update(cursor, value) {
127− return function aff(error, success) {
92+exports._update = function _update(cursor, value) {
93+ return function aff(success, error) {
12894 try {
12995 const request = cursor.update(value);
13096 request.onsuccess = successHandler(success);
13197 request.onerror = errorHandler(error);
13298 } catch (e) {
133− error(e);
99+ error(new Error(e.name));
134100 }
135−
136− return function canceler(_, cancelerError) {
137− cancelerError(new Error("Can't cancel IDB Effects"));
138− };
139101 };
140102};
141103
142−export function _value(cursor) {
104+exports._value = function _value(cursor) {
143105 return cursor.value;
144106};
src/Database/IndexedDB/IDBCursor.purs+142−112
@@ -1,186 +1,216 @@
11-- | A cursor is used to iterate over a range of records in an index or
22-- | an object store in a specific direction.
33module Database.IndexedDB.IDBCursor
4− -- * Interface
5− ( advance
6− , continue
7− , continuePrimaryKey
8− , delete
9− , update
10−
11− -- * Attributes
4+ ( class IDBCursor, advance, continue, continuePrimaryKey, delete, update
125 , direction
136 , key
147 , primaryKey
158 , source
9+ , direction'
10+ , key'
11+ , primaryKey'
12+ , source'
1613 , value
1714 ) where
1815
16+import Prelude (Unit, (>>>))
17+
18+import Control.Monad.Aff (Aff)
19+import Data.Function.Uncurried as Fn
20+import Data.Function.Uncurried (Fn2, Fn3)
21+import Data.Maybe (Maybe)
22+import Data.Nullable (Nullable, toNullable)
23+import Data.Foreign (Foreign, toForeign, unsafeFromForeign)
24+
1925import Database.IndexedDB.Core
2026
21−import Data.Function.Uncurried (Fn2, Fn3)
22−import Data.Function.Uncurried as Fn
23−import Data.Maybe (Maybe)
24−import Data.Nullable (Nullable, toNullable)
25−import Data.String.Read (read)
26−import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, toKey, unsafeFromKey)
27−import Effect.Aff (Aff)
28−import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
29−import Foreign (Foreign, unsafeFromForeign, unsafeToForeign)
30−import Prelude (Unit, ($), (>>>), (<<<), map)
3127
3228--------------------
3329-- INTERFACES
3430--
31+-- | Cursor objects implement the IDBCursor interface.
32+-- | There is only ever one IDBCursor instance representing a given cursor.
33+-- | There is no limit on how many cursors can be used at the same time.
34+class IDBCursor cursor where
35+ -- | Advances the cursor through the next count records in range.
36+ advance :: forall e. cursor -> Int -> Aff (idb :: IDB | e) Unit
37+
38+ -- | Advances the cursor to the next record in range matching or after key.
39+ continue :: forall e. cursor -> Maybe Key -> Aff (idb :: IDB | e) Unit
40+
41+ -- | Advances the cursor to the next record in range matching or after key and primaryKey. Throws an "InvalidAccessError" DOMException if the source is not an index.
42+ continuePrimaryKey :: forall e. cursor -> Key -> Key -> Aff (idb :: IDB | e) Unit
43+
44+ -- | Delete the record pointed at by the cursor with a new value.
45+ delete :: forall e. cursor -> Aff (idb :: IDB | e) Unit
46+
47+ -- | Update the record pointed at by the cursor with a new value.
48+ -- |
49+ -- | Throws a "DataError" DOMException if the effective object store uses
50+ -- | in-line keys and the key would have changed.
51+ update :: forall val e. cursor -> val -> Aff (idb :: IDB | e) Key
3552
36−-- | Advances the cursor through the next count records in range.
37−advance
38− :: forall cursor
39− . (IDBCursor cursor)
40− => cursor
41− -> Int
42− -> Aff Unit
43−advance c =
44− fromEffectFnAff <<< Fn.runFn2 _advance c
45−
46−-- | Advances the cursor to the next record in range matching or after key.
47−continue
48− :: forall k cursor
49− . (IDBKey k)
50− => (IDBCursor cursor)
51− => cursor
52− -> Maybe k
53− -> Aff Unit
54−continue c mk =
55− fromEffectFnAff $ Fn.runFn2 _continue c (toNullable $ map (toKey >>> unsafeFromKey) mk)
56−
57−-- | Advances the cursor to the next record in range matching or after key and primaryKey. Throws an "InvalidAccessError" DOMException if the source is not an index.
58−continuePrimaryKey
59− :: forall k cursor
60− . (IDBKey k)
61− => (IDBCursor cursor)
62− => cursor
63− -> k
64− -> k
65− -> Aff Unit
66−continuePrimaryKey c k1 k2 =
67− fromEffectFnAff $ Fn.runFn3 _continuePrimaryKey c (unsafeFromKey $ toKey k1) (unsafeFromKey $ toKey k2)
68−
69−-- | Delete the record pointed at by the cursor with a new value.
70−delete
71− :: forall cursor
72− . (IDBCursor cursor)
73− => cursor
74− -> Aff Unit
75−delete =
76− fromEffectFnAff <<< _delete
77−
78−-- | Update the record pointed at by the cursor with a new value.
79−-- |
80−-- | Throws a "DataError" DOMException if the effective object store uses
81−-- | in-line keys and the key would have changed.
82−update
83− :: forall val cursor
84− . (IDBCursor cursor)
85− => cursor
86− -> val
87− -> Aff Key
88−update c =
89− map toKey <<< fromEffectFnAff <<< Fn.runFn2 _update c <<< unsafeToForeign
9053
9154--------------------
9255-- ATTRIBUTES
9356--
94−
9557-- | Returns the direction (Next|NextUnique|Prev|PrevUnique) of the cursor.
9658direction
97− :: forall cursor
98− . (IDBConcreteCursor cursor)
99− => cursor
59+ :: KeyCursor
10060 -> CursorDirection
10161direction =
102− Fn.runFn2 _direction (read >>> toNullable)
62+ Fn.runFn2 _direction (parse >>> toNullable)
63+
10364
10465-- | Returns the key of the cursor. Throws a "InvalidStateError" DOMException
10566-- | if the cursor is advancing or is finished.
10667key
107− :: forall cursor
108− . (IDBConcreteCursor cursor)
109− => cursor
110− -> Aff Key
68+ :: forall e
69+ . KeyCursor
70+ -> Aff (idb :: IDB | e) Key
11171key =
112− map toKey <<< fromEffectFnAff <<< _key
72+ _key
73+
11374
11475-- | Returns the effective key of the cursor. Throws a "InvalidStateError" DOMException
11576-- | if the cursor is advancing or is finished.
11677primaryKey
117− :: forall cursor
118− . (IDBConcreteCursor cursor)
119− => cursor
120− -> Aff Key
78+ :: forall e
79+ . KeyCursor
80+ -> Aff (idb :: IDB | e) Key
12181primaryKey =
122− map toKey <<< fromEffectFnAff <<< _primaryKey
82+ _primaryKey
83+
12384
12485-- | Returns the IDBObjectStore or IDBIndex the cursor was opened from.
12586source
126− :: forall cursor
127− . (IDBConcreteCursor cursor)
128− => cursor
87+ :: KeyCursor
12988 -> CursorSource
13089source =
13190 Fn.runFn3 _source ObjectStore Index
13291
92+
93+-- NOTE: Polymorphism not used here cause *we want* those methods to be distincs for
94+-- the concrete types mostly for consistency with the Database.IndexedDB.IDBIndex module
95+-- where the IDBIndex interfaces is shared by Index and ObjectStore, but attributes of
96+-- respective concrete types aren't shared.
97+
98+
99+-- | Returns the direction (Next|NextUnique|Prev|PrevUnique) of the cursor.
100+direction'
101+ :: ValueCursor
102+ -> CursorDirection
103+direction' =
104+ Fn.runFn2 _direction (parse >>> toNullable)
105+
106+
107+-- | Returns the key of the cursor. Throws a "InvalidStateError" DOMException
108+-- | if the cursor is advancing or is finished.
109+key'
110+ :: forall e
111+ . ValueCursor
112+ -> Aff (idb :: IDB | e) Key
113+key' =
114+ _key
115+
116+
117+-- | Returns the effective key of the cursor. Throws a "InvalidStateError" DOMException
118+-- | if the cursor is advancing or is finished.
119+primaryKey'
120+ :: forall e
121+ . ValueCursor
122+ -> Aff (idb :: IDB | e) Key
123+primaryKey' =
124+ _primaryKey
125+
126+
127+-- | Returns the IDBObjectStore or IDBIndex the cursor was opened from.
128+source'
129+ :: ValueCursor
130+ -> CursorSource
131+source' =
132+ Fn.runFn3 _source ObjectStore Index
133+
134+
133135value
134136 :: forall val
135− . ValueCursor
137+ . ValueCursor
136138 -> val
137139value =
138140 _value >>> unsafeFromForeign
139141
142+
140143--------------------
141−-- FFI
144+-- INSTANCES
142145--
146+instance keyCursorKeyCursor :: IDBCursor KeyCursor where
147+ advance = Fn.runFn2 _advance
148+ continue c mk = Fn.runFn2 _continue c (toNullable mk)
149+ continuePrimaryKey = Fn.runFn3 _continuePrimaryKey
150+ delete = _delete
151+ update c = toForeign >>> Fn.runFn2 _update c
143152
153+
154+instance valueCursorKeyCursor :: IDBCursor ValueCursor where
155+ advance = Fn.runFn2 _advance
156+ continue c mk = Fn.runFn2 _continue c (toNullable mk)
157+ continuePrimaryKey = Fn.runFn3 _continuePrimaryKey
158+ delete = _delete
159+ update c = toForeign >>> Fn.runFn2 _update c
160+
161+
162+--------------------
163+-- FFI
164+--
144165foreign import _advance
145− :: forall cursor
146− . Fn2 cursor Int (EffectFnAff Unit)
166+ :: forall cursor e
167+ . Fn2 cursor Int (Aff (idb :: IDB | e) Unit)
168+
147169
148170foreign import _continue
149− :: forall cursor
150− . Fn2 cursor (Nullable Foreign) (EffectFnAff Unit)
171+ :: forall cursor e
172+ . Fn2 cursor (Nullable Key) (Aff (idb :: IDB | e) Unit)
173+
151174
152175foreign import _continuePrimaryKey
153− :: forall cursor
154− . Fn3 cursor Foreign Foreign (EffectFnAff Unit)
176+ :: forall cursor e
177+ . Fn3 cursor Key Key (Aff (idb :: IDB | e) Unit)
178+
155179
156180foreign import _delete
157− :: forall cursor
158− . cursor
159− -> (EffectFnAff Unit)
181+ :: forall cursor e
182+ . cursor
183+ -> (Aff (idb :: IDB | e) Unit)
184+
160185
161186foreign import _direction
162187 :: forall cursor
163− . Fn2 (String -> Nullable CursorDirection) cursor CursorDirection
188+ . Fn2 (String -> Nullable CursorDirection) cursor CursorDirection
189+
164190
165191foreign import _key
166− :: forall cursor
167− . cursor
168− -> EffectFnAff Key
192+ :: forall cursor e
193+ . cursor
194+ -> Aff (idb :: IDB | e) Key
195+
169196
170197foreign import _primaryKey
171− :: forall cursor
172− . cursor
173− -> EffectFnAff Key
198+ :: forall cursor e
199+ . cursor
200+ -> Aff (idb :: IDB | e) Key
201+
174202
175203foreign import _source
176204 :: forall cursor
177− . Fn3 (ObjectStore -> CursorSource) (Index -> CursorSource) cursor CursorSource
205+ . Fn3 (ObjectStore -> CursorSource) (Index -> CursorSource) cursor CursorSource
206+
178207
179208foreign import _update
180− :: forall cursor
181− . Fn2 cursor Foreign (EffectFnAff Foreign)
209+ :: forall cursor e
210+ . Fn2 cursor Foreign (Aff (idb :: IDB | e) Key)
211+
182212
183213foreign import _value
184214 :: forall cursor val
185− . cursor
215+ . cursor
186216 -> val
src/Database/IndexedDB/IDBDatabase.js+28−62
@@ -1,25 +1,21 @@
1−const toArray = function toArray(xs) {
2− return Array.prototype.slice.apply(xs);
3−};
1+const $Core = require('Database.IndexedDB.Core/foreign');
2+
3+const toArray = $Core.toArray;
44
55
6−export function _close(db) {
7− return function aff(error, success) {
6+exports._close = function _close(db) {
7+ return function aff(success, error) {
88 try {
99 db.close();
1010 success();
1111 } catch (e) {
12− error(e);
12+ error(new Error(e.name));
1313 }
14−
15− return function canceler(_, cancelerError) {
16− cancelerError(new Error("Can't cancel IDB Effects"));
17− };
1814 };
1915};
2016
21−export function _createObjectStore(db, name, opts) {
22− return function aff(error, success) {
17+exports._createObjectStore = function _createObjectStore(db, name, opts) {
18+ return function aff(success, error) {
2319 var keyPath;
2420
2521 try {
@@ -43,107 +39,77 @@ export function _createObjectStore(db, name, opts) {
4339 });
4440 success(store);
4541 } catch (e) {
46− error(e);
42+ error(new Error(e.name));
4743 }
48−
49− return function canceler(_, cancelerError) {
50− cancelerError(new Error("Can't cancel IDB Effects"));
51− };
5244 };
5345};
5446
55−export function _deleteObjectStore(db, name) {
56− return function aff(error, success) {
47+exports._deleteObjectStore = function _deleteObjectStore(db, name) {
48+ return function aff(success, error) {
5749 try {
5850 db.deleteObjectStore(name);
5951 success();
6052 } catch (e) {
61− error(e);
53+ error(new Error(e.name));
6254 }
63−
64− return function canceler(_, cancelerError) {
65− cancelerError(new Error("Can't cancel IDB Effects"));
66− };
6755 };
6856};
6957
70−export function _name(db) {
58+exports._name = function _name(db) {
7159 return db.name;
7260};
7361
74−export function _objectStoreNames(db) {
62+exports._objectStoreNames = function _objectStoreNames(db) {
7563 return toArray(db.objectStoreNames);
7664};
7765
78−export function _onAbort(db, f) {
79− return function aff(error, success) {
66+exports._onAbort = function _onAbort(db, f) {
67+ return function aff(success) {
8068 db.onabort = function onabort() {
8169 f();
8270 };
8371 success();
84−
85− return function canceler(_, cancelerError) {
86− cancelerError(new Error("Can't cancel IDB Effects"));
87− };
8872 };
8973};
9074
91−export function _onClose(db, f) {
92− return function aff(error, success) {
75+exports._onClose = function _onClose(db, f) {
76+ return function aff(success) {
9377 db.onclose = function onclose() {
9478 f();
9579 };
9680 success();
97−
98− return function canceler(_, cancelerError) {
99− cancelerError(new Error("Can't cancel IDB Effects"));
100− };
10181 };
10282};
10383
104−export function _onError(db, f) {
105− return function aff(error, success) {
84+exports._onError = function _onError(db, f) {
85+ return function aff(success) {
10686 db.onerror = function onerror(e) {
107− f(e.target.error)();
87+ f(new Error(e.target.error.name))();
10888 };
10989 success();
110−
111− return function canceler(_, cancelerError) {
112− cancelerError(new Error("Can't cancel IDB Effects"));
113− };
11490 };
11591};
11692
117−export function _onVersionChange(db, f) {
118− return function aff(error, success) {
93+exports._onVersionChange = function _onVersionChange(db, f) {
94+ return function aff(success) {
11995 db.onversionchange = function onversionchange(e) {
12096 f({ oldVersion: e.oldVersion, newVersion: e.newVersion })();
12197 };
12298 success();
123−
124− return function canceler(_, cancelerError) {
125− cancelerError(new Error("Can't cancel IDB Effects"));
126− };
12799 };
128100};
129101
130−export function _transaction(db, stores, mode) {
131− return function aff(error, success) {
132− var transaction;
102+exports._transaction = function _transaction(db, stores, mode) {
103+ return function aff(success, error) {
133104 try {
134− transaction = db.transaction(stores, mode);
105+ const transaction = db.transaction(stores, mode);
135106 success(transaction);
136107 } catch (e) {
137− error(e);
108+ error(new Error(e.name));
138109 }
139−
140− return function canceler(_, cancelerError, cancelerSuccess) {
141− transaction.abort();
142− cancelerSuccess();
143− };
144110 };
145111};
146112
147−export function _version(db) {
113+exports._version = function _version(db) {
148114 return db.version;
149115};
src/Database/IndexedDB/IDBDatabase.purs+113−131
@@ -1,215 +1,197 @@
11-- | Each origin has an associated set of databases. A database has zero or more object
22-- | stores which hold the data stored in the database.
33module Database.IndexedDB.IDBDatabase
4− -- * Types
5− ( StoreName
6− , ObjectStoreParameters
7− , defaultParameters
8−
9− -- * Interface
10− , close
11− , createObjectStore
12− , deleteObjectStore
13− , transaction
14−
15− -- * Attributes
4+ ( class IDBDatabase, close, createObjectStore, deleteObjectStore, transaction
5+ , StoreName
166 , name
177 , objectStoreNames
188 , version
19−
20− -- * Event handlers
219 , onAbort
2210 , onClose
2311 , onError
2412 , onVersionChange
2513 ) where
2614
15+import Prelude (Unit, show)
16+
17+import Control.Monad.Aff (Aff)
18+import Control.Monad.Eff (Eff)
19+import Control.Monad.Eff.Exception (Error)
20+import Data.Function.Uncurried as Fn
21+import Data.Function.Uncurried (Fn2, Fn3)
22+
2723import Database.IndexedDB.Core
24+import Database.IndexedDB.IDBObjectStore (IDBObjectStoreParameters)
2825
29−import Data.Function.Uncurried (Fn2, Fn3)
30−import Data.Function.Uncurried as Fn
31−import Effect (Effect)
32−import Effect.Aff (Aff)
33−import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
34−import Effect.Exception (Error)
35−import Prelude (Unit, show, (<<<), ($))
3626
3727--------------------
38−-- TYPES
28+-- INTERFACE
3929--
30+-- | The IDBDatabase interface represents a connection to a database.
31+class IDBDatabase db where
32+ -- | Closes the connection once all running transactions have finished.
33+ close :: forall e. db -> Aff (idb :: IDB | e) Unit
4034
41−-- | Type alias for StoreName
42−type StoreName = String
35+ -- | Creates a new object store with the given name and options and returns a new IDBObjectStore.
36+ -- |
37+ -- | Throws a "InvalidStateError" DOMException if not called within an upgrade transaction
38+ createObjectStore :: forall e. db -> StoreName -> IDBObjectStoreParameters -> Aff (idb :: IDB | e) ObjectStore
4339
44−-- | Options provided when creating an object store.
45−type ObjectStoreParameters =
46− { keyPath :: KeyPath
47− , autoIncrement :: Boolean
48− }
40+ -- | Deletes the object store with the given name.
41+ -- |
42+ -- | Throws a "InvalidStateError" DOMException if not called within an upgrade transaction.
43+ deleteObjectStore :: forall e. db -> StoreName -> Aff (idb :: IDB | e) ObjectStore
4944
50−defaultParameters :: ObjectStoreParameters
51−defaultParameters =
52− { keyPath: []
53− , autoIncrement: false
54− }
45+ -- | Returns a new transaction with the given mode (ReadOnly|ReadWrite)
46+ -- | and scope which in the form of an array of object store names.
47+ transaction :: forall e. db -> Array StoreName -> TransactionMode -> Aff (idb :: IDB | e) Transaction
5548
56−--------------------
57−-- INTERFACE
58−--
5949
60−-- | Closes the connection once all running transactions have finished.
61−close
62− :: forall db
63− . (IDBDatabase db)
64− => db
65− -> Aff Unit
66−close =
67− fromEffectFnAff <<< _close
68−
69−-- | Creates a new object store with the given name and options and returns a new IDBObjectStore.
70−-- |
71−-- | Throws a "InvalidStateError" DOMException if not called within an upgrade transaction
72−createObjectStore
73− :: forall db
74− . (IDBDatabase db)
75− => db
76− -> StoreName
77− -> ObjectStoreParameters
78− -> Aff ObjectStore
79−createObjectStore db name' opts =
80− fromEffectFnAff $ Fn.runFn3 _createObjectStore db name' opts
81−
82−-- | Deletes the object store with the given name.
83−-- |
84−-- | Throws a "InvalidStateError" DOMException if not called within an upgrade transaction.
85−deleteObjectStore
86− :: forall db
87− . (IDBDatabase db)
88− => db
89− -> StoreName
90− -> Aff ObjectStore
91−deleteObjectStore db name' =
92− fromEffectFnAff $ Fn.runFn2 _deleteObjectStore db name'
93−
94−-- | Returns a new transaction with the given mode (ReadOnly|ReadWrite)
95−-- | and scope which in the form of an array of object store names.
96−transaction
97− :: forall db
98− . (IDBDatabase db)
99− => db
100− -> Array StoreName
101− -> TransactionMode
102− -> Aff Transaction
103−transaction db stores mode' =
104− fromEffectFnAff $ Fn.runFn3 _transaction db stores (show mode')
50+type StoreName = String
51+
10552
10653--------------------
10754-- ATTRIBUTES
10855--
109−
11056-- | Returns the name of the database.
11157name
112− :: Database
113− -> String
58+ :: Database
59+ -> String
11460name =
11561 _name
11662
63+
11764-- | Returns a list of the names of object stores in the database.
11865objectStoreNames
119− :: Database
120− -> Array String
66+ :: Database
67+ -> Array String
12168objectStoreNames =
12269 _objectStoreNames
12370
71+
12472-- | Returns the version of the database.
12573version
126− :: Database
127− -> Int
74+ :: Database
75+ -> Int
12876version =
12977 _version
13078
79+
13180--------------------
13281-- EVENT HANDLERS
13382--
13483-- | Event handler for the `abort` event.
13584onAbort
136− :: Database
137− -> Effect Unit
138− -> Aff Unit
85+ :: forall e e'
86+ . Database
87+ -> Eff ( | e') Unit
88+ -> Aff (idb :: IDB | e) Unit
13989onAbort db f =
140− fromEffectFnAff $ Fn.runFn2 _onAbort db f
90+ Fn.runFn2 _onAbort db f
91+
14192
14293-- | Event handler for the `close` event.
14394onClose
144− :: Database
145− -> Effect Unit
146− -> Aff Unit
95+ :: forall e e'
96+ . Database
97+ -> Eff ( | e') Unit
98+ -> Aff (idb :: IDB | e) Unit
14799onClose db f =
148− fromEffectFnAff $ Fn.runFn2 _onClose db f
100+ Fn.runFn2 _onClose db f
101+
149102
150103-- | Event handler for the `error` event.
151104onError
152− :: Database
153− -> (Error -> Effect Unit)
154− -> Aff Unit
105+ :: forall e e'
106+ . Database
107+ -> (Error -> Eff ( | e') Unit)
108+ -> Aff (idb :: IDB | e) Unit
155109onError db f =
156− fromEffectFnAff $ Fn.runFn2 _onError db f
110+ Fn.runFn2 _onError db f
111+
157112
158113-- | Event handler for the `versionchange` event.
159114onVersionChange
160− :: Database
161− -> ( { oldVersion :: Int, newVersion :: Int }
162− -> Effect Unit
163− )
164− -> Aff Unit
115+ :: forall e e'
116+ . Database
117+ -> ({ oldVersion :: Int, newVersion :: Int }
118+ -> Eff ( | e') Unit)
119+ -> Aff (idb :: IDB | e) Unit
165120onVersionChange db f =
166− fromEffectFnAff $ Fn.runFn2 _onVersionChange db f
121+ Fn.runFn2 _onVersionChange db f
122+
167123
168124--------------------
169−-- FFI
125+-- INSTANCES
170126--
127+instance idbDatabaseDatabase :: IDBDatabase Database where
128+ close =
129+ _close
130+
131+ createObjectStore db name' opts =
132+ Fn.runFn3 _createObjectStore db name' opts
133+
134+ deleteObjectStore db name' =
135+ Fn.runFn2 _deleteObjectStore db name'
136+
137+ transaction db stores mode' =
138+ Fn.runFn3 _transaction db stores (show mode')
139+
171140
141+--------------------
142+-- FFI
143+--
172144foreign import _close
173− :: forall db
174− . db
175− -> EffectFnAff Unit
145+ :: forall db e
146+ . db
147+ -> Aff (idb :: IDB | e) Unit
148+
176149
177150foreign import _createObjectStore
178− :: forall db
179− . Fn3 db String { keyPath :: Array String, autoIncrement :: Boolean } (EffectFnAff ObjectStore)
151+ :: forall db e
152+ . Fn3 db String { keyPath :: Array String, autoIncrement :: Boolean } (Aff (idb :: IDB | e) ObjectStore)
153+
180154
181155foreign import _deleteObjectStore
182− :: forall db
183− . Fn2 db String (EffectFnAff ObjectStore)
156+ :: forall db e
157+ . Fn2 db String (Aff (idb :: IDB | e) ObjectStore)
158+
184159
185160foreign import _name
186− :: Database
187− -> String
161+ :: Database
162+ -> String
163+
188164
189165foreign import _objectStoreNames
190− :: Database
191− -> Array String
166+ :: Database
167+ -> Array String
168+
192169
193170foreign import _onAbort
194− :: forall db
195− . Fn2 db (Effect Unit) (EffectFnAff Unit)
171+ :: forall db e e'
172+ . Fn2 db (Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
173+
196174
197175foreign import _onClose
198− :: forall db
199− . Fn2 db (Effect Unit) (EffectFnAff Unit)
176+ :: forall db e e'
177+ . Fn2 db (Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
178+
200179
201180foreign import _onError
202− :: forall db
203− . Fn2 db (Error -> Effect Unit) (EffectFnAff Unit)
181+ :: forall db e e'
182+ . Fn2 db (Error -> Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
183+
204184
205185foreign import _onVersionChange
206− :: forall db
207− . Fn2 db ({ oldVersion :: Int, newVersion :: Int } -> Effect Unit) (EffectFnAff Unit)
186+ :: forall db e e'
187+ . Fn2 db ({ oldVersion :: Int, newVersion :: Int } -> Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
188+
208189
209190foreign import _transaction
210− :: forall db
211− . Fn3 db (Array String) String (EffectFnAff Transaction)
191+ :: forall db e
192+ . Fn3 db (Array String) String (Aff (idb :: IDB | e) Transaction)
193+
212194
213195foreign import _version
214− :: Database
215− -> Int
196+ :: Database
197+ -> Int
src/Database/IndexedDB/IDBFactory.js+8−21
@@ -1,6 +1,6 @@
11const errorHandler = function errorHandler(cb) {
22 return function _handler(e) {
3− cb(e.target.error);
3+ cb(new Error(e.target.error.name));
44 };
55};
66
@@ -14,12 +14,9 @@ const noOp2 = function noOp2() {
1414 return noOp;
1515};
1616
17−const noOp3 = function noOp3() {
18− return noOp2;
19−};
2017
21−export function _deleteDatabase(name) {
22− return function aff(error, success) {
18+exports._deleteDatabase = function _deleteDatabase(name) {
19+ return function aff(success, error) {
2320 try {
2421 const request = indexedDB.deleteDatabase(name);
2522
@@ -29,19 +26,15 @@ export function _deleteDatabase(name) {
2926
3027 request.onerror = errorHandler(error);
3128 } catch (e) {
32− error(e);
29+ error(new Error(e.name));
3330 }
34−
35− return function canceler(_, cancelerError) {
36− cancelerError(new Error("Can't cancel IDB Effects"));
37− };
3831 };
3932};
4033
41−export function _open(fromMaybe, name, mver, req) {
34+exports._open = function _open(fromMaybe, name, mver, req) {
4235 const ver = fromMaybe(undefined)(mver);
4336
44− return function aff(error, success) {
37+ return function aff(success, error) {
4538 try {
4639 const request = indexedDB.open(name, ver);
4740 request.onsuccess = function onSuccess(e) {
@@ -53,18 +46,12 @@ export function _open(fromMaybe, name, mver, req) {
5346 };
5447
5548 request.onupgradeneeded = function onUpgradeNeeded(e) {
56− const meta = { oldVersion: e.oldVersion };
57− // eslint-disable-next-line max-len
58− fromMaybe(noOp3)(req.onUpgradeNeeded)(e.target.result)(e.target.transaction)(meta)();
49+ fromMaybe(noOp2)(req.onUpgradeNeeded)(e.target.result)(e.target.transaction)();
5950 };
6051
6152 request.onerror = errorHandler(error);
6253 } catch (e) {
63− error(e);
54+ error(new Error(e.name));
6455 }
65−
66− return function canceler(_, cancelerError) {
67− cancelerError(new Error("Can't cancel IDB Effects"));
68− };
6956 };
7057};
src/Database/IndexedDB/IDBFactory.purs+32−36
@@ -1,55 +1,47 @@
11-- | Database objects are accessed through methods on the IDBFactory interface.
22-- | A single object implementing this interface is present in the global scope
33-- | of environments that support Indexed DB operations.
4−module Database.IndexedDB.IDBFactory
5− -- * Types
6− ( Callbacks
7− , DatabaseName
8− , Version
4+module Database.IndexedDB.IDBFactory where
95
10− -- * Interface
11− , deleteDatabase
12− , open
13− ) where
6+import Prelude (Unit)
7+
8+import Control.Monad.Aff (Aff)
9+import Control.Monad.Eff (Eff)
10+import Data.Function.Uncurried as Fn
11+import Data.Function.Uncurried (Fn4)
12+import Data.Maybe (Maybe, fromMaybe)
1413
1514import Database.IndexedDB.Core
1615
17−import Data.Function.Uncurried (Fn4)
18−import Data.Function.Uncurried as Fn
19−import Data.Maybe (Maybe, fromMaybe)
20−import Effect (Effect)
21−import Effect.Aff (Aff)
22−import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
23−import Prelude (Unit, ($), (<<<))
2416
2517--------------------
26−-- TYPES
18+-- INTERFACE
2719--
28−
2920-- Type alias for binding listeners to an initial open action.
30−type Callbacks =
31− { onBlocked :: Maybe (Effect Unit)
32− , onUpgradeNeeded :: Maybe (Database -> Transaction -> { oldVersion :: Int } -> Effect Unit)
21+type OpenRequest e =
22+ { onBlocked :: Maybe (Eff (| e) Unit)
23+ , onUpgradeNeeded :: Maybe (Database -> Transaction -> Eff (| e) Unit)
3324 }
3425
26+
3527-- | Type alias for DatabaseName.
3628type DatabaseName = String
3729
30+
3831-- | Type alias for Version.
3932type Version = Int
4033
41−--------------------
42−-- INTERFACE
43−--
4434
4535-- | Attempts to delete the named database. If the database already exists
4636-- | and there are open connections that don’t close in response to a
4737-- | `versionchange` event, the request will be blocked until all they close.
4838deleteDatabase
49− :: DatabaseName
50− -> Aff Int
39+ :: forall e
40+ . DatabaseName
41+ -> Aff (idb :: IDB | e) Int
5142deleteDatabase =
52− fromEffectFnAff <<< _deleteDatabase
43+ _deleteDatabase
44+
5345
5446-- | Attempts to open a connection to the named database with the specified version.
5547-- | If the database already exists with a lower version and there are open connections
@@ -60,20 +52,24 @@ deleteDatabase =
6052-- | When the version isn't provided (`Nothing`), attempts to open a connection to the
6153-- | named database with the current version, or 1 if it does not already exist.
6254open
63− :: DatabaseName
64− -> Maybe Version
65− -> Callbacks
66− -> Aff Database
55+ :: forall e e'
56+ . DatabaseName
57+ -> Maybe Version
58+ -> OpenRequest e'
59+ -> Aff (idb :: IDB | e) Database
6760open name mver req =
68− fromEffectFnAff $ Fn.runFn4 _open fromMaybe name mver req
61+ Fn.runFn4 _open fromMaybe name mver req
62+
6963
7064--------------------
7165-- FFI
7266--
7367foreign import _deleteDatabase
74− :: String
75− -> EffectFnAff Int
68+ :: forall e
69+ . String
70+ -> Aff (idb :: IDB | e) Int
71+
7672
7773foreign import _open
78− :: forall a
79− . Fn4 (a -> Maybe a -> a) String (Maybe Int) (Callbacks) (EffectFnAff Database)
74+ :: forall a e e'
75+ . Fn4 (a -> Maybe a -> a) String (Maybe Int) (OpenRequest e') (Aff (idb :: IDB | e) Database)
src/Database/IndexedDB/IDBIndex.jsdeleted−171
@@ -1,171 +0,0 @@
1−const errorHandler = function errorHandler(cb) {
2− return function _handler(e) {
3− cb(e.target.error);
4− };
5−};
6−
7−const successHandler = function successHandler(cb) {
8− return function _handler(e) {
9− cb(e.target.result);
10− };
11−};
12−
13−
14−export function _keyPath(index) {
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 [];
26−};
27−
28−export function _multiEntry(index) {
29− return index.multiEntry;
30−};
31−
32−export function _name(index) {
33− return index.name;
34−};
35−
36−export function _objectStore(index) {
37− return index.obectStore;
38−};
39−
40−export function _unique(index) {
41− return index.unique;
42−};
43−
44−export function _count(index, query) {
45− return function aff(error, success) {
46− try {
47− const request = index.count(query);
48− request.onsuccess = successHandler(success);
49− request.onerror = errorHandler(error);
50− } catch (e) {
51− error(e);
52− }
53−
54− return function canceler(_, cancelerError) {
55− cancelerError(new Error("Can't cancel IDB Effects"));
56− };
57− };
58−};
59−
60−export function _get(index, range) {
61− return function aff(error, success) {
62− try {
63− const request = index.get(range);
64− request.onsuccess = successHandler(success);
65− request.onerror = errorHandler(error);
66− } catch (e) {
67− error(e);
68− }
69−
70− return function canceler(_, cancelerError) {
71− cancelerError(new Error("Can't cancel IDB Effects"));
72− };
73− };
74−};
75−
76−/*
77− * NOTE: Require some additional work. The array (which isn't necessarily a list of
78− * polymorphic types in js) can't be easily translated to a PureScript array.
79− *
80− * However, it may be doable to convert the result to some key / value structure with values of
81− * different types.
82−export function _getAll(index, query, count) {
83− return function aff(error, success) {
84− const request = index.getAll(query, count);
85− request.onsuccess = successHandler(success);
86− request.onerror = errorHandler(error);
87− };
88−};
89−*/
90−
91−export function _getAllKeys(index, range, count) {
92− return function aff(error, success) {
93− try {
94− const request = index.getAllKeys(range, count || undefined);
95− request.onsuccess = successHandler(success);
96− request.onerror = errorHandler(error);
97− } catch (e) {
98− error(e);
99− }
100−
101− return function canceler(_, cancelerError) {
102− cancelerError(new Error("Can't cancel IDB Effects"));
103− };
104− };
105−};
106−
107−export function _getKey(index, range) {
108− return function aff(error, success) {
109− try {
110− const request = index.getKey(range);
111− request.onsuccess = successHandler(success);
112− request.onerror = errorHandler(error);
113− } catch (e) {
114− error(e);
115− }
116−
117− return function canceler(_, cancelerError) {
118− cancelerError(new Error("Can't cancel IDB Effects"));
119− };
120− };
121−};
122−
123−export function _openCursor(index, query, dir, cb) {
124− return function aff(error, success) {
125− try {
126− const request = index.openCursor(query, dir);
127− request.onsuccess = function onSuccess(e) {
128− if (e.target.result != null) {
129− cb.onSuccess(e.target.result)();
130− } else {
131− cb.onComplete();
132− }
133− };
134− request.onerror = function onError(e) {
135− cb.onError(e.target.error);
136− };
137− success();
138− } catch (e) {
139− error(e);
140− }
141−
142− return function canceler(_, cancelerError) {
143− cancelerError(new Error("Can't cancel IDB Effects"));
144− };
145− };
146−};
147−
148−export function _openKeyCursor(index, query, dir, cb) {
149− return function aff(error, success) {
150− try {
151− const request = index.openKeyCursor(query, dir);
152− request.onsuccess = function onSuccess(e) {
153− if (e.target.result != null) {
154− cb.onSuccess(e.target.result)();
155− } else {
156− cb.onComplete();
157− }
158− };
159− request.onerror = function onError(e) {
160− cb.onError(e.target.error);
161− };
162− success();
163− } catch (e) {
164− error(e);
165− }
166−
167− return function canceler(_, cancelerError) {
168− cancelerError(new Error("Can't cancel IDB Effects"));
169− };
170− };
171−};
src/Database/IndexedDB/IDBIndex.purs+7−197
@@ -1,206 +1,16 @@
11-- | An index allows looking up records in an object store using properties of the values
22-- | in the object stores records.
33module Database.IndexedDB.IDBIndex
4− -- * Types
5− ( Callbacks
6−
7− -- * Interface
8− , count
9− , get
10− , getAllKeys
11− , getKey
12− , openCursor
13− , openKeyCursor
4+ ( module Database.IndexedDB.IDBIndex.Internal
5+ ) where
146
15− -- * Attributes
7+import Database.IndexedDB.IDBIndex.Internal
8+ ( class IDBIndex, count, get, getAllKeys, getKey, openCursor, openKeyCursor
9+ , IDBIndexParameters
1610 , keyPath
1711 , multiEntry
1812 , name
1913 , objectStore
2014 , unique
21− ) where
22−
23−import Database.IndexedDB.Core
24−
25−import Data.Function.Uncurried (Fn2, Fn3, Fn4)
26−import Data.Function.Uncurried as Fn
27−import Data.Maybe (Maybe)
28−import Data.Nullable (Nullable, toMaybe, toNullable)
29−import Database.IndexedDB.IDBKey.Internal (Key, toKey)
30−import Effect (Effect)
31−import Effect.Aff (Aff)
32−import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
33−import Effect.Exception (Error)
34−import Foreign (Foreign, unsafeFromForeign)
35−import Prelude (Unit, map, show, ($), (>>>))
36−
37−--------------------
38−-- TYPES
39−--
40−
41−-- | Callbacks to manipulate a cursor from an Open*Cursor call
42−type Callbacks cursor =
43− { onSuccess :: cursor -> Effect Unit
44− , onError :: Error -> Effect Unit
45− , onComplete :: Effect Unit
46− }
47−
48−--------------------
49−-- INTERFACES
50−--
51−
52−-- | Retrieves the number of records matching the key range in query.
53−count
54− :: forall index
55− . (IDBIndex index)
56− => index
57− -> Maybe KeyRange
58− -> Aff Int
59−count index range =
60− fromEffectFnAff $ Fn.runFn2 _count index (toNullable range)
61−
62−-- | Retrieves the value of the first record matching the given key range in query.
63−-- |
64−-- | NOTE
65−-- | The coercion from `a` to any type is unsafe and might throw a runtime error if incorrect.
66−get
67− :: forall a index
68− . (IDBIndex index)
69− => index
70− -> KeyRange
71− -> Aff (Maybe a)
72−get index range =
73− map (toMaybe >>> map unsafeFromForeign) $ fromEffectFnAff $ Fn.runFn2 _get index range
74−
75−-- | Retrieves the keys of records matching the given key range in query
76−-- | (up to the number given if given).
77−getAllKeys
78− :: forall index
79− . (IDBIndex index)
80− => index
81− -> Maybe KeyRange
82− -> Maybe Int
83− -> Aff (Array Key)
84−getAllKeys index range n =
85− map (map toKey) $ fromEffectFnAff $ Fn.runFn3 _getAllKeys index (toNullable range) (toNullable n)
86−
87−-- | Retrieves the key of the first record matching the given key or key range in query.
88−getKey
89− :: forall index
90− . (IDBIndex index)
91− => index
92− -> KeyRange
93− -> Aff (Maybe Key)
94−getKey index range =
95− map (toMaybe >>> map toKey) $ fromEffectFnAff $ Fn.runFn2 _getKey index range
96−
97−-- | Opens a ValueCursor over the records matching query, ordered by direction.
98−-- | If query is `Nothing`, all records in index are matched.
99−openCursor
100− :: forall index
101− . (IDBIndex index)
102− => index
103− -> Maybe KeyRange
104− -> CursorDirection
105− -> Callbacks ValueCursor
106− -> Aff Unit
107−openCursor index range dir cb =
108− fromEffectFnAff $ Fn.runFn4 _openCursor index (toNullable range) (show dir) cb
109−
110−-- | Opens a KeyCursor over the records matching query, ordered by direction.
111−-- | If query is `Nothing`, all records in index are matched.
112−openKeyCursor
113− :: forall index
114− . (IDBIndex index)
115− => index
116− -> Maybe KeyRange
117− -> CursorDirection
118− -> Callbacks KeyCursor
119− -> Aff Unit
120−openKeyCursor index range dir cb =
121− fromEffectFnAff $ Fn.runFn4 _openKeyCursor index (toNullable range) (show dir) cb
122−
123−--------------------
124−-- ATTRIBUTES
125−--
126−-- | Returns the key path of the index.
127−keyPath
128− :: Index
129− -> KeyPath
130−keyPath =
131− _keyPath
132−
133−-- | Returns true if the index's multiEntry flag is set.
134−multiEntry
135− :: Index
136− -> Boolean
137−multiEntry =
138− _multiEntry
139−
140−-- | Returns the name of the index.
141−name
142− :: Index
143− -> String
144−name =
145− _name
146−
147−-- | Returns the IDBObjectStore the index belongs to.
148−objectStore
149− :: Index
150− -> ObjectStore
151−objectStore =
152− _objectStore
153−
154−-- | Returns true if the index's unique flag is set.
155−unique
156− :: Index
157− -> Boolean
158−unique =
159− _unique
160−
161−--------------------
162−-- FFI
163−--
164−foreign import _keyPath
165− :: Index
166− -> Array String
167−
168−foreign import _multiEntry
169− :: Index
170− -> Boolean
171−
172−foreign import _name
173− :: Index
174− -> String
175−
176−foreign import _objectStore
177− :: Index
178− -> ObjectStore
179−
180−foreign import _unique
181− :: Index
182− -> Boolean
183−
184−foreign import _count
185− :: forall index
186− . Fn2 index (Nullable KeyRange) (EffectFnAff Int)
187−
188−foreign import _get
189− :: forall index
190− . Fn2 index KeyRange (EffectFnAff (Nullable Foreign))
191−
192−foreign import _getAllKeys
193− :: forall index
194− . Fn3 index (Nullable KeyRange) (Nullable Int) (EffectFnAff (Array Foreign))
195−
196−foreign import _getKey
197− :: forall index
198− . Fn2 index KeyRange (EffectFnAff (Nullable Foreign))
199−
200−foreign import _openCursor
201− :: forall index
202− . Fn4 index (Nullable KeyRange) String (Callbacks ValueCursor) (EffectFnAff Unit)
203−
204−foreign import _openKeyCursor
205− :: forall index
206− . Fn4 index (Nullable KeyRange) String (Callbacks KeyCursor) (EffectFnAff Unit)
15+ , defaultParameters
16+ )
src/Database/IndexedDB/IDBIndex/Internal.jsnew file+129
@@ -0,0 +1,129 @@
1+const errorHandler = function errorHandler(cb) {
2+ return function _handler(e) {
3+ cb(new Error(e.target.error.name));
4+ };
5+};
6+
7+const successHandler = function successHandler(cb) {
8+ return function _handler(e) {
9+ cb(e.target.result);
10+ };
11+};
12+
13+
14+exports._keyPath = function _keyPath(index) {
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 [];
26+};
27+
28+exports._multiEntry = function _multiEntry(index) {
29+ return index.multiEntry;
30+};
31+
32+exports._name = function _name(index) {
33+ return index.name;
34+};
35+
36+exports._objectStore = function _objectStore(index) {
37+ return index.obectStore;
38+};
39+
40+exports._unique = function _unique(index) {
41+ return index.unique;
42+};
43+
44+exports._count = function _count(index, query) {
45+ return function aff(success, 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+ }
53+ };
54+};
55+
56+exports._get = function _get(index, range) {
57+ return function aff(success, error) {
58+ try {
59+ const request = index.get(range);
60+ request.onsuccess = successHandler(success);
61+ request.onerror = errorHandler(error);
62+ } catch (e) {
63+ error(new Error(e.name));
64+ }
65+ };
66+};
67+
68+/*
69+ * NOTE: Require some additional work. The array (which isn't necessarily a list of
70+ * polymorphic types in js) can't be easily translated to a PureScript array.
71+ *
72+ * However, it may be doable to convert the result to some key / value structure with values of
73+ * different types.
74+exports._getAll = function _getAll(index, query, count) {
75+ return function aff(success, error) {
76+ const request = index.getAll(query, count);
77+ request.onsuccess = successHandler(success);
78+ request.onerror = errorHandler(error);
79+ };
80+};
81+*/
82+
83+exports._getAllKeys = function _getAllKeys(index, range, count) {
84+ return function aff(success, error) {
85+ try {
86+ const request = index.getAllKeys(range, count || undefined);
87+ request.onsuccess = successHandler(success);
88+ request.onerror = errorHandler(error);
89+ } catch (e) {
90+ error(new Error(e.name));
91+ }
92+ };
93+};
94+
95+exports._getKey = function _getKey(index, range) {
96+ return function aff(success, error) {
97+ try {
98+ const request = index.getKey(range);
99+ request.onsuccess = successHandler(success);
100+ request.onerror = errorHandler(error);
101+ } catch (e) {
102+ error(new Error(e.name));
103+ }
104+ };
105+};
106+
107+exports._openCursor = function _openCursor(index, query, dir) {
108+ return function aff(success, error) {
109+ try {
110+ const request = index.openCursor(query, dir);
111+ request.onsuccess = successHandler(success);
112+ request.onerror = errorHandler(error);
113+ } catch (e) {
114+ error(new Error(e.name));
115+ }
116+ };
117+};
118+
119+exports._openKeyCursor = function _openKeyCursor(index, query, dir) {
120+ return function aff(success, error) {
121+ try {
122+ const request = index.openKeyCursor(query, dir);
123+ request.onsuccess = successHandler(success);
124+ request.onerror = errorHandler(error);
125+ } catch (e) {
126+ error(new Error(e.name));
127+ }
128+ };
129+};
src/Database/IndexedDB/IDBIndex/Internal.pursnew file+217
@@ -0,0 +1,217 @@
1+-- | An index allows looking up records in an object store using properties of the values
2+-- | in the object stores records.
3+module Database.IndexedDB.IDBIndex.Internal where
4+
5+import Prelude
6+
7+import Control.Monad.Aff (Aff)
8+import Data.Foreign (Foreign, unsafeFromForeign)
9+import Data.Function.Uncurried as Fn
10+import Data.Function.Uncurried (Fn2, Fn3)
11+import Data.Maybe (Maybe)
12+import Data.Nullable (Nullable, toMaybe, toNullable)
13+
14+import Database.IndexedDB.Core (IDB, CursorDirection, Index, Key, KeyCursor, KeyRange,
15+ KeyPath, ObjectStore, ValueCursor)
16+
17+
18+--------------------
19+-- INTERFACES
20+--
21+-- | The IDBIndex interface represents an index handle.
22+-- | Any of these methods throw an "TransactionInactiveError" DOMException
23+-- | if called when the transaction is not active.
24+class IDBIndex index where
25+ -- | Retrieves the number of records matching the key range in query.
26+ count :: forall e. index -> Maybe KeyRange -> Aff (idb :: IDB | e) Int
27+
28+ -- | Retrieves the value of the first record matching the given key range in query.
29+ -- |
30+ -- | NOTE
31+ -- | The coercion from `a` to any type is unsafe and might throw a runtime error if incorrect.
32+ get :: forall a e. index -> KeyRange -> Aff (idb :: IDB | e) (Maybe a)
33+
34+ -- | Retrieves the keys of records matching the given key range in query
35+ -- | (up to the number given if given).
36+ getAllKeys :: forall e. index -> Maybe KeyRange -> Maybe Int -> Aff (idb :: IDB | e) (Array Key)
37+
38+ -- | Retrieves the key of the first record matching the given key or key range in query.
39+ getKey :: forall e. index -> KeyRange -> Aff (idb :: IDB | e) (Maybe Key)
40+
41+ -- | Opens a ValueCursor over the records matching query, ordered by direction.
42+ -- | If query is `Nothing`, all records in index are matched.
43+ openCursor :: forall e. index -> Maybe KeyRange -> CursorDirection -> Aff (idb :: IDB | e) ValueCursor
44+
45+ -- | Opens a KeyCursor over the records matching query, ordered by direction.
46+ -- | If query is `Nothing`, all records in index are matched.
47+ openKeyCursor :: forall e. index -> Maybe KeyRange -> CursorDirection -> Aff (idb :: IDB | e) KeyCursor
48+
49+
50+-- | Flags to set on the index.
51+-- |
52+-- | An index has a `unique` flag. When this flag is set, the index enforces that no
53+-- | two records in the index has the same key. If a record in the index’s referenced
54+-- | object store is attempted to be inserted or modified such that evaluating the index’s
55+-- | key path on the records new value yields a result which already exists in the index,
56+-- | then the attempted modification to the object store fails.
57+-- |
58+-- | An index has a `multiEntry` flag. This flag affects how the index behaves when the
59+-- | result of evaluating the index’s key path yields an array key. If the `multiEntry` flag
60+-- | is unset, then a single record whose key is an array key is added to the index.
61+-- | If the `multiEntry` flag is true, then the one record is added to the index for each
62+-- | of the subkeys.
63+type IDBIndexParameters =
64+ { unique :: Boolean
65+ , multiEntry :: Boolean
66+ }
67+
68+
69+defaultParameters :: IDBIndexParameters
70+defaultParameters =
71+ { unique : false
72+ , multiEntry : false
73+ }
74+
75+
76+--------------------
77+-- ATTRIBUTES
78+--
79+-- | Returns the key path of the index.
80+keyPath
81+ :: Index
82+ -> KeyPath
83+keyPath =
84+ _keyPath
85+
86+
87+-- | Returns true if the index's multiEntry flag is set.
88+multiEntry
89+ :: Index
90+ -> Boolean
91+multiEntry =
92+ _multiEntry
93+
94+
95+-- | Returns the name of the index.
96+name
97+ :: Index
98+ -> String
99+name =
100+ _name
101+
102+
103+-- | Returns the IDBObjectStore the index belongs to.
104+objectStore
105+ :: Index
106+ -> ObjectStore
107+objectStore =
108+ _objectStore
109+
110+
111+-- | Returns true if the index's unique flag is set.
112+unique
113+ :: Index
114+ -> Boolean
115+unique =
116+ _unique
117+
118+
119+--------------------
120+-- INSTANCES
121+--
122+instance idbIndexIndex :: IDBIndex Index where
123+ count index range =
124+ Fn.runFn2 _count index (toNullable range)
125+
126+ get index range =
127+ (toMaybe >>> map unsafeFromForeign) <$> Fn.runFn2 _get index range
128+
129+ getAllKeys index range count =
130+ Fn.runFn3 _getAllKeys index (toNullable range) (toNullable count)
131+
132+ getKey index range =
133+ toMaybe <$> Fn.runFn2 _getKey index range
134+
135+ openCursor index range dir =
136+ Fn.runFn3 _openCursor index (toNullable range) (show dir)
137+
138+ openKeyCursor index range dir =
139+ Fn.runFn3 _openKeyCursor index (toNullable range) (show dir)
140+
141+
142+instance idbIndexObjectStore :: IDBIndex ObjectStore where
143+ count store range =
144+ Fn.runFn2 _count store (toNullable range)
145+
146+ get store range =
147+ (toMaybe >>> map unsafeFromForeign) <$> Fn.runFn2 _get store range
148+
149+ getAllKeys store range count =
150+ Fn.runFn3 _getAllKeys store (toNullable range) (toNullable count)
151+
152+ getKey store range =
153+ toMaybe <$> Fn.runFn2 _getKey store range
154+
155+ openCursor store range dir =
156+ Fn.runFn3 _openCursor store (toNullable range) (show dir)
157+
158+ openKeyCursor store range dir =
159+ Fn.runFn3 _openKeyCursor store (toNullable range) (show dir)
160+
161+
162+--------------------
163+-- FFI
164+--
165+foreign import _keyPath
166+ :: Index
167+ -> Array String
168+
169+
170+foreign import _multiEntry
171+ :: Index
172+ -> Boolean
173+
174+
175+foreign import _name
176+ :: Index
177+ -> String
178+
179+
180+foreign import _objectStore
181+ :: Index
182+ -> ObjectStore
183+
184+
185+foreign import _unique
186+ :: Index
187+ -> Boolean
188+
189+
190+foreign import _count
191+ :: forall index e
192+ . Fn2 index (Nullable KeyRange) (Aff (idb :: IDB | e) Int)
193+
194+
195+foreign import _get
196+ :: forall index e
197+ . Fn2 index KeyRange (Aff (idb :: IDB | e) (Nullable Foreign))
198+
199+
200+foreign import _getAllKeys
201+ :: forall index e
202+ . Fn3 index (Nullable KeyRange) (Nullable Int) (Aff (idb :: IDB | e) (Array Key))
203+
204+
205+foreign import _getKey
206+ :: forall index e
207+ . Fn2 index KeyRange (Aff (idb :: IDB | e) (Nullable Key))
208+
209+
210+foreign import _openCursor
211+ :: forall index e
212+ . Fn3 index (Nullable KeyRange) String (Aff (idb :: IDB | e) ValueCursor)
213+
214+
215+foreign import _openKeyCursor
216+ :: forall index e
217+ . Fn3 index (Nullable KeyRange) String (Aff (idb :: IDB | e) KeyCursor)
src/Database/IndexedDB/IDBKey.purs+1−8
@@ -5,11 +5,4 @@ module Database.IndexedDB.IDBKey
55 ( module Database.IndexedDB.IDBKey.Internal
66 ) where
77
8−import Database.IndexedDB.IDBKey.Internal
9− ( class IDBKey
10− , Key
11− , none
12− , toKey
13− , fromKey
14− , unsafeFromKey
15− )
8+import Database.IndexedDB.IDBKey.Internal(Key, class IDBKey, toKey, fromKey, unsafeFromKey)
src/Database/IndexedDB/IDBKey/Internal.js+3−3
@@ -1,8 +1,8 @@
1−export function _dateTimeToForeign(y, m, d, h, mi, s, ms) {
1+exports._dateTimeToForeign = function _dateTimeToForeign(y, m, d, h, mi, s, ms) {
22 return new Date(y, m, d, h, mi, s, ms);
33};
44
5−export function _readDateTime(parse, right, left, date) {
5+exports._readDateTime = function _readDateTime(parse, right, left, date) {
66 if (Object.getPrototypeOf(date) !== Date.prototype) {
77 return left(typeof date);
88 }
@@ -24,7 +24,7 @@ export function _readDateTime(parse, right, left, date) {
2424 return right(mdate);
2525};
2626
27−export function _unsafeReadDateTime(parse, date) {
27+exports._unsafeReadDateTime = function _unsafeReadDateTime(parse, date) {
2828 const y = date.getFullYear();
2929 const m = date.getMonth() + 1;
3030 const d = date.getDate();
src/Database/IndexedDB/IDBKey/Internal.purs+110−99
@@ -2,37 +2,42 @@
22-- |
33-- | NOTE: Binary keys aren't supported yet.
44module Database.IndexedDB.IDBKey.Internal
5− ( Key
6− , class IDBKey
7− , toKey
8− , fromKey
9− , unsafeFromKey
10− , none
11− ) where
5+ ( Key(..)
6+ , class IDBKey, toKey , fromKey , unsafeFromKey
7+ , extractForeign
8+ ) where
129
1310import Prelude
1411
15−import Control.Alt ((<|>))
16−import Control.Monad.Except (ExceptT(..), runExceptT)
17−import Data.Date as Date
18−import Data.DateTime (DateTime(..), Time(..))
19−import Data.Either (Either(..), either, isRight)
20−import Data.Enum (fromEnum, toEnum)
21−import Foreign as Foreign
22−import Foreign (Foreign, F)
23−import Data.Function.Uncurried as Fn
24−import Data.Function.Uncurried (Fn2, Fn4, Fn7)
25−import Data.Identity (Identity(..))
26−import Data.List.NonEmpty (NonEmptyList(..))
27−import Data.List.Types (List(..))
28−import Data.Maybe (Maybe(..))
29−import Data.NonEmpty (NonEmpty(..))
30−import Data.Nullable (Nullable, toNullable)
31−import Data.Time as Time
32−import Data.Traversable (traverse)
12+import Control.Alt ((<|>))
13+import Control.Monad.Except (ExceptT(..), runExceptT)
14+import Data.Date as Date
15+import Data.DateTime (DateTime(..), Time(..))
16+import Data.Enum (fromEnum, toEnum)
17+import Data.Foreign as Foreign
18+import Data.Foreign (Foreign, F)
19+import Data.Function.Uncurried as Fn
20+import Data.Function.Uncurried (Fn2, Fn4, Fn7)
21+import Data.List.NonEmpty (NonEmptyList(..))
22+import Data.List.Types (List(..))
23+import Data.NonEmpty (NonEmpty(..))
24+import Data.Either (Either(..), either, isRight)
25+import Data.Identity (Identity(..))
26+import Data.Nullable (Nullable, toNullable)
27+import Data.Time as Time
28+import Data.Traversable (traverse)
29+
3330
3431newtype Key = Key Foreign
3532
33+
34+extractForeign
35+ :: Key
36+ -> Foreign
37+extractForeign (Key f) =
38+ f
39+
40+
3641--------------------
3742-- INTERFACES
3843--
@@ -40,90 +45,89 @@ newtype Key = Key Foreign
4045-- | to a known type (e.g if you only strings as keys, or perfectly knows the
4146-- | type of a given key).
4247class IDBKey a where
43− toKey :: a -> Key
44− fromKey :: Key -> F a
48+ toKey :: a -> Key
49+ fromKey :: Key -> F a
4550 unsafeFromKey :: Key -> a
4651
47−none :: Maybe Key
48−none =
49− Nothing
5052
5153--------------------
5254-- INSTANCES
5355--
5456instance eqKey :: Eq Key where
5557 eq a b = (runExceptT >>> runIdentity >>> isRight) $
56− eq <$> ((fromKey a) :: F Int) <*> fromKey b
57− <|>
58− eq <$> ((fromKey a) :: F Number) <*> fromKey b
59− <|>
60− eq <$> ((fromKey a) :: F String) <*> fromKey b
61− <|>
62− eq <$> ((fromKey a) :: F DateTime) <*> fromKey b
63− <|>
64− eq <$> ((fromKey a) :: F (Array Key)) <*> fromKey b
58+ eq <$> ((fromKey a) :: F Int) <*> fromKey b
59+ <|>
60+ eq <$> ((fromKey a) :: F Number) <*> fromKey b
61+ <|>
62+ eq <$> ((fromKey a) :: F String) <*> fromKey b
63+ <|>
64+ eq <$> ((fromKey a) :: F DateTime) <*> fromKey b
65+ <|>
66+ eq <$> ((fromKey a) :: F (Array Key)) <*> fromKey b
6567 where
66− runIdentity :: forall a. Identity a -> a
67− runIdentity (Identity x) = x
68+ runIdentity :: forall a. Identity a -> a
69+ runIdentity (Identity x) = x
70+
6871
6972instance ordKey :: Ord Key where
70− compare a b = (runExceptT >>> runIdentity >>> either (const LT) identity) $
71− compare <$> ((fromKey a) :: F Int) <*> fromKey b
72− <|>
73− compare <$> ((fromKey a) :: F Number) <*> fromKey b
74− <|>
75− compare <$> ((fromKey a) :: F String) <*> fromKey b
76− <|>
77− compare <$> ((fromKey a) :: F DateTime) <*> fromKey b
78− <|>
79− compare <$> ((fromKey a) :: F (Array Key)) <*> fromKey b
73+ compare a b = (runExceptT >>> runIdentity >>> either (const LT) id) $
74+ compare <$> ((fromKey a) :: F Int) <*> fromKey b
75+ <|>
76+ compare <$> ((fromKey a) :: F Number) <*> fromKey b
77+ <|>
78+ compare <$> ((fromKey a) :: F String) <*> fromKey b
79+ <|>
80+ compare <$> ((fromKey a) :: F DateTime) <*> fromKey b
81+ <|>
82+ compare <$> ((fromKey a) :: F (Array Key)) <*> fromKey b
8083 where
81− runIdentity :: forall a. Identity a -> a
82− runIdentity (Identity x) = x
84+ runIdentity :: forall a. Identity a -> a
85+ runIdentity (Identity x) = x
86+
8387
8488instance showKey :: Show Key where
8589 show a = (runExceptT >>> format) $
86− (show <$> (fromKey a :: F Int))
87− <|>
88− (show <$> (fromKey a :: F Number))
89− <|>
90− (show <$> (fromKey a :: F String))
91− <|>
92− (show <$> (fromKey a :: F DateTime))
93− <|>
94− (show <$> (fromKey a :: F (Array Key)))
90+ (show <$> (fromKey a :: F Int))
91+ <|>
92+ (show <$> (fromKey a :: F Number))
93+ <|>
94+ (show <$> (fromKey a :: F String))
95+ <|>
96+ (show <$> (fromKey a :: F DateTime))
97+ <|>
98+ (show <$> (fromKey a :: F (Array Key)))
9599 where
96− format :: forall a. Identity (Either a String) -> String
97− format (Identity x) =
98− either (const "(Key)") (\s -> "(Key " <> s <> ")") x
100+ format :: forall a. Identity (Either a String) -> String
101+ format (Identity x) =
102+ either (const "(Key)") (\s -> "(Key " <> s <> ")") x
103+
99104
100105instance idbKeyKey :: IDBKey Key where
101− toKey = identity
102− fromKey = pure
103− unsafeFromKey = identity
106+ toKey = id
107+ fromKey = pure
108+ unsafeFromKey = id
104109
105−instance idbKeyForeign :: IDBKey Foreign where
106− toKey = Key
107− fromKey (Key f) = pure f
108− unsafeFromKey (Key f) = f
109110
110111instance idbKeyInt :: IDBKey Int where
111− toKey = Foreign.unsafeToForeign >>> Key
112− fromKey (Key f) = Foreign.readInt f
112+ toKey = Foreign.toForeign >>> Key
113+ fromKey (Key f) = Foreign.readInt f
113114 unsafeFromKey (Key f) = Foreign.unsafeFromForeign f
114115
116+
115117instance idbKeyNumber :: IDBKey Number where
116− toKey = Foreign.unsafeToForeign >>> Key
117− fromKey (Key f) = Foreign.readNumber f
118+ toKey = Foreign.toForeign >>> Key
119+ fromKey (Key f) = Foreign.readNumber f
118120 unsafeFromKey (Key f) = Foreign.unsafeFromForeign f
119121
122+
120123instance idbKeyString :: IDBKey String where
121− toKey = Foreign.unsafeToForeign >>> Key
122− fromKey (Key f) = Foreign.readString f
124+ toKey = Foreign.toForeign >>> Key
125+ fromKey (Key f) = Foreign.readString f
123126 unsafeFromKey (Key f) = Foreign.unsafeFromForeign f
124127
128+
125129instance idbKeyDate :: IDBKey DateTime where
126− toKey (DateTime d t) = Key $ Fn.runFn7 _dateTimeToForeign
130+ toKey (DateTime d t) = Key $ Fn.runFn7 _dateTimeToForeign
127131 (fromEnum $ Date.year d)
128132 (fromEnum $ Date.month d)
129133 (fromEnum $ Date.day d)
@@ -131,51 +135,58 @@ instance idbKeyDate :: IDBKey DateTime where
131135 (fromEnum $ Time.minute t)
132136 (fromEnum $ Time.second t)
133137 (fromEnum $ Time.millisecond t)
134− fromKey (Key f) = Fn.runFn4 _readDateTime dateTime dateTimeF dateTimeE f
138+ fromKey (Key f) = Fn.runFn4 _readDateTime dateTime dateTimeF dateTimeE f
135139 unsafeFromKey (Key f) = Fn.runFn2 _unsafeReadDateTime dateTime f
136140
141+
137142instance idbKeyArray :: IDBKey a => IDBKey (Array a) where
138− toKey = Foreign.unsafeToForeign >>> Key
139− fromKey (Key f) = Foreign.readArray f >>= traverse (Key >>> fromKey)
143+ toKey = Foreign.toForeign >>> Key
144+ fromKey (Key f) = Foreign.readArray f >>= traverse (Key >>> fromKey)
140145 unsafeFromKey (Key f) = map unsafeFromKey (Foreign.unsafeFromForeign f)
141146
147+
142148-- FFI constructor to build a DateTime from years, months, days, hours, minutes, seconds and millis
143149dateTime
144− :: Int -- ^ years
145− -> Int -- ^ months
146− -> Int -- ^ days
147− -> Int -- ^ hours
148− -> Int -- ^ minutes
149− -> Int -- ^ seconds
150− -> Int -- ^ milliseconds
151− -> Nullable DateTime
150+ :: Int -- ^ years
151+ -> Int -- ^ months
152+ -> Int -- ^ days
153+ -> Int -- ^ hours
154+ -> Int -- ^ minutes
155+ -> Int -- ^ seconds
156+ -> Int -- ^ milliseconds
157+ -> Nullable DateTime
152158dateTime y m d h mi s ms =
153159 toNullable $ DateTime
154− <$> (Date.canonicalDate <$> toEnum y <*> toEnum m <*> toEnum d)
155− <*> (Time <$> toEnum h <*> toEnum mi <*> toEnum s <*> toEnum ms)
160+ <$> (Date.canonicalDate <$> toEnum y <*> toEnum m <*> toEnum d)
161+ <*> (Time <$> toEnum h <*> toEnum mi <*> toEnum s <*> toEnum ms)
162+
156163
157164-- FFI constructor to convert a JS `Date` into a successful `F DateTime`
158165dateTimeF
159− :: DateTime
160− -> F DateTime
166+ :: DateTime
167+ -> F DateTime
161168dateTimeF =
162169 Right >>> Identity >>> ExceptT
163170
171+
164172-- FFI constructor to convert a string into an errored `F DateTime`
165173dateTimeE
166− :: String
167− -> F DateTime
174+ :: String
175+ -> F DateTime
168176dateTimeE =
169177 Foreign.TypeMismatch "Date" >>> flip NonEmpty Nil >>> NonEmptyList >>> Left >>> Identity >>> ExceptT
170178
179+
171180--------------------
172181-- FFI
173182--
174183foreign import _dateTimeToForeign
175− :: Fn7 Int Int Int Int Int Int Int Foreign
184+ :: Fn7 Int Int Int Int Int Int Int Foreign
185+
176186
177187foreign import _readDateTime
178− :: Fn4 (Int -> Int -> Int -> Int -> Int -> Int -> Int -> Nullable DateTime) (DateTime -> F DateTime) (String -> F DateTime) Foreign (F DateTime)
188+ :: Fn4 (Int -> Int -> Int -> Int -> Int -> Int -> Int -> Nullable DateTime) (DateTime -> F DateTime) (String -> F DateTime) Foreign (F DateTime)
189+
179190
180191foreign import _unsafeReadDateTime
181− :: Fn2 (Int -> Int -> Int -> Int -> Int -> Int -> Int -> Nullable DateTime) Foreign DateTime
192+ :: Fn2 (Int -> Int -> Int -> Int -> Int -> Int -> Int -> Nullable DateTime) Foreign DateTime
src/Database/IndexedDB/IDBKeyRange.js+9−9
@@ -1,4 +1,4 @@
1−export function _bound(lower, upper, lowerOpen, upperOpen) {
1+exports._bound = function _bound(lower, upper, lowerOpen, upperOpen) {
22 try {
33 return IDBKeyRange.bound(lower, upper, lowerOpen, upperOpen);
44 } catch (e) {
@@ -6,34 +6,34 @@ export function _bound(lower, upper, lowerOpen, upperOpen) {
66 }
77};
88
9−export function _includes(range, key) {
9+exports._includes = function _includes(range, key) {
1010 return range.includes(key);
1111};
1212
13−export function _lower(range) {
13+exports._lower = function _lower(range) {
1414 return range.lower;
1515};
1616
17−export function _lowerBound(lower, open) {
17+exports._lowerBound = function _lowerBound(lower, open) {
1818 return IDBKeyRange.lowerBound(lower, open);
1919};
2020
21−export function _lowerOpen(range) {
21+exports._lowerOpen = function _lowerOpen(range) {
2222 return range.lowerOpen;
2323};
2424
25−export function _only(key) {
25+exports._only = function _only(key) {
2626 return IDBKeyRange.only(key);
2727};
2828
29−export function _upper(range) {
29+exports._upper = function _upper(range) {
3030 return range.upper;
3131};
3232
33−export function _upperBound(upper, open) {
33+exports._upperBound = function _upperBound(upper, open) {
3434 return IDBKeyRange.upperBound(upper, open);
3535};
3636
37−export function _upperOpen(range) {
37+exports._upperOpen = function _upperOpen(range) {
3838 return range.upperOpen;
3939};
src/Database/IndexedDB/IDBKeyRange.purs+75−91
@@ -1,76 +1,71 @@
11-- | A key has an associated type which is one of: number, date, string, binary, or array.
22module Database.IndexedDB.IDBKeyRange
3− -- * Types
4− ( Open
5−
6− -- * Constructors
3+ ( class IDBKeyRange, includes
74 , only
85 , lowerBound
96 , upperBound
107 , bound
11−
12− -- * Interface
13− , includes
14−
15− -- * Attributes
168 , lower
179 , upper
1810 , lowerOpen
1911 , upperOpen
2012 ) where
2113
22−import Prelude (($), (>>>), map)
14+import Prelude
15+
16+import Data.Foreign (Foreign)
17+import Data.Function.Uncurried as Fn
18+import Data.Function.Uncurried (Fn2, Fn4)
19+import Data.Maybe (Maybe)
20+import Data.Nullable (Nullable, toMaybe)
2321
24−import Foreign (Foreign)
25−import Data.Function.Uncurried as Fn
26−import Data.Function.Uncurried (Fn2, Fn4)
27−import Data.Maybe (Maybe)
28−import Data.Nullable (Nullable, toMaybe)
22+import Database.IndexedDB.Core (KeyRange)
23+import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key(..), toKey, extractForeign)
2924
30−import Database.IndexedDB.Core (class IDBKeyRange, KeyRange)
31−import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, toKey, unsafeFromKey)
3225
3326--------------------
34−-- TYPES
27+-- INTERFACES
3528--
29+-- | The IDBKeyRange interface represents a key range.
30+class IDBKeyRange range where
31+ -- | Returns true if key is included in the range, and false otherwise.
32+ includes :: range -> Key -> Boolean
33+
3634
3735-- | Type alias for open
3836type Open = Boolean
3937
40−--------------------
41−-- CONSTRUCTORS
42−--
4338
4439-- | Returns a new IDBKeyRange spanning only key.
4540only
46− :: forall a
47− . (IDBKey a)
48− => a
49− -> KeyRange
41+ :: forall a. (IDBKey a)
42+ => a
43+ -> KeyRange
5044only key =
51− _only (unsafeFromKey $ toKey key)
45+ _only (extractForeign $ toKey key)
46+
5247
5348-- | Returns a new IDBKeyRange starting at key with no upper bound.
5449-- | If `Open` is `true`, key is not included in the range.
5550lowerBound
56− :: forall a
57− . (IDBKey a)
58− => a
59− -> Open
60− -> KeyRange
51+ :: forall a. (IDBKey a)
52+ => a
53+ -> Open
54+ -> KeyRange
6155lowerBound key open =
62− Fn.runFn2 _lowerBound (unsafeFromKey $ toKey key) open
56+ Fn.runFn2 _lowerBound (extractForeign $ toKey key) open
57+
6358
6459-- | Returns a new IDBKeyRange with no lower bound and ending at key.
6560-- | If `Open` is `true`, key is not included in the range.
6661upperBound
67− :: forall a
68− . (IDBKey a)
69− => a
70− -> Open
71− -> KeyRange
62+ :: forall a. (IDBKey a)
63+ => a
64+ -> Open
65+ -> KeyRange
7266upperBound key open =
73− Fn.runFn2 _upperBound (unsafeFromKey $ toKey key) open
67+ Fn.runFn2 _upperBound (extractForeign $ toKey key) open
68+
7469
7570-- | Returns a new IDBKeyRange spanning from `lower` to `upper`.
7671-- | If `lowerOpen` is `true`, `lower` is not included in the range.
@@ -78,93 +73,82 @@ upperBound key open =
7873-- |
7974-- | It throws a `DataError` if the bound is invalid.
8075bound
81− :: forall key
82− . (IDBKey key)
83− => { lower :: key, upper :: key, lowerOpen :: Boolean, upperOpen :: Boolean }
84− -> Maybe KeyRange
76+ :: forall a. (IDBKey a)
77+ => { lower :: a, upper :: a, lowerOpen :: Boolean, upperOpen :: Boolean }
78+ -> Maybe KeyRange
8579bound { lower: key1, upper: key2, lowerOpen: open1, upperOpen: open2 } =
8680 toMaybe
87− $ Fn.runFn4 _bound (unsafeFromKey $ toKey key1) (unsafeFromKey $ toKey key2) open1 open2
81+ $ Fn.runFn4 _bound (extractForeign $ toKey key1) (extractForeign $ toKey key2) open1 open2
8882
89−--------------------
90−-- INTERFACE
91−--
92−
93−-- | Returns true if key is included in the range, and false otherwise.
94−includes
95− :: forall key range
96− . (IDBKey key)
97− => (IDBKeyRange range)
98− => range
99− -> key
100− -> Boolean
101−includes range =
102− toKey >>> unsafeFromKey >>> Fn.runFn2 _includes range
10383
10484--------------------
10585-- ATTRIBUTES
10686--
10787-- | Returns lower bound if any.
10888lower
109− :: KeyRange
110− -> Maybe Key
89+ :: KeyRange
90+ -> Maybe Key
11191lower =
112− _lower >>> toMaybe >>> map toKey
92+ _lower >>> toMaybe >>> map Key
93+
11394
11495-- | Returns upper bound if any.
11596upper
116− :: KeyRange
117− -> Maybe Key
97+ :: KeyRange
98+ -> Maybe Key
11899upper =
119− _upper >>> toMaybe >>> map toKey
100+ _upper >>> toMaybe >>> map Key
101+
120102
121103-- | Returns true if the lower open flag is set, false otherwise.
122104lowerOpen
123− :: KeyRange
124− -> Boolean
105+ :: KeyRange
106+ -> Boolean
125107lowerOpen =
126108 _lowerOpen
127109
110+
128111-- | Returns true if the upper open flag is set, false otherwise.
129112upperOpen
130− :: KeyRange
131− -> Boolean
113+ :: KeyRange
114+ -> Boolean
132115upperOpen =
133116 _upperOpen
134117
118+
119+--------------------
120+-- INSTANCES
121+--
122+instance idbKeyRangeKeyRange :: IDBKeyRange KeyRange where
123+ includes range =
124+ extractForeign >>> Fn.runFn2 _includes range
125+
126+
135127--------------------
136128-- FFI
137129--
130+foreign import _bound :: Fn4 Foreign Foreign Boolean Boolean (Nullable KeyRange)
131+
132+
133+foreign import _includes :: forall range. Fn2 range Foreign Boolean
134+
135+
136+foreign import _lower :: KeyRange -> Nullable Foreign
137+
138+
139+foreign import _lowerBound :: Fn2 Foreign Boolean KeyRange
140+
138141
139−foreign import _only
140− :: Foreign
141− -> KeyRange
142+foreign import _lowerOpen :: KeyRange -> Boolean
142143
143−foreign import _lowerBound
144− :: Fn2 Foreign Boolean KeyRange
145144
146−foreign import _upperBound
147− :: Fn2 Foreign Boolean KeyRange
145+foreign import _only :: Foreign -> KeyRange
148146
149−foreign import _bound
150− :: Fn4 Foreign Foreign Boolean Boolean (Nullable KeyRange)
151147
152−foreign import _includes
153− :: forall range
154− . Fn2 range Foreign Boolean
148+foreign import _upper :: KeyRange -> Nullable Foreign
155149
156−foreign import _lower
157− :: KeyRange
158− -> Nullable Foreign
159150
160−foreign import _upper
161− :: KeyRange
162− -> Nullable Foreign
151+foreign import _upperBound :: Fn2 Foreign Boolean KeyRange
163152
164−foreign import _lowerOpen
165− :: KeyRange
166− -> Boolean
167153
168−foreign import _upperOpen
169− :: KeyRange
170− -> Boolean
154+foreign import _upperOpen :: KeyRange -> Boolean
src/Database/IndexedDB/IDBObjectStore.js+28−55
@@ -1,6 +1,6 @@
11const errorHandler = function errorHandler(cb) {
22 return function _handler(e) {
3− cb(e.target.error);
3+ cb(new Error(e.target.error.name));
44 };
55};
66
@@ -15,44 +15,36 @@ const toArray = function toArray(xs) {
1515};
1616
1717
18−export function _add(store, value, key) {
19− return function aff(error, success) {
18+exports._add = function _add(store, value, key) {
19+ return function aff(success, error) {
2020 try {
2121 const request = store.add(value, key || undefined);
2222 request.onsuccess = successHandler(success);
2323 request.onerror = errorHandler(error);
2424 } catch (e) {
25− error(e);
25+ error(new Error(e.name));
2626 }
27−
28− return function canceler(_, cancelerError) {
29− cancelerError(new Error("Can't cancel IDB Effects"));
30− };
3127 };
3228};
3329
34−export function _autoIncrement(store) {
30+exports._autoIncrement = function _autoIncrement(store) {
3531 return store.autoIncrement;
3632};
3733
38−export function _clear(store) {
39− return function aff(error, success) {
34+exports._clear = function _clear(store) {
35+ return function aff(success, error) {
4036 try {
4137 const request = store.clear();
4238 request.onsuccess = successHandler(success);
4339 request.onerror = errorHandler(error);
4440 } catch (e) {
45− error(e);
41+ error(new Error(e.name));
4642 }
47−
48− return function canceler(_, cancelerError) {
49− cancelerError(new Error("Can't cancel IDB Effects"));
50− };
5143 };
5244};
5345
54−export function _createIndex(store, name, path, params) {
55− return function aff(error, success) {
46+exports._createIndex = function _createIndex(store, name, path, params) {
47+ return function aff(success, error) {
5648 var keyPath;
5749
5850 try {
@@ -72,66 +64,50 @@ export function _createIndex(store, name, path, params) {
7264 const index = store.createIndex(name, keyPath, params);
7365 success(index);
7466 } catch (e) {
75− error(e);
67+ error(new Error(e.name));
7668 }
77−
78− return function canceler(_, cancelerError) {
79− cancelerError(new Error("Can't cancel IDB Effects"));
80− };
8169 };
8270};
8371
84−export function _deleteIndex(store, name) {
85− return function aff(error, success) {
72+exports._deleteIndex = function _deleteIndex(store, name) {
73+ return function aff(success, error) {
8674 try {
8775 store.deleteIndex(name);
8876 success();
8977 } catch (e) {
90− error(e);
78+ error(new Error(e.name));
9179 }
92−
93− return function canceler(_, cancelerError) {
94− cancelerError(new Error("Can't cancel IDB Effects"));
95− };
9680 };
9781};
9882
99−export function _delete(store, query) {
100− return function aff(error, success) {
83+exports._delete = function _delete(store, query) {
84+ return function aff(success, error) {
10185 try {
10286 const request = store.delete(query);
10387 request.onsuccess = successHandler(success);
10488 request.onerror = errorHandler(error);
10589 } catch (e) {
106− error(e);
90+ error(new Error(e.name));
10791 }
108−
109− return function canceler(_, cancelerError) {
110− cancelerError(new Error("Can't cancel IDB Effects"));
111− };
11292 };
11393};
11494
115−export function _index(store, name) {
116− return function aff(error, success) {
95+exports._index = function _index(store, name) {
96+ return function aff(success, error) {
11797 try {
11898 const index = store.index(name);
11999 success(index);
120100 } catch (e) {
121− error(e);
101+ error(new Error(e.name));
122102 }
123−
124− return function canceler(_, cancelerError) {
125− cancelerError(new Error("Can't cancel IDB Effects"));
126− };
127103 };
128104};
129105
130−export function _indexNames(store) {
106+exports._indexNames = function _indexNames(store) {
131107 return toArray(store.indexNames);
132108};
133109
134−export function _keyPath(store) {
110+exports._keyPath = function _keyPath(store) {
135111 const path = store.keyPath;
136112
137113 if (Array.isArray(path)) {
@@ -145,26 +121,23 @@ export function _keyPath(store) {
145121 return [];
146122};
147123
148−export function _name(store) {
124+exports._name = function _name(store) {
149125 return store.name;
150126};
151127
152−export function _put(store, value, key) {
153− return function aff(error, success) {
128+exports._put = function _put(store, value, key) {
129+ return function aff(success, error) {
154130 try {
155131 const request = store.put(value, key || undefined);
156132 request.onsuccess = successHandler(success);
157133 request.onerror = errorHandler(error);
158134 } catch (e) {
159− error(e);
135+ error(new Error(e.name));
160136 }
161−
162− return function canceler(_, cancelerError) {
163− cancelerError(new Error("Can't cancel IDB Effects"));
164− };
165137 };
166138};
167139
168−export function _transaction(store) {
140+exports._transaction = function _transaction(store) {
169141 return store.transaction;
170142};
143+
src/Database/IndexedDB/IDBObjectStore.purs+125−160
@@ -1,170 +1,93 @@
11-- | An object store is the primary storage mechanism for storing data in a database.
22module Database.IndexedDB.IDBObjectStore
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
3+ ( class IDBObjectStore, add, clear, createIndex, delete, deleteIndex, index, put
4+ , module Database.IndexedDB.IDBIndex.Internal
5+ , IDBObjectStoreParameters
6+ , IndexName
187 , autoIncrement
198 , indexNames
209 , keyPath
2110 , name
2211 , transaction
23−
24− -- * Re-Exports
25− , module Database.IndexedDB.IDBIndex
12+ , defaultParameters
2613 ) where
2714
28−import Database.IndexedDB.Core
15+import Prelude (Unit, ($), (<$>), (>>>))
16+
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)
23+
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)
2927
30−import Data.Function.Uncurried (Fn2, Fn3, Fn4)
31−import Data.Function.Uncurried as Fn
32−import Data.Maybe (Maybe)
33−import Data.Nullable (Nullable, toNullable)
34−import Database.IndexedDB.IDBIndex (count, get, getAllKeys, getKey, openCursor, openKeyCursor)
35−import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, unsafeFromKey, toKey)
36−import Effect.Aff (Aff)
37−import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
38−import Foreign (Foreign)
39−import Prelude (Unit, map, ($), (<$>), (>>>), (<<<))
4028
4129--------------------
42−-- TYPES
30+-- INTERFACES
4331--
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 :: forall v k e. (IDBKey k) => store -> v -> Maybe k -> Aff (idb :: IDB | e) Key
42+
43+ -- | Deletes all records in store.
44+ clear :: forall e. store -> Aff (idb :: IDB | e) Unit
45+
46+ -- | Creates a new index in store with the given name, keyPath and options and
47+ -- | returns a new IDBIndex. If the keyPath and options define constraints that
48+ -- | cannot be satisfied with the data already in store the upgrade transaction
49+ -- | will abort with a "ConstraintError" DOMException.
50+ -- |
51+ -- | Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
52+ createIndex :: forall e. store -> IndexName -> KeyPath -> IDBIndexParameters -> Aff (idb :: IDB | e) Index
53+
54+ -- | Deletes records in store with the given key or in the given key range in query.
55+ delete :: forall e. store -> KeyRange -> Aff (idb :: IDB | e) Unit
56+
57+ -- | Deletes the index in store with the given name.
58+ -- |
59+ -- | Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
60+ deleteIndex :: forall e. store -> IndexName -> Aff (idb :: IDB | e) Unit
61+
62+ -- | Returns an IDBIndex for the index named name in store.
63+ index :: forall e. store -> IndexName -> Aff (idb :: IDB | e) Index
64+
65+ -- | Adds or updates a record in store with the given value and key.
66+ -- |
67+ -- | If the store uses in-line keys and key is specified a "DataError" DOMException
68+ -- | will be thrown.
69+ -- |
70+ -- | If put() is used, any existing record with the key will be replaced.
71+ put :: forall v k e. (IDBKey k) => store -> v -> Maybe k -> Aff (idb :: IDB | e) Key
72+
4473
4574-- | Type alias for IndexName
4675type IndexName = String
4776
48−-- | Flags to set on the index.
49−-- |
50−-- | An index has a `unique` flag. When this flag is set, the index enforces that no
51−-- | two records in the index has the same key. If a record in the index’s referenced
52−-- | object store is attempted to be inserted or modified such that evaluating the index’s
53−-- | key path on the records new value yields a result which already exists in the index,
54−-- | then the attempted modification to the object store fails.
55−-- |
56−-- | An index has a `multiEntry` flag. This flag affects how the index behaves when the
57−-- | result of evaluating the index’s key path yields an array key. If the `multiEntry` flag
58−-- | is unset, then a single record whose key is an array key is added to the index.
59−-- | If the `multiEntry` flag is true, then the one record is added to the index for each
60−-- | of the subkeys.
61−type IndexParameters =
62− { unique :: Boolean
63− , multiEntry :: Boolean
77+
78+-- | Options provided when creating an object store.
79+type IDBObjectStoreParameters =
80+ { keyPath :: KeyPath
81+ , autoIncrement :: Boolean
6482 }
6583
66−defaultParameters :: IndexParameters
84+
85+defaultParameters :: IDBObjectStoreParameters
6786defaultParameters =
68− { unique: false
69− , multiEntry: false
87+ { keyPath : []
88+ , autoIncrement : false
7089 }
7190
72−--------------------
73−-- INTERFACES
74−--
75−
76−-- | Adds or updates a record in store with the given value and key.
77−-- |
78−-- | If the store uses in-line keys and key is specified a "DataError" DOMException
79−-- | will be thrown.
80−-- |
81−-- | If add() is used, and if a record with the key already exists the request will fail,
82−-- | with a "ConstraintError" DOMException.
83−add
84− :: forall key val store
85− . (IDBKey key)
86− => (IDBObjectStore store)
87− => store
88− -> val
89− -> Maybe key
90− -> Aff Key
91−add store value key =
92− map toKey $ fromEffectFnAff $ Fn.runFn3 _add store value (toNullable $ (toKey >>> unsafeFromKey) <$> key)
93−
94−-- | Deletes all records in store.
95−clear
96− :: forall store
97− . (IDBObjectStore store)
98− => store
99− -> Aff Unit
100−clear =
101− fromEffectFnAff <<< _clear
102−
103−-- | Creates a new index in store with the given name, keyPath and options and
104−-- | returns a new IDBIndex. If the keyPath and options define constraints that
105−-- | cannot be satisfied with the data already in store the upgrade transaction
106−-- | will abort with a "ConstraintError" DOMException.
107−-- |
108−-- | Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
109−createIndex
110− :: forall store
111− . (IDBObjectStore store)
112− => store
113− -> IndexName
114− -> KeyPath
115− -> IndexParameters
116− -> Aff Index
117−createIndex store name' path params =
118− fromEffectFnAff $ Fn.runFn4 _createIndex store name' path params
119−
120−-- | Deletes records in store with the given key or in the given key range in query.
121−delete
122− :: forall store
123− . (IDBObjectStore store)
124− => store
125− -> KeyRange
126− -> Aff Unit
127−delete store range =
128− fromEffectFnAff $ Fn.runFn2 _delete store range
129−
130−-- | Deletes the index in store with the given name.
131−-- |
132−-- | Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
133−deleteIndex
134− :: forall store
135− . (IDBObjectStore store)
136− => store
137− -> IndexName
138− -> Aff Unit
139−deleteIndex store name' =
140− fromEffectFnAff $ Fn.runFn2 _deleteIndex store name'
141−
142−-- | Returns an IDBIndex for the index named name in store.
143−index
144− :: forall store
145− . (IDBObjectStore store)
146− => store
147− -> IndexName
148− -> Aff Index
149−index store name' =
150− fromEffectFnAff $ Fn.runFn2 _index store name'
151−
152−-- | Adds or updates a record in store with the given value and key.
153−-- |
154−-- | If the store uses in-line keys and key is specified a "DataError" DOMException
155−-- | will be thrown.
156−-- |
157−-- | If put() is used, any existing record with the key will be replaced.
158−put
159− :: forall val key store
160− . (IDBKey key)
161− => (IDBObjectStore store)
162− => store
163− -> val
164− -> Maybe key
165− -> Aff Key
166−put store value key =
167− map toKey $ fromEffectFnAff $ Fn.runFn3 _put store value (toNullable $ (toKey >>> unsafeFromKey) <$> key)
16891
16992--------------------
17093-- ATTRIBUTES
@@ -176,6 +99,7 @@ autoIncrement
17699autoIncrement =
177100 _autoIncrement
178101
102+
179103--| Returns a list of the names of indexes in the store.
180104indexNames
181105 :: ObjectStore
@@ -183,6 +107,7 @@ indexNames
183107indexNames =
184108 _indexNames
185109
110+
186111-- | Returns the key path of the store, or empty array if none
187112keyPath
188113 :: ObjectStore
@@ -190,6 +115,7 @@ keyPath
190115keyPath =
191116 _keyPath
192117
118+
193119-- | Returns the name of the store.
194120name
195121 :: ObjectStore
@@ -197,6 +123,7 @@ name
197123name =
198124 _name
199125
126+
200127-- | Returns the associated transaction.
201128transaction
202129 :: ObjectStore
@@ -204,53 +131,91 @@ transaction
204131transaction =
205132 _transaction
206133
134+
135+--------------------
136+-- INSTANCES
137+--
138+instance idbObjectStoreObjectStore :: IDBObjectStore ObjectStore where
139+ add store value key =
140+ Key <$> Fn.runFn3 _add store value (toNullable $ (toKey >>> extractForeign) <$> key)
141+
142+ clear =
143+ _clear
144+
145+ createIndex store name' path params =
146+ Fn.runFn4 _createIndex store name' path params
147+
148+ delete store range =
149+ Fn.runFn2 _delete store range
150+
151+ deleteIndex store name' =
152+ Fn.runFn2 _deleteIndex store name'
153+
154+ index store name' =
155+ Fn.runFn2 _index store name'
156+
157+ put store value key =
158+ Key <$> Fn.runFn3 _put store value (toNullable $ (toKey >>> extractForeign) <$> key)
159+
160+
207161--------------------
208162-- FFI
209163--
210164foreign import _add
211− :: forall val store
212− . Fn3 store val (Nullable Foreign) (EffectFnAff Foreign)
165+ :: forall value e
166+ . Fn3 ObjectStore value (Nullable Foreign) (Aff (idb :: IDB | e) Foreign)
167+
213168
214169foreign import _autoIncrement
215170 :: ObjectStore
216171 -> Boolean
217172
173+
218174foreign import _clear
219− :: forall store
220− . store
221− -> EffectFnAff Unit
175+ :: forall e
176+ . ObjectStore
177+ -> Aff (idb :: IDB | e) Unit
178+
222179
223180foreign import _createIndex
224− :: forall store
225− . Fn4 store String (Array String) { unique :: Boolean, multiEntry :: Boolean } (EffectFnAff Index)
181+ :: forall e
182+ . Fn4 ObjectStore String (Array String) { unique :: Boolean, multiEntry :: Boolean } (Aff (idb :: IDB | e) Index)
183+
226184
227185foreign import _delete
228− :: forall store
229− . Fn2 store KeyRange (EffectFnAff Unit)
186+ :: forall e
187+ . Fn2 ObjectStore KeyRange (Aff (idb :: IDB | e) Unit)
188+
230189
231190foreign import _deleteIndex
232− :: forall store
233− . Fn2 store String (EffectFnAff Unit)
191+ :: forall e
192+ . Fn2 ObjectStore String (Aff (idb :: IDB | e) Unit)
193+
234194
235195foreign import _index
236− :: forall store
237− . Fn2 store String (EffectFnAff Index)
196+ :: forall e
197+ . Fn2 ObjectStore String (Aff (idb :: IDB | e) Index)
198+
238199
239200foreign import _indexNames
240201 :: ObjectStore
241202 -> Array String
242203
204+
243205foreign import _keyPath
244206 :: ObjectStore
245207 -> Array String
246208
209+
247210foreign import _name
248211 :: ObjectStore
249212 -> String
250213
214+
251215foreign import _put
252− :: forall val store
253− . Fn3 store val (Nullable Foreign) (EffectFnAff Foreign)
216+ :: forall value e
217+ . Fn3 ObjectStore value (Nullable Foreign) (Aff (idb :: IDB | e) Foreign)
218+
254219
255220foreign import _transaction
256221 :: ObjectStore
src/Database/IndexedDB/IDBTransaction.js+18−38
@@ -1,29 +1,25 @@
1−export function _abort(tx) {
2− return function aff(error, success) {
1+exports._abort = function _abort(tx) {
2+ return function aff(success, error) {
33 try {
44 tx.abort();
55 success();
66 } catch (e) {
7− error(e);
7+ error(new Error(e.name));
88 }
9−
10− return function canceler(_, cancelerError) {
11− cancelerError(new Error("Can't cancel IDB Effects"));
12− };
139 };
1410};
1511
16−export function _db(tx) {
12+exports._db = function _db(tx) {
1713 return tx.db;
1814};
1915
20−export function _error(tx) {
16+exports._error = function _error(tx) {
2117 return tx.error == null
2218 ? null
23− : tx.error;
19+ : new Error(tx.error.name);
2420};
2521
26−export function _mode(ReadOnly, ReadWrite, VersionChange, tx) {
22+exports._mode = function _mode(ReadOnly, ReadWrite, VersionChange, tx) {
2723 if (tx.mode === 'readwrite') {
2824 return ReadWrite;
2925 } else if (tx.mode === 'versionchange') {
@@ -33,60 +29,44 @@ export function _mode(ReadOnly, ReadWrite, VersionChange, tx) {
3329 return ReadOnly;
3430};
3531
36−export function _objectStore(tx, name) {
37− return function aff(error, success) {
32+exports._objectStore = function _objectStore(tx, name) {
33+ return function aff(success, error) {
3834 try {
3935 const store = tx.objectStore(name);
4036 success(store);
4137 } catch (e) {
42− error(e);
38+ error(new Error(e.name));
4339 }
44−
45− return function canceler(_, cancelerError) {
46− cancelerError(new Error("Can't cancel IDB Effects"));
47− };
4840 };
4941};
5042
51−export function _objectStoreNames(tx) {
43+exports._objectStoreNames = function _objectStoreNames(tx) {
5244 return tx.objectStoreNames;
5345};
5446
55−export function _onAbort(tx, f) {
56− return function aff(error, success) {
47+exports._onAbort = function _onAbort(tx, f) {
48+ return function aff(success) {
5749 tx.onabort = function onabort() {
5850 f();
5951 };
6052 success();
61−
62− return function canceler(_, cancelerError) {
63− cancelerError(new Error("Can't cancel IDB Effects"));
64− };
6553 };
6654};
6755
68−export function _onComplete(tx, f) {
69− return function aff(error, success) {
56+exports._onComplete = function _onComplete(tx, f) {
57+ return function aff(success) {
7058 tx.oncomplete = function oncomplete() {
7159 f();
7260 };
7361 success();
74−
75− return function canceler(_, cancelerError) {
76− cancelerError(new Error("Can't cancel IDB Effects"));
77− };
7862 };
7963};
8064
81−export function _onError(tx, f) {
82− return function aff(error, success) {
65+exports._onError = function _onError(tx, f) {
66+ return function aff(success) {
8367 tx.onerror = function onerror(e) {
84− f(e.target.error)();
68+ f(new Error(e.target.error.name))();
8569 };
8670 success();
87−
88− return function canceler(_, cancelerError) {
89− cancelerError(new Error("Can't cancel IDB Effects"));
90− };
9171 };
9272};
src/Database/IndexedDB/IDBTransaction.purs+77−67
@@ -1,66 +1,51 @@
1−-- | An object store is the primary storage mechanism for storing data in a database.
1+-- | A Transaction is used to interact with the data in a database.
2+-- | Whenever data is read or written to the database it is done by using a transaction.
23module Database.IndexedDB.IDBTransaction
3− -- * Interface
4− ( abort
5− , objectStore
6−
7− -- * Attributes
4+ (class IDBTransaction, abort, objectStore
85 , error
96 , mode
10−
11− -- * Event Handlers
127 , onAbort
138 , onComplete
149 , onError
1510 ) where
1611
17−import Database.IndexedDB.Core
12+import Prelude (Unit, (>>>))
13+
14+import Control.Monad.Aff (Aff)
15+import Control.Monad.Eff (Eff)
16+import Control.Monad.Eff.Exception (Error)
17+import Data.Function.Uncurried as Fn
18+import Data.Function.Uncurried (Fn2, Fn4)
19+import Data.Maybe (Maybe)
20+import Data.Nullable (Nullable, toMaybe)
21+
22+import Database.IndexedDB.Core (IDB, Database, ObjectStore, Transaction, TransactionMode(..))
1823
19−import Data.Function.Uncurried (Fn2, Fn4)
20−import Data.Function.Uncurried as Fn
21−import Data.Maybe (Maybe)
22−import Data.Nullable (Nullable, toMaybe)
23−import Effect (Effect)
24−import Effect.Aff (Aff)
25−import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
26−import Effect.Exception (Error)
27−import Prelude (Unit, ($), (<<<), (>>>))
2824
2925--------------------
3026-- INTERFACES
3127--
28+-- | The IDBtransaction interface.
29+class IDBTransaction tx where
30+ -- | Aborts the transaction. All pending requests will fail with a "AbortError"
31+ -- | DOMException and all changes made to the database will be reverted.
32+ abort :: forall e. tx -> Aff (idb :: IDB | e) Unit
33+
34+ -- | Returns an IDBObjectStore in the transaction's scope.
35+ objectStore :: forall e. tx -> String -> Aff (idb :: IDB | e) ObjectStore
3236
33−-- | Aborts the transaction. All pending requests will fail with a "AbortError"
34−-- | DOMException and all changes made to the database will be reverted.
35−abort
36− :: forall tx
37− . (IDBTransaction tx)
38− => tx
39− -> Aff Unit
40−abort =
41− fromEffectFnAff <<< _abort
42−
43−-- | Returns an IDBObjectStore in the transaction's scope.
44−objectStore
45− :: forall tx
46− . (IDBTransaction tx)
47− => tx
48− -> String
49− -> Aff ObjectStore
50−objectStore tx name =
51− fromEffectFnAff $ Fn.runFn2 _objectStore tx name
5237
5338--------------------
5439-- ATTRIBUTES
5540--
56−
57−--- | Returns the transaction’s connection.
41+-- | Returns the transaction’s connection.
5842db
5943 :: Transaction
6044 -> Database
6145db =
6246 _db
6347
48+
6449-- | If the transaction was aborted, returns the error (a DOMException) providing the reason.
6550error
6651 :: Transaction
@@ -76,6 +61,7 @@ mode
7661mode =
7762 Fn.runFn4 _mode ReadOnly ReadWrite VersionChange
7863
64+
7965-- | Returns a list of the names of object stores in the transaction’s scope.
8066-- | For an upgrade transaction this is all object stores in the database.
8167objectStoreNames
@@ -87,67 +73,91 @@ objectStoreNames =
8773--------------------
8874-- EVENT HANDLERS
8975--
90−
9176-- | Event handler for the `abort` event.
9277onAbort
93− :: Transaction
94− -> Effect Unit
95− -> Aff Unit
96−onAbort db' f =
97− fromEffectFnAff $ Fn.runFn2 _onAbort db' f
78+ :: forall e e'
79+ . Transaction
80+ -> Eff ( | e') Unit
81+ -> Aff (idb :: IDB | e) Unit
82+onAbort db f =
83+ Fn.runFn2 _onAbort db f
84+
9885
9986-- | Event handler for the `complete` event.
10087onComplete
101− :: Transaction
102− -> Effect Unit
103− -> Aff Unit
104−onComplete db' f =
105− fromEffectFnAff $ Fn.runFn2 _onComplete db' f
88+ :: forall e e'
89+ . Transaction
90+ -> Eff ( | e') Unit
91+ -> Aff (idb :: IDB | e) Unit
92+onComplete db f =
93+ Fn.runFn2 _onComplete db f
94+
10695
10796-- | Event handler for the `error` event.
10897onError
109− :: Transaction
110− -> (Error -> Effect Unit)
111− -> Aff Unit
112−onError db' f =
113− fromEffectFnAff $ Fn.runFn2 _onError db' f
98+ :: forall e e'
99+ . Transaction
100+ -> (Error -> Eff ( | e') Unit)
101+ -> Aff (idb :: IDB | e) Unit
102+onError db f =
103+ Fn.runFn2 _onError db f
104+
114105
115106--------------------
116−-- FFI
107+-- INSTANCES
117108--
109+instance idbTransactionTransaction :: IDBTransaction Transaction where
110+ objectStore tx name =
111+ Fn.runFn2 _objectStore tx name
112+
113+ abort =
114+ _abort
118115
116+
117+--------------------
118+-- FFI
119+--
119120foreign import _abort
120− :: forall tx
121− . tx
122− -> EffectFnAff Unit
121+ :: forall tx e
122+ . tx
123+ -> Aff (idb :: IDB | e) Unit
124+
123125
124126foreign import _db
125127 :: Transaction
126128 -> Database
127129
130+
128131foreign import _error
129132 :: Transaction
130133 -> (Nullable Error)
131134
135+
132136foreign import _mode
133137 :: Fn4 TransactionMode TransactionMode TransactionMode Transaction TransactionMode
134138
139+
135140foreign import _objectStoreNames
136141 :: Transaction
137142 -> Array String
138143
144+
139145foreign import _objectStore
140− :: forall tx
141− . Fn2 tx String (EffectFnAff ObjectStore)
146+ :: forall tx e
147+ . Fn2 tx String (Aff (idb :: IDB | e) ObjectStore)
148+
142149
143150foreign import _onAbort
144− :: forall tx
145− . Fn2 tx (Effect Unit) (EffectFnAff Unit)
151+ :: forall tx e e'
152+ . Fn2 tx (Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
153+
146154
147155foreign import _onComplete
148− :: forall tx
149− . Fn2 tx (Effect Unit) (EffectFnAff Unit)
156+ :: forall tx e e'
157+ . Fn2 tx (Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
158+
150159
151160foreign import _onError
152− :: forall tx
153− . Fn2 tx (Error -> Effect Unit) (EffectFnAff Unit)
161+ :: forall tx e e'
162+ . Fn2 tx (Error -> Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
163+
test/Main.purs+710−1060
@@ -2,1140 +2,790 @@ module Test.Main where
22
33import Prelude
44
5−import Data.Array (head, drop)
6−import Data.Date as Date
7−import Data.DateTime (DateTime(..), Time(..))
8−import Data.DateTime.Instant (toDateTime)
9−import Data.Either (Either(..))
10−import Data.Enum (toEnum)
11−import Data.Maybe (Maybe(..), isNothing, maybe)
12−import Data.Time.Duration (Milliseconds(..))
13−import Data.Traversable (traverse)
14−import Data.Tuple (Tuple(..), uncurry)
15−import Database.IndexedDB.Core (CursorDirection(..), Database, Index, ObjectStore, Transaction, TransactionMode(..))
16−import Database.IndexedDB.IDBCursor as IDBCursor
17−import Database.IndexedDB.IDBDatabase as IDBDatabase
18−import Database.IndexedDB.IDBFactory as IDBFactory
19−import Database.IndexedDB.IDBIndex as IDBIndex
20−import Database.IndexedDB.IDBKey (Key, none, toKey)
21−import Database.IndexedDB.IDBKeyRange as IDBKeyRange
22−import Database.IndexedDB.IDBObjectStore as IDBObjectStore
23−import Database.IndexedDB.IDBTransaction as IDBTransaction
24−import Effect (Effect)
25−import Effect.Aff (Aff, attempt, delay, forkAff, launchAff, launchAff_)
26−import Effect.Aff.AVar (AVar)
27−import Effect.Aff.AVar as AVar
28−import Effect.Class (liftEffect)
29−import Effect.Console (log)
30−import Effect.Exception (name)
31−import Effect.Now (now)
32−import Test.Spec (describe, it)
33−import Test.Spec.Assertions (shouldEqual, fail)
34−import Test.Spec.Reporter (consoleReporter)
35−import Test.Spec.Runner (runSpec)
5+import Control.Monad.Aff (Aff, launchAff, forkAff, delay, attempt)
6+import Control.Monad.Aff.AVar (AVAR, makeVar, makeVar', modifyVar, peekVar, putVar, takeVar)
7+import Control.Monad.Aff.Console (log)
8+import Control.Monad.Eff (Eff)
9+import Control.Monad.Eff.Class (liftEff)
10+import Control.Monad.Eff.Exception (EXCEPTION, message)
11+import Control.Monad.Eff.Now (NOW, now)
12+import Data.Date as Date
13+import Data.DateTime as DateTime
14+import Data.DateTime (DateTime(..), Time(..))
15+import Data.DateTime.Instant (toDateTime)
16+import Data.Either (Either(..))
17+import Data.Enum (toEnum)
18+import Data.Maybe (Maybe(..), isNothing, maybe)
19+import Data.Time.Duration (Milliseconds(..))
20+import Data.Traversable (traverse)
21+import Data.Tuple (Tuple(..), uncurry)
22+import Test.Spec (describe, describe, it, it)
23+import Test.Spec.Assertions (shouldEqual, fail)
24+import Test.Spec.Mocha (MOCHA, runMocha)
25+
26+import Database.IndexedDB.Core
27+import Database.IndexedDB.IDBKey
28+import Database.IndexedDB.IDBCursor as IDBCursor
29+import Database.IndexedDB.IDBDatabase as IDBDatabase
30+import Database.IndexedDB.IDBFactory as IDBFactory
31+import Database.IndexedDB.IDBIndex as IDBIndex
32+import Database.IndexedDB.IDBKeyRange as IDBKeyRange
33+import Database.IndexedDB.IDBObjectStore as IDBObjectStore
34+import Database.IndexedDB.IDBTransaction as IDBTransaction
3635
37−infixr 7 Tuple as :+:
3836
39−launchAff' :: forall a. Aff a -> Effect Unit
40−launchAff' =
41− launchAff_ <<< void
37+infixr 7 Tuple as :+:
4238
43−modifyAVar :: forall a. (a -> a) -> AVar a -> Aff Unit
44−modifyAVar fn v = do
45− val <- AVar.take v
46− AVar.put (fn val) v
39+launchAff' :: forall a e. Aff e a -> Eff (exception :: EXCEPTION | e) Unit
40+launchAff' aff =
41+ pure unit <* (launchAff aff)
4742
48−main :: Effect Unit
49−main = do
50− launchAff_ $ runSpec [ consoleReporter ] $ do
51− describe "IDBFactory" do
52− let
43+-- main :: forall eff. Eff (now :: NOW, mocha :: MOCHA, idb :: IDB, exception :: EXCEPTION, avar :: AVAR | eff) Unit
44+main = runMocha do
45+ describe "IDBFactory" do
46+ let
5347 tearDown name version db = do
5448 IDBDatabase.close db
5549 version' <- IDBFactory.deleteDatabase name
56− liftEffect $ log $ show version'
5750 version' `shouldEqual` version
5851
59− it "open default" do
60− let
61− name = "db-default"
52+ it "open default" do
53+ let name = "db-default"
6254 version = 1
63− db <- IDBFactory.open name Nothing
64− { onUpgradeNeeded: Nothing
65− , onBlocked: Nothing
66− }
67− IDBDatabase.name db `shouldEqual` name
68− tearDown name version db
55+ db <- IDBFactory.open name Nothing
56+ { onUpgradeNeeded : Nothing
57+ , onBlocked : Nothing
58+ }
59+ IDBDatabase.name db `shouldEqual` name
60+ tearDown name version db
6961
70− it "open specific version" do
71− let
72− name = "db-specific"
73− version = 14
74− db <- IDBFactory.open name (Just version)
75− { onUpgradeNeeded: Nothing
76− , onBlocked: Nothing
77− }
78− IDBDatabase.name db `shouldEqual` name
79− tearDown name version db
8062
81− it "open specific version -> close -> open latest" do
82− let
83− name = "db-latest"
63+ it "open specific version" do
64+ let name = "db-specific"
8465 version = 14
85− db01 <- IDBFactory.open name (Just version)
86− { onUpgradeNeeded: Nothing
87− , onBlocked: Nothing
88− }
89− IDBDatabase.name db01 `shouldEqual` name
90− IDBDatabase.close db01
91− db02 <- IDBFactory.open name Nothing
92− { onUpgradeNeeded: Nothing
93− , onBlocked: Nothing
94− }
95− tearDown name version db02
66+ db <- IDBFactory.open name (Just version)
67+ { onUpgradeNeeded : Nothing
68+ , onBlocked : Nothing
69+ }
70+ IDBDatabase.name db `shouldEqual` name
71+ tearDown name version db
9672
97− it "open + onUpgradeNeed" do
98− let
99− name = "db-upgrade-needed"
73+
74+ it "open specific version -> close -> open latest" do
75+ let name = "db-latest"
76+ version = 14
77+ db <- IDBFactory.open name (Just version)
78+ { onUpgradeNeeded : Nothing
79+ , onBlocked : Nothing
80+ }
81+ IDBDatabase.name db `shouldEqual` name
82+ IDBDatabase.close db
83+ db <- IDBFactory.open name Nothing
84+ { onUpgradeNeeded : Nothing
85+ , onBlocked : Nothing
86+ }
87+ tearDown name version db
88+
89+
90+ it "open + onUpgradeNeed" do
91+ let name = "db-upgrade-needed"
10092 version = 1
101− callback (Tuple varName varVersion) db _ { oldVersion } = do
102− _ <- launchAff $ modifyAVar (const $ IDBDatabase.name db) varName
103− _ <- launchAff $ modifyAVar (const $ oldVersion) varVersion
93+ callback var db _ = do
94+ _ <- launchAff $ modifyVar (const $ IDBDatabase.name db) var
10495 pure unit
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
110− }
111− name' <- AVar.read varName
112− version' <- AVar.read varVersion
113− name' `shouldEqual` name
114− version' `shouldEqual` 0
115− tearDown name version db
116−
117− it "open + onBlocked" do
118− let
119− name = "db-blocked"
96+ var <- makeVar' "-"
97+ db <- IDBFactory.open name Nothing
98+ { onUpgradeNeeded : Just (callback var)
99+ , onBlocked : Nothing
100+ }
101+ name <- peekVar var
102+ name `shouldEqual` name
103+ tearDown name version db
104+
105+
106+ it "open + onBlocked" do
107+ let name = "db-blocked"
120108 version = 14
121109 callback var = do
122− _ <- launchAff $ modifyAVar (const $ "db-blocked") var
110+ _ <- launchAff $ modifyVar (const $ "db-blocked") var
123111 pure unit
124112
125− var <- AVar.new "-"
126− db01 <- IDBFactory.open name Nothing
127− { onUpgradeNeeded: Nothing
128− , onBlocked: Nothing
129− }
130− _ <- forkAff do
131− delay (Milliseconds 100.0)
132− IDBDatabase.close db01
113+ var <- makeVar' "-"
114+ db01 <- IDBFactory.open name Nothing
115+ { onUpgradeNeeded : Nothing
116+ , onBlocked : Nothing
117+ }
118+ _ <- forkAff do
119+ delay (Milliseconds 100.0)
120+ IDBDatabase.close db01
133121
134− db02 <- IDBFactory.open name (Just version)
135− { onUpgradeNeeded: Nothing
136− , onBlocked: Just (callback var)
137− }
138− name' <- AVar.read var
139− name' `shouldEqual` name
140− tearDown name version db02
141−
142− describe "IDBKeyRange" do
143− it "only(int)" do
144− let
145− key = 14
122+ db02 <- IDBFactory.open name (Just version)
123+ { onUpgradeNeeded : Nothing
124+ , onBlocked : Just (callback var)
125+ }
126+ name <- peekVar var
127+ name `shouldEqual` name
128+ tearDown name version db02
129+
130+ describe "IDBKeyRange" do
131+ it "only(int)" do
132+ let key = 14
146133 range = IDBKeyRange.only key
147− IDBKeyRange.includes range (toKey key) `shouldEqual` true
134+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
148135
149− it "only(string)" do
150− let
151− key = "patate"
136+ it "only(string)" do
137+ let key = "patate"
152138 range = IDBKeyRange.only key
153− IDBKeyRange.includes range (toKey key) `shouldEqual` true
139+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
154140
155− it "only(float)" do
156− let
157− key = 14.42
141+ it "only(float)" do
142+ let key = 14.42
158143 range = IDBKeyRange.only key
159− IDBKeyRange.includes range (toKey key) `shouldEqual` true
144+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
160145
161− it "only(date)" do
162− let
163− mkey = DateTime
146+ it "only(date)" do
147+ let mkey = DateTime
164148 <$> (Date.canonicalDate <$> toEnum 2017 <*> toEnum 6 <*> toEnum 23)
165149 <*> (Time <$> toEnum 17 <*> toEnum 59 <*> toEnum 34 <*> toEnum 42)
166150
167− case mkey of
168− Nothing ->
169− fail "unable to create datetime"
170− Just key -> do
171− let range = IDBKeyRange.only key
172− IDBKeyRange.includes range (toKey key) `shouldEqual` true
151+ case mkey of
152+ Nothing ->
153+ fail "unable to create datetime"
154+ Just key -> do
155+ let range = IDBKeyRange.only key
156+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
173157
174− it "only([int])" do
175− let
176− key = [ 14, 42 ]
158+ it "only([int])" do
159+ let key = [14, 42]
177160 range = IDBKeyRange.only key
178− IDBKeyRange.includes range (toKey key) `shouldEqual` true
161+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
179162
180− it "only([string])" do
181− let
182− key = [ "patate", "autruche" ]
163+ it "only([string])" do
164+ let key = ["patate", "autruche"]
183165 range = IDBKeyRange.only key
184− IDBKeyRange.includes range (toKey key) `shouldEqual` true
166+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
185167
186− it "lowerBound(14, open)" do
187− let
188− key = 14
189− open = true
168+ it "lowerBound(14, open)" do
169+ let key = 14
170+ open = true
190171 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
194−
195− it "lowerBound(14, close)" do
196− let
197− key = 14
198− open = false
172+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
173+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
174+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
175+
176+ it "lowerBound(14, close)" do
177+ let key = 14
178+ open = false
199179 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
203−
204− it "upperBound(14, open)" do
205− let
206− key = 14
207− open = true
180+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
181+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
182+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
183+
184+ it "upperBound(14, open)" do
185+ let key = 14
186+ open = true
208187 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
212−
213− it "upperBound(14, close)" do
214− let
215− key = 14
216− open = false
188+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
189+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
190+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
191+
192+ it "upperBound(14, close)" do
193+ let key = 14
194+ open = false
217195 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
221−
222− it "bound(42, 14, open, open) => Nothing" do
223− let
224− lower = 42
225− upper = 14
196+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
197+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
198+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
199+
200+ it "bound(42, 14, open, open) => Nothing" do
201+ let lower = 42
202+ upper = 14
226203 lowerOpen = true
227204 upperOpen = true
228− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
229− isNothing mrange `shouldEqual` true
205+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
206+ isNothing mrange `shouldEqual` true
230207
231− it "bound(14, 42, open, open)" do
232− let
233− lower = 14
234− upper = 42
208+ it "bound(14, 42, open, open)" do
209+ let lower = 14
210+ upper = 42
235211 lowerOpen = true
236212 upperOpen = true
237− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
238− case mrange of
239− Nothing ->
240− fail "invalid range provided"
241− Just range -> do
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)
248−
249− it "bound(14, 42, open, close)" do
250− let
251− lower = 14
252− upper = 42
213+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
214+ case mrange of
215+ Nothing ->
216+ fail "invalid range provided"
217+ Just range -> do
218+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
219+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
220+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
221+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
222+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
223+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
224+
225+ it "bound(14, 42, open, close)" do
226+ let lower = 14
227+ upper = 42
253228 lowerOpen = true
254229 upperOpen = false
255− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
256− case mrange of
257− Nothing ->
258− fail "invalid range provided"
259− Just range -> do
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)
266−
267− it "bound(14, 42, close, open)" do
268− let
269− lower = 14
270− upper = 42
230+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
231+ case mrange of
232+ Nothing ->
233+ fail "invalid range provided"
234+ Just range -> do
235+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
236+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
237+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
238+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
239+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
240+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
241+
242+ it "bound(14, 42, close, open)" do
243+ let lower = 14
244+ upper = 42
271245 lowerOpen = false
272246 upperOpen = true
273− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
274− case mrange of
275− Nothing ->
276− fail "invalid range provided"
277− Just range -> do
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)
284−
285− it "bound(14, 42, close, close)" do
286− let
287− lower = 14
288− upper = 42
247+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
248+ case mrange of
249+ Nothing ->
250+ fail "invalid range provided"
251+ Just range -> do
252+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
253+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
254+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
255+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
256+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
257+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
258+
259+ it "bound(14, 42, close, close)" do
260+ let lower = 14
261+ upper = 42
289262 lowerOpen = false
290263 upperOpen = false
291− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
292− case mrange of
293− Nothing ->
294− fail "invalid range provided"
295− Just range -> do
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)
302−
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
309−
310− describe "IDBDatabase" do
311− let
264+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
265+ case mrange of
266+ Nothing ->
267+ fail "invalid range provided"
268+ Just range -> do
269+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
270+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
271+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
272+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
273+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
274+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
275+
276+ it "can access attributes of a range" do
277+ let range = IDBKeyRange.lowerBound 14 false
278+ IDBKeyRange.lower range `shouldEqual` (Just $ toKey 14)
279+ IDBKeyRange.upper range `shouldEqual` (Nothing :: Maybe Key)
280+ IDBKeyRange.lowerOpen range `shouldEqual` false
281+ IDBKeyRange.upperOpen range `shouldEqual` true
282+
283+ describe "IDBDatabase" do
284+ let
312285 tearDown db = do
313286 IDBDatabase.close db
314287 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
315288 pure unit
316289
317290 setup storeParams = do
318− let
319− onUpgradeNeeded var db _ _ = launchAff' do
320− store <- IDBDatabase.createObjectStore db "store" storeParams
321− _ <- AVar.put { db, store } var
322− pure unit
323−
324− var <- AVar.empty
325− _ <- IDBFactory.open "db" Nothing
326− { onUpgradeNeeded: Just (onUpgradeNeeded var)
327− , onBlocked: Nothing
291+ let onUpgradeNeeded var db _ = launchAff' do
292+ store <- IDBDatabase.createObjectStore db "store" storeParams
293+ _ <- putVar var { db, store }
294+ pure unit
295+
296+ var <- makeVar
297+ db <- IDBFactory.open "db" Nothing
298+ { onUpgradeNeeded : Just (onUpgradeNeeded var)
299+ , onBlocked : Nothing
328300 }
329301
330− AVar.take var
331−
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` []
338− tearDown db
339−
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` []
346− tearDown db
347−
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` []
354− tearDown db
355−
356− it "deleteObjectStore" do
357− let
358− onUpgradeNeeded var db _ _ = launchAff' do
302+ takeVar var
303+
304+ it "createObjectStore (keyPath: [], autoIncrement: true)" do
305+ { db, store } <- setup { keyPath: [], autoIncrement: true }
306+ IDBObjectStore.name store `shouldEqual` "store"
307+ IDBObjectStore.keyPath store `shouldEqual` []
308+ IDBObjectStore.autoIncrement store `shouldEqual` true
309+ IDBObjectStore.indexNames store `shouldEqual` []
310+ tearDown db
311+
312+ it "createObjectStore (keyPath: [\"patate\"], autoIncrement: true)" do
313+ { db, store } <- setup { keyPath: ["patate"], autoIncrement: true }
314+ IDBObjectStore.name store `shouldEqual` "store"
315+ IDBObjectStore.keyPath store `shouldEqual` ["patate"]
316+ IDBObjectStore.autoIncrement store `shouldEqual` true
317+ IDBObjectStore.indexNames store `shouldEqual` []
318+ tearDown db
319+
320+ it "createObjectStore (keyPath: [\"a\", \"b\"], autoIncrement: false)" do
321+ { db, store } <- setup { keyPath: ["patate", "autruche"], autoIncrement: false }
322+ IDBObjectStore.name store `shouldEqual` "store"
323+ IDBObjectStore.keyPath store `shouldEqual` ["patate", "autruche"]
324+ IDBObjectStore.autoIncrement store `shouldEqual` false
325+ IDBObjectStore.indexNames store `shouldEqual` []
326+ tearDown db
327+
328+ it "deleteObjectStore" do
329+ let onUpgradeNeeded var db _ = launchAff' do
359330 _ <- IDBDatabase.deleteObjectStore db "store"
360− AVar.put true var
361−
362− var <- AVar.empty
363− { db } <- setup IDBDatabase.defaultParameters
364− IDBDatabase.close db
365− db' <- IDBFactory.open "db" (Just 999)
366− { onUpgradeNeeded: Just (onUpgradeNeeded var)
367− , onBlocked: Nothing
368− }
369− deleted <- AVar.take var
370− deleted `shouldEqual` true
371− tearDown db'
331+ putVar var true
372332
373− describe "IDBObjectStore" do
374− let
375− tearDown db = do
376− IDBDatabase.close db
377− _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
378− pure unit
379−
380− setup { storeParams, onUpgradeNeeded } = do
381− let
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
386−
387− var <- AVar.empty
388− _ <- IDBFactory.open "db" Nothing
389− { onUpgradeNeeded: Just (onUpgradeNeeded' var)
390− , onBlocked: Nothing
391− }
392−
393− AVar.take var
394−
395− it "add()" do
396− date <- liftEffect $ toDateTime <$> now
397− { db } <- setup
398− { storeParams: { autoIncrement: true, keyPath: [] }
399− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
400− -- no key
401− key <- IDBObjectStore.add store "patate" none
402− (toKey 1) `shouldEqual` key
403−
404− -- int key
405− key' <- IDBObjectStore.add store "patate" (Just 14)
406− (toKey 14) `shouldEqual` key'
407−
408− -- number key
409− key'' <- IDBObjectStore.add store "patate" (Just 14.42)
410− (toKey 14.42) `shouldEqual` key''
333+ var <- makeVar
334+ { db, store } <- setup IDBObjectStore.defaultParameters
335+ IDBDatabase.close db
336+ db <- IDBFactory.open "db" (Just 999) { onUpgradeNeeded : Just (onUpgradeNeeded var)
337+ , onBlocked : Nothing
338+ }
339+ deleted <- takeVar var
340+ deleted `shouldEqual` true
341+ tearDown db
411342
412− -- string key
413− key''' <- IDBObjectStore.add store "patate" (Just "key")
414− (toKey "key") `shouldEqual` key'''
415−
416− -- date key
417− key'''' <- IDBObjectStore.add store "patate" (Just date)
418− (toKey date) `shouldEqual` key''''
419−
420− -- array key
421− key''''' <- IDBObjectStore.add store "patate" (Just $ toKey [ 14, 42 ])
422− (toKey [ 14, 42 ]) `shouldEqual` key'''''
423− }
424− tearDown db
425−
426− it "clear()" do
427− { db } <- setup
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)
434− }
435− tearDown db
436−
437− it "count()" do
438− { db } <- setup
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
444− n `shouldEqual` 2
445− }
446− tearDown db
447−
448− it "getKey()" do
449− { db } <- setup
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)
455−
456− mkey' <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
457− mkey' `shouldEqual` none
458− }
459− tearDown db
460−
461− it "getAllKeys()" do
462− { db } <- setup
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)
468−
469− -- no bounds
470− keys <- IDBObjectStore.getAllKeys store Nothing Nothing
471− keys `shouldEqual` [ key1, key2, key3 ]
472−
473− -- lower bound
474− keys' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 14 true) Nothing
475− keys' `shouldEqual` [ key2, key3 ]
476−
477− -- upper bound
478− keys'' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.upperBound 42 false) Nothing
479− keys'' `shouldEqual` [ key1, key2 ]
480−
481− -- count
482− keys''' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
483− keys''' `shouldEqual` [ key1, key2 ]
484− }
485− tearDown db
486−
487− it "openCursor()" do
488− let
489− cb =
490− { onSuccess: const $ pure unit
491− , onError: show >>> fail >>> launchAff'
492− , onComplete: pure unit
493− }
494− { db } <- setup
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
502− pure unit
503− }
504− tearDown db
505−
506− it "openKeyCursor()" do
507− let
508− cb =
509− { onSuccess: const $ pure unit
510− , onError: show >>> fail >>> launchAff'
511− , onComplete: pure unit
512− }
513− { db } <- setup
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
521− pure unit
522− }
523− tearDown db
524343
525− describe "IDBIndex" do
526− let
344+ describe "IDBObjectStore" do
345+ let
527346 tearDown db = do
528347 IDBDatabase.close db
529348 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
530349 pure unit
531350
532− setup
533− :: forall value
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)
539− }
540− -> Aff { db :: Database, index :: Index, store :: ObjectStore }
541− setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
542− let
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
549−
550− var <- AVar.empty
551− _ <- IDBFactory.open "db" Nothing
552− { onUpgradeNeeded: Just (onUpgradeNeeded' var)
553− , onBlocked: Nothing
554− }
555− AVar.take var
556−
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
562− , keyPath: []
563− , values: []
564− }
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
569− tearDown db
570−
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)
573− txVar <- AVar.empty
574− dbVar <- AVar.empty
575− res <- attempt $ setup
576− { storeParams: IDBDatabase.defaultParameters
577− , indexParams:
578− { unique: true
579− , multiEntry: false
580− }
581− , keyPath: [ "indexedProperty" ]
582− , values:
583− [ { indexedProperty: "bar" } :+: (Just $ toKey 1)
584− , { indexedProperty: "bar" } :+: (Just $ toKey 2)
585− ]
586− , onUpgradeNeeded: Just $ \db tx _ -> launchAff' do
587− IDBTransaction.onAbort tx (onAbort txVar)
588− IDBDatabase.onAbort db (onAbort dbVar)
589− }
590− case res of
591− Right _ ->
592− fail "expected abort"
593− _ -> do
594− shouldEqual true =<< AVar.take txVar
595− shouldEqual true =<< AVar.take dbVar
596−
597− it "the index is usable right after being made" do
598− { db } <- setup
599− { storeParams:
600− { keyPath: [ "key" ]
601− , autoIncrement: false
602− }
603− , indexParams: IDBObjectStore.defaultParameters
604− , keyPath: [ "indexedProperty" ]
605− , values:
606− [ { key: "key1", indexedProperty: "indexed_1" } :+: Nothing
607− , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing
608− , { key: "key3", indexedProperty: "indexed_3" } :+: Nothing
609− ]
610− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
611− val <- IDBIndex.get index (IDBKeyRange.only "indexed_2")
612− ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2")
613− }
614− tearDown db
615−
616− it "empty keyPath" do
617− { db } <- setup
618− { storeParams: IDBDatabase.defaultParameters
619− , indexParams: IDBObjectStore.defaultParameters
620− , keyPath: []
621− , values:
622− [ "object_1" :+: (Just $ toKey 1)
623− , "object_2" :+: (Just $ toKey 2)
624− , "object_3" :+: (Just $ toKey 3)
625− ]
626− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
627− val <- IDBIndex.get index (IDBKeyRange.only "object_3")
628− val `shouldEqual` (Just "object_3")
629− }
630− tearDown db
631−
632− it "index can be valid keys [date]" do
633− date <- liftEffect $ toDateTime <$> now
634− { db } <- setup
635− { storeParams:
636− { keyPath: [ "key" ]
637− , autoIncrement: false
638− }
639− , indexParams: IDBObjectStore.defaultParameters
640− , keyPath: [ "i" ]
641− , values:
642− [ { key: "date", i: (toKey date) } :+: Nothing
643− ]
644− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
645− val <- IDBIndex.get index (IDBKeyRange.only date)
646− ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
647− }
648− tearDown db
649−
650− it "index can be valid keys [num]" do
651− let num = 14
652− { db } <- setup
653− { storeParams:
654− { keyPath: [ "key" ]
655− , autoIncrement: false
656− }
657− , indexParams: IDBObjectStore.defaultParameters
658− , keyPath: [ "i" ]
659− , values:
660− [ { key: "num", i: (toKey num) } :+: Nothing
661− ]
662− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
663− val <- IDBIndex.get index (IDBKeyRange.only num)
664− ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
665− }
666− tearDown db
667−
668− it "index can be valid keys [array]" do
669− let array = [ "patate", "autruche" ]
670− { db } <- setup
671− { storeParams:
672− { keyPath: [ "key" ]
673− , autoIncrement: false
674− }
675− , indexParams: IDBObjectStore.defaultParameters
676− , keyPath: [ "i" ]
677− , values:
678− [ { key: "array", i: (toKey array) } :+: Nothing
679− ]
680− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
681− val <- IDBIndex.get index (IDBKeyRange.only array)
682− ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
683− }
684− tearDown db
685−
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" ]
691− , values:
692− [ { key: 14, indexedProperty: "patate" } :+: Nothing
693− ]
694− , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
695− let
696− cb =
697− { onSuccess: const $ pure unit
698− , onError: show >>> fail >>> launchAff'
699− , onComplete: pure unit
700− }
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
705− case cursor of
706− Right _ -> fail "expected InvalidStateError"
707− Left err -> name err `shouldEqual` "InvalidStateError"
708− }
709− case res of
710− Right _ -> fail "expected InvalidStateError"
711− _ -> pure unit
712−
713− it "openKeyCursor() - throw TransactionInactiveError on aborted transaction" do
714− { db } <- setup
715− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
716− , indexParams: IDBObjectStore.defaultParameters
717− , keyPath: [ "indexedProperty" ]
718− , values:
719− [ { key: 14, indexedProperty: "patate" } :+: Nothing
720− ]
721− , onUpgradeNeeded: Nothing
722− }
723− let
724− cb =
725− { onSuccess: const $ pure unit
726− , onError: show >>> fail >>> launchAff'
727− , onComplete: pure unit
728− }
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
735− case cursor of
736− Right _ -> fail "expected TransactionInactiveError"
737− Left err -> name err `shouldEqual` "TransactionInactiveError"
738−
739− tearDown db
740−
741− it "openKeyCursor() - throw InvalidStateError when the index is deleted" do
742− { db } <- setup
743− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
744− , indexParams: IDBObjectStore.defaultParameters
745− , keyPath: [ "indexedProperty" ]
746− , values:
747− [ { key: 14, indexedProperty: "patate" } :+: Nothing
748− ]
749− , onUpgradeNeeded: Just $ \_ tx index -> launchAff' do
750− let
751− cb =
752− { onSuccess: const $ pure unit
753− , onError: show >>> fail >>> launchAff'
754− , onComplete: pure unit
755− }
756− store <- IDBTransaction.objectStore tx "store"
757− IDBObjectStore.deleteIndex store "index"
758− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
759− case cursor of
760− Right _ -> fail "expected InvalidStateError"
761− Left err -> name err `shouldEqual` "InvalidStateError"
762− }
763−
764− tearDown db
765−
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" ]
771− , values:
772− [ { key: 14, indexedProperty: "patate" } :+: Nothing
773− ]
774− , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
775− let
776− cb =
777− { onSuccess: const $ pure unit
778− , onError: show >>> fail >>> launchAff'
779− , onComplete: pure unit
780− }
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
785− case cursor of
786− Right _ -> fail "expected InvalidStateError"
787− Left err -> name err `shouldEqual` "InvalidStateError"
788− }
789− case res of
790− Right _ -> fail "expected InvalidStateError"
791− _ -> pure unit
792−
793− it "openCursor() - throw TransactionInactiveError on aborted transaction" do
794− { db } <- setup
795− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
796− , indexParams: IDBObjectStore.defaultParameters
797− , keyPath: [ "indexedProperty" ]
798− , values:
799− [ { key: 14, indexedProperty: "patate" } :+: Nothing
800− ]
801− , onUpgradeNeeded: Nothing
802− }
803− let
804− cb =
805− { onSuccess: const $ pure unit
806− , onError: show >>> fail >>> launchAff'
807− , onComplete: pure unit
351+ setup { storeParams, onUpgradeNeeded } = do
352+ let onUpgradeNeeded' var db _ = launchAff' do
353+ store <- IDBDatabase.createObjectStore db "store" storeParams
354+ liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store)
355+ putVar var { db, store }
356+
357+ var <- makeVar
358+ db <- IDBFactory.open "db" Nothing
359+ { onUpgradeNeeded : Just (onUpgradeNeeded' var)
360+ , onBlocked : Nothing
808361 }
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
815− case cursor of
816− Right _ -> fail "expected TransactionInactiveError"
817− Left err -> name err `shouldEqual` "TransactionInactiveError"
818−
819− tearDown db
820−
821− it "openCursor() - throw InvalidStateError when the index is deleted" do
822− { db } <- setup
823− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
824− , indexParams: IDBObjectStore.defaultParameters
825− , keyPath: [ "indexedProperty" ]
826− , values:
827− [ { key: 14, indexedProperty: "patate" } :+: Nothing
828− ]
829− , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
830− let
831− cb =
832− { onSuccess: const $ pure unit
833− , onError: show >>> fail >>> launchAff'
834− , onComplete: pure unit
835− }
836− store <- IDBTransaction.objectStore tx "store"
837− IDBObjectStore.deleteIndex store "index"
838− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
839− case cursor of
840− Right _ -> fail "expected InvalidStateError"
841− Left err -> name err `shouldEqual` "InvalidStateError"
842− }
843362
844− tearDown db
845−
846− it "getKey() - multiEntry - adding keys" do
847− { db } <- setup
848− { storeParams: IDBDatabase.defaultParameters
849− , indexParams: { unique: false, multiEntry: true }
850− , keyPath: [ "name" ]
851− , values:
852− [ { name: [ "patate", "autruche" ] } :+: (Just $ toKey 1)
853− , { name: [ "bob" ] } :+: (Just $ toKey 2)
854− ]
855− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
856− key <- IDBIndex.getKey index (IDBKeyRange.only "patate")
857− key `shouldEqual` (Just $ toKey 1)
858−
859− key' <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
860− key' `shouldEqual` (Just $ toKey 1)
861−
862− key'' <- IDBIndex.getKey index (IDBKeyRange.only "bob")
863− key'' `shouldEqual` (Just $ toKey 2)
864− }
865− tearDown db
866−
867− it "get() - returns the record with the first key in the range" do
868− { db } <- setup
869− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
870− , indexParams: IDBObjectStore.defaultParameters
871− , keyPath: [ "indexedProperty" ]
872− , values:
873− [ { key: 14, indexedProperty: "patate" } :+: Nothing
874− , { key: 42, indexedProperty: "autruche" } :+: Nothing
875− , { key: 1337, indexedProperty: "baguette" } :+: Nothing
876− ]
877− , onUpgradeNeeded: Nothing
878− }
879−
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)
885−
886− tearDown db
363+ takeVar var
364+
365+ it "add()" do
366+ date <- liftEff $ toDateTime <$> now
367+ { db } <- setup
368+ { storeParams: { autoIncrement: true, keyPath: [] }
369+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
370+ -- no key
371+ key <- IDBObjectStore.add store "patate" (Nothing :: Maybe Key)
372+ (toKey 1) `shouldEqual` key
373+
374+ -- int key
375+ key <- IDBObjectStore.add store "patate" (Just 14)
376+ (toKey 14) `shouldEqual` key
377+
378+ -- number key
379+ key <- IDBObjectStore.add store "patate" (Just 14.42)
380+ (toKey 14.42) `shouldEqual` key
381+
382+ -- string key
383+ key <- IDBObjectStore.add store "patate" (Just "key")
384+ (toKey "key") `shouldEqual` key
385+
386+ -- date key
387+ key <- IDBObjectStore.add store "patate" (Just date)
388+ (toKey date) `shouldEqual` key
389+
390+ -- array key
391+ key <- IDBObjectStore.add store "patate" (Just $ toKey [14, 42])
392+ (toKey [14, 42]) `shouldEqual` key
393+ }
394+ tearDown db
395+
396+ it "clear()" do
397+ { db } <- setup
398+ { storeParams: IDBObjectStore.defaultParameters
399+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
400+ key <- IDBObjectStore.add store "patate" (Just 14)
401+ _ <- IDBObjectStore.clear store
402+ val <- IDBObjectStore.get store (IDBKeyRange.only key)
403+ val `shouldEqual` (Nothing :: Maybe String)
404+ }
405+ tearDown db
406+
407+ it "count()" do
408+ { db } <- setup
409+ { storeParams: IDBObjectStore.defaultParameters
410+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
411+ key <- IDBObjectStore.add store "patate" (Just 14)
412+ key <- IDBObjectStore.add store "autruche" (Just 42)
413+ n <- IDBObjectStore.count store Nothing
414+ n `shouldEqual` 2
415+ }
416+ tearDown db
417+
418+ it "getKey()" do
419+ { db } <- setup
420+ { storeParams: IDBObjectStore.defaultParameters
421+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
422+ key <- IDBObjectStore.add store "patate" (Just 14)
423+ mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14)
424+ mkey `shouldEqual` (Just key)
425+
426+ mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
427+ mkey `shouldEqual` (Nothing :: Maybe Key)
428+ }
429+ tearDown db
430+
431+ it "getAllKeys()" do
432+ { db } <- setup
433+ { storeParams: IDBObjectStore.defaultParameters
434+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
435+ key1 <- IDBObjectStore.add store "patate" (Just 14)
436+ key2 <- IDBObjectStore.add store "autruche" (Just 42)
437+ key3 <- IDBObjectStore.add store 14 (Just 1337)
438+
439+ -- no bounds
440+ keys <- IDBObjectStore.getAllKeys store Nothing Nothing
441+ keys `shouldEqual` [key1, key2, key3]
442+
443+ -- lower bound
444+ keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 14 true) Nothing
445+ keys `shouldEqual` [key2, key3]
446+
447+ -- upper bound
448+ keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.upperBound 42 false) Nothing
449+ keys `shouldEqual` [key1, key2]
450+
451+ -- count
452+ keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
453+ keys `shouldEqual` [key1, key2]
454+ }
455+ tearDown db
456+
457+ it "openCursor()" do
458+ { db } <- setup
459+ { storeParams: IDBObjectStore.defaultParameters
460+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
461+ _ <- IDBObjectStore.openCursor store Nothing Next
462+ _ <- IDBObjectStore.openCursor store Nothing NextUnique
463+ _ <- IDBObjectStore.openCursor store Nothing Prev
464+ _ <- IDBObjectStore.openCursor store Nothing PrevUnique
465+ _ <- IDBObjectStore.openCursor store (Just $ IDBKeyRange.upperBound 1 true) Next
466+ pure unit
467+ }
468+ tearDown db
469+
470+ it "openKeyCursor()" do
471+ { db } <- setup
472+ { storeParams: IDBObjectStore.defaultParameters
473+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
474+ _ <- IDBObjectStore.openKeyCursor store Nothing Next
475+ _ <- IDBObjectStore.openKeyCursor store Nothing NextUnique
476+ _ <- IDBObjectStore.openKeyCursor store Nothing Prev
477+ _ <- IDBObjectStore.openKeyCursor store Nothing PrevUnique
478+ _ <- IDBObjectStore.openKeyCursor store (Just $ IDBKeyRange.lowerBound 1 true) Next
479+ pure unit
480+ }
481+ tearDown db
887482
888− describe "IDBCursor" do
889− let
483+ describe "IDBIndex" do
484+ let
890485 tearDown db = do
891486 IDBDatabase.close db
892487 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
893488 pure unit
894489
895− setup
896− :: forall value
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)
902− }
903− -> Aff { db :: Database, index :: Index, store :: ObjectStore }
490+ setup :: forall value e e'.
491+ { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
492+ , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
493+ , values :: Array (Tuple value (Maybe Key))
494+ , keyPath :: Array String
495+ , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
496+ } -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
904497 setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
905− let
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
912−
913− var <- AVar.empty
914− db <- IDBFactory.open "db" Nothing
915− { onUpgradeNeeded: Just (onUpgradeNeeded' var)
916− , onBlocked: Nothing
917− }
918− AVar.take var
919−
920− it "continue() - iterate to the next record" do
921− { db } <- setup
922− { storeParams: IDBDatabase.defaultParameters
923− , indexParams: IDBObjectStore.defaultParameters
924− , keyPath: []
925− , values:
926− [ "cupcake" :+: (Just $ toKey 4)
927− , "pancake" :+: (Just $ toKey 2)
928− , "pie" :+: (Just $ toKey 1)
929− , "pie" :+: (Just $ toKey 3)
930− ]
931− , onUpgradeNeeded: Nothing
932− }
933− let
934− cb vdone vvals =
935− { onComplete: launchAff' do
936− AVar.put unit vdone
937−
938− , onError: \error -> launchAff' do
939− fail $ "unexpected error: " <> show error
940−
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
947− }
948− vdone <- AVar.empty
949− vvals <- AVar.new
950− [ { v: "pie", k: toKey 1 }
951− , { v: "pancake", k: toKey 2 }
952− , { v: "pie", k: toKey 3 }
953− , { v: "cupcake", k: toKey 4 }
954− ]
955− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
956− store <- IDBTransaction.objectStore tx "store"
957− IDBObjectStore.openCursor store Nothing Next (cb vdone vvals)
958− AVar.take vdone
959− tearDown db
960−
961− it "continue() - attempt to iterate in the wrong direction" do
962− { db } <- setup
963− { storeParams: IDBDatabase.defaultParameters
964− , indexParams: IDBObjectStore.defaultParameters
965− , keyPath: []
966− , values:
967− [ "cupcake" :+: (Just $ toKey 4)
968− , "pancake" :+: (Just $ toKey 2)
969− , "pie" :+: (Just $ toKey 1)
970− , "pie" :+: (Just $ toKey 3)
971− ]
972− , onUpgradeNeeded: Nothing
973− }
974− let
975− cb vdone =
976− { onComplete: launchAff' do
977− fail $ "shouldn't complete"
978−
979− , onError: \error -> launchAff' do
980− fail $ "unexpected error: " <> show error
981−
982− , onSuccess: \cursor -> launchAff' do
983− res <- attempt $ IDBCursor.continue cursor (Just 1)
984− case res of
985− Left err -> do
986− name err `shouldEqual` "DataError"
987− AVar.put unit vdone
988− Right _ -> do
989− fail "expected continue to fail"
498+ let onUpgradeNeeded' var db tx = launchAff' do
499+ store <- IDBDatabase.createObjectStore db "store" storeParams
500+ _ <- traverse (uncurry (IDBObjectStore.add store)) values
501+ index <- IDBObjectStore.createIndex store "index" keyPath indexParams
502+ liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
503+ putVar var { db, index, store }
504+
505+ var <- makeVar
506+ db <- IDBFactory.open "db" Nothing
507+ { onUpgradeNeeded : Just (onUpgradeNeeded' var)
508+ , onBlocked : Nothing
990509 }
991− vdone <- AVar.empty
992− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
993− store <- IDBTransaction.objectStore tx "store"
994− IDBObjectStore.openCursor store Nothing Next (cb vdone)
995− AVar.take vdone
996− tearDown db
997−
998− it "advance() - iterate cursor number of times specified by count" do
999− { db } <- setup
1000− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1001− , indexParams: IDBObjectStore.defaultParameters
1002− , keyPath: []
1003− , values:
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
1008− ]
1009− , onUpgradeNeeded: Nothing
1010− }
1011− let
1012− cb vdone vjump =
1013− { onComplete: launchAff' do
1014− AVar.put unit vdone
1015−
1016− , onError: \error -> launchAff' do
1017− fail $ "unexpected error: " <> show error
1018−
1019− , onSuccess: \cursor -> launchAff' do
1020− jump <- AVar.take vjump
1021− if jump then do
1022− IDBCursor.advance cursor 3
1023− else do
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
1029− }
1030− vdone <- AVar.empty
1031− vjump <- AVar.new true
1032− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
1033− store <- IDBTransaction.objectStore tx "store"
1034− IDBObjectStore.openCursor store Nothing Next (cb vdone vjump)
1035− AVar.take vdone
1036− tearDown db
1037−
1038− it "delete() - remove a record from the object store" do
1039− { db } <- setup
1040− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1041− , indexParams: IDBObjectStore.defaultParameters
1042− , keyPath: []
1043− , values:
1044− [ { 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
1048− ]
1049− , onUpgradeNeeded: Nothing
1050− }
1051− let
1052− cb vdone store =
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
1057−
1058− , onError: \error -> launchAff' do
1059− fail $ "unexpected error: " <> show error
1060−
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
1066− }
1067− vdone <- AVar.empty
1068− tx <- IDBDatabase.transaction db [ "store" ] ReadWrite
1069− store <- IDBTransaction.objectStore tx "store"
1070− IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1071− AVar.take vdone
1072− tearDown db
1073−
1074− it "update() - modify a record in the object store" do
1075− { db } <- setup
1076− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1077− , indexParams: IDBObjectStore.defaultParameters
1078− , keyPath: []
1079− , values:
1080− [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1081− ]
1082− , onUpgradeNeeded: Nothing
1083− }
1084− let
1085− cb vdone store =
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
1090−
1091− , onError: \error -> launchAff' do
1092− fail $ "unexpected error: " <> show error
1093−
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
1100− }
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)
1105− AVar.take vdone
1106− tearDown db
1107−
1108− it "update() - throw ReadOnlyError after update on ReadOnly transaction" do
1109− { db } <- setup
1110− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1111− , indexParams: IDBObjectStore.defaultParameters
1112− , keyPath: []
1113− , values:
1114− [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1115− ]
1116− , onUpgradeNeeded: Nothing
1117− }
1118− let
1119− cb vdone =
1120− { onComplete: launchAff' do
1121− fail $ "shouldn't complete"
1122−
1123− , onError: \error -> launchAff' do
1124− fail $ "unexpected error: " <> show error
1125−
1126− , onSuccess: \cursor -> launchAff' do
1127− res <- attempt $ IDBCursor.update cursor "patate"
1128− case res of
1129− Left err -> do
1130− name err `shouldEqual` "ReadOnlyError"
1131− AVar.put unit vdone
1132− Right _ ->
1133− fail $ "expected ReadOnlyError"
1134− }
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)
1139− AVar.take vdone
1140− tearDown db
1141−
510+ takeVar var
511+
512+
513+ it "returns an IDBIndex and the properties are set correctly" do
514+ { db, index } <- setup
515+ { storeParams : IDBObjectStore.defaultParameters
516+ , indexParams : IDBIndex.defaultParameters
517+ , onUpgradeNeeded : Nothing
518+ , keyPath : []
519+ , values : []
520+ }
521+ IDBIndex.name index `shouldEqual` "index"
522+ IDBIndex.keyPath index `shouldEqual` []
523+ IDBIndex.unique index `shouldEqual` IDBIndex.defaultParameters.unique
524+ IDBIndex.multiEntry index `shouldEqual` IDBIndex.defaultParameters.multiEntry
525+ tearDown db
526+
527+ it "attempt to create an index that requires unique values on an object store already contains duplicates" do
528+ let onAbort var = launchAff' (putVar var true)
529+ txVar <- makeVar
530+ dbVar <- makeVar
531+ res <- attempt $ setup
532+ { storeParams : IDBObjectStore.defaultParameters
533+ , indexParams : { unique : true
534+ , multiEntry : false
535+ }
536+ , keyPath : ["indexedProperty"]
537+ , values : [ { indexedProperty: "bar" } :+: (Just $ toKey 1)
538+ , { indexedProperty: "bar" } :+: (Just $ toKey 2)
539+ ]
540+ , onUpgradeNeeded : Just $ \db tx _ -> launchAff' do
541+ IDBTransaction.onAbort tx (onAbort txVar)
542+ IDBDatabase.onAbort db (onAbort dbVar)
543+ }
544+ case res of
545+ Right _ ->
546+ fail "expected abort"
547+ _ -> do
548+ shouldEqual true =<< takeVar txVar
549+ shouldEqual true =<< takeVar dbVar
550+
551+
552+ it "the index is usable right after being made" do
553+ { db } <- setup
554+ { storeParams : { keyPath : ["key"]
555+ , autoIncrement : false
556+ }
557+ , indexParams : IDBIndex.defaultParameters
558+ , keyPath : ["indexedProperty"]
559+ , values : [ { key: "key1", indexedProperty: "indexed_1" } :+: Nothing
560+ , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing
561+ , { key: "key3", indexedProperty: "indexed_3" } :+: Nothing
562+ ]
563+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
564+ val <- IDBIndex.get index (IDBKeyRange.only "indexed_2")
565+ ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2")
566+ }
567+ tearDown db
568+
569+ it "empty keyPath" do
570+ { db } <- setup
571+ { storeParams : IDBObjectStore.defaultParameters
572+ , indexParams : IDBIndex.defaultParameters
573+ , keyPath : []
574+ , values : [ "object_1" :+: (Just $ toKey 1)
575+ , "object_2" :+: (Just $ toKey 2)
576+ , "object_3" :+: (Just $ toKey 3)
577+ ]
578+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
579+ val <- IDBIndex.get index (IDBKeyRange.only "object_3")
580+ val `shouldEqual` (Just "object_3")
581+ }
582+ tearDown db
583+
584+ it "index can be valid keys [date]" do
585+ date <- liftEff $ toDateTime <$> now
586+ { db } <- setup
587+ { storeParams : { keyPath: ["key"]
588+ , autoIncrement: false
589+ }
590+ , indexParams : IDBIndex.defaultParameters
591+ , keyPath : ["i"]
592+ , values : [ { key: "date", i: (toKey date) } :+: Nothing
593+ ]
594+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
595+ val <- IDBIndex.get index (IDBKeyRange.only date)
596+ ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
597+ }
598+ tearDown db
599+
600+ it "index can be valid keys [num]" do
601+ let num = 14
602+ { db } <- setup
603+ { storeParams : { keyPath: ["key"]
604+ , autoIncrement: false
605+ }
606+ , indexParams : IDBIndex.defaultParameters
607+ , keyPath : ["i"]
608+ , values : [ { key: "num", i: (toKey num) } :+: Nothing
609+ ]
610+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
611+ val <- IDBIndex.get index (IDBKeyRange.only num)
612+ ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
613+ }
614+ tearDown db
615+
616+ it "index can be valid keys [array]" do
617+ let array = ["patate", "autruche"]
618+ { db } <- setup
619+ { storeParams : { keyPath: ["key"]
620+ , autoIncrement: false
621+ }
622+ , indexParams : IDBIndex.defaultParameters
623+ , keyPath : ["i"]
624+ , values : [ { key: "array", i: (toKey array) } :+: Nothing
625+ ]
626+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
627+ val <- IDBIndex.get index (IDBKeyRange.only array)
628+ ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
629+ }
630+ tearDown db
631+
632+ it "openKeyCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
633+ res <- attempt $ setup
634+ { storeParams : { keyPath: ["key"], autoIncrement: false }
635+ , indexParams : IDBIndex.defaultParameters
636+ , keyPath : ["indexedProperty"]
637+ , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
638+ ]
639+ , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
640+ IDBTransaction.onAbort tx (pure unit)
641+ IDBDatabase.onAbort db (pure unit)
642+ IDBTransaction.abort tx
643+ cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next
644+ case cursor of
645+ Right _ -> fail "expected InvalidStateError"
646+ Left err -> message err `shouldEqual` "InvalidStateError"
647+ }
648+ case res of
649+ Right _ -> fail "expected InvalidStateError"
650+ _ -> pure unit
651+
652+ it "openKeyCursor() - throw TransactionInactiveError on aborted transaction" do
653+ { db } <- setup
654+ { storeParams : { keyPath: ["key"], autoIncrement: false }
655+ , indexParams : IDBIndex.defaultParameters
656+ , keyPath : ["indexedProperty"]
657+ , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
658+ ]
659+ , onUpgradeNeeded : Nothing
660+ }
661+
662+ tx <- IDBDatabase.transaction db ["store"] ReadOnly
663+ store <- IDBTransaction.objectStore tx "store"
664+ index <- IDBObjectStore.index store "index"
665+ IDBTransaction.onAbort tx (pure unit)
666+ IDBTransaction.abort tx
667+ cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next
668+ case cursor of
669+ Right _ -> fail "expected TransactionInactiveError"
670+ Left err -> message err `shouldEqual` "TransactionInactiveError"
671+
672+ tearDown db
673+
674+ it "openKeyCursor() - throw InvalidStateError when the index is deleted" do
675+ { db } <- setup
676+ { storeParams : { keyPath: ["key"], autoIncrement: false }
677+ , indexParams : IDBIndex.defaultParameters
678+ , keyPath : ["indexedProperty"]
679+ , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
680+ ]
681+ , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
682+ store <- IDBTransaction.objectStore tx "store"
683+ IDBObjectStore.deleteIndex store "index"
684+ cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next
685+ case cursor of
686+ Right _ -> fail "expected InvalidStateError"
687+ Left err -> message err `shouldEqual` "InvalidStateError"
688+ }
689+
690+ tearDown db
691+
692+ it "openCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
693+ res <- attempt $ setup
694+ { storeParams : { keyPath: ["key"], autoIncrement: false }
695+ , indexParams : IDBIndex.defaultParameters
696+ , keyPath : ["indexedProperty"]
697+ , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
698+ ]
699+ , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
700+ IDBTransaction.onAbort tx (pure unit)
701+ IDBDatabase.onAbort db (pure unit)
702+ IDBTransaction.abort tx
703+ cursor <- attempt $ IDBIndex.openCursor index Nothing Next
704+ case cursor of
705+ Right _ -> fail "expected InvalidStateError"
706+ Left err -> message err `shouldEqual` "InvalidStateError"
707+ }
708+ case res of
709+ Right _ -> fail "expected InvalidStateError"
710+ _ -> pure unit
711+
712+ it "openCursor() - throw TransactionInactiveError on aborted transaction" do
713+ { db } <- setup
714+ { storeParams : { keyPath: ["key"], autoIncrement: false }
715+ , indexParams : IDBIndex.defaultParameters
716+ , keyPath : ["indexedProperty"]
717+ , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
718+ ]
719+ , onUpgradeNeeded : Nothing
720+ }
721+
722+ tx <- IDBDatabase.transaction db ["store"] ReadOnly
723+ store <- IDBTransaction.objectStore tx "store"
724+ index <- IDBObjectStore.index store "index"
725+ IDBTransaction.onAbort tx (pure unit)
726+ IDBTransaction.abort tx
727+ cursor <- attempt $ IDBIndex.openCursor index Nothing Next
728+ case cursor of
729+ Right _ -> fail "expected TransactionInactiveError"
730+ Left err -> message err `shouldEqual` "TransactionInactiveError"
731+
732+ tearDown db
733+
734+ it "openCursor() - throw InvalidStateError when the index is deleted" do
735+ { db } <- setup
736+ { storeParams : { keyPath: ["key"], autoIncrement: false }
737+ , indexParams : IDBIndex.defaultParameters
738+ , keyPath : ["indexedProperty"]
739+ , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
740+ ]
741+ , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
742+ store <- IDBTransaction.objectStore tx "store"
743+ IDBObjectStore.deleteIndex store "index"
744+ cursor <- attempt $ IDBIndex.openCursor index Nothing Next
745+ case cursor of
746+ Right _ -> fail "expected InvalidStateError"
747+ Left err -> message err `shouldEqual` "InvalidStateError"
748+ }
749+
750+ tearDown db
751+
752+ it "getKey() - multiEntry - adding keys" do
753+ { db } <- setup
754+ { storeParams : IDBObjectStore.defaultParameters
755+ , indexParams : { unique: false, multiEntry: true }
756+ , keyPath : ["name"]
757+ , values : [ { name: ["patate", "autruche"] } :+: (Just $ toKey 1)
758+ , { name: ["bob"] } :+: (Just $ toKey 2)
759+ ]
760+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
761+ key <- IDBIndex.getKey index (IDBKeyRange.only "patate")
762+ key `shouldEqual` (Just $ toKey 1)
763+
764+ key <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
765+ key `shouldEqual` (Just $ toKey 1)
766+
767+ key <- IDBIndex.getKey index (IDBKeyRange.only "bob")
768+ key `shouldEqual` (Just $ toKey 2)
769+ }
770+ tearDown db
771+
772+
773+ it "get() - returns the record with the first key in the range" do
774+ { db } <- setup
775+ { storeParams : { keyPath: ["key"], autoIncrement: false }
776+ , indexParams : IDBIndex.defaultParameters
777+ , keyPath : ["indexedProperty"]
778+ , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
779+ , { key: 42, indexedProperty: "autruche" } :+: Nothing
780+ , { key: 1337, indexedProperty: "baguette" } :+: Nothing
781+ ]
782+ , onUpgradeNeeded : Nothing
783+ }
784+
785+ tx <- IDBDatabase.transaction db ["store"] ReadOnly
786+ store <- IDBTransaction.objectStore tx "store"
787+ index <- IDBObjectStore.index store "index"
788+ val <- IDBIndex.get index (IDBKeyRange.lowerBound "autruche" false)
789+ ((\r -> r.key) <$> val) `shouldEqual` (Just 42)
790+
791+ tearDown db
yarn.lockdeleted−81
@@ -1,81 +0,0 @@
1−# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2−# yarn lockfile v1
3−
4−
5−base64-arraybuffer-es6@^0.7.0:
6− version "0.7.0"
7− resolved "https://registry.yarnpkg.com/base64-arraybuffer-es6/-/base64-arraybuffer-es6-0.7.0.tgz#dbe1e6c87b1bf1ca2875904461a7de40f21abc86"
8− integrity sha512-ESyU/U1CFZDJUdr+neHRhNozeCv72Y7Vm0m1DCbjX3KBjT6eYocvAJlSk6+8+HkVwXlT1FNxhGW6q3UKAlCvvw==
9−
10−domexception@^1.0.1:
11− version "1.0.1"
12− resolved "https://registry.yarnpkg.com/domexception/-/domexception-1.0.1.tgz#937442644ca6a31261ef36e3ec677fe805582c90"
13− integrity sha512-raigMkn7CJNNo6Ihro1fzG7wr3fHuYVytzquZKX5n0yizGsTcYgzdIUwj1X9pK0VvjeihV+XiclP+DjwbsSKug==
14− dependencies:
15− webidl-conversions "^4.0.2"
16−
17−fake-indexeddb@^4.0.1:
18− version "4.0.1"
19− resolved "https://registry.yarnpkg.com/fake-indexeddb/-/fake-indexeddb-4.0.1.tgz#09bb2468e21d0832b2177e894765fb109edac8fb"
20− integrity sha512-hFRyPmvEZILYgdcLBxVdHLik4Tj3gDTu/g7s9ZDOiU3sTNiGx+vEu1ri/AMsFJUZ/1sdRbAVrEcKndh3sViBcA==
21− dependencies:
22− realistic-structured-clone "^3.0.0"
23−
24−lodash@^4.7.0:
25− version "4.17.21"
26− resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
27− integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
28−
29−punycode@^2.1.1:
30− version "2.3.0"
31− resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.3.0.tgz#f67fa67c94da8f4d0cfff981aee4118064199b8f"
32− integrity sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA==
33−
34−realistic-structured-clone@^3.0.0:
35− version "3.0.0"
36− resolved "https://registry.yarnpkg.com/realistic-structured-clone/-/realistic-structured-clone-3.0.0.tgz#7b518049ce2dad41ac32b421cd297075b00e3e35"
37− integrity sha512-rOjh4nuWkAqf9PWu6JVpOWD4ndI+JHfgiZeMmujYcPi+fvILUu7g6l26TC1K5aBIp34nV+jE1cDO75EKOfHC5Q==
38− dependencies:
39− domexception "^1.0.1"
40− typeson "^6.1.0"
41− typeson-registry "^1.0.0-alpha.20"
42−
43−tr46@^2.1.0:
44− version "2.1.0"
45− resolved "https://registry.yarnpkg.com/tr46/-/tr46-2.1.0.tgz#fa87aa81ca5d5941da8cbf1f9b749dc969a4e240"
46− integrity sha512-15Ih7phfcdP5YxqiB+iDtLoaTz4Nd35+IiAv0kQ5FNKHzXgdWqPoTIqEDDJmXceQt4JZk6lVPT8lnDlPpGDppw==
47− dependencies:
48− punycode "^2.1.1"
49−
50−typeson-registry@^1.0.0-alpha.20:
51− version "1.0.0-alpha.39"
52− resolved "https://registry.yarnpkg.com/typeson-registry/-/typeson-registry-1.0.0-alpha.39.tgz#9e0f5aabd5eebfcffd65a796487541196f4b1211"
53− integrity sha512-NeGDEquhw+yfwNhguLPcZ9Oj0fzbADiX4R0WxvoY8nGhy98IbzQy1sezjoEFWOywOboj/DWehI+/aUlRVrJnnw==
54− dependencies:
55− base64-arraybuffer-es6 "^0.7.0"
56− typeson "^6.0.0"
57− whatwg-url "^8.4.0"
58−
59−typeson@^6.0.0, typeson@^6.1.0:
60− version "6.1.0"
61− resolved "https://registry.yarnpkg.com/typeson/-/typeson-6.1.0.tgz#5b2a53705a5f58ff4d6f82f965917cabd0d7448b"
62− integrity sha512-6FTtyGr8ldU0pfbvW/eOZrEtEkczHRUtduBnA90Jh9kMPCiFNnXIon3vF41N0S4tV1HHQt4Hk1j4srpESziCaA==
63−
64−webidl-conversions@^4.0.2:
65− version "4.0.2"
66− resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad"
67− integrity sha512-YQ+BmxuTgd6UXZW3+ICGfyqRyHXVlD5GtQr5+qjiNW7bF0cqrzX500HVXPBOvgXb5YnzDd+h0zqyv61KUD7+Sg==
68−
69−webidl-conversions@^6.1.0:
70− version "6.1.0"
71− resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-6.1.0.tgz#9111b4d7ea80acd40f5270d666621afa78b69514"
72− integrity sha512-qBIvFLGiBpLjfwmYAaHPXsn+ho5xZnGvyGvsarywGNc8VyQJUMHJ8OBKGGrPER0okBeMDaan4mNBlgBROxuI8w==
73−
74−whatwg-url@^8.4.0:
75− version "8.7.0"
76− resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-8.7.0.tgz#656a78e510ff8f3937bc0bcbe9f5c0ac35941b77"
77− integrity sha512-gAojqb/m9Q8a5IV96E3fHJM70AzCkgt4uXYX2O7EmuyOnLrViCQlsEBmF9UQIu3/aeAIp2U17rtbpZWNntQqdg==
78− dependencies:
79− lodash "^4.7.0"
80− tr46 "^2.1.0"
81− webidl-conversions "^6.1.0"