git.iko.soy/purescript-indexed-DB
on master help sign in
Migrated to PureScript 15
d307cd4iko
.gitignore+2
@@ -77,3 +77,5 @@ releases
77.purs*
77.purs*
78.psa*
78.psa*
79dist
79dist
80+.spago
81+test.js
bower.jsondeleted−44
@@ -1,44 +0,0 @@
1−{
2− "name": "purescript-indexeddb",
3− "description": "An API wrapper around IndexedDB",
4− "version": "4.0.0",
5− "authors": [
6− "Matthias Benkort <matthias.benkort@gmail.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/truqu/purescript-indexedDB.git"
26− },
27− "dependencies": {
28− "purescript-aff": "^4.0.2",
29− "purescript-datetime": "^3.2.0",
30− "purescript-eff": "^3.1.0",
31− "purescript-exceptions": "^3.0.0",
32− "purescript-foreign": "^4.0.1",
33− "purescript-maybe": "^3.0.0",
34− "purescript-nullable": "^3.0.0",
35− "purescript-prelude": "^3.1.0",
36− "purescript-read": "^1.0.0"
37− },
38− "devDependencies": {
39− "purescript-now": "^3.0.0",
40− "purescript-psci-support": "^3.0.0",
41− "purescript-spec": "^2.0.0",
42− "purescript-spec-mocha": "^2.0.0"
43− }
44−}
deploy.sh → dev/deploy.shrenamed

The content is the same.

dev/test.shnew file+8
@@ -0,0 +1,8 @@
1+#!/bin/sh
2+
3+set -e
4+
5+spago bundle-app -m Test.Main --to test.js
6+
7+xdg-open test.html
8+
karma.browserstack.conf.jsdeleted−91
@@ -1,91 +0,0 @@
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.jsdeleted−25
@@ -1,25 +0,0 @@
1−module.exports = config => {
2− config.set({
3− autoWatch: true,
4− singleRun: true,
5− browsers: ["Chrome", "Firefox"],
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.jsondeleted−42
@@ -1,42 +0,0 @@
1−{
2− "name": "purescript-indexeddb",
3− "version": "3.0.0",
4− "description": "A wrapper around the IndexedDB API",
5− "scripts": {
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"
31− },
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"
40− },
41− "homepage": "https://github.com/Truqu/purescript-indexeddb#readme"
42−}
packages.dhallnew file+120
@@ -0,0 +1,120 @@
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+
106+ with spec-mocha =
107+ { repo =
108+ "https://github.com/ilyakooo0/purescript-spec-mocha"
109+ , version = "v4.0.2", dependencies =
110+ [ "aff"
111+ , "datetime"
112+ , "effect"
113+ , "either"
114+ , "foldable-traversable"
115+ , "maybe"
116+ , "prelude"
117+ , "spec"
118+ ]
119+ }
120+in upstream
spago.dhallnew file+43
@@ -0,0 +1,43 @@
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 = "indexeddb"
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+ , "spec-mocha"
38+ , "transformers"
39+ , "tuples"
40+ ]
41+, packages = ./packages.dhall
42+, sources = [ "src/**/*.purs", "test/**/*.purs" ]
43+}
src/Database/IndexedDB/Core.js+6−6
@@ -3,7 +3,7 @@ const toArray = function toArray(xs) {
3};
3};
4
4
5
5
6−exports._showCursor = function _showCursor(cursor) {
6+export function _showCursor(cursor) {
7 return '(IDBCursor ' +
7 return '(IDBCursor ' +
8 '{ direction: ' + cursor.direction +
8 '{ direction: ' + cursor.direction +
9 ', key: ' + cursor.key +
9 ', key: ' + cursor.key +
@@ -11,7 +11,7 @@ exports._showCursor = function _showCursor(cursor) {
11 ' })';
11 ' })';
12};
12};
13
13
14−exports._showDatabase = function _showDatabase(db) {
14+export function _showDatabase(db) {
15 return '(IDBDatabase ' +
15 return '(IDBDatabase ' +
16 '{ name: ' + db.name +
16 '{ name: ' + db.name +
17 ', objectStoreNames: [' + toArray(db.objectStoreNames).join(', ') + ']' +
17 ', objectStoreNames: [' + toArray(db.objectStoreNames).join(', ') + ']' +
@@ -19,7 +19,7 @@ exports._showDatabase = function _showDatabase(db) {
19 ' })';
19 ' })';
20};
20};
21
21
22−exports._showIndex = function _showIndex(index) {
22+export function _showIndex(index) {
23 return '(IDBIndex ' +
23 return '(IDBIndex ' +
24 '{ name: ' + index.name +
24 '{ name: ' + index.name +
25 ', keyPath: ' + index.keyPath +
25 ', keyPath: ' + index.keyPath +
@@ -28,7 +28,7 @@ exports._showIndex = function _showIndex(index) {
28 ' })';
28 ' })';
29};
29};
30
30
31−exports._showKeyRange = function _showKeyRange(range) {
31+export function _showKeyRange(range) {
32 return '(IDBKeyRange ' +
32 return '(IDBKeyRange ' +
33 '{ lower: ' + range.lower +
33 '{ lower: ' + range.lower +
34 ', upper: ' + range.upper +
34 ', upper: ' + range.upper +
@@ -37,7 +37,7 @@ exports._showKeyRange = function _showKeyRange(range) {
37 ' })';
37 ' })';
38};
38};
39
39
40−exports._showObjectStore = function _showObjectStore(store) {
40+export function _showObjectStore(store) {
41 return '(IDBObjectStore ' +
41 return '(IDBObjectStore ' +
42 '{ autoIncrement: ' + store.autoIncrement +
42 '{ autoIncrement: ' + store.autoIncrement +
43 ', indexNames: [' + toArray(store.indexNames).join(', ') + ']' +
43 ', indexNames: [' + toArray(store.indexNames).join(', ') + ']' +
@@ -46,7 +46,7 @@ exports._showObjectStore = function _showObjectStore(store) {
46 ' })';
46 ' })';
47};
47};
48
48
49−exports._showTransaction = function _showTransaction(tx) {
49+export function _showTransaction(tx) {
50 return '(IDBTransaction ' +
50 return '(IDBTransaction ' +
51 '{ error: ' + tx.error +
51 '{ error: ' + tx.error +
52 ', mode: ' + tx.mode +
52 ', mode: ' + tx.mode +
src/Database/IndexedDB/Core.purs+21−74
@@ -2,10 +2,11 @@
2-- | those types.
2-- | those types.
3module Database.IndexedDB.Core
3module Database.IndexedDB.Core
4 -- * Effects
4 -- * Effects
5+ (
5− ( IDB
6+ -- IDB
6
7
7− -- * Types
8+ -- * Types
8− , Database
9+ Database
9 , Index
10 , Index
10 , KeyCursor
11 , KeyCursor
11 , KeyRange
12 , KeyRange
@@ -30,23 +31,13 @@ module Database.IndexedDB.Core
30 , module Database.IndexedDB.IDBKey
31 , module Database.IndexedDB.IDBKey
31 ) where
32 ) where
32
33
33−import Prelude (class Show)
34+import Prelude (class Show)
34
35
35−import Control.Monad.Eff (kind Effect)
36−import Data.Maybe (Maybe(..))
36+import Data.Maybe (Maybe(..))
37−import Data.String.Read (class Read)
37+import Data.String.Read (class Read)
38
38
39import Database.IndexedDB.IDBKey
39import Database.IndexedDB.IDBKey
40
40
41−
42−--------------------
43−-- Effects
44−--
45−
46−-- | IDB Effects, manifestation that something happened with the IndexedDB
47−foreign import data IDB :: Effect
48−
49−
50--------------------
41--------------------
51-- TYPES
42-- TYPES
52--
43--
@@ -55,35 +46,28 @@ foreign import data IDB :: Effect
55-- | stores which hold the data stored in the database.
46-- | stores which hold the data stored in the database.
56foreign import data Database :: Type
47foreign import data Database :: Type
57
48
58−
59-- | An index allows looking up records in an object store using properties of the values
49-- | An index allows looking up records in an object store using properties of the values
60-- | in the object stores records.
50-- | in the object stores records.
61foreign import data Index :: Type
51foreign import data Index :: Type
62
52
63−
64-- | A key range is a continuous interval over some data type used for keys.
53-- | A key range is a continuous interval over some data type used for keys.
65foreign import data KeyRange :: Type
54foreign import data KeyRange :: Type
66
55
67−
68-- | A cursor is used to iterate over a range of records in an index or an object store
56-- | A cursor is used to iterate over a range of records in an index or an object store
69-- | in a specific direction. A KeyCursor doesn't hold any value.
57-- | in a specific direction. A KeyCursor doesn't hold any value.
70foreign import data KeyCursor :: Type
58foreign import data KeyCursor :: Type
71
59
72−
73-- | A Transaction is used to interact with the data in a database.
60-- | A Transaction is used to interact with the data in a database.
74-- | Whenever data is read or written to the database it is done by using a transaction.
61-- | Whenever data is read or written to the database it is done by using a transaction.
75foreign import data Transaction :: Type
62foreign import data Transaction :: Type
76
63
77−
78-- | An object store is the primary storage mechanism for storing data in a database.
64-- | An object store is the primary storage mechanism for storing data in a database.
79foreign import data ObjectStore :: Type
65foreign import data ObjectStore :: Type
80
66
81−
82-- | A cursor is used to iterate over a range of records in an index or an object store
67-- | A cursor is used to iterate over a range of records in an index or an object store
83-- | in a specific direction. A ValueCursor also holds the value corresponding to matching key.
68-- | in a specific direction. A ValueCursor also holds the value corresponding to matching key.
84foreign import data ValueCursor :: Type
69foreign import data ValueCursor :: Type
85
70
86−
87-- | A cursor has a direction that determines whether it moves in monotonically
71-- | A cursor has a direction that determines whether it moves in monotonically
88-- | increasing or decreasing order of the record keys when iterated, and if it
72-- | increasing or decreasing order of the record keys when iterated, and if it
89-- | skips duplicated values when iterating indexes.
73-- | skips duplicated values when iterating indexes.
@@ -95,7 +79,6 @@ data CursorDirection
95 | Prev
79 | Prev
96 | PrevUnique
80 | PrevUnique
97
81
98−
99-- | If the source of a cursor is an object store, the effective object store of
82-- | If the source of a cursor is an object store, the effective object store of
100-- | the cursor is that object store and the effective key of the cursor is the
83-- | the cursor is that object store and the effective key of the cursor is the
101-- | cursor’s position. If the source of a cursor is an index, the effective object
84-- | cursor’s position. If the source of a cursor is an index, the effective object
@@ -105,7 +88,6 @@ data CursorSource
105 = ObjectStore ObjectStore
88 = ObjectStore ObjectStore
106 | Index Index
89 | Index Index
107
90
108−
109-- | A key path is a list of strings that defines how to extract a key from a value.
91-- | A key path is a list of strings that defines how to extract a key from a value.
110-- | A valid key path is one of:
92-- | A valid key path is one of:
111-- |
93-- |
@@ -115,15 +97,13 @@ data CursorSource
115-- | - A non-empty list containing only strings conforming to the above requirements.
97-- | - A non-empty list containing only strings conforming to the above requirements.
116type KeyPath = Array String
98type KeyPath = Array String
117
99
118−
119-- | A transaction has a mode that determines which types of interactions can be performed
100-- | A transaction has a mode that determines which types of interactions can be performed
120-- | upon that transaction. The mode is set when the transaction is created and remains
101-- | upon that transaction. The mode is set when the transaction is created and remains
121-- | fixed for the life of the transaction.
102-- | fixed for the life of the transaction.
122data TransactionMode
103data TransactionMode
123− = ReadOnly -- ^ The transaction is only allowed to read data.
104+ = ReadOnly -- ^ The transaction is only allowed to read data.
124− | ReadWrite -- ^ The transaction is allowed to read, modify and delete data from existing object stores
105+ | ReadWrite -- ^ The transaction is allowed to read, modify and delete data from existing object stores
125− | 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.
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.
126−
127
107
128--------------------
108--------------------
129-- Classes
109-- Classes
@@ -133,132 +113,104 @@ data TransactionMode
133-- | specific attributes (see KeyCursor or CursorWithValue).
113-- | specific attributes (see KeyCursor or CursorWithValue).
134class (IDBCursor cursor) <= IDBConcreteCursor cursor
114class (IDBCursor cursor) <= IDBConcreteCursor cursor
135
115
136−
137-- | Cursor objects implement the IDBCursor interface.
116-- | Cursor objects implement the IDBCursor interface.
138-- | There is only ever one IDBCursor instance representing a given cursor.
117-- | There is only ever one IDBCursor instance representing a given cursor.
139-- | There is no limit on how many cursors can be used at the same time.
118-- | There is no limit on how many cursors can be used at the same time.
140class IDBCursor cursor
119class IDBCursor cursor
141
120
142−
143-- | The IDBDatabase interface represents a connection to a database.
121-- | The IDBDatabase interface represents a connection to a database.
144class IDBDatabase db
122class IDBDatabase db
145
123
146−
147-- | The IDBIndex interface represents an index handle.
124-- | The IDBIndex interface represents an index handle.
148-- | Any of these methods throw an "TransactionInactiveError" DOMException
125-- | Any of these methods throw an "TransactionInactiveError" DOMException
149-- | if called when the transaction is not active.
126-- | if called when the transaction is not active.
150class IDBIndex index
127class IDBIndex index
151
128
152−
153-- | The IDBKeyRange interface represents a key range.
129-- | The IDBKeyRange interface represents a key range.
154class IDBKeyRange range
130class IDBKeyRange range
155
131
156−
157-- | The IDBtransaction interface.
132-- | The IDBtransaction interface.
158class IDBTransaction tx
133class IDBTransaction tx
159
134
160−
161-- | The IDBObjectStore interface represents an object store handle.
135-- | The IDBObjectStore interface represents an object store handle.
162class IDBObjectStore store
136class IDBObjectStore store
163
137
164−
165--------------------
138--------------------
166-- INSTANCES
139-- INSTANCES
167--
140--
168
141
169instance idbCursorKeyCursor :: IDBCursor KeyCursor
142instance idbCursorKeyCursor :: IDBCursor KeyCursor
170
143
171−
172instance idbCursorValueCursor :: IDBCursor ValueCursor
144instance idbCursorValueCursor :: IDBCursor ValueCursor
173
145
174−
175instance idbConcreteCursorKeyCursor :: IDBConcreteCursor KeyCursor
146instance idbConcreteCursorKeyCursor :: IDBConcreteCursor KeyCursor
176
147
177−
178instance idbConcreteCursorValueCursor :: IDBConcreteCursor ValueCursor
148instance idbConcreteCursorValueCursor :: IDBConcreteCursor ValueCursor
179
149
180−
181instance idbDatabaseDatabase :: IDBDatabase Database
150instance idbDatabaseDatabase :: IDBDatabase Database
182
151
183−
184instance idbIndexIndex :: IDBIndex Index
152instance idbIndexIndex :: IDBIndex Index
185
153
186−
187instance idbIndexObjectStore :: IDBIndex ObjectStore
154instance idbIndexObjectStore :: IDBIndex ObjectStore
188
155
189−
190instance idbKeyRangeKeyRange :: IDBKeyRange KeyRange
156instance idbKeyRangeKeyRange :: IDBKeyRange KeyRange
191
157
192−
193instance idbObjectStoreObjectStore :: IDBObjectStore ObjectStore
158instance idbObjectStoreObjectStore :: IDBObjectStore ObjectStore
194
159
195−
196instance idbTransactionTransaction :: IDBTransaction Transaction
160instance idbTransactionTransaction :: IDBTransaction Transaction
197
161
198−
199instance showKeyCursor :: Show KeyCursor where
162instance showKeyCursor :: Show KeyCursor where
200 show = _showCursor
163 show = _showCursor
201
164
202−
203instance showValueCursor :: Show ValueCursor where
165instance showValueCursor :: Show ValueCursor where
204 show = _showCursor
166 show = _showCursor
205
167
206−
207instance showIndex :: Show Index where
168instance showIndex :: Show Index where
208 show = _showIndex
169 show = _showIndex
209
170
210−
211instance showKeyRange :: Show KeyRange where
171instance showKeyRange :: Show KeyRange where
212 show = _showKeyRange
172 show = _showKeyRange
213
173
214−
215instance showObjectStore :: Show ObjectStore where
174instance showObjectStore :: Show ObjectStore where
216 show = _showObjectStore
175 show = _showObjectStore
217
176
218−
219instance showTransaction :: Show Transaction where
177instance showTransaction :: Show Transaction where
220 show = _showTransaction
178 show = _showTransaction
221
179
222−
223instance showDatabase :: Show Database where
180instance showDatabase :: Show Database where
224 show = _showDatabase
181 show = _showDatabase
225
182
226−
227instance showCursorDirection :: Show CursorDirection where
183instance showCursorDirection :: Show CursorDirection where
228 show x =
184 show x =
229 case x of
185 case x of
230− Next -> "next"
186+ Next -> "next"
231 NextUnique -> "nextunique"
187 NextUnique -> "nextunique"
232− Prev -> "prev"
188+ Prev -> "prev"
233 PrevUnique -> "prevunique"
189 PrevUnique -> "prevunique"
234
190
235−
236instance showTransactionMode :: Show TransactionMode where
191instance showTransactionMode :: Show TransactionMode where
237 show x =
192 show x =
238 case x of
193 case x of
239− ReadOnly -> "readonly"
194+ ReadOnly -> "readonly"
240− ReadWrite -> "readwrite"
195+ ReadWrite -> "readwrite"
241 VersionChange -> "versionchange"
196 VersionChange -> "versionchange"
242
197
243−
244instance readCursorDirection :: Read CursorDirection where
198instance readCursorDirection :: Read CursorDirection where
245 read s =
199 read s =
246 case s of
200 case s of
247− "next" -> Just Next
201+ "next" -> Just Next
248 "nextunique" -> Just NextUnique
202 "nextunique" -> Just NextUnique
249− "prev" -> Just Prev
203+ "prev" -> Just Prev
250 "prevunique" -> Just PrevUnique
204 "prevunique" -> Just PrevUnique
251− _ -> Nothing
205+ _ -> Nothing
252−
253
206
254instance readTransactionMode :: Read TransactionMode where
207instance readTransactionMode :: Read TransactionMode where
255 read s =
208 read s =
256 case s of
209 case s of
257− "readonly" -> Just ReadOnly
210+ "readonly" -> Just ReadOnly
258− "readwrite" -> Just ReadWrite
211+ "readwrite" -> Just ReadWrite
259 "versionchange" -> Just VersionChange
212 "versionchange" -> Just VersionChange
260− _ -> Nothing
213+ _ -> Nothing
261−
262
214
263--------------------
215--------------------
264-- FFI
216-- FFI
@@ -266,30 +218,25 @@ instance readTransactionMode :: Read TransactionMode where
266
218
267foreign import _showCursor
219foreign import _showCursor
268 :: forall cursor
220 :: forall cursor
269− . cursor
221+ . cursor
270 -> String
222 -> String
271
223
272−
273foreign import _showDatabase
224foreign import _showDatabase
274 :: Database
225 :: Database
275 -> String
226 -> String
276
227
277−
278foreign import _showIndex
228foreign import _showIndex
279 :: Index
229 :: Index
280 -> String
230 -> String
281
231
282−
283foreign import _showKeyRange
232foreign import _showKeyRange
284 :: KeyRange
233 :: KeyRange
285 -> String
234 -> String
286
235
287−
288foreign import _showObjectStore
236foreign import _showObjectStore
289 :: ObjectStore
237 :: ObjectStore
290 -> String
238 -> String
291
239
292−
293foreign import _showTransaction
240foreign import _showTransaction
294 :: Transaction
241 :: Transaction
295 -> String
242 -> String
src/Database/IndexedDB/IDBCursor.js+10−10
@@ -10,7 +10,7 @@ const successHandler = function successHandler(cb) {
10 };
10 };
11};
11};
12
12
13−exports._advance = function _advance(cursor, count) {
13+export function _advance(cursor, count) {
14 return function aff(error, success) {
14 return function aff(error, success) {
15 try {
15 try {
16 cursor.advance(count);
16 cursor.advance(count);
@@ -25,7 +25,7 @@ exports._advance = function _advance(cursor, count) {
25 };
25 };
26};
26};
27
27
28−exports._continue = function _continue(cursor, key) {
28+export function _continue(cursor, key) {
29 return function aff(error, success) {
29 return function aff(error, success) {
30 try {
30 try {
31 cursor.continue(key || undefined);
31 cursor.continue(key || undefined);
@@ -40,7 +40,7 @@ exports._continue = function _continue(cursor, key) {
40 };
40 };
41};
41};
42
42
43−exports._continuePrimaryKey = function _continuePrimaryKey(cursor, key, primaryKey) {
43+export function _continuePrimaryKey(cursor, key, primaryKey) {
44 return function aff(error, success) {
44 return function aff(error, success) {
45 try {
45 try {
46 cursor.continuePrimaryKey(key, primaryKey);
46 cursor.continuePrimaryKey(key, primaryKey);
@@ -55,7 +55,7 @@ exports._continuePrimaryKey = function _continuePrimaryKey(cursor, key, primaryK
55 };
55 };
56};
56};
57
57
58−exports._delete = function _delete(cursor) {
58+export function _delete(cursor) {
59 return function aff(error, success) {
59 return function aff(error, success) {
60 try {
60 try {
61 const request = cursor.delete();
61 const request = cursor.delete();
@@ -71,11 +71,11 @@ exports._delete = function _delete(cursor) {
71 };
71 };
72};
72};
73
73
74−exports._direction = function _direction(fromString, cursor) {
74+export function _direction(fromString, cursor) {
75 return fromString(cursor.direction);
75 return fromString(cursor.direction);
76};
76};
77
77
78−exports._key = function _key(cursor) {
78+export function _key(cursor) {
79 return function aff(error, success) {
79 return function aff(error, success) {
80 try {
80 try {
81 success(cursor.key);
81 success(cursor.key);
@@ -89,7 +89,7 @@ exports._key = function _key(cursor) {
89 };
89 };
90};
90};
91
91
92−exports._primaryKey = function _primaryKey(cursor) {
92+export function _primaryKey(cursor) {
93 return function aff(error, success) {
93 return function aff(error, success) {
94 try {
94 try {
95 success(cursor.primaryKey);
95 success(cursor.primaryKey);
@@ -103,7 +103,7 @@ exports._primaryKey = function _primaryKey(cursor) {
103 };
103 };
104};
104};
105
105
106−exports._source = function _source(IDBObjectStore, IDBIndex, cursor) {
106+export function _source(IDBObjectStore, IDBIndex, cursor) {
107 switch (cursor.source.constructor.name) {
107 switch (cursor.source.constructor.name) {
108 case 'IDBIndex':
108 case 'IDBIndex':
109 return IDBIndex(cursor.source);
109 return IDBIndex(cursor.source);
@@ -123,7 +123,7 @@ exports._source = function _source(IDBObjectStore, IDBIndex, cursor) {
123 }
123 }
124};
124};
125
125
126−exports._update = function _update(cursor, value) {
126+export function _update(cursor, value) {
127 return function aff(error, success) {
127 return function aff(error, success) {
128 try {
128 try {
129 const request = cursor.update(value);
129 const request = cursor.update(value);
@@ -139,6 +139,6 @@ exports._update = function _update(cursor, value) {
139 };
139 };
140};
140};
141
141
142−exports._value = function _value(cursor) {
142+export function _value(cursor) {
143 return cursor.value;
143 return cursor.value;
144};
144};
src/Database/IndexedDB/IDBCursor.purs+75−85
@@ -16,20 +16,18 @@ module Database.IndexedDB.IDBCursor
16 , value
16 , value
17 ) where
17 ) where
18
18
19−import Prelude (Unit, ($), (>>>), (<<<), map)
20−
21−import Control.Monad.Aff (Aff)
22−import Control.Monad.Aff.Compat (fromEffFnAff, EffFnAff)
23−import Data.Foreign (Foreign, toForeign, unsafeFromForeign)
24−import Data.Function.Uncurried as Fn
25−import Data.Function.Uncurried (Fn2, Fn3)
26−import Data.Maybe (Maybe)
27−import Data.Nullable (Nullable, toNullable)
28−import Data.String.Read (read)
29−
30import Database.IndexedDB.Core
19import Database.IndexedDB.Core
31−import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, toKey, unsafeFromKey)
32
20
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)
33
31
34--------------------
32--------------------
35-- INTERFACES
33-- INTERFACES
@@ -37,56 +35,58 @@ import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, toKey, unsafeFromK
37
35
38-- | Advances the cursor through the next count records in range.
36-- | Advances the cursor through the next count records in range.
39advance
37advance
38+ :: forall cursor
40− :: forall e cursor. (IDBCursor cursor)
39+ . (IDBCursor cursor)
41− => cursor
40+ => cursor
42− -> Int
41+ -> Int
43− -> Aff (idb :: IDB | e) Unit
42+ -> Aff Unit
44advance c =
43advance c =
45− fromEffFnAff <<< Fn.runFn2 _advance c
44+ fromEffectFnAff <<< Fn.runFn2 _advance c
46−
47
45
48-- | Advances the cursor to the next record in range matching or after key.
46-- | Advances the cursor to the next record in range matching or after key.
49continue
47continue
48+ :: forall k cursor
49+ . (IDBKey k)
50− :: forall e k cursor. (IDBKey k) => (IDBCursor cursor)
50+ => (IDBCursor cursor)
51− => cursor
51+ => cursor
52− -> Maybe k
52+ -> Maybe k
53− -> Aff (idb :: IDB | e) Unit
53+ -> Aff Unit
54continue c mk =
54continue c mk =
55− fromEffFnAff $ Fn.runFn2 _continue c (toNullable $ map (toKey >>> unsafeFromKey) mk)
55+ fromEffectFnAff $ Fn.runFn2 _continue c (toNullable $ map (toKey >>> unsafeFromKey) mk)
56−
57
56
58-- | 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.
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.
59continuePrimaryKey
58continuePrimaryKey
59+ :: forall k cursor
60+ . (IDBKey k)
60− :: forall e k cursor. (IDBKey k) => (IDBCursor cursor)
61+ => (IDBCursor cursor)
61− => cursor
62+ => cursor
62− -> k
63+ -> k
63− -> k
64+ -> k
64− -> Aff (idb :: IDB | e) Unit
65+ -> Aff Unit
65continuePrimaryKey c k1 k2 =
66continuePrimaryKey c k1 k2 =
66− fromEffFnAff $ Fn.runFn3 _continuePrimaryKey c (unsafeFromKey $ toKey k1) (unsafeFromKey $ toKey k2)
67+ fromEffectFnAff $ Fn.runFn3 _continuePrimaryKey c (unsafeFromKey $ toKey k1) (unsafeFromKey $ toKey k2)
67−
68
68
69-- | Delete the record pointed at by the cursor with a new value.
69-- | Delete the record pointed at by the cursor with a new value.
70delete
70delete
71+ :: forall cursor
71− :: forall e cursor. (IDBCursor cursor)
72+ . (IDBCursor cursor)
72− => cursor
73+ => cursor
73− -> Aff (idb :: IDB | e) Unit
74+ -> Aff Unit
74delete =
75delete =
75− fromEffFnAff <<< _delete
76+ fromEffectFnAff <<< _delete
76−
77
77
78-- | Update the record pointed at by the cursor with a new value.
78-- | Update the record pointed at by the cursor with a new value.
79-- |
79-- |
80-- | Throws a "DataError" DOMException if the effective object store uses
80-- | Throws a "DataError" DOMException if the effective object store uses
81-- | in-line keys and the key would have changed.
81-- | in-line keys and the key would have changed.
82update
82update
83− :: forall val e cursor. (IDBCursor cursor)
83+ :: forall val cursor
84+ . (IDBCursor cursor)
84− => cursor
85+ => cursor
85− -> val
86+ -> val
86− -> Aff (idb :: IDB | e) Key
87+ -> Aff Key
87update c =
88update c =
88− map toKey <<< fromEffFnAff <<< Fn.runFn2 _update c <<< toForeign
89+ map toKey <<< fromEffectFnAff <<< Fn.runFn2 _update c <<< unsafeToForeign
89−
90
90
91--------------------
91--------------------
92-- ATTRIBUTES
92-- ATTRIBUTES
@@ -94,103 +94,93 @@ update c =
94
94
95-- | Returns the direction (Next|NextUnique|Prev|PrevUnique) of the cursor.
95-- | Returns the direction (Next|NextUnique|Prev|PrevUnique) of the cursor.
96direction
96direction
97+ :: forall cursor
97− :: forall cursor. (IDBConcreteCursor cursor)
98+ . (IDBConcreteCursor cursor)
98 => cursor
99 => cursor
99 -> CursorDirection
100 -> CursorDirection
100direction =
101direction =
101 Fn.runFn2 _direction (read >>> toNullable)
102 Fn.runFn2 _direction (read >>> toNullable)
102
103
103−
104-- | Returns the key of the cursor. Throws a "InvalidStateError" DOMException
104-- | Returns the key of the cursor. Throws a "InvalidStateError" DOMException
105-- | if the cursor is advancing or is finished.
105-- | if the cursor is advancing or is finished.
106key
106key
107+ :: forall cursor
107− :: forall e cursor. (IDBConcreteCursor cursor)
108+ . (IDBConcreteCursor cursor)
108 => cursor
109 => cursor
109− -> Aff (idb :: IDB | e) Key
110+ -> Aff Key
110key =
111key =
111− map toKey <<< fromEffFnAff <<< _key
112+ map toKey <<< fromEffectFnAff <<< _key
112−
113
113
114-- | Returns the effective key of the cursor. Throws a "InvalidStateError" DOMException
114-- | Returns the effective key of the cursor. Throws a "InvalidStateError" DOMException
115-- | if the cursor is advancing or is finished.
115-- | if the cursor is advancing or is finished.
116primaryKey
116primaryKey
117+ :: forall cursor
117− :: forall e cursor. (IDBConcreteCursor cursor)
118+ . (IDBConcreteCursor cursor)
118 => cursor
119 => cursor
119− -> Aff (idb :: IDB | e) Key
120+ -> Aff Key
120primaryKey =
121primaryKey =
121− map toKey <<< fromEffFnAff <<< _primaryKey
122+ map toKey <<< fromEffectFnAff <<< _primaryKey
122−
123
123
124-- | Returns the IDBObjectStore or IDBIndex the cursor was opened from.
124-- | Returns the IDBObjectStore or IDBIndex the cursor was opened from.
125source
125source
126+ :: forall cursor
126− :: forall cursor. (IDBConcreteCursor cursor)
127+ . (IDBConcreteCursor cursor)
127 => cursor
128 => cursor
128 -> CursorSource
129 -> CursorSource
129source =
130source =
130 Fn.runFn3 _source ObjectStore Index
131 Fn.runFn3 _source ObjectStore Index
131
132
132−
133value
133value
134 :: forall val
134 :: forall val
135− . ValueCursor
135+ . ValueCursor
136 -> val
136 -> val
137value =
137value =
138 _value >>> unsafeFromForeign
138 _value >>> unsafeFromForeign
139
139
140−
141--------------------
140--------------------
142-- FFI
141-- FFI
143--
142--
144
143
145foreign import _advance
144foreign import _advance
146− :: forall cursor e
145+ :: forall cursor
147− . Fn2 cursor Int (EffFnAff (idb :: IDB | e) Unit)
146+ . Fn2 cursor Int (EffectFnAff Unit)
148−
149
147
150foreign import _continue
148foreign import _continue
151− :: forall cursor e
149+ :: forall cursor
152− . Fn2 cursor (Nullable Foreign) (EffFnAff (idb :: IDB | e) Unit)
150+ . Fn2 cursor (Nullable Foreign) (EffectFnAff Unit)
153−
154
151
155foreign import _continuePrimaryKey
152foreign import _continuePrimaryKey
156− :: forall cursor e
153+ :: forall cursor
157− . Fn3 cursor Foreign Foreign (EffFnAff (idb :: IDB | e) Unit)
154+ . Fn3 cursor Foreign Foreign (EffectFnAff Unit)
158−
159
155
160foreign import _delete
156foreign import _delete
161− :: forall cursor e
157+ :: forall cursor
162− . cursor
158+ . cursor
163− -> (EffFnAff (idb :: IDB | e) Unit)
159+ -> (EffectFnAff Unit)
164−
165
160
166foreign import _direction
161foreign import _direction
167 :: forall cursor
162 :: forall cursor
168− . Fn2 (String -> Nullable CursorDirection) cursor CursorDirection
163+ . Fn2 (String -> Nullable CursorDirection) cursor CursorDirection
169−
170
164
171foreign import _key
165foreign import _key
172− :: forall cursor e
166+ :: forall cursor
173− . cursor
167+ . cursor
174− -> EffFnAff (idb :: IDB | e) Key
168+ -> EffectFnAff Key
175−
176
169
177foreign import _primaryKey
170foreign import _primaryKey
178− :: forall cursor e
171+ :: forall cursor
179− . cursor
172+ . cursor
180− -> EffFnAff (idb :: IDB | e) Key
173+ -> EffectFnAff Key
181−
182
174
183foreign import _source
175foreign import _source
184 :: forall cursor
176 :: forall cursor
185− . Fn3 (ObjectStore -> CursorSource) (Index -> CursorSource) cursor CursorSource
177+ . Fn3 (ObjectStore -> CursorSource) (Index -> CursorSource) cursor CursorSource
186−
187
178
188foreign import _update
179foreign import _update
189− :: forall cursor e
180+ :: forall cursor
190− . Fn2 cursor Foreign (EffFnAff (idb :: IDB | e) Foreign)
181+ . Fn2 cursor Foreign (EffectFnAff Foreign)
191−
192
182
193foreign import _value
183foreign import _value
194 :: forall cursor val
184 :: forall cursor val
195− . cursor
185+ . cursor
196 -> val
186 -> val
src/Database/IndexedDB/IDBDatabase.js+11−11
@@ -3,7 +3,7 @@ const toArray = function toArray(xs) {
3};
3};
4
4
5
5
6−exports._close = function _close(db) {
6+export function _close(db) {
7 return function aff(error, success) {
7 return function aff(error, success) {
8 try {
8 try {
9 db.close();
9 db.close();
@@ -18,7 +18,7 @@ exports._close = function _close(db) {
18 };
18 };
19};
19};
20
20
21−exports._createObjectStore = function _createObjectStore(db, name, opts) {
21+export function _createObjectStore(db, name, opts) {
22 return function aff(error, success) {
22 return function aff(error, success) {
23 var keyPath;
23 var keyPath;
24
24
@@ -52,7 +52,7 @@ exports._createObjectStore = function _createObjectStore(db, name, opts) {
52 };
52 };
53};
53};
54
54
55−exports._deleteObjectStore = function _deleteObjectStore(db, name) {
55+export function _deleteObjectStore(db, name) {
56 return function aff(error, success) {
56 return function aff(error, success) {
57 try {
57 try {
58 db.deleteObjectStore(name);
58 db.deleteObjectStore(name);
@@ -67,15 +67,15 @@ exports._deleteObjectStore = function _deleteObjectStore(db, name) {
67 };
67 };
68};
68};
69
69
70−exports._name = function _name(db) {
70+export function _name(db) {
71 return db.name;
71 return db.name;
72};
72};
73
73
74−exports._objectStoreNames = function _objectStoreNames(db) {
74+export function _objectStoreNames(db) {
75 return toArray(db.objectStoreNames);
75 return toArray(db.objectStoreNames);
76};
76};
77
77
78−exports._onAbort = function _onAbort(db, f) {
78+export function _onAbort(db, f) {
79 return function aff(error, success) {
79 return function aff(error, success) {
80 db.onabort = function onabort() {
80 db.onabort = function onabort() {
81 f();
81 f();
@@ -88,7 +88,7 @@ exports._onAbort = function _onAbort(db, f) {
88 };
88 };
89};
89};
90
90
91−exports._onClose = function _onClose(db, f) {
91+export function _onClose(db, f) {
92 return function aff(error, success) {
92 return function aff(error, success) {
93 db.onclose = function onclose() {
93 db.onclose = function onclose() {
94 f();
94 f();
@@ -101,7 +101,7 @@ exports._onClose = function _onClose(db, f) {
101 };
101 };
102};
102};
103
103
104−exports._onError = function _onError(db, f) {
104+export function _onError(db, f) {
105 return function aff(error, success) {
105 return function aff(error, success) {
106 db.onerror = function onerror(e) {
106 db.onerror = function onerror(e) {
107 f(e.target.error)();
107 f(e.target.error)();
@@ -114,7 +114,7 @@ exports._onError = function _onError(db, f) {
114 };
114 };
115};
115};
116
116
117−exports._onVersionChange = function _onVersionChange(db, f) {
117+export function _onVersionChange(db, f) {
118 return function aff(error, success) {
118 return function aff(error, success) {
119 db.onversionchange = function onversionchange(e) {
119 db.onversionchange = function onversionchange(e) {
120 f({ oldVersion: e.oldVersion, newVersion: e.newVersion })();
120 f({ oldVersion: e.oldVersion, newVersion: e.newVersion })();
@@ -127,7 +127,7 @@ exports._onVersionChange = function _onVersionChange(db, f) {
127 };
127 };
128};
128};
129
129
130−exports._transaction = function _transaction(db, stores, mode) {
130+export function _transaction(db, stores, mode) {
131 return function aff(error, success) {
131 return function aff(error, success) {
132 var transaction;
132 var transaction;
133 try {
133 try {
@@ -144,6 +144,6 @@ exports._transaction = function _transaction(db, stores, mode) {
144 };
144 };
145};
145};
146
146
147−exports._version = function _version(db) {
147+export function _version(db) {
148 return db.version;
148 return db.version;
149};
149};
src/Database/IndexedDB/IDBDatabase.purs+73−98
@@ -24,17 +24,15 @@ module Database.IndexedDB.IDBDatabase
24 , onVersionChange
24 , onVersionChange
25 ) where
25 ) where
26
26
27−import Prelude (Unit, show, (<<<), ($))
28−
29−import Control.Monad.Aff (Aff)
30−import Control.Monad.Aff.Compat (fromEffFnAff, EffFnAff)
31−import Control.Monad.Eff (Eff)
32−import Control.Monad.Eff.Exception (Error)
33−import Data.Function.Uncurried as Fn
34−import Data.Function.Uncurried (Fn2, Fn3)
35−
36import Database.IndexedDB.Core
27import Database.IndexedDB.Core
37
28
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, (<<<), ($))
38
36
39--------------------
37--------------------
40-- TYPES
38-- TYPES
@@ -43,70 +41,67 @@ import Database.IndexedDB.Core
43-- | Type alias for StoreName
41-- | Type alias for StoreName
44type StoreName = String
42type StoreName = String
45
43
46−
47-- | Options provided when creating an object store.
44-- | Options provided when creating an object store.
48type ObjectStoreParameters =
45type ObjectStoreParameters =
49− { keyPath :: KeyPath
46+ { keyPath :: KeyPath
50 , autoIncrement :: Boolean
47 , autoIncrement :: Boolean
51 }
48 }
52
49
53−
54defaultParameters :: ObjectStoreParameters
50defaultParameters :: ObjectStoreParameters
55defaultParameters =
51defaultParameters =
56− { keyPath : []
52+ { keyPath: []
57− , autoIncrement : false
53+ , autoIncrement: false
58 }
54 }
59
55
60−
61--------------------
56--------------------
62-- INTERFACE
57-- INTERFACE
63--
58--
64
59
65-- | Closes the connection once all running transactions have finished.
60-- | Closes the connection once all running transactions have finished.
66close
61close
62+ :: forall db
67− :: forall e db. (IDBDatabase db)
63+ . (IDBDatabase db)
68 => db
64 => db
69− -> Aff (idb :: IDB | e) Unit
65+ -> Aff Unit
70close =
66close =
71− fromEffFnAff <<< _close
67+ fromEffectFnAff <<< _close
72−
73
68
74-- | Creates a new object store with the given name and options and returns a new IDBObjectStore.
69-- | Creates a new object store with the given name and options and returns a new IDBObjectStore.
75-- |
70-- |
76-- | Throws a "InvalidStateError" DOMException if not called within an upgrade transaction
71-- | Throws a "InvalidStateError" DOMException if not called within an upgrade transaction
77createObjectStore
72createObjectStore
73+ :: forall db
78− :: forall e db. (IDBDatabase db)
74+ . (IDBDatabase db)
79 => db
75 => db
80 -> StoreName
76 -> StoreName
81 -> ObjectStoreParameters
77 -> ObjectStoreParameters
82− -> Aff (idb :: IDB | e) ObjectStore
78+ -> Aff ObjectStore
83createObjectStore db name' opts =
79createObjectStore db name' opts =
84− fromEffFnAff $ Fn.runFn3 _createObjectStore db name' opts
80+ fromEffectFnAff $ Fn.runFn3 _createObjectStore db name' opts
85−
86
81
87-- | Deletes the object store with the given name.
82-- | Deletes the object store with the given name.
88-- |
83-- |
89-- | Throws a "InvalidStateError" DOMException if not called within an upgrade transaction.
84-- | Throws a "InvalidStateError" DOMException if not called within an upgrade transaction.
90deleteObjectStore
85deleteObjectStore
86+ :: forall db
91− :: forall e db. (IDBDatabase db)
87+ . (IDBDatabase db)
92 => db
88 => db
93 -> StoreName
89 -> StoreName
94− -> Aff (idb :: IDB | e) ObjectStore
90+ -> Aff ObjectStore
95deleteObjectStore db name' =
91deleteObjectStore db name' =
96− fromEffFnAff $ Fn.runFn2 _deleteObjectStore db name'
92+ fromEffectFnAff $ Fn.runFn2 _deleteObjectStore db name'
97−
98
93
99-- | Returns a new transaction with the given mode (ReadOnly|ReadWrite)
94-- | Returns a new transaction with the given mode (ReadOnly|ReadWrite)
100-- | and scope which in the form of an array of object store names.
95-- | and scope which in the form of an array of object store names.
101transaction
96transaction
97+ :: forall db
102− :: forall e db. (IDBDatabase db)
98+ . (IDBDatabase db)
103 => db
99 => db
104 -> Array StoreName
100 -> Array StoreName
105 -> TransactionMode
101 -> TransactionMode
106− -> Aff (idb :: IDB | e) Transaction
102+ -> Aff Transaction
107transaction db stores mode' =
103transaction db stores mode' =
108− fromEffFnAff $ Fn.runFn3 _transaction db stores (show mode')
104+ fromEffectFnAff $ Fn.runFn3 _transaction db stores (show mode')
109−
110
105
111--------------------
106--------------------
112-- ATTRIBUTES
107-- ATTRIBUTES
@@ -114,127 +109,107 @@ transaction db stores mode' =
114
109
115-- | Returns the name of the database.
110-- | Returns the name of the database.
116name
111name
117− :: Database
112+ :: Database
118− -> String
113+ -> String
119name =
114name =
120 _name
115 _name
121
116
122−
123-- | Returns a list of the names of object stores in the database.
117-- | Returns a list of the names of object stores in the database.
124objectStoreNames
118objectStoreNames
125− :: Database
119+ :: Database
126− -> Array String
120+ -> Array String
127objectStoreNames =
121objectStoreNames =
128 _objectStoreNames
122 _objectStoreNames
129
123
130−
131-- | Returns the version of the database.
124-- | Returns the version of the database.
132version
125version
133− :: Database
126+ :: Database
134− -> Int
127+ -> Int
135version =
128version =
136 _version
129 _version
137
130
138−
139--------------------
131--------------------
140-- EVENT HANDLERS
132-- EVENT HANDLERS
141--
133--
142-- | Event handler for the `abort` event.
134-- | Event handler for the `abort` event.
143onAbort
135onAbort
144− :: forall e e'
145− . Database
136+ :: Database
146− -> Eff ( | e') Unit
137+ -> Effect Unit
147− -> Aff (idb :: IDB | e) Unit
138+ -> Aff Unit
148onAbort db f =
139onAbort db f =
149− fromEffFnAff $ Fn.runFn2 _onAbort db f
140+ fromEffectFnAff $ Fn.runFn2 _onAbort db f
150−
151
141
152-- | Event handler for the `close` event.
142-- | Event handler for the `close` event.
153onClose
143onClose
154− :: forall e e'
155− . Database
144+ :: Database
156− -> Eff ( | e') Unit
145+ -> Effect Unit
157− -> Aff (idb :: IDB | e) Unit
146+ -> Aff Unit
158onClose db f =
147onClose db f =
159− fromEffFnAff $ Fn.runFn2 _onClose db f
148+ fromEffectFnAff $ Fn.runFn2 _onClose db f
160−
161
149
162-- | Event handler for the `error` event.
150-- | Event handler for the `error` event.
163onError
151onError
164− :: forall e e'
165− . Database
152+ :: Database
166− -> (Error -> Eff ( | e') Unit)
153+ -> (Error -> Effect Unit)
167− -> Aff (idb :: IDB | e) Unit
154+ -> Aff Unit
168onError db f =
155onError db f =
169− fromEffFnAff $ Fn.runFn2 _onError db f
156+ fromEffectFnAff $ Fn.runFn2 _onError db f
170−
171
157
172-- | Event handler for the `versionchange` event.
158-- | Event handler for the `versionchange` event.
173onVersionChange
159onVersionChange
174− :: forall e e'
175− . Database
160+ :: Database
176− -> ({ oldVersion :: Int, newVersion :: Int }
161+ -> ( { oldVersion :: Int, newVersion :: Int }
177− -> Eff ( | e') Unit)
162+ -> Effect Unit
163+ )
178− -> Aff (idb :: IDB | e) Unit
164+ -> Aff Unit
179onVersionChange db f =
165onVersionChange db f =
180− fromEffFnAff $ Fn.runFn2 _onVersionChange db f
166+ fromEffectFnAff $ Fn.runFn2 _onVersionChange db f
181−
182
167
183--------------------
168--------------------
184-- FFI
169-- FFI
185--
170--
186
171
187foreign import _close
172foreign import _close
188− :: forall db e
173+ :: forall db
189− . db
174+ . db
190− -> EffFnAff (idb :: IDB | e) Unit
175+ -> EffectFnAff Unit
191−
192
176
193foreign import _createObjectStore
177foreign import _createObjectStore
194− :: forall db e
178+ :: forall db
195− . Fn3 db String { keyPath :: Array String, autoIncrement :: Boolean } (EffFnAff (idb :: IDB | e) ObjectStore)
179+ . Fn3 db String { keyPath :: Array String, autoIncrement :: Boolean } (EffectFnAff ObjectStore)
196−
197
180
198foreign import _deleteObjectStore
181foreign import _deleteObjectStore
199− :: forall db e
182+ :: forall db
200− . Fn2 db String (EffFnAff (idb :: IDB | e) ObjectStore)
183+ . Fn2 db String (EffectFnAff ObjectStore)
201−
202
184
203foreign import _name
185foreign import _name
204− :: Database
186+ :: Database
205− -> String
187+ -> String
206−
207
188
208foreign import _objectStoreNames
189foreign import _objectStoreNames
209− :: Database
190+ :: Database
210− -> Array String
191+ -> Array String
211−
212
192
213foreign import _onAbort
193foreign import _onAbort
214− :: forall db e e'
194+ :: forall db
215− . Fn2 db (Eff ( | e') Unit) (EffFnAff (idb :: IDB | e) Unit)
195+ . Fn2 db (Effect Unit) (EffectFnAff Unit)
216−
217
196
218foreign import _onClose
197foreign import _onClose
219− :: forall db e e'
198+ :: forall db
220− . Fn2 db (Eff ( | e') Unit) (EffFnAff (idb :: IDB | e) Unit)
199+ . Fn2 db (Effect Unit) (EffectFnAff Unit)
221−
222
200
223foreign import _onError
201foreign import _onError
224− :: forall db e e'
202+ :: forall db
225− . Fn2 db (Error -> Eff ( | e') Unit) (EffFnAff (idb :: IDB | e) Unit)
203+ . Fn2 db (Error -> Effect Unit) (EffectFnAff Unit)
226−
227
204
228foreign import _onVersionChange
205foreign import _onVersionChange
229− :: forall db e e'
206+ :: forall db
230− . Fn2 db ({ oldVersion :: Int, newVersion :: Int } -> Eff ( | e') Unit) (EffFnAff (idb :: IDB | e) Unit)
207+ . Fn2 db ({ oldVersion :: Int, newVersion :: Int } -> Effect Unit) (EffectFnAff Unit)
231−
232
208
233foreign import _transaction
209foreign import _transaction
234− :: forall db e
210+ :: forall db
235− . Fn3 db (Array String) String (EffFnAff (idb :: IDB | e) Transaction)
211+ . Fn3 db (Array String) String (EffectFnAff Transaction)
236−
237
212
238foreign import _version
213foreign import _version
239− :: Database
214+ :: Database
240− -> Int
215+ -> Int
src/Database/IndexedDB/IDBFactory.js+2−2
@@ -18,7 +18,7 @@ const noOp3 = function noOp3() {
18 return noOp2;
18 return noOp2;
19};
19};
20
20
21−exports._deleteDatabase = function _deleteDatabase(name) {
21+export function _deleteDatabase(name) {
22 return function aff(error, success) {
22 return function aff(error, success) {
23 try {
23 try {
24 const request = indexedDB.deleteDatabase(name);
24 const request = indexedDB.deleteDatabase(name);
@@ -38,7 +38,7 @@ exports._deleteDatabase = function _deleteDatabase(name) {
38 };
38 };
39};
39};
40
40
41−exports._open = function _open(fromMaybe, name, mver, req) {
41+export function _open(fromMaybe, name, mver, req) {
42 const ver = fromMaybe(undefined)(mver);
42 const ver = fromMaybe(undefined)(mver);
43
43
44 return function aff(error, success) {
44 return function aff(error, success) {
src/Database/IndexedDB/IDBFactory.purs+22−33
@@ -12,37 +12,32 @@ module Database.IndexedDB.IDBFactory
12 , open
12 , open
13 ) where
13 ) where
14
14
15−import Prelude (Unit, ($), (<<<))
16−
17−import Control.Monad.Aff (Aff)
18−import Control.Monad.Aff.Compat (fromEffFnAff, EffFnAff)
19−import Control.Monad.Eff (Eff)
20−import Data.Function.Uncurried as Fn
21−import Data.Function.Uncurried (Fn4)
22−import Data.Maybe (Maybe, fromMaybe)
23−
24import Database.IndexedDB.Core
15import Database.IndexedDB.Core
25
16
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, ($), (<<<))
26
24
27--------------------
25--------------------
28-- TYPES
26-- TYPES
29--
27--
30
28
31-- Type alias for binding listeners to an initial open action.
29-- Type alias for binding listeners to an initial open action.
32−type Callbacks e =
30+type Callbacks =
33− { onBlocked :: Maybe (Eff (| e) Unit)
31+ { onBlocked :: Maybe (Effect Unit)
34− , onUpgradeNeeded :: Maybe (Database -> Transaction -> { oldVersion :: Int } -> Eff (| e) Unit)
32+ , onUpgradeNeeded :: Maybe (Database -> Transaction -> { oldVersion :: Int } -> Effect Unit)
35 }
33 }
36
34
37−
38-- | Type alias for DatabaseName.
35-- | Type alias for DatabaseName.
39type DatabaseName = String
36type DatabaseName = String
40
37
41−
42-- | Type alias for Version.
38-- | Type alias for Version.
43type Version = Int
39type Version = Int
44
40
45−
46--------------------
41--------------------
47-- INTERFACE
42-- INTERFACE
48--
43--
@@ -51,12 +46,10 @@ type Version = Int
51-- | and there are open connections that don’t close in response to a
46-- | and there are open connections that don’t close in response to a
52-- | `versionchange` event, the request will be blocked until all they close.
47-- | `versionchange` event, the request will be blocked until all they close.
53deleteDatabase
48deleteDatabase
54− :: forall e
55− . DatabaseName
49+ :: DatabaseName
56− -> Aff (idb :: IDB | e) Int
50+ -> Aff Int
57deleteDatabase =
51deleteDatabase =
58− fromEffFnAff <<< _deleteDatabase
52+ fromEffectFnAff <<< _deleteDatabase
59−
60
53
61-- | Attempts to open a connection to the named database with the specified version.
54-- | Attempts to open a connection to the named database with the specified version.
62-- | If the database already exists with a lower version and there are open connections
55-- | If the database already exists with a lower version and there are open connections
@@ -67,24 +60,20 @@ deleteDatabase =
67-- | When the version isn't provided (`Nothing`), attempts to open a connection to the
60-- | When the version isn't provided (`Nothing`), attempts to open a connection to the
68-- | named database with the current version, or 1 if it does not already exist.
61-- | named database with the current version, or 1 if it does not already exist.
69open
62open
70− :: forall e e'
71− . DatabaseName
63+ :: DatabaseName
72− -> Maybe Version
64+ -> Maybe Version
73− -> Callbacks e'
65+ -> Callbacks
74− -> Aff (idb :: IDB | e) Database
66+ -> Aff Database
75open name mver req =
67open name mver req =
76− fromEffFnAff $ Fn.runFn4 _open fromMaybe name mver req
68+ fromEffectFnAff $ Fn.runFn4 _open fromMaybe name mver req
77−
78
69
79--------------------
70--------------------
80-- FFI
71-- FFI
81--
72--
82foreign import _deleteDatabase
73foreign import _deleteDatabase
83− :: forall e
84− . String
74+ :: String
85− -> EffFnAff (idb :: IDB | e) Int
75+ -> EffectFnAff Int
86−
87
76
88foreign import _open
77foreign import _open
89− :: forall a e e'
78+ :: forall a
90− . Fn4 (a -> Maybe a -> a) String (Maybe Int) (Callbacks e') (EffFnAff (idb :: IDB | e) Database)
79+ . Fn4 (a -> Maybe a -> a) String (Maybe Int) (Callbacks) (EffectFnAff Database)
src/Database/IndexedDB/IDBIndex.js+12−12
@@ -11,7 +11,7 @@ const successHandler = function successHandler(cb) {
11};
11};
12
12
13
13
14−exports._keyPath = function _keyPath(index) {
14+export function _keyPath(index) {
15 const path = index.keyPath;
15 const path = index.keyPath;
16
16
17 if (Array.isArray(path)) {
17 if (Array.isArray(path)) {
@@ -25,23 +25,23 @@ exports._keyPath = function _keyPath(index) {
25 return [];
25 return [];
26};
26};
27
27
28−exports._multiEntry = function _multiEntry(index) {
28+export function _multiEntry(index) {
29 return index.multiEntry;
29 return index.multiEntry;
30};
30};
31
31
32−exports._name = function _name(index) {
32+export function _name(index) {
33 return index.name;
33 return index.name;
34};
34};
35
35
36−exports._objectStore = function _objectStore(index) {
36+export function _objectStore(index) {
37 return index.obectStore;
37 return index.obectStore;
38};
38};
39
39
40−exports._unique = function _unique(index) {
40+export function _unique(index) {
41 return index.unique;
41 return index.unique;
42};
42};
43
43
44−exports._count = function _count(index, query) {
44+export function _count(index, query) {
45 return function aff(error, success) {
45 return function aff(error, success) {
46 try {
46 try {
47 const request = index.count(query);
47 const request = index.count(query);
@@ -57,7 +57,7 @@ exports._count = function _count(index, query) {
57 };
57 };
58};
58};
59
59
60−exports._get = function _get(index, range) {
60+export function _get(index, range) {
61 return function aff(error, success) {
61 return function aff(error, success) {
62 try {
62 try {
63 const request = index.get(range);
63 const request = index.get(range);
@@ -79,7 +79,7 @@ exports._get = function _get(index, range) {
79 *
79 *
80 * However, it may be doable to convert the result to some key / value structure with values of
80 * However, it may be doable to convert the result to some key / value structure with values of
81 * different types.
81 * different types.
82−exports._getAll = function _getAll(index, query, count) {
82+export function _getAll(index, query, count) {
83 return function aff(error, success) {
83 return function aff(error, success) {
84 const request = index.getAll(query, count);
84 const request = index.getAll(query, count);
85 request.onsuccess = successHandler(success);
85 request.onsuccess = successHandler(success);
@@ -88,7 +88,7 @@ exports._getAll = function _getAll(index, query, count) {
88};
88};
89*/
89*/
90
90
91−exports._getAllKeys = function _getAllKeys(index, range, count) {
91+export function _getAllKeys(index, range, count) {
92 return function aff(error, success) {
92 return function aff(error, success) {
93 try {
93 try {
94 const request = index.getAllKeys(range, count || undefined);
94 const request = index.getAllKeys(range, count || undefined);
@@ -104,7 +104,7 @@ exports._getAllKeys = function _getAllKeys(index, range, count) {
104 };
104 };
105};
105};
106
106
107−exports._getKey = function _getKey(index, range) {
107+export function _getKey(index, range) {
108 return function aff(error, success) {
108 return function aff(error, success) {
109 try {
109 try {
110 const request = index.getKey(range);
110 const request = index.getKey(range);
@@ -120,7 +120,7 @@ exports._getKey = function _getKey(index, range) {
120 };
120 };
121};
121};
122
122
123−exports._openCursor = function _openCursor(index, query, dir, cb) {
123+export function _openCursor(index, query, dir, cb) {
124 return function aff(error, success) {
124 return function aff(error, success) {
125 try {
125 try {
126 const request = index.openCursor(query, dir);
126 const request = index.openCursor(query, dir);
@@ -145,7 +145,7 @@ exports._openCursor = function _openCursor(index, query, dir, cb) {
145 };
145 };
146};
146};
147
147
148−exports._openKeyCursor = function _openKeyCursor(index, query, dir, cb) {
148+export function _openKeyCursor(index, query, dir, cb) {
149 return function aff(error, success) {
149 return function aff(error, success) {
150 try {
150 try {
151 const request = index.openKeyCursor(query, dir);
151 const request = index.openKeyCursor(query, dir);
src/Database/IndexedDB/IDBIndex.purs+78−96
@@ -20,205 +20,187 @@ module Database.IndexedDB.IDBIndex
20 , unique
20 , unique
21 ) where
21 ) where
22
22
23−import Prelude (Unit, map, show, (<$>), (>>>), ($))
24−
25−import Control.Monad.Aff (Aff)
26−import Control.Monad.Aff.Compat (EffFnAff, fromEffFnAff)
27−import Control.Monad.Eff (Eff)
28−import Control.Monad.Eff.Exception (Error)
29−import Data.Foreign (Foreign, unsafeFromForeign)
30−import Data.Function.Uncurried as Fn
31−import Data.Function.Uncurried (Fn2, Fn3, Fn4)
32−import Data.Maybe (Maybe)
33−import Data.Nullable (Nullable, toMaybe, toNullable)
34−
35import Database.IndexedDB.Core
23import Database.IndexedDB.Core
36−import Database.IndexedDB.IDBKey.Internal (Key, toKey)
37
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, ($), (>>>))
38
36
39--------------------
37--------------------
40-- TYPES
38-- TYPES
41--
39--
42
40
43-- | Callbacks to manipulate a cursor from an Open*Cursor call
41-- | Callbacks to manipulate a cursor from an Open*Cursor call
44−type Callbacks cursor e =
42+type Callbacks cursor =
45− { onSuccess :: cursor -> Eff ( | e) Unit
43+ { onSuccess :: cursor -> Effect Unit
46− , onError :: Error -> Eff ( | e) Unit
44+ , onError :: Error -> Effect Unit
47− , onComplete :: Eff ( | e) Unit
45+ , onComplete :: Effect Unit
48 }
46 }
49
47
50−
51--------------------
48--------------------
52-- INTERFACES
49-- INTERFACES
53--
50--
54
51
55-- | Retrieves the number of records matching the key range in query.
52-- | Retrieves the number of records matching the key range in query.
56count
53count
54+ :: forall index
57− :: forall e index. (IDBIndex index)
55+ . (IDBIndex index)
58− => index
56+ => index
59 -> Maybe KeyRange
57 -> Maybe KeyRange
60− -> Aff (idb :: IDB | e) Int
58+ -> Aff Int
61count index range =
59count index range =
62− fromEffFnAff $ Fn.runFn2 _count index (toNullable range)
60+ fromEffectFnAff $ Fn.runFn2 _count index (toNullable range)
63−
64
61
65-- | Retrieves the value of the first record matching the given key range in query.
62-- | Retrieves the value of the first record matching the given key range in query.
66-- |
63-- |
67-- | NOTE
64-- | NOTE
68-- | The coercion from `a` to any type is unsafe and might throw a runtime error if incorrect.
65-- | The coercion from `a` to any type is unsafe and might throw a runtime error if incorrect.
69get
66get
67+ :: forall a index
70− :: forall a e index. (IDBIndex index)
68+ . (IDBIndex index)
71 => index
69 => index
72 -> KeyRange
70 -> KeyRange
73− -> Aff (idb :: IDB | e) (Maybe a)
71+ -> Aff (Maybe a)
74get index range =
72get index range =
75− map (toMaybe >>> map unsafeFromForeign) $ fromEffFnAff $ Fn.runFn2 _get index range
73+ map (toMaybe >>> map unsafeFromForeign) $ fromEffectFnAff $ Fn.runFn2 _get index range
76−
77
74
78-- | Retrieves the keys of records matching the given key range in query
75-- | Retrieves the keys of records matching the given key range in query
79-- | (up to the number given if given).
76-- | (up to the number given if given).
80getAllKeys
77getAllKeys
78+ :: forall index
81− :: forall e index. (IDBIndex index)
79+ . (IDBIndex index)
82 => index
80 => index
83 -> Maybe KeyRange
81 -> Maybe KeyRange
84 -> Maybe Int
82 -> Maybe Int
85− -> Aff (idb :: IDB | e) (Array Key)
83+ -> Aff (Array Key)
86getAllKeys index range n =
84getAllKeys index range n =
87− map (map toKey) $ fromEffFnAff $ Fn.runFn3 _getAllKeys index (toNullable range) (toNullable n)
85+ map (map toKey) $ fromEffectFnAff $ Fn.runFn3 _getAllKeys index (toNullable range) (toNullable n)
88−
89
86
90-- | Retrieves the key of the first record matching the given key or key range in query.
87-- | Retrieves the key of the first record matching the given key or key range in query.
91getKey
88getKey
89+ :: forall index
92− :: forall e index. (IDBIndex index)
90+ . (IDBIndex index)
93 => index
91 => index
94 -> KeyRange
92 -> KeyRange
95− -> Aff (idb :: IDB | e) (Maybe Key)
93+ -> Aff (Maybe Key)
96getKey index range =
94getKey index range =
97− map (toMaybe >>> map toKey) $ fromEffFnAff $ Fn.runFn2 _getKey index range
95+ map (toMaybe >>> map toKey) $ fromEffectFnAff $ Fn.runFn2 _getKey index range
98−
99
96
100-- | Opens a ValueCursor over the records matching query, ordered by direction.
97-- | Opens a ValueCursor over the records matching query, ordered by direction.
101-- | If query is `Nothing`, all records in index are matched.
98-- | If query is `Nothing`, all records in index are matched.
102openCursor
99openCursor
100+ :: forall index
103− :: forall e e' index. (IDBIndex index)
101+ . (IDBIndex index)
104− => index
102+ => index
105 -> Maybe KeyRange
103 -> Maybe KeyRange
106 -> CursorDirection
104 -> CursorDirection
107− -> Callbacks ValueCursor e'
105+ -> Callbacks ValueCursor
108− -> Aff (idb :: IDB | e) Unit
106+ -> Aff Unit
109openCursor index range dir cb =
107openCursor index range dir cb =
110− fromEffFnAff $ Fn.runFn4 _openCursor index (toNullable range) (show dir) cb
108+ fromEffectFnAff $ Fn.runFn4 _openCursor index (toNullable range) (show dir) cb
111−
112
109
113-- | Opens a KeyCursor over the records matching query, ordered by direction.
110-- | Opens a KeyCursor over the records matching query, ordered by direction.
114-- | If query is `Nothing`, all records in index are matched.
111-- | If query is `Nothing`, all records in index are matched.
115openKeyCursor
112openKeyCursor
113+ :: forall index
116− :: forall e e' index. (IDBIndex index)
114+ . (IDBIndex index)
117− => index
115+ => index
118− -> Maybe KeyRange
116+ -> Maybe KeyRange
119− -> CursorDirection
117+ -> CursorDirection
120− -> Callbacks KeyCursor e'
118+ -> Callbacks KeyCursor
121− -> Aff (idb :: IDB | e) Unit
119+ -> Aff Unit
122openKeyCursor index range dir cb =
120openKeyCursor index range dir cb =
123− fromEffFnAff $ Fn.runFn4 _openKeyCursor index (toNullable range) (show dir) cb
121+ fromEffectFnAff $ Fn.runFn4 _openKeyCursor index (toNullable range) (show dir) cb
124−
125
122
126--------------------
123--------------------
127-- ATTRIBUTES
124-- ATTRIBUTES
128--
125--
129-- | Returns the key path of the index.
126-- | Returns the key path of the index.
130keyPath
127keyPath
131− :: Index
128+ :: Index
132− -> KeyPath
129+ -> KeyPath
133keyPath =
130keyPath =
134 _keyPath
131 _keyPath
135
132
136−
137-- | Returns true if the index's multiEntry flag is set.
133-- | Returns true if the index's multiEntry flag is set.
138multiEntry
134multiEntry
139− :: Index
135+ :: Index
140− -> Boolean
136+ -> Boolean
141multiEntry =
137multiEntry =
142 _multiEntry
138 _multiEntry
143
139
144−
145-- | Returns the name of the index.
140-- | Returns the name of the index.
146name
141name
147− :: Index
142+ :: Index
148− -> String
143+ -> String
149name =
144name =
150 _name
145 _name
151
146
152−
153-- | Returns the IDBObjectStore the index belongs to.
147-- | Returns the IDBObjectStore the index belongs to.
154objectStore
148objectStore
155− :: Index
149+ :: Index
156− -> ObjectStore
150+ -> ObjectStore
157objectStore =
151objectStore =
158 _objectStore
152 _objectStore
159
153
160−
161-- | Returns true if the index's unique flag is set.
154-- | Returns true if the index's unique flag is set.
162unique
155unique
163− :: Index
156+ :: Index
164− -> Boolean
157+ -> Boolean
165unique =
158unique =
166 _unique
159 _unique
167
160
168−
169--------------------
161--------------------
170-- FFI
162-- FFI
171--
163--
172foreign import _keyPath
164foreign import _keyPath
173− :: Index
165+ :: Index
174− -> Array String
166+ -> Array String
175−
176
167
177foreign import _multiEntry
168foreign import _multiEntry
178− :: Index
169+ :: Index
179− -> Boolean
170+ -> Boolean
180−
181
171
182foreign import _name
172foreign import _name
183− :: Index
173+ :: Index
184− -> String
174+ -> String
185−
186
175
187foreign import _objectStore
176foreign import _objectStore
188− :: Index
177+ :: Index
189− -> ObjectStore
178+ -> ObjectStore
190−
191
179
192foreign import _unique
180foreign import _unique
193− :: Index
181+ :: Index
194− -> Boolean
182+ -> Boolean
195−
196
183
197foreign import _count
184foreign import _count
198− :: forall index e
185+ :: forall index
199− . Fn2 index (Nullable KeyRange) (EffFnAff (idb :: IDB | e) Int)
186+ . Fn2 index (Nullable KeyRange) (EffectFnAff Int)
200−
201
187
202foreign import _get
188foreign import _get
203− :: forall index e
189+ :: forall index
204− . Fn2 index KeyRange (EffFnAff (idb :: IDB | e) (Nullable Foreign))
190+ . Fn2 index KeyRange (EffectFnAff (Nullable Foreign))
205−
206
191
207foreign import _getAllKeys
192foreign import _getAllKeys
208− :: forall index e
193+ :: forall index
209− . Fn3 index (Nullable KeyRange) (Nullable Int) (EffFnAff (idb :: IDB | e) (Array Foreign))
194+ . Fn3 index (Nullable KeyRange) (Nullable Int) (EffectFnAff (Array Foreign))
210−
211
195
212foreign import _getKey
196foreign import _getKey
213− :: forall index e
197+ :: forall index
214− . Fn2 index KeyRange (EffFnAff (idb :: IDB | e) (Nullable Foreign))
198+ . Fn2 index KeyRange (EffectFnAff (Nullable Foreign))
215−
216
199
217foreign import _openCursor
200foreign import _openCursor
218− :: forall index e e'
201+ :: forall index
219− . Fn4 index (Nullable KeyRange) String (Callbacks ValueCursor e') (EffFnAff (idb :: IDB | e) Unit)
202+ . Fn4 index (Nullable KeyRange) String (Callbacks ValueCursor) (EffectFnAff Unit)
220−
221
203
222foreign import _openKeyCursor
204foreign import _openKeyCursor
223− :: forall index e e'
205+ :: forall index
224− . Fn4 index (Nullable KeyRange) String (Callbacks KeyCursor e') (EffFnAff (idb :: IDB | e) Unit)
206+ . Fn4 index (Nullable KeyRange) String (Callbacks KeyCursor) (EffectFnAff Unit)
src/Database/IndexedDB/IDBKey/Internal.js+3−3
@@ -1,8 +1,8 @@
1−exports._dateTimeToForeign = function _dateTimeToForeign(y, m, d, h, mi, s, ms) {
1+export function _dateTimeToForeign(y, m, d, h, mi, s, ms) {
2 return new Date(y, m, d, h, mi, s, ms);
2 return new Date(y, m, d, h, mi, s, ms);
3};
3};
4
4
5−exports._readDateTime = function _readDateTime(parse, right, left, date) {
5+export function _readDateTime(parse, right, left, date) {
6 if (Object.getPrototypeOf(date) !== Date.prototype) {
6 if (Object.getPrototypeOf(date) !== Date.prototype) {
7 return left(typeof date);
7 return left(typeof date);
8 }
8 }
@@ -24,7 +24,7 @@ exports._readDateTime = function _readDateTime(parse, right, left, date) {
24 return right(mdate);
24 return right(mdate);
25};
25};
26
26
27−exports._unsafeReadDateTime = function _unsafeReadDateTime(parse, date) {
27+export function _unsafeReadDateTime(parse, date) {
28 const y = date.getFullYear();
28 const y = date.getFullYear();
29 const m = date.getMonth() + 1;
29 const m = date.getMonth() + 1;
30 const d = date.getDate();
30 const d = date.getDate();
src/Database/IndexedDB/IDBKey/Internal.purs+90−106
@@ -3,35 +3,36 @@
3-- | NOTE: Binary keys aren't supported yet.
3-- | NOTE: Binary keys aren't supported yet.
4module Database.IndexedDB.IDBKey.Internal
4module Database.IndexedDB.IDBKey.Internal
5 ( Key
5 ( Key
6+ , class IDBKey
7+ , toKey
8+ , fromKey
6− , class IDBKey, toKey , fromKey , unsafeFromKey
9+ , unsafeFromKey
7 , none
10 , none
8 ) where
11 ) where
9
12
10import Prelude
13import Prelude
11
14
12−import Control.Alt ((<|>))
15+import Control.Alt ((<|>))
13−import Control.Monad.Except (ExceptT(..), runExceptT)
16+import Control.Monad.Except (ExceptT(..), runExceptT)
14−import Data.Date as Date
17+import Data.Date as Date
15−import Data.DateTime (DateTime(..), Time(..))
18+import Data.DateTime (DateTime(..), Time(..))
16−import Data.Either (Either(..), either, isRight)
19+import Data.Either (Either(..), either, isRight)
17−import Data.Enum (fromEnum, toEnum)
20+import Data.Enum (fromEnum, toEnum)
18−import Data.Foreign as Foreign
21+import Foreign as Foreign
19−import Data.Foreign (Foreign, F)
22+import Foreign (Foreign, F)
20−import Data.Function.Uncurried as Fn
23+import Data.Function.Uncurried as Fn
21import Data.Function.Uncurried (Fn2, Fn4, Fn7)
24import Data.Function.Uncurried (Fn2, Fn4, Fn7)
22−import Data.Identity (Identity(..))
25+import Data.Identity (Identity(..))
23−import Data.List.NonEmpty (NonEmptyList(..))
26+import Data.List.NonEmpty (NonEmptyList(..))
24−import Data.List.Types (List(..))
27+import Data.List.Types (List(..))
25−import Data.Maybe (Maybe(..))
28+import Data.Maybe (Maybe(..))
26−import Data.NonEmpty (NonEmpty(..))
29+import Data.NonEmpty (NonEmpty(..))
27−import Data.Nullable (Nullable, toNullable)
30+import Data.Nullable (Nullable, toNullable)
28−import Data.Time as Time
31+import Data.Time as Time
29−import Data.Traversable (traverse)
32+import Data.Traversable (traverse)
30−
31
33
32newtype Key = Key Foreign
34newtype Key = Key Foreign
33
35
34−
35--------------------
36--------------------
36-- INTERFACES
37-- INTERFACES
37--
38--
@@ -39,100 +40,90 @@ newtype Key = Key Foreign
39-- | to a known type (e.g if you only strings as keys, or perfectly knows the
40-- | to a known type (e.g if you only strings as keys, or perfectly knows the
40-- | type of a given key).
41-- | type of a given key).
41class IDBKey a where
42class IDBKey a where
42− toKey :: a -> Key
43+ toKey :: a -> Key
43− fromKey :: Key -> F a
44+ fromKey :: Key -> F a
44 unsafeFromKey :: Key -> a
45 unsafeFromKey :: Key -> a
45
46
46−
47none :: Maybe Key
47none :: Maybe Key
48none =
48none =
49 Nothing
49 Nothing
50
50
51−
52--------------------
51--------------------
53-- INSTANCES
52-- INSTANCES
54--
53--
55instance eqKey :: Eq Key where
54instance eqKey :: Eq Key where
56 eq a b = (runExceptT >>> runIdentity >>> isRight) $
55 eq a b = (runExceptT >>> runIdentity >>> isRight) $
57− eq <$> ((fromKey a) :: F Int) <*> fromKey b
56+ eq <$> ((fromKey a) :: F Int) <*> fromKey b
58− <|>
57+ <|>
59− eq <$> ((fromKey a) :: F Number) <*> fromKey b
58+ eq <$> ((fromKey a) :: F Number) <*> fromKey b
60− <|>
59+ <|>
61− eq <$> ((fromKey a) :: F String) <*> fromKey b
60+ eq <$> ((fromKey a) :: F String) <*> fromKey b
62− <|>
61+ <|>
63− eq <$> ((fromKey a) :: F DateTime) <*> fromKey b
62+ eq <$> ((fromKey a) :: F DateTime) <*> fromKey b
64− <|>
63+ <|>
65− eq <$> ((fromKey a) :: F (Array Key)) <*> fromKey b
64+ eq <$> ((fromKey a) :: F (Array Key)) <*> fromKey b
66 where
65 where
67− runIdentity :: forall a. Identity a -> a
66+ runIdentity :: forall a. Identity a -> a
68− runIdentity (Identity x) = x
67+ runIdentity (Identity x) = x
69−
70
68
71instance ordKey :: Ord Key where
69instance ordKey :: Ord Key where
72− compare a b = (runExceptT >>> runIdentity >>> either (const LT) id) $
70+ compare a b = (runExceptT >>> runIdentity >>> either (const LT) identity) $
73− compare <$> ((fromKey a) :: F Int) <*> fromKey b
71+ compare <$> ((fromKey a) :: F Int) <*> fromKey b
74− <|>
72+ <|>
75− compare <$> ((fromKey a) :: F Number) <*> fromKey b
73+ compare <$> ((fromKey a) :: F Number) <*> fromKey b
76− <|>
74+ <|>
77− compare <$> ((fromKey a) :: F String) <*> fromKey b
75+ compare <$> ((fromKey a) :: F String) <*> fromKey b
78− <|>
76+ <|>
79− compare <$> ((fromKey a) :: F DateTime) <*> fromKey b
77+ compare <$> ((fromKey a) :: F DateTime) <*> fromKey b
80− <|>
78+ <|>
81− compare <$> ((fromKey a) :: F (Array Key)) <*> fromKey b
79+ compare <$> ((fromKey a) :: F (Array Key)) <*> fromKey b
82 where
80 where
83− runIdentity :: forall a. Identity a -> a
81+ runIdentity :: forall a. Identity a -> a
84− runIdentity (Identity x) = x
82+ runIdentity (Identity x) = x
85−
86
83
87instance showKey :: Show Key where
84instance showKey :: Show Key where
88 show a = (runExceptT >>> format) $
85 show a = (runExceptT >>> format) $
89− (show <$> (fromKey a :: F Int))
86+ (show <$> (fromKey a :: F Int))
90− <|>
87+ <|>
91− (show <$> (fromKey a :: F Number))
88+ (show <$> (fromKey a :: F Number))
92− <|>
89+ <|>
93− (show <$> (fromKey a :: F String))
90+ (show <$> (fromKey a :: F String))
94− <|>
91+ <|>
95− (show <$> (fromKey a :: F DateTime))
92+ (show <$> (fromKey a :: F DateTime))
96− <|>
93+ <|>
97− (show <$> (fromKey a :: F (Array Key)))
94+ (show <$> (fromKey a :: F (Array Key)))
98 where
95 where
99− format :: forall a. Identity (Either a String) -> String
96+ format :: forall a. Identity (Either a String) -> String
100− format (Identity x) =
97+ format (Identity x) =
101− either (const "(Key)") (\s -> "(Key " <> s <> ")") x
98+ either (const "(Key)") (\s -> "(Key " <> s <> ")") x
102−
103
99
104instance idbKeyKey :: IDBKey Key where
100instance idbKeyKey :: IDBKey Key where
105− toKey = id
101+ toKey = identity
106− fromKey = pure
102+ fromKey = pure
107− unsafeFromKey = id
103+ unsafeFromKey = identity
108−
109
104
110instance idbKeyForeign :: IDBKey Foreign where
105instance idbKeyForeign :: IDBKey Foreign where
111− toKey = Key
106+ toKey = Key
112− fromKey (Key f) = pure f
107+ fromKey (Key f) = pure f
113 unsafeFromKey (Key f) = f
108 unsafeFromKey (Key f) = f
114
109
115−
116instance idbKeyInt :: IDBKey Int where
110instance idbKeyInt :: IDBKey Int where
117− toKey = Foreign.toForeign >>> Key
111+ toKey = Foreign.unsafeToForeign >>> Key
118− fromKey (Key f) = Foreign.readInt f
112+ fromKey (Key f) = Foreign.readInt f
119 unsafeFromKey (Key f) = Foreign.unsafeFromForeign f
113 unsafeFromKey (Key f) = Foreign.unsafeFromForeign f
120
114
121−
122instance idbKeyNumber :: IDBKey Number where
115instance idbKeyNumber :: IDBKey Number where
123− toKey = Foreign.toForeign >>> Key
116+ toKey = Foreign.unsafeToForeign >>> Key
124− fromKey (Key f) = Foreign.readNumber f
117+ fromKey (Key f) = Foreign.readNumber f
125 unsafeFromKey (Key f) = Foreign.unsafeFromForeign f
118 unsafeFromKey (Key f) = Foreign.unsafeFromForeign f
126
119
127−
128instance idbKeyString :: IDBKey String where
120instance idbKeyString :: IDBKey String where
129− toKey = Foreign.toForeign >>> Key
121+ toKey = Foreign.unsafeToForeign >>> Key
130− fromKey (Key f) = Foreign.readString f
122+ fromKey (Key f) = Foreign.readString f
131 unsafeFromKey (Key f) = Foreign.unsafeFromForeign f
123 unsafeFromKey (Key f) = Foreign.unsafeFromForeign f
132
124
133−
134instance idbKeyDate :: IDBKey DateTime where
125instance idbKeyDate :: IDBKey DateTime where
135− toKey (DateTime d t) = Key $ Fn.runFn7 _dateTimeToForeign
126+ toKey (DateTime d t) = Key $ Fn.runFn7 _dateTimeToForeign
136 (fromEnum $ Date.year d)
127 (fromEnum $ Date.year d)
137 (fromEnum $ Date.month d)
128 (fromEnum $ Date.month d)
138 (fromEnum $ Date.day d)
129 (fromEnum $ Date.day d)
@@ -140,58 +131,51 @@ instance idbKeyDate :: IDBKey DateTime where
140 (fromEnum $ Time.minute t)
131 (fromEnum $ Time.minute t)
141 (fromEnum $ Time.second t)
132 (fromEnum $ Time.second t)
142 (fromEnum $ Time.millisecond t)
133 (fromEnum $ Time.millisecond t)
143− fromKey (Key f) = Fn.runFn4 _readDateTime dateTime dateTimeF dateTimeE f
134+ fromKey (Key f) = Fn.runFn4 _readDateTime dateTime dateTimeF dateTimeE f
144 unsafeFromKey (Key f) = Fn.runFn2 _unsafeReadDateTime dateTime f
135 unsafeFromKey (Key f) = Fn.runFn2 _unsafeReadDateTime dateTime f
145
136
146−
147instance idbKeyArray :: IDBKey a => IDBKey (Array a) where
137instance idbKeyArray :: IDBKey a => IDBKey (Array a) where
148− toKey = Foreign.toForeign >>> Key
138+ toKey = Foreign.unsafeToForeign >>> Key
149− fromKey (Key f) = Foreign.readArray f >>= traverse (Key >>> fromKey)
139+ fromKey (Key f) = Foreign.readArray f >>= traverse (Key >>> fromKey)
150 unsafeFromKey (Key f) = map unsafeFromKey (Foreign.unsafeFromForeign f)
140 unsafeFromKey (Key f) = map unsafeFromKey (Foreign.unsafeFromForeign f)
151
141
152−
153-- FFI constructor to build a DateTime from years, months, days, hours, minutes, seconds and millis
142-- FFI constructor to build a DateTime from years, months, days, hours, minutes, seconds and millis
154dateTime
143dateTime
155− :: Int -- ^ years
144+ :: Int -- ^ years
156− -> Int -- ^ months
145+ -> Int -- ^ months
157− -> Int -- ^ days
146+ -> Int -- ^ days
158− -> Int -- ^ hours
147+ -> Int -- ^ hours
159− -> Int -- ^ minutes
148+ -> Int -- ^ minutes
160− -> Int -- ^ seconds
149+ -> Int -- ^ seconds
161− -> Int -- ^ milliseconds
150+ -> Int -- ^ milliseconds
162− -> Nullable DateTime
151+ -> Nullable DateTime
163dateTime y m d h mi s ms =
152dateTime y m d h mi s ms =
164 toNullable $ DateTime
153 toNullable $ DateTime
165− <$> (Date.canonicalDate <$> toEnum y <*> toEnum m <*> toEnum d)
154+ <$> (Date.canonicalDate <$> toEnum y <*> toEnum m <*> toEnum d)
166− <*> (Time <$> toEnum h <*> toEnum mi <*> toEnum s <*> toEnum ms)
155+ <*> (Time <$> toEnum h <*> toEnum mi <*> toEnum s <*> toEnum ms)
167−
168
156
169-- FFI constructor to convert a JS `Date` into a successful `F DateTime`
157-- FFI constructor to convert a JS `Date` into a successful `F DateTime`
170dateTimeF
158dateTimeF
171− :: DateTime
159+ :: DateTime
172− -> F DateTime
160+ -> F DateTime
173dateTimeF =
161dateTimeF =
174 Right >>> Identity >>> ExceptT
162 Right >>> Identity >>> ExceptT
175
163
176−
177-- FFI constructor to convert a string into an errored `F DateTime`
164-- FFI constructor to convert a string into an errored `F DateTime`
178dateTimeE
165dateTimeE
179− :: String
166+ :: String
180− -> F DateTime
167+ -> F DateTime
181dateTimeE =
168dateTimeE =
182 Foreign.TypeMismatch "Date" >>> flip NonEmpty Nil >>> NonEmptyList >>> Left >>> Identity >>> ExceptT
169 Foreign.TypeMismatch "Date" >>> flip NonEmpty Nil >>> NonEmptyList >>> Left >>> Identity >>> ExceptT
183
170
184−
185--------------------
171--------------------
186-- FFI
172-- FFI
187--
173--
188foreign import _dateTimeToForeign
174foreign import _dateTimeToForeign
189− :: Fn7 Int Int Int Int Int Int Int Foreign
175+ :: Fn7 Int Int Int Int Int Int Int Foreign
190−
191
176
192foreign import _readDateTime
177foreign import _readDateTime
193− :: Fn4 (Int -> Int -> Int -> Int -> Int -> Int -> Int -> Nullable DateTime) (DateTime -> F DateTime) (String -> F DateTime) Foreign (F DateTime)
178+ :: Fn4 (Int -> Int -> Int -> Int -> Int -> Int -> Int -> Nullable DateTime) (DateTime -> F DateTime) (String -> F DateTime) Foreign (F DateTime)
194−
195
179
196foreign import _unsafeReadDateTime
180foreign import _unsafeReadDateTime
197− :: Fn2 (Int -> Int -> Int -> Int -> Int -> Int -> Int -> Nullable DateTime) Foreign DateTime
181+ :: Fn2 (Int -> Int -> Int -> Int -> Int -> Int -> Int -> Nullable DateTime) Foreign DateTime
src/Database/IndexedDB/IDBKeyRange.js+9−9
@@ -1,4 +1,4 @@
1−exports._bound = function _bound(lower, upper, lowerOpen, upperOpen) {
1+export function _bound(lower, upper, lowerOpen, upperOpen) {
2 try {
2 try {
3 return IDBKeyRange.bound(lower, upper, lowerOpen, upperOpen);
3 return IDBKeyRange.bound(lower, upper, lowerOpen, upperOpen);
4 } catch (e) {
4 } catch (e) {
@@ -6,34 +6,34 @@ exports._bound = function _bound(lower, upper, lowerOpen, upperOpen) {
6 }
6 }
7};
7};
8
8
9−exports._includes = function _includes(range, key) {
9+export function _includes(range, key) {
10 return range.includes(key);
10 return range.includes(key);
11};
11};
12
12
13−exports._lower = function _lower(range) {
13+export function _lower(range) {
14 return range.lower;
14 return range.lower;
15};
15};
16
16
17−exports._lowerBound = function _lowerBound(lower, open) {
17+export function _lowerBound(lower, open) {
18 return IDBKeyRange.lowerBound(lower, open);
18 return IDBKeyRange.lowerBound(lower, open);
19};
19};
20
20
21−exports._lowerOpen = function _lowerOpen(range) {
21+export function _lowerOpen(range) {
22 return range.lowerOpen;
22 return range.lowerOpen;
23};
23};
24
24
25−exports._only = function _only(key) {
25+export function _only(key) {
26 return IDBKeyRange.only(key);
26 return IDBKeyRange.only(key);
27};
27};
28
28
29−exports._upper = function _upper(range) {
29+export function _upper(range) {
30 return range.upper;
30 return range.upper;
31};
31};
32
32
33−exports._upperBound = function _upperBound(upper, open) {
33+export function _upperBound(upper, open) {
34 return IDBKeyRange.upperBound(upper, open);
34 return IDBKeyRange.upperBound(upper, open);
35};
35};
36
36
37−exports._upperOpen = function _upperOpen(range) {
37+export function _upperOpen(range) {
38 return range.upperOpen;
38 return range.upperOpen;
39};
39};
src/Database/IndexedDB/IDBKeyRange.purs+30−42
@@ -19,18 +19,17 @@ module Database.IndexedDB.IDBKeyRange
19 , upperOpen
19 , upperOpen
20 ) where
20 ) where
21
21
22−import Prelude (($), (>>>), map)
22+import Prelude (($), (>>>), map)
23
23
24−import Data.Foreign (Foreign)
24+import Foreign (Foreign)
25−import Data.Function.Uncurried as Fn
25+import Data.Function.Uncurried as Fn
26−import Data.Function.Uncurried (Fn2, Fn4)
26+import Data.Function.Uncurried (Fn2, Fn4)
27−import Data.Maybe (Maybe)
27+import Data.Maybe (Maybe)
28−import Data.Nullable (Nullable, toMaybe)
28+import Data.Nullable (Nullable, toMaybe)
29
29
30−import Database.IndexedDB.Core (class IDBKeyRange, KeyRange)
30+import Database.IndexedDB.Core (class IDBKeyRange, KeyRange)
31import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, toKey, unsafeFromKey)
31import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, toKey, unsafeFromKey)
32
32
33−
34--------------------
33--------------------
35-- TYPES
34-- TYPES
36--
35--
@@ -38,55 +37,54 @@ import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, toKey, unsafeFromK
38-- | Type alias for open
37-- | Type alias for open
39type Open = Boolean
38type Open = Boolean
40
39
41−
42--------------------
40--------------------
43-- CONSTRUCTORS
41-- CONSTRUCTORS
44--
42--
45
43
46-- | Returns a new IDBKeyRange spanning only key.
44-- | Returns a new IDBKeyRange spanning only key.
47only
45only
48− :: forall a. (IDBKey a)
46+ :: forall a
47+ . (IDBKey a)
49− => a
48+ => a
50− -> KeyRange
49+ -> KeyRange
51only key =
50only key =
52 _only (unsafeFromKey $ toKey key)
51 _only (unsafeFromKey $ toKey key)
53
52
54−
55-- | Returns a new IDBKeyRange starting at key with no upper bound.
53-- | Returns a new IDBKeyRange starting at key with no upper bound.
56-- | If `Open` is `true`, key is not included in the range.
54-- | If `Open` is `true`, key is not included in the range.
57lowerBound
55lowerBound
58− :: forall a. (IDBKey a)
56+ :: forall a
57+ . (IDBKey a)
59− => a
58+ => a
60− -> Open
59+ -> Open
61− -> KeyRange
60+ -> KeyRange
62lowerBound key open =
61lowerBound key open =
63 Fn.runFn2 _lowerBound (unsafeFromKey $ toKey key) open
62 Fn.runFn2 _lowerBound (unsafeFromKey $ toKey key) open
64
63
65−
66-- | Returns a new IDBKeyRange with no lower bound and ending at key.
64-- | Returns a new IDBKeyRange with no lower bound and ending at key.
67-- | If `Open` is `true`, key is not included in the range.
65-- | If `Open` is `true`, key is not included in the range.
68upperBound
66upperBound
69− :: forall a. (IDBKey a)
67+ :: forall a
68+ . (IDBKey a)
70− => a
69+ => a
71− -> Open
70+ -> Open
72− -> KeyRange
71+ -> KeyRange
73upperBound key open =
72upperBound key open =
74 Fn.runFn2 _upperBound (unsafeFromKey $ toKey key) open
73 Fn.runFn2 _upperBound (unsafeFromKey $ toKey key) open
75
74
76−
77-- | Returns a new IDBKeyRange spanning from `lower` to `upper`.
75-- | Returns a new IDBKeyRange spanning from `lower` to `upper`.
78-- | If `lowerOpen` is `true`, `lower` is not included in the range.
76-- | If `lowerOpen` is `true`, `lower` is not included in the range.
79-- | If `upperOpen` is `true`, `upper` is not included in the range.
77-- | If `upperOpen` is `true`, `upper` is not included in the range.
80-- |
78-- |
81-- | It throws a `DataError` if the bound is invalid.
79-- | It throws a `DataError` if the bound is invalid.
82bound
80bound
83− :: forall key. (IDBKey key)
81+ :: forall key
82+ . (IDBKey key)
84− => { lower :: key, upper :: key, lowerOpen :: Boolean, upperOpen :: Boolean }
83+ => { lower :: key, upper :: key, lowerOpen :: Boolean, upperOpen :: Boolean }
85− -> Maybe KeyRange
84+ -> Maybe KeyRange
86bound { lower: key1, upper: key2, lowerOpen: open1, upperOpen: open2 } =
85bound { lower: key1, upper: key2, lowerOpen: open1, upperOpen: open2 } =
87 toMaybe
86 toMaybe
88− $ Fn.runFn4 _bound (unsafeFromKey $ toKey key1) (unsafeFromKey $ toKey key2) open1 open2
87+ $ Fn.runFn4 _bound (unsafeFromKey $ toKey key1) (unsafeFromKey $ toKey key2) open1 open2
89−
90
88
91--------------------
89--------------------
92-- INTERFACE
90-- INTERFACE
@@ -94,14 +92,15 @@ bound { lower: key1, upper: key2, lowerOpen: open1, upperOpen: open2 } =
94
92
95-- | Returns true if key is included in the range, and false otherwise.
93-- | Returns true if key is included in the range, and false otherwise.
96includes
94includes
95+ :: forall key range
96+ . (IDBKey key)
97− :: forall key range. (IDBKey key) => (IDBKeyRange range)
97+ => (IDBKeyRange range)
98 => range
98 => range
99 -> key
99 -> key
100 -> Boolean
100 -> Boolean
101includes range =
101includes range =
102 toKey >>> unsafeFromKey >>> Fn.runFn2 _includes range
102 toKey >>> unsafeFromKey >>> Fn.runFn2 _includes range
103
103
104−
105--------------------
104--------------------
106-- ATTRIBUTES
105-- ATTRIBUTES
107--
106--
@@ -112,7 +111,6 @@ lower
112lower =
111lower =
113 _lower >>> toMaybe >>> map toKey
112 _lower >>> toMaybe >>> map toKey
114
113
115−
116-- | Returns upper bound if any.
114-- | Returns upper bound if any.
117upper
115upper
118 :: KeyRange
116 :: KeyRange
@@ -120,7 +118,6 @@ upper
120upper =
118upper =
121 _upper >>> toMaybe >>> map toKey
119 _upper >>> toMaybe >>> map toKey
122
120
123−
124-- | Returns true if the lower open flag is set, false otherwise.
121-- | Returns true if the lower open flag is set, false otherwise.
125lowerOpen
122lowerOpen
126 :: KeyRange
123 :: KeyRange
@@ -128,7 +125,6 @@ lowerOpen
128lowerOpen =
125lowerOpen =
129 _lowerOpen
126 _lowerOpen
130
127
131−
132-- | Returns true if the upper open flag is set, false otherwise.
128-- | Returns true if the upper open flag is set, false otherwise.
133upperOpen
129upperOpen
134 :: KeyRange
130 :: KeyRange
@@ -136,7 +132,6 @@ upperOpen
136upperOpen =
132upperOpen =
137 _upperOpen
133 _upperOpen
138
134
139−
140--------------------
135--------------------
141-- FFI
136-- FFI
142--
137--
@@ -148,35 +143,28 @@ foreign import _only
148foreign import _lowerBound
143foreign import _lowerBound
149 :: Fn2 Foreign Boolean KeyRange
144 :: Fn2 Foreign Boolean KeyRange
150
145
151−
152foreign import _upperBound
146foreign import _upperBound
153 :: Fn2 Foreign Boolean KeyRange
147 :: Fn2 Foreign Boolean KeyRange
154
148
155−
156foreign import _bound
149foreign import _bound
157 :: Fn4 Foreign Foreign Boolean Boolean (Nullable KeyRange)
150 :: Fn4 Foreign Foreign Boolean Boolean (Nullable KeyRange)
158
151
159−
160foreign import _includes
152foreign import _includes
161 :: forall range
153 :: forall range
162− . Fn2 range Foreign Boolean
154+ . Fn2 range Foreign Boolean
163−
164
155
165foreign import _lower
156foreign import _lower
166 :: KeyRange
157 :: KeyRange
167 -> Nullable Foreign
158 -> Nullable Foreign
168
159
169−
170foreign import _upper
160foreign import _upper
171 :: KeyRange
161 :: KeyRange
172 -> Nullable Foreign
162 -> Nullable Foreign
173
163
174−
175foreign import _lowerOpen
164foreign import _lowerOpen
176 :: KeyRange
165 :: KeyRange
177 -> Boolean
166 -> Boolean
178
167
179−
180foreign import _upperOpen
168foreign import _upperOpen
181 :: KeyRange
169 :: KeyRange
182 -> Boolean
170 -> Boolean
src/Database/IndexedDB/IDBObjectStore.js+12−12
@@ -15,7 +15,7 @@ const toArray = function toArray(xs) {
15};
15};
16
16
17
17
18−exports._add = function _add(store, value, key) {
18+export function _add(store, value, key) {
19 return function aff(error, success) {
19 return function aff(error, success) {
20 try {
20 try {
21 const request = store.add(value, key || undefined);
21 const request = store.add(value, key || undefined);
@@ -31,11 +31,11 @@ exports._add = function _add(store, value, key) {
31 };
31 };
32};
32};
33
33
34−exports._autoIncrement = function _autoIncrement(store) {
34+export function _autoIncrement(store) {
35 return store.autoIncrement;
35 return store.autoIncrement;
36};
36};
37
37
38−exports._clear = function _clear(store) {
38+export function _clear(store) {
39 return function aff(error, success) {
39 return function aff(error, success) {
40 try {
40 try {
41 const request = store.clear();
41 const request = store.clear();
@@ -51,7 +51,7 @@ exports._clear = function _clear(store) {
51 };
51 };
52};
52};
53
53
54−exports._createIndex = function _createIndex(store, name, path, params) {
54+export function _createIndex(store, name, path, params) {
55 return function aff(error, success) {
55 return function aff(error, success) {
56 var keyPath;
56 var keyPath;
57
57
@@ -81,7 +81,7 @@ exports._createIndex = function _createIndex(store, name, path, params) {
81 };
81 };
82};
82};
83
83
84−exports._deleteIndex = function _deleteIndex(store, name) {
84+export function _deleteIndex(store, name) {
85 return function aff(error, success) {
85 return function aff(error, success) {
86 try {
86 try {
87 store.deleteIndex(name);
87 store.deleteIndex(name);
@@ -96,7 +96,7 @@ exports._deleteIndex = function _deleteIndex(store, name) {
96 };
96 };
97};
97};
98
98
99−exports._delete = function _delete(store, query) {
99+export function _delete(store, query) {
100 return function aff(error, success) {
100 return function aff(error, success) {
101 try {
101 try {
102 const request = store.delete(query);
102 const request = store.delete(query);
@@ -112,7 +112,7 @@ exports._delete = function _delete(store, query) {
112 };
112 };
113};
113};
114
114
115−exports._index = function _index(store, name) {
115+export function _index(store, name) {
116 return function aff(error, success) {
116 return function aff(error, success) {
117 try {
117 try {
118 const index = store.index(name);
118 const index = store.index(name);
@@ -127,11 +127,11 @@ exports._index = function _index(store, name) {
127 };
127 };
128};
128};
129
129
130−exports._indexNames = function _indexNames(store) {
130+export function _indexNames(store) {
131 return toArray(store.indexNames);
131 return toArray(store.indexNames);
132};
132};
133
133
134−exports._keyPath = function _keyPath(store) {
134+export function _keyPath(store) {
135 const path = store.keyPath;
135 const path = store.keyPath;
136
136
137 if (Array.isArray(path)) {
137 if (Array.isArray(path)) {
@@ -145,11 +145,11 @@ exports._keyPath = function _keyPath(store) {
145 return [];
145 return [];
146};
146};
147
147
148−exports._name = function _name(store) {
148+export function _name(store) {
149 return store.name;
149 return store.name;
150};
150};
151
151
152−exports._put = function _put(store, value, key) {
152+export function _put(store, value, key) {
153 return function aff(error, success) {
153 return function aff(error, success) {
154 try {
154 try {
155 const request = store.put(value, key || undefined);
155 const request = store.put(value, key || undefined);
@@ -165,6 +165,6 @@ exports._put = function _put(store, value, key) {
165 };
165 };
166};
166};
167
167
168−exports._transaction = function _transaction(store) {
168+export function _transaction(store) {
169 return store.transaction;
169 return store.transaction;
170};
170};
src/Database/IndexedDB/IDBObjectStore.purs+58−76
@@ -25,19 +25,18 @@ module Database.IndexedDB.IDBObjectStore
25 , module Database.IndexedDB.IDBIndex
25 , module Database.IndexedDB.IDBIndex
26 ) where
26 ) where
27
27
28−import Prelude (Unit, map, ($), (<$>), (>>>), (<<<))
29−
30−import Control.Monad.Aff (Aff)
31−import Control.Monad.Aff.Compat (EffFnAff, fromEffFnAff)
32−import Data.Foreign (Foreign)
33−import Data.Function.Uncurried as Fn
34−import Data.Function.Uncurried (Fn2, Fn3, Fn4)
35−import Data.Maybe (Maybe)
36−import Data.Nullable (Nullable, toNullable)
37−
38import Database.IndexedDB.Core
28import Database.IndexedDB.Core
29+
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)
39−import Database.IndexedDB.IDBIndex (count, get, getAllKeys, getKey, openCursor, openKeyCursor)
34+import Database.IndexedDB.IDBIndex (count, get, getAllKeys, getKey, openCursor, openKeyCursor)
40import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, unsafeFromKey, toKey)
35import 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, ($), (<$>), (>>>), (<<<))
41
40
42--------------------
41--------------------
43-- TYPES
42-- TYPES
@@ -46,7 +45,6 @@ import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, unsafeFromKey, toK
46-- | Type alias for IndexName
45-- | Type alias for IndexName
47type IndexName = String
46type IndexName = String
48
47
49−
50-- | Flags to set on the index.
48-- | Flags to set on the index.
51-- |
49-- |
52-- | An index has a `unique` flag. When this flag is set, the index enforces that no
50-- | An index has a `unique` flag. When this flag is set, the index enforces that no
@@ -61,18 +59,16 @@ type IndexName = String
61-- | If the `multiEntry` flag is true, then the one record is added to the index for each
59-- | If the `multiEntry` flag is true, then the one record is added to the index for each
62-- | of the subkeys.
60-- | of the subkeys.
63type IndexParameters =
61type IndexParameters =
64− { unique :: Boolean
62+ { unique :: Boolean
65 , multiEntry :: Boolean
63 , multiEntry :: Boolean
66 }
64 }
67
65
68−
69defaultParameters :: IndexParameters
66defaultParameters :: IndexParameters
70defaultParameters =
67defaultParameters =
71− { unique : false
68+ { unique: false
72− , multiEntry : false
69+ , multiEntry: false
73 }
70 }
74
71
75−
76--------------------
72--------------------
77-- INTERFACES
73-- INTERFACES
78--
74--
@@ -85,23 +81,24 @@ defaultParameters =
85-- | If add() is used, and if a record with the key already exists the request will fail,
81-- | If add() is used, and if a record with the key already exists the request will fail,
86-- | with a "ConstraintError" DOMException.
82-- | with a "ConstraintError" DOMException.
87add
83add
84+ :: forall key val store
85+ . (IDBKey key)
88− :: forall e key val store. (IDBKey key) => (IDBObjectStore store)
86+ => (IDBObjectStore store)
89 => store
87 => store
90 -> val
88 -> val
91 -> Maybe key
89 -> Maybe key
92− -> Aff (idb :: IDB | e) Key
90+ -> Aff Key
93add store value key =
91add store value key =
94− map toKey $ fromEffFnAff $ Fn.runFn3 _add store value (toNullable $ (toKey >>> unsafeFromKey) <$> key)
92+ map toKey $ fromEffectFnAff $ Fn.runFn3 _add store value (toNullable $ (toKey >>> unsafeFromKey) <$> key)
95−
96
93
97-- | Deletes all records in store.
94-- | Deletes all records in store.
98clear
95clear
96+ :: forall store
99− :: forall e store. (IDBObjectStore store)
97+ . (IDBObjectStore store)
100 => store
98 => store
101− -> Aff (idb :: IDB | e) Unit
99+ -> Aff Unit
102clear =
100clear =
103− fromEffFnAff <<< _clear
101+ fromEffectFnAff <<< _clear
104−
105
102
106-- | Creates a new index in store with the given name, keyPath and options and
103-- | Creates a new index in store with the given name, keyPath and options and
107-- | returns a new IDBIndex. If the keyPath and options define constraints that
104-- | returns a new IDBIndex. If the keyPath and options define constraints that
@@ -110,47 +107,47 @@ clear =
110-- |
107-- |
111-- | Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
108-- | Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
112createIndex
109createIndex
110+ :: forall store
113− :: forall e store. (IDBObjectStore store)
111+ . (IDBObjectStore store)
114 => store
112 => store
115 -> IndexName
113 -> IndexName
116 -> KeyPath
114 -> KeyPath
117 -> IndexParameters
115 -> IndexParameters
118− -> Aff (idb :: IDB | e) Index
116+ -> Aff Index
119createIndex store name' path params =
117createIndex store name' path params =
120− fromEffFnAff $ Fn.runFn4 _createIndex store name' path params
118+ fromEffectFnAff $ Fn.runFn4 _createIndex store name' path params
121−
122
119
123-- | Deletes records in store with the given key or in the given key range in query.
120-- | Deletes records in store with the given key or in the given key range in query.
124delete
121delete
122+ :: forall store
125− :: forall e store. (IDBObjectStore store)
123+ . (IDBObjectStore store)
126 => store
124 => store
127 -> KeyRange
125 -> KeyRange
128− -> Aff (idb :: IDB | e) Unit
126+ -> Aff Unit
129delete store range =
127delete store range =
130− fromEffFnAff $ Fn.runFn2 _delete store range
128+ fromEffectFnAff $ Fn.runFn2 _delete store range
131−
132
129
133-- | Deletes the index in store with the given name.
130-- | Deletes the index in store with the given name.
134-- |
131-- |
135-- | Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
132-- | Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
136deleteIndex
133deleteIndex
134+ :: forall store
137− :: forall e store. (IDBObjectStore store)
135+ . (IDBObjectStore store)
138 => store
136 => store
139 -> IndexName
137 -> IndexName
140− -> Aff (idb :: IDB | e) Unit
138+ -> Aff Unit
141deleteIndex store name' =
139deleteIndex store name' =
142− fromEffFnAff $ Fn.runFn2 _deleteIndex store name'
140+ fromEffectFnAff $ Fn.runFn2 _deleteIndex store name'
143−
144
141
145-- | Returns an IDBIndex for the index named name in store.
142-- | Returns an IDBIndex for the index named name in store.
146index
143index
144+ :: forall store
147− :: forall e store. (IDBObjectStore store)
145+ . (IDBObjectStore store)
148 => store
146 => store
149 -> IndexName
147 -> IndexName
150− -> Aff (idb :: IDB | e) Index
148+ -> Aff Index
151index store name' =
149index store name' =
152− fromEffFnAff $ Fn.runFn2 _index store name'
150+ fromEffectFnAff $ Fn.runFn2 _index store name'
153−
154
151
155-- | Adds or updates a record in store with the given value and key.
152-- | Adds or updates a record in store with the given value and key.
156-- |
153-- |
@@ -159,14 +156,15 @@ index store name' =
159-- |
156-- |
160-- | If put() is used, any existing record with the key will be replaced.
157-- | If put() is used, any existing record with the key will be replaced.
161put
158put
159+ :: forall val key store
160+ . (IDBKey key)
162− :: forall e val key store. (IDBKey key) => (IDBObjectStore store)
161+ => (IDBObjectStore store)
163 => store
162 => store
164 -> val
163 -> val
165 -> Maybe key
164 -> Maybe key
166− -> Aff (idb :: IDB | e) Key
165+ -> Aff Key
167put store value key =
166put store value key =
168− map toKey $ fromEffFnAff $ Fn.runFn3 _put store value (toNullable $ (toKey >>> unsafeFromKey) <$> key)
167+ map toKey $ fromEffectFnAff $ Fn.runFn3 _put store value (toNullable $ (toKey >>> unsafeFromKey) <$> key)
169−
170
168
171--------------------
169--------------------
172-- ATTRIBUTES
170-- ATTRIBUTES
@@ -178,7 +176,6 @@ autoIncrement
178autoIncrement =
176autoIncrement =
179 _autoIncrement
177 _autoIncrement
180
178
181−
182--| Returns a list of the names of indexes in the store.
179--| Returns a list of the names of indexes in the store.
183indexNames
180indexNames
184 :: ObjectStore
181 :: ObjectStore
@@ -186,7 +183,6 @@ indexNames
186indexNames =
183indexNames =
187 _indexNames
184 _indexNames
188
185
189−
190-- | Returns the key path of the store, or empty array if none
186-- | Returns the key path of the store, or empty array if none
191keyPath
187keyPath
192 :: ObjectStore
188 :: ObjectStore
@@ -194,7 +190,6 @@ keyPath
194keyPath =
190keyPath =
195 _keyPath
191 _keyPath
196
192
197−
198-- | Returns the name of the store.
193-- | Returns the name of the store.
199name
194name
200 :: ObjectStore
195 :: ObjectStore
@@ -202,7 +197,6 @@ name
202name =
197name =
203 _name
198 _name
204
199
205−
206-- | Returns the associated transaction.
200-- | Returns the associated transaction.
207transaction
201transaction
208 :: ObjectStore
202 :: ObjectStore
@@ -210,65 +204,53 @@ transaction
210transaction =
204transaction =
211 _transaction
205 _transaction
212
206
213−
214--------------------
207--------------------
215-- FFI
208-- FFI
216--
209--
217foreign import _add
210foreign import _add
218− :: forall e val store
211+ :: forall val store
219− . Fn3 store val (Nullable Foreign) (EffFnAff (idb :: IDB | e) Foreign)
212+ . Fn3 store val (Nullable Foreign) (EffectFnAff Foreign)
220−
221
213
222foreign import _autoIncrement
214foreign import _autoIncrement
223 :: ObjectStore
215 :: ObjectStore
224 -> Boolean
216 -> Boolean
225
217
226−
227foreign import _clear
218foreign import _clear
228− :: forall e store
219+ :: forall store
229− . store
220+ . store
230− -> EffFnAff (idb :: IDB | e) Unit
221+ -> EffectFnAff Unit
231−
232
222
233foreign import _createIndex
223foreign import _createIndex
234− :: forall e store
224+ :: forall store
235− . Fn4 store String (Array String) { unique :: Boolean, multiEntry :: Boolean } (EffFnAff (idb :: IDB | e) Index)
225+ . Fn4 store String (Array String) { unique :: Boolean, multiEntry :: Boolean } (EffectFnAff Index)
236−
237
226
238foreign import _delete
227foreign import _delete
239− :: forall e store
228+ :: forall store
240− . Fn2 store KeyRange (EffFnAff (idb :: IDB | e) Unit)
229+ . Fn2 store KeyRange (EffectFnAff Unit)
241−
242
230
243foreign import _deleteIndex
231foreign import _deleteIndex
244− :: forall e store
232+ :: forall store
245− . Fn2 store String (EffFnAff (idb :: IDB | e) Unit)
233+ . Fn2 store String (EffectFnAff Unit)
246−
247
234
248foreign import _index
235foreign import _index
249− :: forall e store
236+ :: forall store
250− . Fn2 store String (EffFnAff (idb :: IDB | e) Index)
237+ . Fn2 store String (EffectFnAff Index)
251−
252
238
253foreign import _indexNames
239foreign import _indexNames
254 :: ObjectStore
240 :: ObjectStore
255 -> Array String
241 -> Array String
256
242
257−
258foreign import _keyPath
243foreign import _keyPath
259 :: ObjectStore
244 :: ObjectStore
260 -> Array String
245 -> Array String
261
246
262−
263foreign import _name
247foreign import _name
264 :: ObjectStore
248 :: ObjectStore
265 -> String
249 -> String
266
250
267−
268foreign import _put
251foreign import _put
269− :: forall e val store
252+ :: forall val store
270− . Fn3 store val (Nullable Foreign) (EffFnAff (idb :: IDB | e) Foreign)
253+ . Fn3 store val (Nullable Foreign) (EffectFnAff Foreign)
271−
272
254
273foreign import _transaction
255foreign import _transaction
274 :: ObjectStore
256 :: ObjectStore
src/Database/IndexedDB/IDBTransaction.js+9−9
@@ -1,4 +1,4 @@
1−exports._abort = function _abort(tx) {
1+export function _abort(tx) {
2 return function aff(error, success) {
2 return function aff(error, success) {
3 try {
3 try {
4 tx.abort();
4 tx.abort();
@@ -13,17 +13,17 @@ exports._abort = function _abort(tx) {
13 };
13 };
14};
14};
15
15
16−exports._db = function _db(tx) {
16+export function _db(tx) {
17 return tx.db;
17 return tx.db;
18};
18};
19
19
20−exports._error = function _error(tx) {
20+export function _error(tx) {
21 return tx.error == null
21 return tx.error == null
22 ? null
22 ? null
23 : tx.error;
23 : tx.error;
24};
24};
25
25
26−exports._mode = function _mode(ReadOnly, ReadWrite, VersionChange, tx) {
26+export function _mode(ReadOnly, ReadWrite, VersionChange, tx) {
27 if (tx.mode === 'readwrite') {
27 if (tx.mode === 'readwrite') {
28 return ReadWrite;
28 return ReadWrite;
29 } else if (tx.mode === 'versionchange') {
29 } else if (tx.mode === 'versionchange') {
@@ -33,7 +33,7 @@ exports._mode = function _mode(ReadOnly, ReadWrite, VersionChange, tx) {
33 return ReadOnly;
33 return ReadOnly;
34};
34};
35
35
36−exports._objectStore = function _objectStore(tx, name) {
36+export function _objectStore(tx, name) {
37 return function aff(error, success) {
37 return function aff(error, success) {
38 try {
38 try {
39 const store = tx.objectStore(name);
39 const store = tx.objectStore(name);
@@ -48,11 +48,11 @@ exports._objectStore = function _objectStore(tx, name) {
48 };
48 };
49};
49};
50
50
51−exports._objectStoreNames = function _objectStoreNames(tx) {
51+export function _objectStoreNames(tx) {
52 return tx.objectStoreNames;
52 return tx.objectStoreNames;
53};
53};
54
54
55−exports._onAbort = function _onAbort(tx, f) {
55+export function _onAbort(tx, f) {
56 return function aff(error, success) {
56 return function aff(error, success) {
57 tx.onabort = function onabort() {
57 tx.onabort = function onabort() {
58 f();
58 f();
@@ -65,7 +65,7 @@ exports._onAbort = function _onAbort(tx, f) {
65 };
65 };
66};
66};
67
67
68−exports._onComplete = function _onComplete(tx, f) {
68+export function _onComplete(tx, f) {
69 return function aff(error, success) {
69 return function aff(error, success) {
70 tx.oncomplete = function oncomplete() {
70 tx.oncomplete = function oncomplete() {
71 f();
71 f();
@@ -78,7 +78,7 @@ exports._onComplete = function _onComplete(tx, f) {
78 };
78 };
79};
79};
80
80
81−exports._onError = function _onError(tx, f) {
81+export function _onError(tx, f) {
82 return function aff(error, success) {
82 return function aff(error, success) {
83 tx.onerror = function onerror(e) {
83 tx.onerror = function onerror(e) {
84 f(e.target.error)();
84 f(e.target.error)();
src/Database/IndexedDB/IDBTransaction.purs+40−59
@@ -14,19 +14,17 @@ module Database.IndexedDB.IDBTransaction
14 , onError
14 , onError
15 ) where
15 ) where
16
16
17−import Prelude (Unit, ($), (<<<), (>>>))
18−
19−import Control.Monad.Aff (Aff)
20−import Control.Monad.Aff.Compat (EffFnAff, fromEffFnAff)
21−import Control.Monad.Eff (Eff)
22−import Control.Monad.Eff.Exception (Error)
23−import Data.Function.Uncurried as Fn
24−import Data.Function.Uncurried (Fn2, Fn4)
25−import Data.Maybe (Maybe)
26−import Data.Nullable (Nullable, toMaybe)
27−
28import Database.IndexedDB.Core
17import Database.IndexedDB.Core
29
18
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, ($), (<<<), (>>>))
30
28
31--------------------
29--------------------
32-- INTERFACES
30-- INTERFACES
@@ -35,21 +33,22 @@ import Database.IndexedDB.Core
35-- | Aborts the transaction. All pending requests will fail with a "AbortError"
33-- | Aborts the transaction. All pending requests will fail with a "AbortError"
36-- | DOMException and all changes made to the database will be reverted.
34-- | DOMException and all changes made to the database will be reverted.
37abort
35abort
36+ :: forall tx
38− :: forall e tx. (IDBTransaction tx)
37+ . (IDBTransaction tx)
39 => tx
38 => tx
40− -> Aff (idb :: IDB | e) Unit
39+ -> Aff Unit
41abort =
40abort =
42− fromEffFnAff <<< _abort
41+ fromEffectFnAff <<< _abort
43
42
44-- | Returns an IDBObjectStore in the transaction's scope.
43-- | Returns an IDBObjectStore in the transaction's scope.
45objectStore
44objectStore
45+ :: forall tx
46− :: forall e tx. (IDBTransaction tx)
46+ . (IDBTransaction tx)
47 => tx
47 => tx
48 -> String
48 -> String
49− -> Aff (idb :: IDB | e) ObjectStore
49+ -> Aff ObjectStore
50objectStore tx name =
50objectStore tx name =
51− fromEffFnAff $ Fn.runFn2 _objectStore tx name
51+ fromEffectFnAff $ Fn.runFn2 _objectStore tx name
52−
53
52
54--------------------
53--------------------
55-- ATTRIBUTES
54-- ATTRIBUTES
@@ -62,7 +61,6 @@ db
62db =
61db =
63 _db
62 _db
64
63
65−
66-- | If the transaction was aborted, returns the error (a DOMException) providing the reason.
64-- | If the transaction was aborted, returns the error (a DOMException) providing the reason.
67error
65error
68 :: Transaction
66 :: Transaction
@@ -70,7 +68,6 @@ error
70error =
68error =
71 _error >>> toMaybe
69 _error >>> toMaybe
72
70
73−
74-- | Returns the mode the transaction was created with (`ReadOnly|ReadWrite`)
71-- | Returns the mode the transaction was created with (`ReadOnly|ReadWrite`)
75-- | , or `VersionChange` for an upgrade transaction.
72-- | , or `VersionChange` for an upgrade transaction.
76mode
73mode
@@ -79,7 +76,6 @@ mode
79mode =
76mode =
80 Fn.runFn4 _mode ReadOnly ReadWrite VersionChange
77 Fn.runFn4 _mode ReadOnly ReadWrite VersionChange
81
78
82−
83-- | Returns a list of the names of object stores in the transaction’s scope.
79-- | Returns a list of the names of object stores in the transaction’s scope.
84-- | For an upgrade transaction this is all object stores in the database.
80-- | For an upgrade transaction this is all object stores in the database.
85objectStoreNames
81objectStoreNames
@@ -88,85 +84,70 @@ objectStoreNames
88objectStoreNames =
84objectStoreNames =
89 _objectStoreNames
85 _objectStoreNames
90
86
91−
92--------------------
87--------------------
93-- EVENT HANDLERS
88-- EVENT HANDLERS
94--
89--
95
90
96-- | Event handler for the `abort` event.
91-- | Event handler for the `abort` event.
97onAbort
92onAbort
98− :: forall e e'
99− . Transaction
93+ :: Transaction
100− -> Eff ( | e') Unit
94+ -> Effect Unit
101− -> Aff (idb :: IDB | e) Unit
95+ -> Aff Unit
102onAbort db' f =
96onAbort db' f =
103− fromEffFnAff $ Fn.runFn2 _onAbort db' f
97+ fromEffectFnAff $ Fn.runFn2 _onAbort db' f
104−
105
98
106-- | Event handler for the `complete` event.
99-- | Event handler for the `complete` event.
107onComplete
100onComplete
108− :: forall e e'
109− . Transaction
101+ :: Transaction
110− -> Eff ( | e') Unit
102+ -> Effect Unit
111− -> Aff (idb :: IDB | e) Unit
103+ -> Aff Unit
112onComplete db' f =
104onComplete db' f =
113− fromEffFnAff $ Fn.runFn2 _onComplete db' f
105+ fromEffectFnAff $ Fn.runFn2 _onComplete db' f
114−
115
106
116-- | Event handler for the `error` event.
107-- | Event handler for the `error` event.
117onError
108onError
118− :: forall e e'
119− . Transaction
109+ :: Transaction
120− -> (Error -> Eff ( | e') Unit)
110+ -> (Error -> Effect Unit)
121− -> Aff (idb :: IDB | e) Unit
111+ -> Aff Unit
122onError db' f =
112onError db' f =
123− fromEffFnAff $ Fn.runFn2 _onError db' f
113+ fromEffectFnAff $ Fn.runFn2 _onError db' f
124−
125
114
126--------------------
115--------------------
127-- FFI
116-- FFI
128--
117--
129
118
130foreign import _abort
119foreign import _abort
131− :: forall tx e
120+ :: forall tx
132− . tx
121+ . tx
133− -> EffFnAff (idb :: IDB | e) Unit
122+ -> EffectFnAff Unit
134−
135
123
136foreign import _db
124foreign import _db
137 :: Transaction
125 :: Transaction
138 -> Database
126 -> Database
139
127
140−
141foreign import _error
128foreign import _error
142 :: Transaction
129 :: Transaction
143 -> (Nullable Error)
130 -> (Nullable Error)
144
131
145−
146foreign import _mode
132foreign import _mode
147 :: Fn4 TransactionMode TransactionMode TransactionMode Transaction TransactionMode
133 :: Fn4 TransactionMode TransactionMode TransactionMode Transaction TransactionMode
148
134
149−
150foreign import _objectStoreNames
135foreign import _objectStoreNames
151 :: Transaction
136 :: Transaction
152 -> Array String
137 -> Array String
153
138
154−
155foreign import _objectStore
139foreign import _objectStore
156− :: forall tx e
140+ :: forall tx
157− . Fn2 tx String (EffFnAff (idb :: IDB | e) ObjectStore)
141+ . Fn2 tx String (EffectFnAff ObjectStore)
158−
159
142
160foreign import _onAbort
143foreign import _onAbort
161− :: forall tx e e'
144+ :: forall tx
162− . Fn2 tx (Eff ( | e') Unit) (EffFnAff (idb :: IDB | e) Unit)
145+ . Fn2 tx (Effect Unit) (EffectFnAff Unit)
163−
164
146
165foreign import _onComplete
147foreign import _onComplete
166− :: forall tx e e'
148+ :: forall tx
167− . Fn2 tx (Eff ( | e') Unit) (EffFnAff (idb :: IDB | e) Unit)
149+ . Fn2 tx (Effect Unit) (EffectFnAff Unit)
168−
169
150
170foreign import _onError
151foreign import _onError
171− :: forall tx e e'
152+ :: forall tx
172− . Fn2 tx (Error -> Eff ( | e') Unit) (EffFnAff (idb :: IDB | e) Unit)
153+ . Fn2 tx (Error -> Effect Unit) (EffectFnAff Unit)
test.htmlnew file+25
@@ -0,0 +1,25 @@
1+<!DOCTYPE html>
2+<html lang="en">
3+
4+<head>
5+ <meta charset="UTF-8">
6+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
7+ <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
8+ <link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css">
9+</head>
10+
11+<body>
12+ <div id="mocha"></div>
13+ <script src="https://unpkg.com/chai/chai.js"></script>
14+ <script src="https://unpkg.com/mocha/mocha.js"></script>
15+ <script class="mocha-init">
16+ mocha.setup('bdd');
17+ mocha.checkLeaks();
18+ </script>
19+ <script src="test.js"></script>
20+ <script>
21+ mocha.run()
22+ </script>
23+</body>
24+
25+</html>
test/Main.purs+1099−1039
@@ -2,1079 +2,1139 @@ module Test.Main where
2
2
3import Prelude
3import Prelude
4
4
5−import Control.Monad.Aff (Aff, launchAff, launchAff_, forkAff, delay, attempt)
5+import Data.Array (head, drop)
6−import Control.Monad.Aff.AVar (AVAR, AVar, makeVar, makeEmptyVar, readVar, 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, name)
11−import Control.Monad.Eff.Now (NOW, now)
12−import Data.Date as Date
6+import Data.Date as Date
13−import Data.DateTime as DateTime
7+import Data.DateTime (DateTime(..), Time(..))
14−import Data.DateTime (DateTime(..), Time(..))
15−import Data.DateTime.Instant (toDateTime)
8+import Data.DateTime.Instant (toDateTime)
16−import Data.Either (Either(..))
9+import Data.Either (Either(..))
17−import Data.Enum (toEnum)
10+import Data.Enum (toEnum)
18−import Data.Maybe (Maybe(..), isNothing, maybe)
11+import Data.Maybe (Maybe(..), isNothing, maybe)
19−import Data.Array (head, drop)
20−import Data.Time.Duration (Milliseconds(..))
12+import Data.Time.Duration (Milliseconds(..))
21−import Data.Traversable (traverse)
13+import Data.Traversable (traverse)
22−import Data.Tuple (Tuple(..), uncurry)
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)
23−import Test.Spec (describe, describeOnly, it)
32+import Test.Spec (describe, it)
24−import Test.Spec.Assertions (shouldEqual, fail)
33+import Test.Spec.Assertions (shouldEqual, fail)
25−import Test.Spec.Mocha (MOCHA, runMocha)
34+import Test.Spec.Mocha (runMocha)
26−
27−import Database.IndexedDB.Core
28−import Database.IndexedDB.IDBKey
29−import Database.IndexedDB.IDBCursor as IDBCursor
30−import Database.IndexedDB.IDBDatabase as IDBDatabase
31−import Database.IndexedDB.IDBFactory as IDBFactory
32−import Database.IndexedDB.IDBIndex as IDBIndex
33−import Database.IndexedDB.IDBKeyRange as IDBKeyRange
34−import Database.IndexedDB.IDBObjectStore as IDBObjectStore
35−import Database.IndexedDB.IDBTransaction as IDBTransaction
36−
37
35
38infixr 7 Tuple as :+:
36infixr 7 Tuple as :+:
39
37
40−launchAff' :: forall a e. Aff e a -> Eff e Unit
38+launchAff' :: forall a. Aff a -> Effect Unit
41launchAff' =
39launchAff' =
42− launchAff_
40+ launchAff_ <<< void
41+
42+modifyAVar :: forall a. (a -> a) -> AVar a -> Aff Unit
43+modifyAVar fn v = do
44+ val <- AVar.take v
45+ AVar.put (fn val) v
46+
47+main :: Effect Unit
48+main =
49+ runMocha
50+ do
51+ describe "IDBFactory" do
52+ let
53+ tearDown name version db = do
54+ IDBDatabase.close db
55+ version' <- IDBFactory.deleteDatabase name
56+ liftEffect $ log $ show version'
57+ version' `shouldEqual` version
58+
59+ it "open default" do
60+ let
61+ name = "db-default"
62+ 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
69+
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
80+
81+ it "open specific version -> close -> open latest" do
82+ let
83+ name = "db-latest"
84+ 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
96+
97+ it "open + onUpgradeNeed" do
98+ let
99+ name = "db-upgrade-needed"
100+ version = 1
101+ callback (Tuple varName varVersion) db _ { oldVersion } = do
102+ _ <- launchAff $ modifyAVar (const $ IDBDatabase.name db) varName
103+ _ <- launchAff $ modifyAVar (const $ oldVersion) varVersion
104+ 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"
120+ version = 14
121+ callback var = do
122+ _ <- launchAff $ modifyAVar (const $ "db-blocked") var
123+ pure unit
124+
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
43
133
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
146+ range = IDBKeyRange.only key
147+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
44
148
45−modifyVar :: forall eff a. (a -> a) -> AVar a -> Aff (avar :: AVAR | eff) Unit
149+ it "only(string)" do
46−modifyVar fn v = do
150+ let
47− val <- takeVar v
151+ key = "patate"
48− putVar (fn val) v
152+ range = IDBKeyRange.only key
153+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
49
154
155+ it "only(float)" do
156+ let
157+ key = 14.42
158+ range = IDBKeyRange.only key
159+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
50
160
51−-- main :: forall eff. Eff (now :: NOW, mocha :: MOCHA, idb :: IDB, exception :: EXCEPTION, avar :: AVAR | eff) Unit
161+ it "only(date)" do
52−main = runMocha do
162+ let
53− describe "IDBFactory" do
163+ mkey = DateTime
54− let
164+ <$> (Date.canonicalDate <$> toEnum 2017 <*> toEnum 6 <*> toEnum 23)
55− tearDown name version db = do
165+ <*> (Time <$> toEnum 17 <*> toEnum 59 <*> toEnum 34 <*> toEnum 42)
56− IDBDatabase.close db
166+
57− version' <- IDBFactory.deleteDatabase name
167+ case mkey of
58− version' `shouldEqual` version
59−
60− it "open default" do
61− let name = "db-default"
62− 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
69−
70−
71− it "open specific version" do
72− let 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
80−
81−
82− it "open specific version -> close -> open latest" do
83− let name = "db-latest"
84− 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
96−
97− it "open + onUpgradeNeed" do
98− let name = "db-upgrade-needed"
99− version = 1
100− callback (Tuple varName varVersion) db _ { oldVersion } = do
101− _ <- launchAff $ modifyVar (const $ IDBDatabase.name db) varName
102− _ <- launchAff $ modifyVar (const $ oldVersion) varVersion
103− pure unit
104− varName <- makeVar "-"
105− varVersion <- makeVar (-1)
106− db <- IDBFactory.open name Nothing
107− { onUpgradeNeeded : Just (callback (Tuple varName varVersion))
108− , onBlocked : Nothing
168+ Nothing ->
109− }
169+ fail "unable to create datetime"
110− name' <- readVar varName
170+ Just key -> do
111− version' <- readVar varVersion
171+ let range = IDBKeyRange.only key
112− name' `shouldEqual` name
172+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
113− version' `shouldEqual` 0
173+
114− tearDown name version db
174+ it "only([int])" do
115−
175+ let
116− it "open + onBlocked" do
176+ key = [ 14, 42 ]
117− let name = "db-blocked"
177+ range = IDBKeyRange.only key
118− version = 14
178+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
119− callback var = do
120− _ <- launchAff $ modifyVar (const $ "db-blocked") var
121− pure unit
122
179
123− var <- makeVar "-"
124− db01 <- IDBFactory.open name Nothing
125− { onUpgradeNeeded : Nothing
126− , onBlocked : Nothing
127− }
128− _ <- forkAff do
129− delay (Milliseconds 100.0)
130− IDBDatabase.close db01
131−
132− db02 <- IDBFactory.open name (Just version)
133− { onUpgradeNeeded : Nothing
134− , onBlocked : Just (callback var)
135− }
136− name' <- readVar var
137− name' `shouldEqual` name
138− tearDown name version db02
139−
140− describe "IDBKeyRange" do
141− it "only(int)" do
142− let key = 14
143− range = IDBKeyRange.only key
144− IDBKeyRange.includes range (toKey key) `shouldEqual` true
145−
146− it "only(string)" do
180+ it "only([string])" do
181+ let
147− let key = "patate"
182+ key = [ "patate", "autruche" ]
148− range = IDBKeyRange.only key
149− IDBKeyRange.includes range (toKey key) `shouldEqual` true
150−
151− it "only(float)" do
152− let key = 14.42
153− range = IDBKeyRange.only key
154− IDBKeyRange.includes range (toKey key) `shouldEqual` true
155−
156− it "only(date)" do
157− let mkey = DateTime
158− <$> (Date.canonicalDate <$> toEnum 2017 <*> toEnum 6 <*> toEnum 23)
159− <*> (Time <$> toEnum 17 <*> toEnum 59 <*> toEnum 34 <*> toEnum 42)
160−
161− case mkey of
162− Nothing ->
163− fail "unable to create datetime"
164− Just key -> do
165− let range = IDBKeyRange.only key
183+ range = IDBKeyRange.only key
166 IDBKeyRange.includes range (toKey key) `shouldEqual` true
184 IDBKeyRange.includes range (toKey key) `shouldEqual` true
167
185
168− it "only([int])" do
186+ it "lowerBound(14, open)" do
169− let key = [14, 42]
187+ let
170− range = IDBKeyRange.only key
188+ key = 14
171− IDBKeyRange.includes range (toKey key) `shouldEqual` true
172−
173− it "only([string])" do
174− let key = ["patate", "autruche"]
175− range = IDBKeyRange.only key
176− IDBKeyRange.includes range (toKey key) `shouldEqual` true
177−
178− it "lowerBound(14, open)" do
179− let key = 14
180− open = true
189+ open = true
181− range = IDBKeyRange.lowerBound key open
190+ range = IDBKeyRange.lowerBound key open
182− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
191+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
183− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
192+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
184− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
193+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
185−
194+
186− it "lowerBound(14, close)" do
195+ it "lowerBound(14, close)" do
196+ let
187− let key = 14
197+ key = 14
188− open = false
198+ open = false
189− range = IDBKeyRange.lowerBound key open
199+ range = IDBKeyRange.lowerBound key open
190− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
200+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
191− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
201+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
192− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
202+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
193−
203+
194− it "upperBound(14, open)" do
204+ it "upperBound(14, open)" do
195− let key = 14
205+ let
206+ key = 14
196− open = true
207+ open = true
197− range = IDBKeyRange.upperBound key open
208+ range = IDBKeyRange.upperBound key open
198− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
209+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
199− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
210+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
200− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
211+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
201−
212+
202− it "upperBound(14, close)" do
213+ it "upperBound(14, close)" do
203− let key = 14
214+ let
215+ key = 14
204− open = false
216+ open = false
205− range = IDBKeyRange.upperBound key open
217+ range = IDBKeyRange.upperBound key open
206− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
218+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
207− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
219+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
208− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
220+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
209−
221+
210− it "bound(42, 14, open, open) => Nothing" do
222+ it "bound(42, 14, open, open) => Nothing" do
223+ let
211− let lower = 42
224+ lower = 42
212− upper = 14
225+ upper = 14
213− lowerOpen = true
226+ lowerOpen = true
214− upperOpen = true
227+ upperOpen = true
215− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
228+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
216− isNothing mrange `shouldEqual` true
229+ isNothing mrange `shouldEqual` true
217−
230+
218− it "bound(14, 42, open, open)" do
231+ it "bound(14, 42, open, open)" do
219− let lower = 14
232+ let
233+ lower = 14
220− upper = 42
234+ upper = 42
221− lowerOpen = true
235+ lowerOpen = true
222− upperOpen = true
236+ upperOpen = true
223− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
237+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
224− case mrange of
238+ case mrange of
225− Nothing ->
239+ Nothing ->
226− fail "invalid range provided"
240+ fail "invalid range provided"
227− Just range -> do
241+ Just range -> do
228− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
242+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
229− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
243+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
230− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
244+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
231− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
245+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
232− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
246+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
233− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
247+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
234−
248+
235− it "bound(14, 42, open, close)" do
249+ it "bound(14, 42, open, close)" do
236− let lower = 14
250+ let
251+ lower = 14
237− upper = 42
252+ upper = 42
238− lowerOpen = true
253+ lowerOpen = true
239− upperOpen = false
254+ upperOpen = false
240− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
255+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
241− case mrange of
256+ case mrange of
242− Nothing ->
257+ Nothing ->
243− fail "invalid range provided"
258+ fail "invalid range provided"
244− Just range -> do
259+ Just range -> do
245− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
260+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
246− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
261+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
247− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
262+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
248− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
263+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
249− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
264+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
250− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
265+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
251−
266+
252− it "bound(14, 42, close, open)" do
267+ it "bound(14, 42, close, open)" do
253− let lower = 14
268+ let
269+ lower = 14
254− upper = 42
270+ upper = 42
255− lowerOpen = false
271+ lowerOpen = false
256− upperOpen = true
272+ upperOpen = true
257− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
273+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
258− case mrange of
274+ case mrange of
259− Nothing ->
275+ Nothing ->
260− fail "invalid range provided"
276+ fail "invalid range provided"
261− Just range -> do
277+ Just range -> do
262− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
278+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
263− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
279+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
264− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
280+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
265− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
281+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
266− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
282+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
267− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
283+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
268−
284+
269− it "bound(14, 42, close, close)" do
285+ it "bound(14, 42, close, close)" do
270− let lower = 14
286+ let
287+ lower = 14
271− upper = 42
288+ upper = 42
272− lowerOpen = false
289+ lowerOpen = false
273− upperOpen = false
290+ upperOpen = false
274− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
291+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
275− case mrange of
292+ case mrange of
276− Nothing ->
293+ Nothing ->
277− fail "invalid range provided"
294+ fail "invalid range provided"
278− Just range -> do
295+ Just range -> do
279− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
296+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
280− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
297+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
281− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
298+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
282− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
299+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
283− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
300+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
284− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
301+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
285−
302+
286− it "can access attributes of a range" do
303+ it "can access attributes of a range" do
287− let range = IDBKeyRange.lowerBound 14 false
304+ let range = IDBKeyRange.lowerBound 14 false
288− IDBKeyRange.lower range `shouldEqual` (Just $ toKey 14)
305+ IDBKeyRange.lower range `shouldEqual` (Just $ toKey 14)
289− IDBKeyRange.upper range `shouldEqual` none
306+ IDBKeyRange.upper range `shouldEqual` none
290− IDBKeyRange.lowerOpen range `shouldEqual` false
307+ IDBKeyRange.lowerOpen range `shouldEqual` false
291− IDBKeyRange.upperOpen range `shouldEqual` true
308+ IDBKeyRange.upperOpen range `shouldEqual` true
292−
309+
293− describe "IDBDatabase" do
310+ describe "IDBDatabase" do
294− let
311+ let
295− tearDown db = do
312+ tearDown db = do
296− IDBDatabase.close db
313+ IDBDatabase.close db
297− _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
314+ _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
298− pure unit
315+ pure unit
299
316
300− setup storeParams = do
317+ setup storeParams = do
318+ let
301− let onUpgradeNeeded var db _ _ = launchAff' do
319+ onUpgradeNeeded var db _ _ = launchAff' do
302 store <- IDBDatabase.createObjectStore db "store" storeParams
320 store <- IDBDatabase.createObjectStore db "store" storeParams
303− _ <- putVar { db, store } var
321+ _ <- AVar.put { db, store } var
304 pure unit
322 pure unit
305
323
306− var <- makeEmptyVar
324+ var <- AVar.empty
307− db <- IDBFactory.open "db" Nothing
325+ _ <- IDBFactory.open "db" Nothing
308− { onUpgradeNeeded : Just (onUpgradeNeeded var)
326+ { onUpgradeNeeded: Just (onUpgradeNeeded var)
309− , onBlocked : Nothing
327+ , onBlocked: Nothing
310− }
328+ }
311−
329+
312− takeVar var
330+ AVar.take var
313−
331+
314− it "createObjectStore (keyPath: [], autoIncrement: true)" do
332+ it "createObjectStore (keyPath: [], autoIncrement: true)" do
315− { db, store } <- setup { keyPath: [], autoIncrement: true }
333+ { db, store } <- setup { keyPath: [], autoIncrement: true }
316− IDBObjectStore.name store `shouldEqual` "store"
334+ IDBObjectStore.name store `shouldEqual` "store"
317− IDBObjectStore.keyPath store `shouldEqual` []
335+ IDBObjectStore.keyPath store `shouldEqual` []
318− IDBObjectStore.autoIncrement store `shouldEqual` true
336+ IDBObjectStore.autoIncrement store `shouldEqual` true
319− IDBObjectStore.indexNames store `shouldEqual` []
337+ IDBObjectStore.indexNames store `shouldEqual` []
320− tearDown db
338+ tearDown db
321−
339+
322− it "createObjectStore (keyPath: [\"patate\"], autoIncrement: true)" do
340+ it "createObjectStore (keyPath: [\"patate\"], autoIncrement: true)" do
323− { db, store } <- setup { keyPath: ["patate"], autoIncrement: true }
341+ { db, store } <- setup { keyPath: [ "patate" ], autoIncrement: true }
324− IDBObjectStore.name store `shouldEqual` "store"
342+ IDBObjectStore.name store `shouldEqual` "store"
325− IDBObjectStore.keyPath store `shouldEqual` ["patate"]
343+ IDBObjectStore.keyPath store `shouldEqual` [ "patate" ]
326− IDBObjectStore.autoIncrement store `shouldEqual` true
344+ IDBObjectStore.autoIncrement store `shouldEqual` true
327− IDBObjectStore.indexNames store `shouldEqual` []
345+ IDBObjectStore.indexNames store `shouldEqual` []
328− tearDown db
346+ tearDown db
329−
347+
330− it "createObjectStore (keyPath: [\"a\", \"b\"], autoIncrement: false)" do
348+ it "createObjectStore (keyPath: [\"a\", \"b\"], autoIncrement: false)" do
331− { db, store } <- setup { keyPath: ["patate", "autruche"], autoIncrement: false }
349+ { db, store } <- setup { keyPath: [ "patate", "autruche" ], autoIncrement: false }
332− IDBObjectStore.name store `shouldEqual` "store"
350+ IDBObjectStore.name store `shouldEqual` "store"
333− IDBObjectStore.keyPath store `shouldEqual` ["patate", "autruche"]
351+ IDBObjectStore.keyPath store `shouldEqual` [ "patate", "autruche" ]
334− IDBObjectStore.autoIncrement store `shouldEqual` false
352+ IDBObjectStore.autoIncrement store `shouldEqual` false
335− IDBObjectStore.indexNames store `shouldEqual` []
353+ IDBObjectStore.indexNames store `shouldEqual` []
336− tearDown db
354+ tearDown db
337−
355+
338− it "deleteObjectStore" do
356+ it "deleteObjectStore" do
357+ let
339− let onUpgradeNeeded var db _ _ = launchAff' do
358+ onUpgradeNeeded var db _ _ = launchAff' do
340− _ <- IDBDatabase.deleteObjectStore db "store"
359+ _ <- IDBDatabase.deleteObjectStore db "store"
341− putVar true var
360+ AVar.put true var
342−
361+
343− var <- makeEmptyVar
362+ var <- AVar.empty
344− { db, store } <- setup IDBDatabase.defaultParameters
363+ { db } <- setup IDBDatabase.defaultParameters
345− IDBDatabase.close db
346− db' <- IDBFactory.open "db" (Just 999) { onUpgradeNeeded : Just (onUpgradeNeeded var)
347− , onBlocked : Nothing
348− }
349− deleted <- takeVar var
350− deleted `shouldEqual` true
351− tearDown db'
352−
353−
354− describe "IDBObjectStore" do
355− let
356− tearDown db = do
357 IDBDatabase.close db
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'
372+
373+ describe "IDBObjectStore" do
374+ let
375+ tearDown db = do
376+ IDBDatabase.close db
358− _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
377+ _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
359− pure unit
378+ pure unit
360
379
361− setup { storeParams, onUpgradeNeeded } = do
380+ setup { storeParams, onUpgradeNeeded } = do
381+ let
362− let onUpgradeNeeded' var db _ _ = launchAff' do
382+ onUpgradeNeeded' var db _ _ = launchAff' do
363 store <- IDBDatabase.createObjectStore db "store" storeParams
383 store <- IDBDatabase.createObjectStore db "store" storeParams
364− liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store)
384+ liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure store)
365− putVar { db, store } var
385+ AVar.put { db, store } var
366−
386+
367− var <- makeEmptyVar
387+ var <- AVar.empty
368− db <- IDBFactory.open "db" Nothing
388+ _ <- IDBFactory.open "db" Nothing
369− { onUpgradeNeeded : Just (onUpgradeNeeded' var)
389+ { onUpgradeNeeded: Just (onUpgradeNeeded' var)
370− , onBlocked : Nothing
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''
411+
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'''''
371 }
423 }
372−
424+ tearDown db
373− takeVar var
425+
374−
375− it "add()" do
376− date <- liftEff $ toDateTime <$> now
377− { db } <- setup
378− { storeParams: { autoIncrement: true, keyPath: [] }
379− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
380− -- no key
381− key <- IDBObjectStore.add store "patate" none
382− (toKey 1) `shouldEqual` key
383−
384− -- int key
385− key' <- IDBObjectStore.add store "patate" (Just 14)
386− (toKey 14) `shouldEqual` key'
387−
388− -- number key
389− key'' <- IDBObjectStore.add store "patate" (Just 14.42)
390− (toKey 14.42) `shouldEqual` key''
391−
392− -- string key
393− key''' <- IDBObjectStore.add store "patate" (Just "key")
394− (toKey "key") `shouldEqual` key'''
395−
396− -- date key
397− key'''' <- IDBObjectStore.add store "patate" (Just date)
398− (toKey date) `shouldEqual` key''''
399−
400− -- array key
401− key''''' <- IDBObjectStore.add store "patate" (Just $ toKey [14, 42])
402− (toKey [14, 42]) `shouldEqual` key'''''
403− }
404− tearDown db
405−
406− it "clear()" do
426+ it "clear()" do
407− { db } <- setup
427+ { db } <- setup
408− { storeParams: IDBDatabase.defaultParameters
428+ { storeParams: IDBDatabase.defaultParameters
409− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
429+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
410− key <- IDBObjectStore.add store "patate" (Just 14)
430+ key <- IDBObjectStore.add store "patate" (Just 14)
411− _ <- IDBObjectStore.clear store
431+ _ <- IDBObjectStore.clear store
412− val <- IDBObjectStore.get store (IDBKeyRange.only key)
432+ val <- IDBObjectStore.get store (IDBKeyRange.only key)
413− val `shouldEqual` (Nothing :: Maybe String)
433+ val `shouldEqual` (Nothing :: Maybe String)
414− }
415− tearDown db
416−
417− it "count()" do
418− { db } <- setup
419− { storeParams: IDBDatabase.defaultParameters
420− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
421− _ <- IDBObjectStore.add store "patate" (Just 14)
422− _ <- IDBObjectStore.add store "autruche" (Just 42)
423− n <- IDBObjectStore.count store Nothing
424− n `shouldEqual` 2
425− }
426− tearDown db
427−
428− it "getKey()" do
429− { db } <- setup
430− { storeParams: IDBDatabase.defaultParameters
431− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
432− key <- IDBObjectStore.add store "patate" (Just 14)
433− mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14)
434− mkey `shouldEqual` (Just key)
435−
436− mkey' <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
437− mkey' `shouldEqual` none
438− }
439− tearDown db
440−
441− it "getAllKeys()" do
442− { db } <- setup
443− { storeParams: IDBDatabase.defaultParameters
444− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
445− key1 <- IDBObjectStore.add store "patate" (Just 14)
446− key2 <- IDBObjectStore.add store "autruche" (Just 42)
447− key3 <- IDBObjectStore.add store 14 (Just 1337)
448−
449− -- no bounds
450− keys <- IDBObjectStore.getAllKeys store Nothing Nothing
451− keys `shouldEqual` [key1, key2, key3]
452−
453− -- lower bound
454− keys' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 14 true) Nothing
455− keys' `shouldEqual` [key2, key3]
456−
457− -- upper bound
458− keys'' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.upperBound 42 false) Nothing
459− keys'' `shouldEqual` [key1, key2]
460−
461− -- count
462− keys''' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
463− keys''' `shouldEqual` [key1, key2]
464− }
465− tearDown db
466−
467− it "openCursor()" do
468− let cb =
469− { onSuccess : const $ pure unit
470− , onError : show >>> fail >>> launchAff'
471− , onComplete: pure unit
472 }
434 }
435+ tearDown db
436+
437+ it "count()" do
473− { db } <- setup
438+ { db } <- setup
474− { storeParams: IDBDatabase.defaultParameters
439+ { storeParams: IDBDatabase.defaultParameters
475− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
440+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
476− _ <- IDBObjectStore.openCursor store Nothing Next cb
477− _ <- IDBObjectStore.openCursor store Nothing NextUnique cb
441+ _ <- IDBObjectStore.add store "patate" (Just 14)
478− _ <- IDBObjectStore.openCursor store Nothing Prev cb
442+ _ <- IDBObjectStore.add store "autruche" (Just 42)
479− _ <- IDBObjectStore.openCursor store Nothing PrevUnique cb
443+ n <- IDBObjectStore.count store Nothing
480− _ <- IDBObjectStore.openCursor store (Just $ IDBKeyRange.upperBound 1 true) Next cb
444+ n `shouldEqual` 2
481− pure unit
482− }
483− tearDown db
484−
485− it "openKeyCursor()" do
486− let cb =
487− { onSuccess : const $ pure unit
488− , onError : show >>> fail >>> launchAff'
489− , onComplete: pure unit
490 }
445 }
491− { db } <- setup
446+ tearDown db
492− { storeParams: IDBDatabase.defaultParameters
447+
493− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
448+ it "getKey()" do
494− _ <- IDBObjectStore.openKeyCursor store Nothing Next cb
449+ { db } <- setup
495− _ <- IDBObjectStore.openKeyCursor store Nothing NextUnique cb
450+ { storeParams: IDBDatabase.defaultParameters
496− _ <- IDBObjectStore.openKeyCursor store Nothing Prev cb
451+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
497− _ <- IDBObjectStore.openKeyCursor store Nothing PrevUnique cb
452+ key <- IDBObjectStore.add store "patate" (Just 14)
498− _ <- IDBObjectStore.openKeyCursor store (Just $ IDBKeyRange.lowerBound 1 true) Next cb
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
524+
525+ describe "IDBIndex" do
526+ let
527+ tearDown db = do
528+ IDBDatabase.close db
529+ _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
499 pure unit
530 pure unit
500− }
501− tearDown db
502
531
503− describe "IDBIndex" do
532+ setup
504− let
505− tearDown db = do
506− IDBDatabase.close db
507− _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
508− pure unit
509−
510− setup :: forall value e e'.
533+ :: forall value
511− { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
534+ . { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
512− , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
535+ , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
513− , values :: Array (Tuple value (Maybe Key))
536+ , values :: Array (Tuple value (Maybe Key))
514− , keyPath :: Array String
537+ , keyPath :: Array String
515− , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
538+ , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Effect Unit)
539+ }
516− } -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
540+ -> Aff { db :: Database, index :: Index, store :: ObjectStore }
517− setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
541+ setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
542+ let
518− let onUpgradeNeeded' var db tx _ = launchAff' do
543+ onUpgradeNeeded' var db tx _ = launchAff' do
519 store <- IDBDatabase.createObjectStore db "store" storeParams
544 store <- IDBDatabase.createObjectStore db "store" storeParams
520− _ <- traverse (uncurry (IDBObjectStore.add store)) values
545+ _ <- traverse (uncurry (IDBObjectStore.add store)) values
521 index <- IDBObjectStore.createIndex store "index" keyPath indexParams
546 index <- IDBObjectStore.createIndex store "index" keyPath indexParams
522− liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
547+ liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
523− putVar { db, index, store } var
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+ }
524
763
525− var <- makeEmptyVar
764+ tearDown db
526− db <- IDBFactory.open "db" Nothing
765+
527− { onUpgradeNeeded : Just (onUpgradeNeeded' var)
766+ it "openCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
528− , onBlocked : Nothing
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"
529 }
788 }
530− takeVar var
531−
532−
533− it "returns an IDBIndex and the properties are set correctly" do
534− { db, index } <- setup
535− { storeParams : IDBDatabase.defaultParameters
536− , indexParams : IDBObjectStore.defaultParameters
537− , onUpgradeNeeded : Nothing
538− , keyPath : []
539− , values : []
540− }
541− IDBIndex.name index `shouldEqual` "index"
542− IDBIndex.keyPath index `shouldEqual` []
543− IDBIndex.unique index `shouldEqual` IDBObjectStore.defaultParameters.unique
544− IDBIndex.multiEntry index `shouldEqual` IDBObjectStore.defaultParameters.multiEntry
545− tearDown db
546−
547− it "attempt to create an index that requires unique values on an object store already contains duplicates" do
548− let onAbort var = launchAff' (putVar true var)
549− txVar <- makeEmptyVar
550− dbVar <- makeEmptyVar
551− res <- attempt $ setup
552− { storeParams : IDBDatabase.defaultParameters
553− , indexParams : { unique : true
554− , multiEntry : false
555− }
556− , keyPath : ["indexedProperty"]
557− , values : [ { indexedProperty: "bar" } :+: (Just $ toKey 1)
558− , { indexedProperty: "bar" } :+: (Just $ toKey 2)
559− ]
560− , onUpgradeNeeded : Just $ \db tx _ -> launchAff' do
561− IDBTransaction.onAbort tx (onAbort txVar)
562− IDBDatabase.onAbort db (onAbort dbVar)
563− }
564− case res of
789+ case res of
565− Right _ ->
566− fail "expected abort"
790+ Right _ -> fail "expected InvalidStateError"
567− _ -> do
568− shouldEqual true =<< takeVar txVar
569− shouldEqual true =<< takeVar dbVar
570−
571−
572− it "the index is usable right after being made" do
573− { db } <- setup
574− { storeParams : { keyPath : ["key"]
575− , autoIncrement : false
576− }
577− , indexParams : IDBObjectStore.defaultParameters
578− , keyPath : ["indexedProperty"]
579− , values : [ { key: "key1", indexedProperty: "indexed_1" } :+: Nothing
580− , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing
581− , { key: "key3", indexedProperty: "indexed_3" } :+: Nothing
582− ]
583− , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
584− val <- IDBIndex.get index (IDBKeyRange.only "indexed_2")
585− ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2")
586− }
587− tearDown db
588−
589− it "empty keyPath" do
590− { db } <- setup
591− { storeParams : IDBDatabase.defaultParameters
592− , indexParams : IDBObjectStore.defaultParameters
593− , keyPath : []
594− , values : [ "object_1" :+: (Just $ toKey 1)
595− , "object_2" :+: (Just $ toKey 2)
596− , "object_3" :+: (Just $ toKey 3)
597− ]
598− , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
599− val <- IDBIndex.get index (IDBKeyRange.only "object_3")
600− val `shouldEqual` (Just "object_3")
601− }
602− tearDown db
603−
604− it "index can be valid keys [date]" do
605− date <- liftEff $ toDateTime <$> now
606− { db } <- setup
607− { storeParams : { keyPath: ["key"]
608− , autoIncrement: false
609− }
610− , indexParams : IDBObjectStore.defaultParameters
611− , keyPath : ["i"]
612− , values : [ { key: "date", i: (toKey date) } :+: Nothing
613− ]
614− , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
615− val <- IDBIndex.get index (IDBKeyRange.only date)
616− ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
617− }
618− tearDown db
619−
620− it "index can be valid keys [num]" do
621− let num = 14
622− { db } <- setup
623− { storeParams : { keyPath: ["key"]
624− , autoIncrement: false
625− }
626− , indexParams : IDBObjectStore.defaultParameters
627− , keyPath : ["i"]
628− , values : [ { key: "num", i: (toKey num) } :+: Nothing
629− ]
630− , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
631− val <- IDBIndex.get index (IDBKeyRange.only num)
632− ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
633− }
634− tearDown db
635−
636− it "index can be valid keys [array]" do
637− let array = ["patate", "autruche"]
638− { db } <- setup
639− { storeParams : { keyPath: ["key"]
640− , autoIncrement: false
641− }
642− , indexParams : IDBObjectStore.defaultParameters
643− , keyPath : ["i"]
644− , values : [ { key: "array", i: (toKey array) } :+: Nothing
645− ]
646− , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
647− val <- IDBIndex.get index (IDBKeyRange.only array)
648− ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
649− }
650− tearDown db
651−
652− it "openKeyCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
653− res <- attempt $ setup
654− { storeParams : { keyPath: ["key"], autoIncrement: false }
655− , indexParams : IDBObjectStore.defaultParameters
656− , keyPath : ["indexedProperty"]
657− , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
658− ]
659− , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
660− let cb =
661− { onSuccess : const $ pure unit
791+ _ -> pure unit
662− , onError : show >>> fail >>> launchAff'
792+
663− , onComplete: pure unit
664− }
665− IDBTransaction.onAbort tx (pure unit)
666− IDBDatabase.onAbort db (pure unit)
667− IDBTransaction.abort tx
668− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
669− case cursor of
670− Right _ -> fail "expected InvalidStateError"
671− Left err -> name err `shouldEqual` "InvalidStateError"
672− }
673− case res of
674− Right _ -> fail "expected InvalidStateError"
675− _ -> pure unit
676−
677− it "openKeyCursor() - throw TransactionInactiveError on aborted transaction" do
793+ it "openCursor() - throw TransactionInactiveError on aborted transaction" do
678− { db } <- setup
794+ { db } <- setup
679− { storeParams : { keyPath: ["key"], autoIncrement: false }
795+ { storeParams: { keyPath: [ "key" ], autoIncrement: false }
680− , indexParams : IDBObjectStore.defaultParameters
796+ , indexParams: IDBObjectStore.defaultParameters
681− , keyPath : ["indexedProperty"]
797+ , keyPath: [ "indexedProperty" ]
798+ , values:
682− , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
799+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
683− ]
800+ ]
684− , onUpgradeNeeded : Nothing
801+ , onUpgradeNeeded: Nothing
685− }
686− let cb =
687− { onSuccess : const $ pure unit
688− , onError : show >>> fail >>> launchAff'
689− , onComplete: pure unit
690 }
802 }
803+ let
804+ cb =
805+ { onSuccess: const $ pure unit
806+ , onError: show >>> fail >>> launchAff'
807+ , onComplete: pure unit
808+ }
691− tx <- IDBDatabase.transaction db ["store"] ReadOnly
809+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
692− store <- IDBTransaction.objectStore tx "store"
810+ store <- IDBTransaction.objectStore tx "store"
693− index <- IDBObjectStore.index store "index"
811+ index <- IDBObjectStore.index store "index"
694− IDBTransaction.onAbort tx (pure unit)
812+ IDBTransaction.onAbort tx (pure unit)
695− IDBTransaction.abort tx
813+ IDBTransaction.abort tx
696− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
814+ cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
697− case cursor of
815+ case cursor of
698− Right _ -> fail "expected TransactionInactiveError"
816+ Right _ -> fail "expected TransactionInactiveError"
699− Left err -> name err `shouldEqual` "TransactionInactiveError"
817+ Left err -> name err `shouldEqual` "TransactionInactiveError"
700−
818+
701− tearDown db
819+ tearDown db
702−
820+
703− it "openKeyCursor() - throw InvalidStateError when the index is deleted" do
821+ it "openCursor() - throw InvalidStateError when the index is deleted" do
704− { db } <- setup
822+ { db } <- setup
705− { storeParams : { keyPath: ["key"], autoIncrement: false }
823+ { storeParams: { keyPath: [ "key" ], autoIncrement: false }
706− , indexParams : IDBObjectStore.defaultParameters
824+ , indexParams: IDBObjectStore.defaultParameters
707− , keyPath : ["indexedProperty"]
825+ , keyPath: [ "indexedProperty" ]
826+ , values:
708− , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
827+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
709− ]
828+ ]
710− , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
829+ , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
711− let cb =
830+ let
831+ cb =
712− { onSuccess : const $ pure unit
832+ { onSuccess: const $ pure unit
713− , onError : show >>> fail >>> launchAff'
833+ , onError: show >>> fail >>> launchAff'
714− , onComplete: pure unit
834+ , onComplete: pure unit
715− }
835+ }
716− store <- IDBTransaction.objectStore tx "store"
836+ store <- IDBTransaction.objectStore tx "store"
717− IDBObjectStore.deleteIndex store "index"
837+ IDBObjectStore.deleteIndex store "index"
718− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
838+ cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
719− case cursor of
839+ case cursor of
720− Right _ -> fail "expected InvalidStateError"
840+ Right _ -> fail "expected InvalidStateError"
721− Left err -> name err `shouldEqual` "InvalidStateError"
722− }
723−
724− tearDown db
725−
726− it "openCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
727− res <- attempt $ setup
728− { storeParams : { keyPath: ["key"], autoIncrement: false }
729− , indexParams : IDBObjectStore.defaultParameters
730− , keyPath : ["indexedProperty"]
731− , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
732− ]
733− , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
734− let cb =
735− { onSuccess : const $ pure unit
736− , onError : show >>> fail >>> launchAff'
737− , onComplete: pure unit
738− }
739− IDBTransaction.onAbort tx (pure unit)
740− IDBDatabase.onAbort db (pure unit)
741− IDBTransaction.abort tx
742− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
743− case cursor of
744− Right _ -> fail "expected InvalidStateError"
745− Left err -> name err `shouldEqual` "InvalidStateError"
841+ Left err -> name err `shouldEqual` "InvalidStateError"
746− }
747− case res of
748− Right _ -> fail "expected InvalidStateError"
749− _ -> pure unit
750−
751− it "openCursor() - throw TransactionInactiveError on aborted transaction" do
752− { db } <- setup
753− { storeParams : { keyPath: ["key"], autoIncrement: false }
754− , indexParams : IDBObjectStore.defaultParameters
755− , keyPath : ["indexedProperty"]
756− , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
757− ]
758− , onUpgradeNeeded : Nothing
759− }
760− let cb =
761− { onSuccess : const $ pure unit
762− , onError : show >>> fail >>> launchAff'
763− , onComplete: pure unit
764 }
842 }
765− tx <- IDBDatabase.transaction db ["store"] ReadOnly
766− store <- IDBTransaction.objectStore tx "store"
767− index <- IDBObjectStore.index store "index"
768− IDBTransaction.onAbort tx (pure unit)
769− IDBTransaction.abort tx
770− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
771− case cursor of
772− Right _ -> fail "expected TransactionInactiveError"
773− Left err -> name err `shouldEqual` "TransactionInactiveError"
774−
775− tearDown db
776−
777− it "openCursor() - throw InvalidStateError when the index is deleted" do
778− { db } <- setup
779− { storeParams : { keyPath: ["key"], autoIncrement: false }
780− , indexParams : IDBObjectStore.defaultParameters
781− , keyPath : ["indexedProperty"]
782− , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
783− ]
784− , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
785− let cb =
786− { onSuccess : const $ pure unit
787− , onError : show >>> fail >>> launchAff'
788− , onComplete: pure unit
789− }
790− store <- IDBTransaction.objectStore tx "store"
791− IDBObjectStore.deleteIndex store "index"
792− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
793− case cursor of
794− Right _ -> fail "expected InvalidStateError"
795− Left err -> name err `shouldEqual` "InvalidStateError"
796− }
797−
798− tearDown db
799−
800− it "getKey() - multiEntry - adding keys" do
801− { db } <- setup
802− { storeParams : IDBDatabase.defaultParameters
803− , indexParams : { unique: false, multiEntry: true }
804− , keyPath : ["name"]
805− , values : [ { name: ["patate", "autruche"] } :+: (Just $ toKey 1)
806− , { name: ["bob"] } :+: (Just $ toKey 2)
807− ]
808− , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
809− key <- IDBIndex.getKey index (IDBKeyRange.only "patate")
810− key `shouldEqual` (Just $ toKey 1)
811−
812− key' <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
813− key' `shouldEqual` (Just $ toKey 1)
814−
815− key'' <- IDBIndex.getKey index (IDBKeyRange.only "bob")
816− key'' `shouldEqual` (Just $ toKey 2)
817− }
818− tearDown db
819−
820−
821− it "get() - returns the record with the first key in the range" do
822− { db } <- setup
823− { storeParams : { keyPath: ["key"], autoIncrement: false }
824− , indexParams : IDBObjectStore.defaultParameters
825− , keyPath : ["indexedProperty"]
826− , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
827− , { key: 42, indexedProperty: "autruche" } :+: Nothing
828− , { key: 1337, indexedProperty: "baguette" } :+: Nothing
829− ]
830− , onUpgradeNeeded : Nothing
831− }
832−
833− tx <- IDBDatabase.transaction db ["store"] ReadOnly
834− store <- IDBTransaction.objectStore tx "store"
835− index <- IDBObjectStore.index store "index"
836− val <- IDBIndex.get index (IDBKeyRange.lowerBound "autruche" false)
837− ((\r -> r.key) <$> val) `shouldEqual` (Just 42)
838−
839− tearDown db
840−
841− describe "IDBCursor" do
842− let
843− tearDown db = do
844− IDBDatabase.close db
845− _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
846− pure unit
847−
848− setup :: forall value e e'.
849− { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
850− , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
851− , values :: Array (Tuple value (Maybe Key))
852− , keyPath :: Array String
853− , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
854− } -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
855− setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
856− let onUpgradeNeeded' var db tx _ = launchAff' do
857− store <- IDBDatabase.createObjectStore db "store" storeParams
858− _ <- traverse (uncurry (IDBObjectStore.add store)) values
859− index <- IDBObjectStore.createIndex store "index" keyPath indexParams
860− liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
861− putVar { db, index, store } var
862
843
863− var <- makeEmptyVar
844+ tearDown db
864− db <- IDBFactory.open "db" Nothing
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+ ]
865− { onUpgradeNeeded : Just (onUpgradeNeeded' var)
855+ , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
866− , onBlocked : Nothing
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
867 }
878 }
868− takeVar var
879+
869−
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
887+
888+ describe "IDBCursor" do
889+ let
890+ tearDown db = do
891+ IDBDatabase.close db
892+ _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
893+ pure unit
894+
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 }
904+ 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+
870− it "continue() - iterate to the next record" do
920+ it "continue() - iterate to the next record" do
871− { db } <- setup
921+ { db } <- setup
872− { storeParams : IDBDatabase.defaultParameters
922+ { storeParams: IDBDatabase.defaultParameters
873− , indexParams : IDBObjectStore.defaultParameters
923+ , indexParams: IDBObjectStore.defaultParameters
874− , keyPath : []
924+ , keyPath: []
925+ , values:
875− , values : [ "cupcake" :+: (Just $ toKey 4)
926+ [ "cupcake" :+: (Just $ toKey 4)
876− , "pancake" :+: (Just $ toKey 2)
927+ , "pancake" :+: (Just $ toKey 2)
877− , "pie" :+: (Just $ toKey 1)
928+ , "pie" :+: (Just $ toKey 1)
878− , "pie" :+: (Just $ toKey 3)
929+ , "pie" :+: (Just $ toKey 3)
879− ]
930+ ]
880− , onUpgradeNeeded: Nothing
931+ , onUpgradeNeeded: Nothing
881− }
882− let cb vdone vvals =
883− { onComplete: launchAff' do
884− putVar unit vdone
885−
886− , onError: \error -> launchAff' do
887− fail $ "unexpected error: " <> show error
888−
889− , onSuccess: \cursor -> launchAff' do
890− vals <- takeVar vvals
891− pure (IDBCursor.value cursor) >>= shouldEqual (maybe "" _.v $ head vals)
892− IDBCursor.primaryKey cursor >>= shouldEqual (maybe (toKey 0) _.k $ head vals)
893− putVar (drop 1 vals) vvals
894− IDBCursor.continue cursor none
895 }
932 }
896− vdone <- makeEmptyVar
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
897− vvals <- makeVar
949+ vvals <- AVar.new
898− [ { v: "pie" , k: toKey 1 }
950+ [ { v: "pie", k: toKey 1 }
899− , { v: "pancake", k: toKey 2 }
951+ , { v: "pancake", k: toKey 2 }
900− , { v: "pie" , k: toKey 3 }
952+ , { v: "pie", k: toKey 3 }
901− , { v: "cupcake", k: toKey 4 }
953+ , { v: "cupcake", k: toKey 4 }
902− ]
954+ ]
903− tx <- IDBDatabase.transaction db ["store"] ReadOnly
955+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
904− store <- IDBTransaction.objectStore tx "store"
956+ store <- IDBTransaction.objectStore tx "store"
905− IDBObjectStore.openCursor store Nothing Next (cb vdone vvals)
957+ IDBObjectStore.openCursor store Nothing Next (cb vdone vvals)
906− takeVar vdone
958+ AVar.take vdone
907− tearDown db
959+ tearDown db
908−
960+
909− it "continue() - attempt to iterate in the wrong direction" do
961+ it "continue() - attempt to iterate in the wrong direction" do
910− { db } <- setup
962+ { db } <- setup
911− { storeParams : IDBDatabase.defaultParameters
963+ { storeParams: IDBDatabase.defaultParameters
912− , indexParams : IDBObjectStore.defaultParameters
964+ , indexParams: IDBObjectStore.defaultParameters
913− , keyPath : []
965+ , keyPath: []
966+ , values:
914− , values : [ "cupcake" :+: (Just $ toKey 4)
967+ [ "cupcake" :+: (Just $ toKey 4)
915− , "pancake" :+: (Just $ toKey 2)
968+ , "pancake" :+: (Just $ toKey 2)
916− , "pie" :+: (Just $ toKey 1)
969+ , "pie" :+: (Just $ toKey 1)
917− , "pie" :+: (Just $ toKey 3)
970+ , "pie" :+: (Just $ toKey 3)
918− ]
971+ ]
919− , onUpgradeNeeded: Nothing
972+ , onUpgradeNeeded: Nothing
920− }
921− let cb vdone =
922− { onComplete: launchAff' do
923− fail $ "shouldn't complete"
924−
925− , onError: \error -> launchAff' do
926− fail $ "unexpected error: " <> show error
927−
928− , onSuccess: \cursor -> launchAff' do
929− res <- attempt $ IDBCursor.continue cursor (Just 1)
930− case res of
931− Left err -> do
932− name err `shouldEqual` "DataError"
933− putVar unit vdone
934− Right _ -> do
935− fail "expected continue to fail"
936 }
973 }
974+ let
937− vdone <- makeEmptyVar
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"
990+ }
991+ vdone <- AVar.empty
938− tx <- IDBDatabase.transaction db ["store"] ReadOnly
992+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
939− store <- IDBTransaction.objectStore tx "store"
993+ store <- IDBTransaction.objectStore tx "store"
940− IDBObjectStore.openCursor store Nothing Next (cb vdone)
994+ IDBObjectStore.openCursor store Nothing Next (cb vdone)
941− takeVar vdone
995+ AVar.take vdone
942− tearDown db
996+ tearDown db
943−
997+
944−
945− it "advance() - iterate cursor number of times specified by count" do
998+ it "advance() - iterate cursor number of times specified by count" do
946− { db } <- setup
999+ { db } <- setup
947− { storeParams : { keyPath: ["pKey"], autoIncrement: false }
1000+ { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
948− , indexParams : IDBObjectStore.defaultParameters
1001+ , indexParams: IDBObjectStore.defaultParameters
949− , keyPath : []
1002+ , keyPath: []
1003+ , values:
950− , values : [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1004+ [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
951− , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
1005+ , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
952− , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
1006+ , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
953− , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
1007+ , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
954− ]
1008+ ]
955− , onUpgradeNeeded: Nothing
1009+ , onUpgradeNeeded: Nothing
956− }
957− let cb vdone vjump =
958− { onComplete: launchAff' do
959− putVar unit vdone
960−
961− , onError: \error -> launchAff' do
962− fail $ "unexpected error: " <> show error
963−
964− , onSuccess: \cursor -> launchAff' do
965− jump <- takeVar vjump
966− if jump
967− then do
968− IDBCursor.advance cursor 3
969− else do
970− let value = IDBCursor.value cursor
971− value.pKey `shouldEqual` "pkey_3"
972− value.iKey `shouldEqual` "ikey_3"
973− IDBCursor.continue cursor none
974− putVar false vjump
975 }
1010 }
976− vdone <- makeEmptyVar
1011+ let
977− vjump <- makeVar true
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
978− tx <- IDBDatabase.transaction db ["store"] ReadOnly
1032+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
979− store <- IDBTransaction.objectStore tx "store"
1033+ store <- IDBTransaction.objectStore tx "store"
980− IDBObjectStore.openCursor store Nothing Next (cb vdone vjump)
1034+ IDBObjectStore.openCursor store Nothing Next (cb vdone vjump)
981− takeVar vdone
1035+ AVar.take vdone
982− tearDown db
1036+ tearDown db
983−
1037+
984− it "delete() - remove a record from the object store" do
1038+ it "delete() - remove a record from the object store" do
985− { db } <- setup
1039+ { db } <- setup
986− { storeParams : { keyPath: ["pKey"], autoIncrement: false }
1040+ { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
987− , indexParams : IDBObjectStore.defaultParameters
1041+ , indexParams: IDBObjectStore.defaultParameters
988− , keyPath : []
1042+ , keyPath: []
1043+ , values:
989− , values : [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1044+ [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
990− , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
1045+ , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
991− , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
1046+ , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
992− , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
1047+ , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
993− ]
1048+ ]
994− , onUpgradeNeeded: Nothing
1049+ , onUpgradeNeeded: Nothing
995− }
996− let cb vdone store =
997− { onComplete: launchAff' do
998− mval <- map _.pKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
999− mval `shouldEqual` (Nothing :: Maybe String)
1000− putVar unit vdone
1001−
1002− , onError: \error -> launchAff' do
1003− fail $ "unexpected error: " <> show error
1004−
1005− , onSuccess: \cursor -> launchAff' do
1006− let value = IDBCursor.value cursor
1007− value.pKey `shouldEqual` "pkey_0"
1008− IDBCursor.delete cursor
1009− IDBCursor.advance cursor 4
1010 }
1050 }
1011− vdone <- makeEmptyVar
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
1012− tx <- IDBDatabase.transaction db ["store"] ReadWrite
1068+ tx <- IDBDatabase.transaction db [ "store" ] ReadWrite
1013− store <- IDBTransaction.objectStore tx "store"
1069+ store <- IDBTransaction.objectStore tx "store"
1014− IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1070+ IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1015− takeVar vdone
1071+ AVar.take vdone
1016− tearDown db
1072+ tearDown db
1017−
1073+
1018− it "update() - modify a record in the object store" do
1074+ it "update() - modify a record in the object store" do
1019− { db } <- setup
1075+ { db } <- setup
1020− { storeParams : { keyPath: ["pKey"], autoIncrement: false }
1076+ { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1021− , indexParams : IDBObjectStore.defaultParameters
1077+ , indexParams: IDBObjectStore.defaultParameters
1022− , keyPath : []
1078+ , keyPath: []
1079+ , values:
1023− , values : [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1080+ [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1024− ]
1081+ ]
1025− , onUpgradeNeeded: Nothing
1082+ , onUpgradeNeeded: Nothing
1026− }
1027− let cb vdone store =
1028− { onComplete: launchAff' do
1029− mval <- map _.iKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
1030− mval `shouldEqual` (Just "patate")
1031− putVar unit vdone
1032−
1033− , onError: \error -> launchAff' do
1034− fail $ "unexpected error: " <> show error
1035−
1036− , onSuccess: \cursor -> launchAff' do
1037− let value = IDBCursor.value cursor
1038− value.pKey `shouldEqual` "pkey_0"
1039− key <- IDBCursor.update cursor { pKey: "pkey_0", iKey: "patate" }
1040− key `shouldEqual` toKey "pkey_0"
1041− IDBCursor.advance cursor 4
1042 }
1083 }
1043− vdone <- makeEmptyVar
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
1044− tx <- IDBDatabase.transaction db ["store"] ReadWrite
1102+ tx <- IDBDatabase.transaction db [ "store" ] ReadWrite
1045− store <- IDBTransaction.objectStore tx "store"
1103+ store <- IDBTransaction.objectStore tx "store"
1046− IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1104+ IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1047− takeVar vdone
1105+ AVar.take vdone
1048− tearDown db
1106+ tearDown db
1049−
1107+
1050− it "update() - throw ReadOnlyError after update on ReadOnly transaction" do
1108+ it "update() - throw ReadOnlyError after update on ReadOnly transaction" do
1051− { db } <- setup
1109+ { db } <- setup
1052− { storeParams : { keyPath: ["pKey"], autoIncrement: false }
1110+ { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1053− , indexParams : IDBObjectStore.defaultParameters
1111+ , indexParams: IDBObjectStore.defaultParameters
1054− , keyPath : []
1112+ , keyPath: []
1113+ , values:
1055− , values : [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1114+ [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1056− ]
1115+ ]
1057− , onUpgradeNeeded: Nothing
1116+ , onUpgradeNeeded: Nothing
1058− }
1059− let cb vdone =
1060− { onComplete: launchAff' do
1061− fail $ "shouldn't complete"
1062−
1063− , onError: \error -> launchAff' do
1064− fail $ "unexpected error: " <> show error
1065−
1066− , onSuccess: \cursor -> launchAff' do
1067− res <- attempt $ IDBCursor.update cursor "patate"
1068− case res of
1069− Left err -> do
1070− name err `shouldEqual` "ReadOnlyError"
1071− putVar unit vdone
1072− Right _ ->
1073− fail $ "expected ReadOnlyError"
1074 }
1117 }
1118+ let
1075− vdone <- makeEmptyVar
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
1076− tx <- IDBDatabase.transaction db ["store"] ReadOnly
1136+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
1077− store <- IDBTransaction.objectStore tx "store"
1137+ store <- IDBTransaction.objectStore tx "store"
1078− IDBObjectStore.openCursor store Nothing Next (cb vdone)
1138+ IDBObjectStore.openCursor store Nothing Next (cb vdone)
1079− takeVar vdone
1139+ AVar.take vdone
1080− tearDown db
1140+ tearDown db