Migrated to PureScript 15

This commit is contained in:
iko 2023-01-02 12:48:03 +03:00
parent ef6b6498c3
commit d307cd4377
Signed by untrusted user: iko
GPG Key ID: 82C257048D1026F2
29 changed files with 1803 additions and 1929 deletions

2
.gitignore vendored
View File

@ -77,3 +77,5 @@ releases
.purs*
.psa*
dist
.spago
test.js

View File

@ -1,44 +0,0 @@
{
"name": "purescript-indexeddb",
"description": "An API wrapper around IndexedDB",
"version": "4.0.0",
"authors": [
"Matthias Benkort <matthias.benkort@gmail.com>"
],
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"output",
"test",
"dist",
"releases",
"package.json",
"karma.conf.js",
"karma.browserstack.conf.js",
"deploy.sh"
],
"license": "MIT",
"homepage": "https://github.com/truqu/purescript-indexedDB",
"repository": {
"type": "git",
"url": "git://github.com/truqu/purescript-indexedDB.git"
},
"dependencies": {
"purescript-aff": "^4.0.2",
"purescript-datetime": "^3.2.0",
"purescript-eff": "^3.1.0",
"purescript-exceptions": "^3.0.0",
"purescript-foreign": "^4.0.1",
"purescript-maybe": "^3.0.0",
"purescript-nullable": "^3.0.0",
"purescript-prelude": "^3.1.0",
"purescript-read": "^1.0.0"
},
"devDependencies": {
"purescript-now": "^3.0.0",
"purescript-psci-support": "^3.0.0",
"purescript-spec": "^2.0.0",
"purescript-spec-mocha": "^2.0.0"
}
}

8
dev/test.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/sh
set -e
spago bundle-app -m Test.Main --to test.js
xdg-open test.html

View File

@ -1,91 +0,0 @@
module.exports = (config) => {
config.set({
browsers: [
'FirefoxMAC',
'ChromeMAC',
// 'SafariMAC',
'OperaMAC',
'FirefoxWIN',
'ChromeWIN',
'OperaWIN',
],
files: [
'dist/karma/index.js',
],
frameworks: [
'mocha',
],
plugins: [
'karma-browserstack-launcher',
'karma-mocha',
],
reporters: [
'dots',
'BrowserStack',
],
singleRun: true,
client: {
mocha: {
timeout: 10000,
},
},
browserStack: {
username: process.env.BROWSERSTACK_USERNAME,
accessKey: process.env.BROWSERSTACK_ACCESSKEY,
timeout: 1500,
captureTimeout: 500,
},
customLaunchers: {
FirefoxMAC: {
base: 'BrowserStack',
browser: 'Firefox',
browser_version: '51',
os: 'OS X',
os_version: 'Sierra',
},
ChromeMAC: {
base: 'BrowserStack',
browser: 'Chrome',
browser_version: '58',
os: 'OS X',
os_version: 'Sierra',
},
SafariMAC: {
base: 'BrowserStack',
browser: 'Safari',
browser_version: '10.1',
os: 'OS X',
os_version: 'Sierra',
},
OperaMAC: {
base: 'BrowserStack',
browser: 'Opera',
browser_version: '46',
os: 'OS X',
os_version: 'Sierra',
},
FirefoxWIN: {
base: 'BrowserStack',
browser: 'Firefox',
browser_version: '51',
os: 'WINDOWS',
os_version: '10',
},
ChromeWIN: {
base: 'BrowserStack',
browser: 'Chrome',
browser_version: '58',
os: 'WINDOWS',
os_version: '10',
},
OperaWIN: {
base: 'BrowserStack',
browser: 'Opera',
browser_version: '46',
os: 'WINDOWS',
os_version: '10',
},
},
});
};

View File

@ -1,25 +0,0 @@
module.exports = config => {
config.set({
autoWatch: true,
singleRun: true,
browsers: ["Chrome", "Firefox"],
files: [
"dist/karma/index.js",
],
frameworks: [
"mocha",
],
plugins: [
"karma-chrome-launcher",
"karma-firefox-launcher",
"karma-spec-reporter",
"karma-mocha",
],
reporters: ["spec"],
client: {
mocha: {
timeout: 10000
}
}
});
};

View File

@ -1,42 +0,0 @@
{
"name": "purescript-indexeddb",
"version": "3.0.0",
"description": "A wrapper around the IndexedDB API",
"scripts": {
"prepare:release": "mkdir -p releases/github && cp -r README.md LICENSE src bower.json releases/github",
"test:build": "mkdir -p dist/karma && pulp browserify -j 4 --main \"Test.Main\" -I test --to dist/karma/index.js",
"test:run": "karma start karma.conf.js",
"test:run:browserstack": "karma start karma.browserstack.conf.js",
"test:browserstack": "npm run test:build && npm run test:run:browserstack",
"test": "npm run test:build && npm run test:run",
"start": "find . -type f -regex '.*\\(src\\|test\\).*' ! -regex '.*bower_components.*\\|.*node_modules.*|.*\\.sw.*' | entr -s 'npm run test -s'"
},
"dependencies": {},
"devDependencies": {
"bower": "1.8.0",
"eslint": "3.19.0",
"eslint-config-airbnb": "15.0.1",
"eslint-plugin-import": "2.5.0",
"eslint-plugin-jsx-a11y": "5.0.3",
"eslint-plugin-react": "7.1.0",
"karma": "1.7.0",
"karma-browserstack-launcher": "1.3.0",
"karma-chrome-launcher": "2.1.1",
"karma-firefox-launcher": "1.0.1",
"karma-mocha": "1.3.0",
"karma-spec-reporter": "0.0.31",
"mocha": "3.4.2",
"pulp": "11.0.0",
"purescript": "0.11.5"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Truqu/purescript-indexeddb.git"
},
"author": "Matthias Benkort <matthias.benkort@truqu.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/Truqu/purescript-indexeddb/issues"
},
"homepage": "https://github.com/Truqu/purescript-indexeddb#readme"
}

120
packages.dhall Normal file
View File

@ -0,0 +1,120 @@
{-
Welcome to your new Dhall package-set!
Below are instructions for how to edit this file for most use
cases, so that you don't need to know Dhall to use it.
## Use Cases
Most will want to do one or both of these options:
1. Override/Patch a package's dependency
2. Add a package not already in the default package set
This file will continue to work whether you use one or both options.
Instructions for each option are explained below.
### Overriding/Patching a package
Purpose:
- Change a package's dependency to a newer/older release than the
default package set's release
- Use your own modified version of some dependency that may
include new API, changed API, removed API by
using your custom git repo of the library rather than
the package set's repo
Syntax:
where `entityName` is one of the following:
- dependencies
- repo
- version
-------------------------------
let upstream = --
in upstream
with packageName.entityName = "new value"
-------------------------------
Example:
-------------------------------
let upstream = --
in upstream
with halogen.version = "master"
with halogen.repo = "https://example.com/path/to/git/repo.git"
with halogen-vdom.version = "v4.0.0"
with halogen-vdom.dependencies = [ "extra-dependency" ] # halogen-vdom.dependencies
-------------------------------
### Additions
Purpose:
- Add packages that aren't already included in the default package set
Syntax:
where `<version>` is:
- a tag (i.e. "v4.0.0")
- a branch (i.e. "master")
- commit hash (i.e. "701f3e44aafb1a6459281714858fadf2c4c2a977")
-------------------------------
let upstream = --
in upstream
with new-package-name =
{ dependencies =
[ "dependency1"
, "dependency2"
]
, repo =
"https://example.com/path/to/git/repo.git"
, version =
"<version>"
}
-------------------------------
Example:
-------------------------------
let upstream = --
in upstream
with benchotron =
{ dependencies =
[ "arrays"
, "exists"
, "profunctor"
, "strings"
, "quickcheck"
, "lcg"
, "transformers"
, "foldable-traversable"
, "exceptions"
, "node-fs"
, "node-buffer"
, "node-readline"
, "datetime"
, "now"
]
, repo =
"https://github.com/hdgarrood/purescript-benchotron.git"
, version =
"v7.0.0"
}
-------------------------------
-}
let upstream =
https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20221124/packages.dhall
sha256:792255bbd8e2141468d967325f3e78246cb7e500bf3ab6d76d5b22dffbb09d49
with spec-mocha =
{ repo =
"https://github.com/ilyakooo0/purescript-spec-mocha"
, version = "v4.0.2", dependencies =
[ "aff"
, "datetime"
, "effect"
, "either"
, "foldable-traversable"
, "maybe"
, "prelude"
, "spec"
]
}
in upstream

43
spago.dhall Normal file
View File

@ -0,0 +1,43 @@
{-
Welcome to a Spago project!
You can edit this file as you like.
Need help? See the following resources:
- Spago documentation: https://github.com/purescript/spago
- Dhall language tour: https://docs.dhall-lang.org/tutorials/Language-Tour.html
When creating a new Spago project, you can use
`spago init --no-comments` or `spago init -C`
to generate this file without the comments in this block.
-}
{ name = "indexeddb"
, dependencies =
[ "aff"
, "arrays"
, "avar"
, "console"
, "control"
, "datetime"
, "effect"
, "either"
, "enums"
, "exceptions"
, "foldable-traversable"
, "foreign"
, "functions"
, "identity"
, "lists"
, "maybe"
, "nonempty"
, "now"
, "nullable"
, "prelude"
, "read"
, "spec"
, "spec-mocha"
, "transformers"
, "tuples"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
}

View File

@ -3,7 +3,7 @@ const toArray = function toArray(xs) {
};
exports._showCursor = function _showCursor(cursor) {
export function _showCursor(cursor) {
return '(IDBCursor ' +
'{ direction: ' + cursor.direction +
', key: ' + cursor.key +
@ -11,7 +11,7 @@ exports._showCursor = function _showCursor(cursor) {
' })';
};
exports._showDatabase = function _showDatabase(db) {
export function _showDatabase(db) {
return '(IDBDatabase ' +
'{ name: ' + db.name +
', objectStoreNames: [' + toArray(db.objectStoreNames).join(', ') + ']' +
@ -19,7 +19,7 @@ exports._showDatabase = function _showDatabase(db) {
' })';
};
exports._showIndex = function _showIndex(index) {
export function _showIndex(index) {
return '(IDBIndex ' +
'{ name: ' + index.name +
', keyPath: ' + index.keyPath +
@ -28,7 +28,7 @@ exports._showIndex = function _showIndex(index) {
' })';
};
exports._showKeyRange = function _showKeyRange(range) {
export function _showKeyRange(range) {
return '(IDBKeyRange ' +
'{ lower: ' + range.lower +
', upper: ' + range.upper +
@ -37,7 +37,7 @@ exports._showKeyRange = function _showKeyRange(range) {
' })';
};
exports._showObjectStore = function _showObjectStore(store) {
export function _showObjectStore(store) {
return '(IDBObjectStore ' +
'{ autoIncrement: ' + store.autoIncrement +
', indexNames: [' + toArray(store.indexNames).join(', ') + ']' +
@ -46,7 +46,7 @@ exports._showObjectStore = function _showObjectStore(store) {
' })';
};
exports._showTransaction = function _showTransaction(tx) {
export function _showTransaction(tx) {
return '(IDBTransaction ' +
'{ error: ' + tx.error +
', mode: ' + tx.mode +

View File

@ -2,10 +2,11 @@
-- | those types.
module Database.IndexedDB.Core
-- * Effects
( IDB
(
-- IDB
-- * Types
, Database
-- * Types
Database
, Index
, KeyCursor
, KeyRange
@ -30,23 +31,13 @@ module Database.IndexedDB.Core
, module Database.IndexedDB.IDBKey
) where
import Prelude (class Show)
import Prelude (class Show)
import Control.Monad.Eff (kind Effect)
import Data.Maybe (Maybe(..))
import Data.String.Read (class Read)
import Data.Maybe (Maybe(..))
import Data.String.Read (class Read)
import Database.IndexedDB.IDBKey
--------------------
-- Effects
--
-- | IDB Effects, manifestation that something happened with the IndexedDB
foreign import data IDB :: Effect
--------------------
-- TYPES
--
@ -55,35 +46,28 @@ foreign import data IDB :: Effect
-- | stores which hold the data stored in the database.
foreign import data Database :: Type
-- | An index allows looking up records in an object store using properties of the values
-- | in the object stores records.
foreign import data Index :: Type
-- | A key range is a continuous interval over some data type used for keys.
foreign import data KeyRange :: Type
-- | A cursor is used to iterate over a range of records in an index or an object store
-- | in a specific direction. A KeyCursor doesn't hold any value.
foreign import data KeyCursor :: Type
-- | A Transaction is used to interact with the data in a database.
-- | Whenever data is read or written to the database it is done by using a transaction.
foreign import data Transaction :: Type
-- | An object store is the primary storage mechanism for storing data in a database.
foreign import data ObjectStore :: Type
-- | A cursor is used to iterate over a range of records in an index or an object store
-- | in a specific direction. A ValueCursor also holds the value corresponding to matching key.
foreign import data ValueCursor :: Type
-- | A cursor has a direction that determines whether it moves in monotonically
-- | increasing or decreasing order of the record keys when iterated, and if it
-- | skips duplicated values when iterating indexes.
@ -95,7 +79,6 @@ data CursorDirection
| Prev
| PrevUnique
-- | If the source of a cursor is an object store, the effective object store of
-- | the cursor is that object store and the effective key of the cursor is the
-- | cursors position. If the source of a cursor is an index, the effective object
@ -105,7 +88,6 @@ data CursorSource
= ObjectStore ObjectStore
| Index Index
-- | A key path is a list of strings that defines how to extract a key from a value.
-- | A valid key path is one of:
-- |
@ -115,15 +97,13 @@ data CursorSource
-- | - A non-empty list containing only strings conforming to the above requirements.
type KeyPath = Array String
-- | A transaction has a mode that determines which types of interactions can be performed
-- | upon that transaction. The mode is set when the transaction is created and remains
-- | fixed for the life of the transaction.
data TransactionMode
= ReadOnly -- ^ The transaction is only allowed to read data.
| ReadWrite -- ^ The transaction is allowed to read, modify and delete data from existing object stores
| 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.
= ReadOnly -- ^ The transaction is only allowed to read data.
| ReadWrite -- ^ The transaction is allowed to read, modify and delete data from existing object stores
| 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.
--------------------
-- Classes
@ -133,132 +113,104 @@ data TransactionMode
-- | specific attributes (see KeyCursor or CursorWithValue).
class (IDBCursor cursor) <= IDBConcreteCursor cursor
-- | Cursor objects implement the IDBCursor interface.
-- | There is only ever one IDBCursor instance representing a given cursor.
-- | There is no limit on how many cursors can be used at the same time.
class IDBCursor cursor
-- | The IDBDatabase interface represents a connection to a database.
class IDBDatabase db
-- | The IDBIndex interface represents an index handle.
-- | Any of these methods throw an "TransactionInactiveError" DOMException
-- | if called when the transaction is not active.
class IDBIndex index
-- | The IDBKeyRange interface represents a key range.
class IDBKeyRange range
-- | The IDBtransaction interface.
class IDBTransaction tx
-- | The IDBObjectStore interface represents an object store handle.
class IDBObjectStore store
--------------------
-- INSTANCES
--
instance idbCursorKeyCursor :: IDBCursor KeyCursor
instance idbCursorValueCursor :: IDBCursor ValueCursor
instance idbConcreteCursorKeyCursor :: IDBConcreteCursor KeyCursor
instance idbConcreteCursorValueCursor :: IDBConcreteCursor ValueCursor
instance idbDatabaseDatabase :: IDBDatabase Database
instance idbIndexIndex :: IDBIndex Index
instance idbIndexObjectStore :: IDBIndex ObjectStore
instance idbKeyRangeKeyRange :: IDBKeyRange KeyRange
instance idbObjectStoreObjectStore :: IDBObjectStore ObjectStore
instance idbTransactionTransaction :: IDBTransaction Transaction
instance showKeyCursor :: Show KeyCursor where
show = _showCursor
instance showValueCursor :: Show ValueCursor where
show = _showCursor
instance showIndex :: Show Index where
show = _showIndex
instance showKeyRange :: Show KeyRange where
show = _showKeyRange
instance showObjectStore :: Show ObjectStore where
show = _showObjectStore
instance showTransaction :: Show Transaction where
show = _showTransaction
instance showDatabase :: Show Database where
show = _showDatabase
instance showCursorDirection :: Show CursorDirection where
show x =
case x of
Next -> "next"
Next -> "next"
NextUnique -> "nextunique"
Prev -> "prev"
Prev -> "prev"
PrevUnique -> "prevunique"
instance showTransactionMode :: Show TransactionMode where
show x =
case x of
ReadOnly -> "readonly"
ReadWrite -> "readwrite"
ReadOnly -> "readonly"
ReadWrite -> "readwrite"
VersionChange -> "versionchange"
instance readCursorDirection :: Read CursorDirection where
read s =
case s of
"next" -> Just Next
"next" -> Just Next
"nextunique" -> Just NextUnique
"prev" -> Just Prev
"prev" -> Just Prev
"prevunique" -> Just PrevUnique
_ -> Nothing
_ -> Nothing
instance readTransactionMode :: Read TransactionMode where
read s =
case s of
"readonly" -> Just ReadOnly
"readwrite" -> Just ReadWrite
"readonly" -> Just ReadOnly
"readwrite" -> Just ReadWrite
"versionchange" -> Just VersionChange
_ -> Nothing
_ -> Nothing
--------------------
-- FFI
@ -266,30 +218,25 @@ instance readTransactionMode :: Read TransactionMode where
foreign import _showCursor
:: forall cursor
. cursor
. cursor
-> String
foreign import _showDatabase
:: Database
-> String
foreign import _showIndex
:: Index
-> String
foreign import _showKeyRange
:: KeyRange
-> String
foreign import _showObjectStore
:: ObjectStore
-> String
foreign import _showTransaction
:: Transaction
-> String

View File

@ -10,7 +10,7 @@ const successHandler = function successHandler(cb) {
};
};
exports._advance = function _advance(cursor, count) {
export function _advance(cursor, count) {
return function aff(error, success) {
try {
cursor.advance(count);
@ -25,7 +25,7 @@ exports._advance = function _advance(cursor, count) {
};
};
exports._continue = function _continue(cursor, key) {
export function _continue(cursor, key) {
return function aff(error, success) {
try {
cursor.continue(key || undefined);
@ -40,7 +40,7 @@ exports._continue = function _continue(cursor, key) {
};
};
exports._continuePrimaryKey = function _continuePrimaryKey(cursor, key, primaryKey) {
export function _continuePrimaryKey(cursor, key, primaryKey) {
return function aff(error, success) {
try {
cursor.continuePrimaryKey(key, primaryKey);
@ -55,7 +55,7 @@ exports._continuePrimaryKey = function _continuePrimaryKey(cursor, key, primaryK
};
};
exports._delete = function _delete(cursor) {
export function _delete(cursor) {
return function aff(error, success) {
try {
const request = cursor.delete();
@ -71,11 +71,11 @@ exports._delete = function _delete(cursor) {
};
};
exports._direction = function _direction(fromString, cursor) {
export function _direction(fromString, cursor) {
return fromString(cursor.direction);
};
exports._key = function _key(cursor) {
export function _key(cursor) {
return function aff(error, success) {
try {
success(cursor.key);
@ -89,7 +89,7 @@ exports._key = function _key(cursor) {
};
};
exports._primaryKey = function _primaryKey(cursor) {
export function _primaryKey(cursor) {
return function aff(error, success) {
try {
success(cursor.primaryKey);
@ -103,7 +103,7 @@ exports._primaryKey = function _primaryKey(cursor) {
};
};
exports._source = function _source(IDBObjectStore, IDBIndex, cursor) {
export function _source(IDBObjectStore, IDBIndex, cursor) {
switch (cursor.source.constructor.name) {
case 'IDBIndex':
return IDBIndex(cursor.source);
@ -123,7 +123,7 @@ exports._source = function _source(IDBObjectStore, IDBIndex, cursor) {
}
};
exports._update = function _update(cursor, value) {
export function _update(cursor, value) {
return function aff(error, success) {
try {
const request = cursor.update(value);
@ -139,6 +139,6 @@ exports._update = function _update(cursor, value) {
};
};
exports._value = function _value(cursor) {
export function _value(cursor) {
return cursor.value;
};

View File

@ -16,20 +16,18 @@ module Database.IndexedDB.IDBCursor
, value
) where
import Prelude (Unit, ($), (>>>), (<<<), map)
import Control.Monad.Aff (Aff)
import Control.Monad.Aff.Compat (fromEffFnAff, EffFnAff)
import Data.Foreign (Foreign, toForeign, unsafeFromForeign)
import Data.Function.Uncurried as Fn
import Data.Function.Uncurried (Fn2, Fn3)
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toNullable)
import Data.String.Read (read)
import Database.IndexedDB.Core
import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, toKey, unsafeFromKey)
import Data.Function.Uncurried (Fn2, Fn3)
import Data.Function.Uncurried as Fn
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toNullable)
import Data.String.Read (read)
import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, toKey, unsafeFromKey)
import Effect.Aff (Aff)
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
import Foreign (Foreign, unsafeFromForeign, unsafeToForeign)
import Prelude (Unit, ($), (>>>), (<<<), map)
--------------------
-- INTERFACES
@ -37,56 +35,58 @@ import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, toKey, unsafeFromK
-- | Advances the cursor through the next count records in range.
advance
:: forall e cursor. (IDBCursor cursor)
=> cursor
-> Int
-> Aff (idb :: IDB | e) Unit
:: forall cursor
. (IDBCursor cursor)
=> cursor
-> Int
-> Aff Unit
advance c =
fromEffFnAff <<< Fn.runFn2 _advance c
fromEffectFnAff <<< Fn.runFn2 _advance c
-- | Advances the cursor to the next record in range matching or after key.
continue
:: forall e k cursor. (IDBKey k) => (IDBCursor cursor)
=> cursor
-> Maybe k
-> Aff (idb :: IDB | e) Unit
:: forall k cursor
. (IDBKey k)
=> (IDBCursor cursor)
=> cursor
-> Maybe k
-> Aff Unit
continue c mk =
fromEffFnAff $ Fn.runFn2 _continue c (toNullable $ map (toKey >>> unsafeFromKey) mk)
fromEffectFnAff $ Fn.runFn2 _continue c (toNullable $ map (toKey >>> unsafeFromKey) mk)
-- | 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.
continuePrimaryKey
:: forall e k cursor. (IDBKey k) => (IDBCursor cursor)
=> cursor
-> k
-> k
-> Aff (idb :: IDB | e) Unit
:: forall k cursor
. (IDBKey k)
=> (IDBCursor cursor)
=> cursor
-> k
-> k
-> Aff Unit
continuePrimaryKey c k1 k2 =
fromEffFnAff $ Fn.runFn3 _continuePrimaryKey c (unsafeFromKey $ toKey k1) (unsafeFromKey $ toKey k2)
fromEffectFnAff $ Fn.runFn3 _continuePrimaryKey c (unsafeFromKey $ toKey k1) (unsafeFromKey $ toKey k2)
-- | Delete the record pointed at by the cursor with a new value.
delete
:: forall e cursor. (IDBCursor cursor)
=> cursor
-> Aff (idb :: IDB | e) Unit
:: forall cursor
. (IDBCursor cursor)
=> cursor
-> Aff Unit
delete =
fromEffFnAff <<< _delete
fromEffectFnAff <<< _delete
-- | Update the record pointed at by the cursor with a new value.
-- |
-- | Throws a "DataError" DOMException if the effective object store uses
-- | in-line keys and the key would have changed.
update
:: forall val e cursor. (IDBCursor cursor)
=> cursor
-> val
-> Aff (idb :: IDB | e) Key
:: forall val cursor
. (IDBCursor cursor)
=> cursor
-> val
-> Aff Key
update c =
map toKey <<< fromEffFnAff <<< Fn.runFn2 _update c <<< toForeign
map toKey <<< fromEffectFnAff <<< Fn.runFn2 _update c <<< unsafeToForeign
--------------------
-- ATTRIBUTES
@ -94,103 +94,93 @@ update c =
-- | Returns the direction (Next|NextUnique|Prev|PrevUnique) of the cursor.
direction
:: forall cursor. (IDBConcreteCursor cursor)
:: forall cursor
. (IDBConcreteCursor cursor)
=> cursor
-> CursorDirection
direction =
Fn.runFn2 _direction (read >>> toNullable)
-- | Returns the key of the cursor. Throws a "InvalidStateError" DOMException
-- | if the cursor is advancing or is finished.
key
:: forall e cursor. (IDBConcreteCursor cursor)
:: forall cursor
. (IDBConcreteCursor cursor)
=> cursor
-> Aff (idb :: IDB | e) Key
-> Aff Key
key =
map toKey <<< fromEffFnAff <<< _key
map toKey <<< fromEffectFnAff <<< _key
-- | Returns the effective key of the cursor. Throws a "InvalidStateError" DOMException
-- | if the cursor is advancing or is finished.
primaryKey
:: forall e cursor. (IDBConcreteCursor cursor)
:: forall cursor
. (IDBConcreteCursor cursor)
=> cursor
-> Aff (idb :: IDB | e) Key
-> Aff Key
primaryKey =
map toKey <<< fromEffFnAff <<< _primaryKey
map toKey <<< fromEffectFnAff <<< _primaryKey
-- | Returns the IDBObjectStore or IDBIndex the cursor was opened from.
source
:: forall cursor. (IDBConcreteCursor cursor)
:: forall cursor
. (IDBConcreteCursor cursor)
=> cursor
-> CursorSource
source =
Fn.runFn3 _source ObjectStore Index
value
:: forall val
. ValueCursor
. ValueCursor
-> val
value =
_value >>> unsafeFromForeign
--------------------
-- FFI
--
foreign import _advance
:: forall cursor e
. Fn2 cursor Int (EffFnAff (idb :: IDB | e) Unit)
:: forall cursor
. Fn2 cursor Int (EffectFnAff Unit)
foreign import _continue
:: forall cursor e
. Fn2 cursor (Nullable Foreign) (EffFnAff (idb :: IDB | e) Unit)
:: forall cursor
. Fn2 cursor (Nullable Foreign) (EffectFnAff Unit)
foreign import _continuePrimaryKey
:: forall cursor e
. Fn3 cursor Foreign Foreign (EffFnAff (idb :: IDB | e) Unit)
:: forall cursor
. Fn3 cursor Foreign Foreign (EffectFnAff Unit)
foreign import _delete
:: forall cursor e
. cursor
-> (EffFnAff (idb :: IDB | e) Unit)
:: forall cursor
. cursor
-> (EffectFnAff Unit)
foreign import _direction
:: forall cursor
. Fn2 (String -> Nullable CursorDirection) cursor CursorDirection
. Fn2 (String -> Nullable CursorDirection) cursor CursorDirection
foreign import _key
:: forall cursor e
. cursor
-> EffFnAff (idb :: IDB | e) Key
:: forall cursor
. cursor
-> EffectFnAff Key
foreign import _primaryKey
:: forall cursor e
. cursor
-> EffFnAff (idb :: IDB | e) Key
:: forall cursor
. cursor
-> EffectFnAff Key
foreign import _source
:: forall cursor
. Fn3 (ObjectStore -> CursorSource) (Index -> CursorSource) cursor CursorSource
. Fn3 (ObjectStore -> CursorSource) (Index -> CursorSource) cursor CursorSource
foreign import _update
:: forall cursor e
. Fn2 cursor Foreign (EffFnAff (idb :: IDB | e) Foreign)
:: forall cursor
. Fn2 cursor Foreign (EffectFnAff Foreign)
foreign import _value
:: forall cursor val
. cursor
. cursor
-> val

View File

@ -3,7 +3,7 @@ const toArray = function toArray(xs) {
};
exports._close = function _close(db) {
export function _close(db) {
return function aff(error, success) {
try {
db.close();
@ -18,7 +18,7 @@ exports._close = function _close(db) {
};
};
exports._createObjectStore = function _createObjectStore(db, name, opts) {
export function _createObjectStore(db, name, opts) {
return function aff(error, success) {
var keyPath;
@ -52,7 +52,7 @@ exports._createObjectStore = function _createObjectStore(db, name, opts) {
};
};
exports._deleteObjectStore = function _deleteObjectStore(db, name) {
export function _deleteObjectStore(db, name) {
return function aff(error, success) {
try {
db.deleteObjectStore(name);
@ -67,15 +67,15 @@ exports._deleteObjectStore = function _deleteObjectStore(db, name) {
};
};
exports._name = function _name(db) {
export function _name(db) {
return db.name;
};
exports._objectStoreNames = function _objectStoreNames(db) {
export function _objectStoreNames(db) {
return toArray(db.objectStoreNames);
};
exports._onAbort = function _onAbort(db, f) {
export function _onAbort(db, f) {
return function aff(error, success) {
db.onabort = function onabort() {
f();
@ -88,7 +88,7 @@ exports._onAbort = function _onAbort(db, f) {
};
};
exports._onClose = function _onClose(db, f) {
export function _onClose(db, f) {
return function aff(error, success) {
db.onclose = function onclose() {
f();
@ -101,7 +101,7 @@ exports._onClose = function _onClose(db, f) {
};
};
exports._onError = function _onError(db, f) {
export function _onError(db, f) {
return function aff(error, success) {
db.onerror = function onerror(e) {
f(e.target.error)();
@ -114,7 +114,7 @@ exports._onError = function _onError(db, f) {
};
};
exports._onVersionChange = function _onVersionChange(db, f) {
export function _onVersionChange(db, f) {
return function aff(error, success) {
db.onversionchange = function onversionchange(e) {
f({ oldVersion: e.oldVersion, newVersion: e.newVersion })();
@ -127,7 +127,7 @@ exports._onVersionChange = function _onVersionChange(db, f) {
};
};
exports._transaction = function _transaction(db, stores, mode) {
export function _transaction(db, stores, mode) {
return function aff(error, success) {
var transaction;
try {
@ -144,6 +144,6 @@ exports._transaction = function _transaction(db, stores, mode) {
};
};
exports._version = function _version(db) {
export function _version(db) {
return db.version;
};

View File

@ -24,17 +24,15 @@ module Database.IndexedDB.IDBDatabase
, onVersionChange
) where
import Prelude (Unit, show, (<<<), ($))
import Control.Monad.Aff (Aff)
import Control.Monad.Aff.Compat (fromEffFnAff, EffFnAff)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Exception (Error)
import Data.Function.Uncurried as Fn
import Data.Function.Uncurried (Fn2, Fn3)
import Database.IndexedDB.Core
import Data.Function.Uncurried (Fn2, Fn3)
import Data.Function.Uncurried as Fn
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
import Effect.Exception (Error)
import Prelude (Unit, show, (<<<), ($))
--------------------
-- TYPES
@ -43,70 +41,67 @@ import Database.IndexedDB.Core
-- | Type alias for StoreName
type StoreName = String
-- | Options provided when creating an object store.
type ObjectStoreParameters =
{ keyPath :: KeyPath
{ keyPath :: KeyPath
, autoIncrement :: Boolean
}
defaultParameters :: ObjectStoreParameters
defaultParameters =
{ keyPath : []
, autoIncrement : false
{ keyPath: []
, autoIncrement: false
}
--------------------
-- INTERFACE
--
-- | Closes the connection once all running transactions have finished.
close
:: forall e db. (IDBDatabase db)
:: forall db
. (IDBDatabase db)
=> db
-> Aff (idb :: IDB | e) Unit
-> Aff Unit
close =
fromEffFnAff <<< _close
fromEffectFnAff <<< _close
-- | Creates a new object store with the given name and options and returns a new IDBObjectStore.
-- |
-- | Throws a "InvalidStateError" DOMException if not called within an upgrade transaction
createObjectStore
:: forall e db. (IDBDatabase db)
:: forall db
. (IDBDatabase db)
=> db
-> StoreName
-> ObjectStoreParameters
-> Aff (idb :: IDB | e) ObjectStore
-> Aff ObjectStore
createObjectStore db name' opts =
fromEffFnAff $ Fn.runFn3 _createObjectStore db name' opts
fromEffectFnAff $ Fn.runFn3 _createObjectStore db name' opts
-- | Deletes the object store with the given name.
-- |
-- | Throws a "InvalidStateError" DOMException if not called within an upgrade transaction.
deleteObjectStore
:: forall e db. (IDBDatabase db)
:: forall db
. (IDBDatabase db)
=> db
-> StoreName
-> Aff (idb :: IDB | e) ObjectStore
-> Aff ObjectStore
deleteObjectStore db name' =
fromEffFnAff $ Fn.runFn2 _deleteObjectStore db name'
fromEffectFnAff $ Fn.runFn2 _deleteObjectStore db name'
-- | Returns a new transaction with the given mode (ReadOnly|ReadWrite)
-- | and scope which in the form of an array of object store names.
transaction
:: forall e db. (IDBDatabase db)
:: forall db
. (IDBDatabase db)
=> db
-> Array StoreName
-> TransactionMode
-> Aff (idb :: IDB | e) Transaction
-> Aff Transaction
transaction db stores mode' =
fromEffFnAff $ Fn.runFn3 _transaction db stores (show mode')
fromEffectFnAff $ Fn.runFn3 _transaction db stores (show mode')
--------------------
-- ATTRIBUTES
@ -114,127 +109,107 @@ transaction db stores mode' =
-- | Returns the name of the database.
name
:: Database
-> String
:: Database
-> String
name =
_name
-- | Returns a list of the names of object stores in the database.
objectStoreNames
:: Database
-> Array String
:: Database
-> Array String
objectStoreNames =
_objectStoreNames
-- | Returns the version of the database.
version
:: Database
-> Int
:: Database
-> Int
version =
_version
--------------------
-- EVENT HANDLERS
--
-- | Event handler for the `abort` event.
onAbort
:: forall e e'
. Database
-> Eff ( | e') Unit
-> Aff (idb :: IDB | e) Unit
:: Database
-> Effect Unit
-> Aff Unit
onAbort db f =
fromEffFnAff $ Fn.runFn2 _onAbort db f
fromEffectFnAff $ Fn.runFn2 _onAbort db f
-- | Event handler for the `close` event.
onClose
:: forall e e'
. Database
-> Eff ( | e') Unit
-> Aff (idb :: IDB | e) Unit
:: Database
-> Effect Unit
-> Aff Unit
onClose db f =
fromEffFnAff $ Fn.runFn2 _onClose db f
fromEffectFnAff $ Fn.runFn2 _onClose db f
-- | Event handler for the `error` event.
onError
:: forall e e'
. Database
-> (Error -> Eff ( | e') Unit)
-> Aff (idb :: IDB | e) Unit
:: Database
-> (Error -> Effect Unit)
-> Aff Unit
onError db f =
fromEffFnAff $ Fn.runFn2 _onError db f
fromEffectFnAff $ Fn.runFn2 _onError db f
-- | Event handler for the `versionchange` event.
onVersionChange
:: forall e e'
. Database
-> ({ oldVersion :: Int, newVersion :: Int }
-> Eff ( | e') Unit)
-> Aff (idb :: IDB | e) Unit
:: Database
-> ( { oldVersion :: Int, newVersion :: Int }
-> Effect Unit
)
-> Aff Unit
onVersionChange db f =
fromEffFnAff $ Fn.runFn2 _onVersionChange db f
fromEffectFnAff $ Fn.runFn2 _onVersionChange db f
--------------------
-- FFI
--
foreign import _close
:: forall db e
. db
-> EffFnAff (idb :: IDB | e) Unit
:: forall db
. db
-> EffectFnAff Unit
foreign import _createObjectStore
:: forall db e
. Fn3 db String { keyPath :: Array String, autoIncrement :: Boolean } (EffFnAff (idb :: IDB | e) ObjectStore)
:: forall db
. Fn3 db String { keyPath :: Array String, autoIncrement :: Boolean } (EffectFnAff ObjectStore)
foreign import _deleteObjectStore
:: forall db e
. Fn2 db String (EffFnAff (idb :: IDB | e) ObjectStore)
:: forall db
. Fn2 db String (EffectFnAff ObjectStore)
foreign import _name
:: Database
-> String
:: Database
-> String
foreign import _objectStoreNames
:: Database
-> Array String
:: Database
-> Array String
foreign import _onAbort
:: forall db e e'
. Fn2 db (Eff ( | e') Unit) (EffFnAff (idb :: IDB | e) Unit)
:: forall db
. Fn2 db (Effect Unit) (EffectFnAff Unit)
foreign import _onClose
:: forall db e e'
. Fn2 db (Eff ( | e') Unit) (EffFnAff (idb :: IDB | e) Unit)
:: forall db
. Fn2 db (Effect Unit) (EffectFnAff Unit)
foreign import _onError
:: forall db e e'
. Fn2 db (Error -> Eff ( | e') Unit) (EffFnAff (idb :: IDB | e) Unit)
:: forall db
. Fn2 db (Error -> Effect Unit) (EffectFnAff Unit)
foreign import _onVersionChange
:: forall db e e'
. Fn2 db ({ oldVersion :: Int, newVersion :: Int } -> Eff ( | e') Unit) (EffFnAff (idb :: IDB | e) Unit)
:: forall db
. Fn2 db ({ oldVersion :: Int, newVersion :: Int } -> Effect Unit) (EffectFnAff Unit)
foreign import _transaction
:: forall db e
. Fn3 db (Array String) String (EffFnAff (idb :: IDB | e) Transaction)
:: forall db
. Fn3 db (Array String) String (EffectFnAff Transaction)
foreign import _version
:: Database
-> Int
:: Database
-> Int

View File

@ -18,7 +18,7 @@ const noOp3 = function noOp3() {
return noOp2;
};
exports._deleteDatabase = function _deleteDatabase(name) {
export function _deleteDatabase(name) {
return function aff(error, success) {
try {
const request = indexedDB.deleteDatabase(name);
@ -38,7 +38,7 @@ exports._deleteDatabase = function _deleteDatabase(name) {
};
};
exports._open = function _open(fromMaybe, name, mver, req) {
export function _open(fromMaybe, name, mver, req) {
const ver = fromMaybe(undefined)(mver);
return function aff(error, success) {

View File

@ -12,37 +12,32 @@ module Database.IndexedDB.IDBFactory
, open
) where
import Prelude (Unit, ($), (<<<))
import Control.Monad.Aff (Aff)
import Control.Monad.Aff.Compat (fromEffFnAff, EffFnAff)
import Control.Monad.Eff (Eff)
import Data.Function.Uncurried as Fn
import Data.Function.Uncurried (Fn4)
import Data.Maybe (Maybe, fromMaybe)
import Database.IndexedDB.Core
import Data.Function.Uncurried (Fn4)
import Data.Function.Uncurried as Fn
import Data.Maybe (Maybe, fromMaybe)
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
import Prelude (Unit, ($), (<<<))
--------------------
-- TYPES
--
-- Type alias for binding listeners to an initial open action.
type Callbacks e =
{ onBlocked :: Maybe (Eff (| e) Unit)
, onUpgradeNeeded :: Maybe (Database -> Transaction -> { oldVersion :: Int } -> Eff (| e) Unit)
type Callbacks =
{ onBlocked :: Maybe (Effect Unit)
, onUpgradeNeeded :: Maybe (Database -> Transaction -> { oldVersion :: Int } -> Effect Unit)
}
-- | Type alias for DatabaseName.
type DatabaseName = String
-- | Type alias for Version.
type Version = Int
--------------------
-- INTERFACE
--
@ -51,12 +46,10 @@ type Version = Int
-- | and there are open connections that dont close in response to a
-- | `versionchange` event, the request will be blocked until all they close.
deleteDatabase
:: forall e
. DatabaseName
-> Aff (idb :: IDB | e) Int
:: DatabaseName
-> Aff Int
deleteDatabase =
fromEffFnAff <<< _deleteDatabase
fromEffectFnAff <<< _deleteDatabase
-- | Attempts to open a connection to the named database with the specified version.
-- | If the database already exists with a lower version and there are open connections
@ -67,24 +60,20 @@ deleteDatabase =
-- | When the version isn't provided (`Nothing`), attempts to open a connection to the
-- | named database with the current version, or 1 if it does not already exist.
open
:: forall e e'
. DatabaseName
-> Maybe Version
-> Callbacks e'
-> Aff (idb :: IDB | e) Database
:: DatabaseName
-> Maybe Version
-> Callbacks
-> Aff Database
open name mver req =
fromEffFnAff $ Fn.runFn4 _open fromMaybe name mver req
fromEffectFnAff $ Fn.runFn4 _open fromMaybe name mver req
--------------------
-- FFI
--
foreign import _deleteDatabase
:: forall e
. String
-> EffFnAff (idb :: IDB | e) Int
:: String
-> EffectFnAff Int
foreign import _open
:: forall a e e'
. Fn4 (a -> Maybe a -> a) String (Maybe Int) (Callbacks e') (EffFnAff (idb :: IDB | e) Database)
:: forall a
. Fn4 (a -> Maybe a -> a) String (Maybe Int) (Callbacks) (EffectFnAff Database)

View File

@ -11,7 +11,7 @@ const successHandler = function successHandler(cb) {
};
exports._keyPath = function _keyPath(index) {
export function _keyPath(index) {
const path = index.keyPath;
if (Array.isArray(path)) {
@ -25,23 +25,23 @@ exports._keyPath = function _keyPath(index) {
return [];
};
exports._multiEntry = function _multiEntry(index) {
export function _multiEntry(index) {
return index.multiEntry;
};
exports._name = function _name(index) {
export function _name(index) {
return index.name;
};
exports._objectStore = function _objectStore(index) {
export function _objectStore(index) {
return index.obectStore;
};
exports._unique = function _unique(index) {
export function _unique(index) {
return index.unique;
};
exports._count = function _count(index, query) {
export function _count(index, query) {
return function aff(error, success) {
try {
const request = index.count(query);
@ -57,7 +57,7 @@ exports._count = function _count(index, query) {
};
};
exports._get = function _get(index, range) {
export function _get(index, range) {
return function aff(error, success) {
try {
const request = index.get(range);
@ -79,7 +79,7 @@ exports._get = function _get(index, range) {
*
* However, it may be doable to convert the result to some key / value structure with values of
* different types.
exports._getAll = function _getAll(index, query, count) {
export function _getAll(index, query, count) {
return function aff(error, success) {
const request = index.getAll(query, count);
request.onsuccess = successHandler(success);
@ -88,7 +88,7 @@ exports._getAll = function _getAll(index, query, count) {
};
*/
exports._getAllKeys = function _getAllKeys(index, range, count) {
export function _getAllKeys(index, range, count) {
return function aff(error, success) {
try {
const request = index.getAllKeys(range, count || undefined);
@ -104,7 +104,7 @@ exports._getAllKeys = function _getAllKeys(index, range, count) {
};
};
exports._getKey = function _getKey(index, range) {
export function _getKey(index, range) {
return function aff(error, success) {
try {
const request = index.getKey(range);
@ -120,7 +120,7 @@ exports._getKey = function _getKey(index, range) {
};
};
exports._openCursor = function _openCursor(index, query, dir, cb) {
export function _openCursor(index, query, dir, cb) {
return function aff(error, success) {
try {
const request = index.openCursor(query, dir);
@ -145,7 +145,7 @@ exports._openCursor = function _openCursor(index, query, dir, cb) {
};
};
exports._openKeyCursor = function _openKeyCursor(index, query, dir, cb) {
export function _openKeyCursor(index, query, dir, cb) {
return function aff(error, success) {
try {
const request = index.openKeyCursor(query, dir);

View File

@ -20,205 +20,187 @@ module Database.IndexedDB.IDBIndex
, unique
) where
import Prelude (Unit, map, show, (<$>), (>>>), ($))
import Control.Monad.Aff (Aff)
import Control.Monad.Aff.Compat (EffFnAff, fromEffFnAff)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Exception (Error)
import Data.Foreign (Foreign, unsafeFromForeign)
import Data.Function.Uncurried as Fn
import Data.Function.Uncurried (Fn2, Fn3, Fn4)
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe, toNullable)
import Database.IndexedDB.Core
import Database.IndexedDB.IDBKey.Internal (Key, toKey)
import Data.Function.Uncurried (Fn2, Fn3, Fn4)
import Data.Function.Uncurried as Fn
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe, toNullable)
import Database.IndexedDB.IDBKey.Internal (Key, toKey)
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
import Effect.Exception (Error)
import Foreign (Foreign, unsafeFromForeign)
import Prelude (Unit, map, show, ($), (>>>))
--------------------
-- TYPES
--
-- | Callbacks to manipulate a cursor from an Open*Cursor call
type Callbacks cursor e =
{ onSuccess :: cursor -> Eff ( | e) Unit
, onError :: Error -> Eff ( | e) Unit
, onComplete :: Eff ( | e) Unit
type Callbacks cursor =
{ onSuccess :: cursor -> Effect Unit
, onError :: Error -> Effect Unit
, onComplete :: Effect Unit
}
--------------------
-- INTERFACES
--
-- | Retrieves the number of records matching the key range in query.
count
:: forall e index. (IDBIndex index)
=> index
:: forall index
. (IDBIndex index)
=> index
-> Maybe KeyRange
-> Aff (idb :: IDB | e) Int
-> Aff Int
count index range =
fromEffFnAff $ Fn.runFn2 _count index (toNullable range)
fromEffectFnAff $ Fn.runFn2 _count index (toNullable range)
-- | Retrieves the value of the first record matching the given key range in query.
-- |
-- | NOTE
-- | The coercion from `a` to any type is unsafe and might throw a runtime error if incorrect.
get
:: forall a e index. (IDBIndex index)
:: forall a index
. (IDBIndex index)
=> index
-> KeyRange
-> Aff (idb :: IDB | e) (Maybe a)
-> Aff (Maybe a)
get index range =
map (toMaybe >>> map unsafeFromForeign) $ fromEffFnAff $ Fn.runFn2 _get index range
map (toMaybe >>> map unsafeFromForeign) $ fromEffectFnAff $ Fn.runFn2 _get index range
-- | Retrieves the keys of records matching the given key range in query
-- | (up to the number given if given).
getAllKeys
:: forall e index. (IDBIndex index)
:: forall index
. (IDBIndex index)
=> index
-> Maybe KeyRange
-> Maybe Int
-> Aff (idb :: IDB | e) (Array Key)
-> Aff (Array Key)
getAllKeys index range n =
map (map toKey) $ fromEffFnAff $ Fn.runFn3 _getAllKeys index (toNullable range) (toNullable n)
map (map toKey) $ fromEffectFnAff $ Fn.runFn3 _getAllKeys index (toNullable range) (toNullable n)
-- | Retrieves the key of the first record matching the given key or key range in query.
getKey
:: forall e index. (IDBIndex index)
:: forall index
. (IDBIndex index)
=> index
-> KeyRange
-> Aff (idb :: IDB | e) (Maybe Key)
-> Aff (Maybe Key)
getKey index range =
map (toMaybe >>> map toKey) $ fromEffFnAff $ Fn.runFn2 _getKey index range
map (toMaybe >>> map toKey) $ fromEffectFnAff $ Fn.runFn2 _getKey index range
-- | Opens a ValueCursor over the records matching query, ordered by direction.
-- | If query is `Nothing`, all records in index are matched.
openCursor
:: forall e e' index. (IDBIndex index)
=> index
:: forall index
. (IDBIndex index)
=> index
-> Maybe KeyRange
-> CursorDirection
-> Callbacks ValueCursor e'
-> Aff (idb :: IDB | e) Unit
-> Callbacks ValueCursor
-> Aff Unit
openCursor index range dir cb =
fromEffFnAff $ Fn.runFn4 _openCursor index (toNullable range) (show dir) cb
fromEffectFnAff $ Fn.runFn4 _openCursor index (toNullable range) (show dir) cb
-- | Opens a KeyCursor over the records matching query, ordered by direction.
-- | If query is `Nothing`, all records in index are matched.
openKeyCursor
:: forall e e' index. (IDBIndex index)
=> index
-> Maybe KeyRange
-> CursorDirection
-> Callbacks KeyCursor e'
-> Aff (idb :: IDB | e) Unit
:: forall index
. (IDBIndex index)
=> index
-> Maybe KeyRange
-> CursorDirection
-> Callbacks KeyCursor
-> Aff Unit
openKeyCursor index range dir cb =
fromEffFnAff $ Fn.runFn4 _openKeyCursor index (toNullable range) (show dir) cb
fromEffectFnAff $ Fn.runFn4 _openKeyCursor index (toNullable range) (show dir) cb
--------------------
-- ATTRIBUTES
--
-- | Returns the key path of the index.
keyPath
:: Index
-> KeyPath
:: Index
-> KeyPath
keyPath =
_keyPath
-- | Returns true if the index's multiEntry flag is set.
multiEntry
:: Index
-> Boolean
:: Index
-> Boolean
multiEntry =
_multiEntry
-- | Returns the name of the index.
name
:: Index
-> String
:: Index
-> String
name =
_name
-- | Returns the IDBObjectStore the index belongs to.
objectStore
:: Index
-> ObjectStore
:: Index
-> ObjectStore
objectStore =
_objectStore
-- | Returns true if the index's unique flag is set.
unique
:: Index
-> Boolean
:: Index
-> Boolean
unique =
_unique
--------------------
-- FFI
--
foreign import _keyPath
:: Index
-> Array String
:: Index
-> Array String
foreign import _multiEntry
:: Index
-> Boolean
:: Index
-> Boolean
foreign import _name
:: Index
-> String
:: Index
-> String
foreign import _objectStore
:: Index
-> ObjectStore
:: Index
-> ObjectStore
foreign import _unique
:: Index
-> Boolean
:: Index
-> Boolean
foreign import _count
:: forall index e
. Fn2 index (Nullable KeyRange) (EffFnAff (idb :: IDB | e) Int)
:: forall index
. Fn2 index (Nullable KeyRange) (EffectFnAff Int)
foreign import _get
:: forall index e
. Fn2 index KeyRange (EffFnAff (idb :: IDB | e) (Nullable Foreign))
:: forall index
. Fn2 index KeyRange (EffectFnAff (Nullable Foreign))
foreign import _getAllKeys
:: forall index e
. Fn3 index (Nullable KeyRange) (Nullable Int) (EffFnAff (idb :: IDB | e) (Array Foreign))
:: forall index
. Fn3 index (Nullable KeyRange) (Nullable Int) (EffectFnAff (Array Foreign))
foreign import _getKey
:: forall index e
. Fn2 index KeyRange (EffFnAff (idb :: IDB | e) (Nullable Foreign))
:: forall index
. Fn2 index KeyRange (EffectFnAff (Nullable Foreign))
foreign import _openCursor
:: forall index e e'
. Fn4 index (Nullable KeyRange) String (Callbacks ValueCursor e') (EffFnAff (idb :: IDB | e) Unit)
:: forall index
. Fn4 index (Nullable KeyRange) String (Callbacks ValueCursor) (EffectFnAff Unit)
foreign import _openKeyCursor
:: forall index e e'
. Fn4 index (Nullable KeyRange) String (Callbacks KeyCursor e') (EffFnAff (idb :: IDB | e) Unit)
:: forall index
. Fn4 index (Nullable KeyRange) String (Callbacks KeyCursor) (EffectFnAff Unit)

View File

@ -1,8 +1,8 @@
exports._dateTimeToForeign = function _dateTimeToForeign(y, m, d, h, mi, s, ms) {
export function _dateTimeToForeign(y, m, d, h, mi, s, ms) {
return new Date(y, m, d, h, mi, s, ms);
};
exports._readDateTime = function _readDateTime(parse, right, left, date) {
export function _readDateTime(parse, right, left, date) {
if (Object.getPrototypeOf(date) !== Date.prototype) {
return left(typeof date);
}
@ -24,7 +24,7 @@ exports._readDateTime = function _readDateTime(parse, right, left, date) {
return right(mdate);
};
exports._unsafeReadDateTime = function _unsafeReadDateTime(parse, date) {
export function _unsafeReadDateTime(parse, date) {
const y = date.getFullYear();
const m = date.getMonth() + 1;
const d = date.getDate();

View File

@ -3,35 +3,36 @@
-- | NOTE: Binary keys aren't supported yet.
module Database.IndexedDB.IDBKey.Internal
( Key
, class IDBKey, toKey , fromKey , unsafeFromKey
, class IDBKey
, toKey
, fromKey
, unsafeFromKey
, none
) where
import Prelude
import Control.Alt ((<|>))
import Control.Monad.Except (ExceptT(..), runExceptT)
import Data.Date as Date
import Data.DateTime (DateTime(..), Time(..))
import Data.Either (Either(..), either, isRight)
import Data.Enum (fromEnum, toEnum)
import Data.Foreign as Foreign
import Data.Foreign (Foreign, F)
import Data.Function.Uncurried as Fn
import Control.Alt ((<|>))
import Control.Monad.Except (ExceptT(..), runExceptT)
import Data.Date as Date
import Data.DateTime (DateTime(..), Time(..))
import Data.Either (Either(..), either, isRight)
import Data.Enum (fromEnum, toEnum)
import Foreign as Foreign
import Foreign (Foreign, F)
import Data.Function.Uncurried as Fn
import Data.Function.Uncurried (Fn2, Fn4, Fn7)
import Data.Identity (Identity(..))
import Data.List.NonEmpty (NonEmptyList(..))
import Data.List.Types (List(..))
import Data.Maybe (Maybe(..))
import Data.NonEmpty (NonEmpty(..))
import Data.Nullable (Nullable, toNullable)
import Data.Time as Time
import Data.Traversable (traverse)
import Data.Identity (Identity(..))
import Data.List.NonEmpty (NonEmptyList(..))
import Data.List.Types (List(..))
import Data.Maybe (Maybe(..))
import Data.NonEmpty (NonEmpty(..))
import Data.Nullable (Nullable, toNullable)
import Data.Time as Time
import Data.Traversable (traverse)
newtype Key = Key Foreign
--------------------
-- INTERFACES
--
@ -39,100 +40,90 @@ newtype Key = Key Foreign
-- | to a known type (e.g if you only strings as keys, or perfectly knows the
-- | type of a given key).
class IDBKey a where
toKey :: a -> Key
fromKey :: Key -> F a
toKey :: a -> Key
fromKey :: Key -> F a
unsafeFromKey :: Key -> a
none :: Maybe Key
none =
Nothing
--------------------
-- INSTANCES
--
instance eqKey :: Eq Key where
eq a b = (runExceptT >>> runIdentity >>> isRight) $
eq <$> ((fromKey a) :: F Int) <*> fromKey b
<|>
eq <$> ((fromKey a) :: F Number) <*> fromKey b
<|>
eq <$> ((fromKey a) :: F String) <*> fromKey b
<|>
eq <$> ((fromKey a) :: F DateTime) <*> fromKey b
<|>
eq <$> ((fromKey a) :: F (Array Key)) <*> fromKey b
eq <$> ((fromKey a) :: F Int) <*> fromKey b
<|>
eq <$> ((fromKey a) :: F Number) <*> fromKey b
<|>
eq <$> ((fromKey a) :: F String) <*> fromKey b
<|>
eq <$> ((fromKey a) :: F DateTime) <*> fromKey b
<|>
eq <$> ((fromKey a) :: F (Array Key)) <*> fromKey b
where
runIdentity :: forall a. Identity a -> a
runIdentity (Identity x) = x
runIdentity :: forall a. Identity a -> a
runIdentity (Identity x) = x
instance ordKey :: Ord Key where
compare a b = (runExceptT >>> runIdentity >>> either (const LT) id) $
compare <$> ((fromKey a) :: F Int) <*> fromKey b
<|>
compare <$> ((fromKey a) :: F Number) <*> fromKey b
<|>
compare <$> ((fromKey a) :: F String) <*> fromKey b
<|>
compare <$> ((fromKey a) :: F DateTime) <*> fromKey b
<|>
compare <$> ((fromKey a) :: F (Array Key)) <*> fromKey b
compare a b = (runExceptT >>> runIdentity >>> either (const LT) identity) $
compare <$> ((fromKey a) :: F Int) <*> fromKey b
<|>
compare <$> ((fromKey a) :: F Number) <*> fromKey b
<|>
compare <$> ((fromKey a) :: F String) <*> fromKey b
<|>
compare <$> ((fromKey a) :: F DateTime) <*> fromKey b
<|>
compare <$> ((fromKey a) :: F (Array Key)) <*> fromKey b
where
runIdentity :: forall a. Identity a -> a
runIdentity (Identity x) = x
runIdentity :: forall a. Identity a -> a
runIdentity (Identity x) = x
instance showKey :: Show Key where
show a = (runExceptT >>> format) $
(show <$> (fromKey a :: F Int))
<|>
(show <$> (fromKey a :: F Number))
<|>
(show <$> (fromKey a :: F String))
<|>
(show <$> (fromKey a :: F DateTime))
<|>
(show <$> (fromKey a :: F (Array Key)))
(show <$> (fromKey a :: F Int))
<|>
(show <$> (fromKey a :: F Number))
<|>
(show <$> (fromKey a :: F String))
<|>
(show <$> (fromKey a :: F DateTime))
<|>
(show <$> (fromKey a :: F (Array Key)))
where
format :: forall a. Identity (Either a String) -> String
format (Identity x) =
either (const "(Key)") (\s -> "(Key " <> s <> ")") x
format :: forall a. Identity (Either a String) -> String
format (Identity x) =
either (const "(Key)") (\s -> "(Key " <> s <> ")") x
instance idbKeyKey :: IDBKey Key where
toKey = id
fromKey = pure
unsafeFromKey = id
toKey = identity
fromKey = pure
unsafeFromKey = identity
instance idbKeyForeign :: IDBKey Foreign where
toKey = Key
fromKey (Key f) = pure f
toKey = Key
fromKey (Key f) = pure f
unsafeFromKey (Key f) = f
instance idbKeyInt :: IDBKey Int where
toKey = Foreign.toForeign >>> Key
fromKey (Key f) = Foreign.readInt f
toKey = Foreign.unsafeToForeign >>> Key
fromKey (Key f) = Foreign.readInt f
unsafeFromKey (Key f) = Foreign.unsafeFromForeign f
instance idbKeyNumber :: IDBKey Number where
toKey = Foreign.toForeign >>> Key
fromKey (Key f) = Foreign.readNumber f
toKey = Foreign.unsafeToForeign >>> Key
fromKey (Key f) = Foreign.readNumber f
unsafeFromKey (Key f) = Foreign.unsafeFromForeign f
instance idbKeyString :: IDBKey String where
toKey = Foreign.toForeign >>> Key
fromKey (Key f) = Foreign.readString f
toKey = Foreign.unsafeToForeign >>> Key
fromKey (Key f) = Foreign.readString f
unsafeFromKey (Key f) = Foreign.unsafeFromForeign f
instance idbKeyDate :: IDBKey DateTime where
toKey (DateTime d t) = Key $ Fn.runFn7 _dateTimeToForeign
toKey (DateTime d t) = Key $ Fn.runFn7 _dateTimeToForeign
(fromEnum $ Date.year d)
(fromEnum $ Date.month d)
(fromEnum $ Date.day d)
@ -140,58 +131,51 @@ instance idbKeyDate :: IDBKey DateTime where
(fromEnum $ Time.minute t)
(fromEnum $ Time.second t)
(fromEnum $ Time.millisecond t)
fromKey (Key f) = Fn.runFn4 _readDateTime dateTime dateTimeF dateTimeE f
fromKey (Key f) = Fn.runFn4 _readDateTime dateTime dateTimeF dateTimeE f
unsafeFromKey (Key f) = Fn.runFn2 _unsafeReadDateTime dateTime f
instance idbKeyArray :: IDBKey a => IDBKey (Array a) where
toKey = Foreign.toForeign >>> Key
fromKey (Key f) = Foreign.readArray f >>= traverse (Key >>> fromKey)
toKey = Foreign.unsafeToForeign >>> Key
fromKey (Key f) = Foreign.readArray f >>= traverse (Key >>> fromKey)
unsafeFromKey (Key f) = map unsafeFromKey (Foreign.unsafeFromForeign f)
-- FFI constructor to build a DateTime from years, months, days, hours, minutes, seconds and millis
dateTime
:: Int -- ^ years
-> Int -- ^ months
-> Int -- ^ days
-> Int -- ^ hours
-> Int -- ^ minutes
-> Int -- ^ seconds
-> Int -- ^ milliseconds
-> Nullable DateTime
:: Int -- ^ years
-> Int -- ^ months
-> Int -- ^ days
-> Int -- ^ hours
-> Int -- ^ minutes
-> Int -- ^ seconds
-> Int -- ^ milliseconds
-> Nullable DateTime
dateTime y m d h mi s ms =
toNullable $ DateTime
<$> (Date.canonicalDate <$> toEnum y <*> toEnum m <*> toEnum d)
<*> (Time <$> toEnum h <*> toEnum mi <*> toEnum s <*> toEnum ms)
<$> (Date.canonicalDate <$> toEnum y <*> toEnum m <*> toEnum d)
<*> (Time <$> toEnum h <*> toEnum mi <*> toEnum s <*> toEnum ms)
-- FFI constructor to convert a JS `Date` into a successful `F DateTime`
dateTimeF
:: DateTime
-> F DateTime
:: DateTime
-> F DateTime
dateTimeF =
Right >>> Identity >>> ExceptT
-- FFI constructor to convert a string into an errored `F DateTime`
dateTimeE
:: String
-> F DateTime
:: String
-> F DateTime
dateTimeE =
Foreign.TypeMismatch "Date" >>> flip NonEmpty Nil >>> NonEmptyList >>> Left >>> Identity >>> ExceptT
--------------------
-- FFI
--
foreign import _dateTimeToForeign
:: Fn7 Int Int Int Int Int Int Int Foreign
:: Fn7 Int Int Int Int Int Int Int Foreign
foreign import _readDateTime
:: Fn4 (Int -> Int -> Int -> Int -> Int -> Int -> Int -> Nullable DateTime) (DateTime -> F DateTime) (String -> F DateTime) Foreign (F DateTime)
:: Fn4 (Int -> Int -> Int -> Int -> Int -> Int -> Int -> Nullable DateTime) (DateTime -> F DateTime) (String -> F DateTime) Foreign (F DateTime)
foreign import _unsafeReadDateTime
:: Fn2 (Int -> Int -> Int -> Int -> Int -> Int -> Int -> Nullable DateTime) Foreign DateTime
:: Fn2 (Int -> Int -> Int -> Int -> Int -> Int -> Int -> Nullable DateTime) Foreign DateTime

View File

@ -1,4 +1,4 @@
exports._bound = function _bound(lower, upper, lowerOpen, upperOpen) {
export function _bound(lower, upper, lowerOpen, upperOpen) {
try {
return IDBKeyRange.bound(lower, upper, lowerOpen, upperOpen);
} catch (e) {
@ -6,34 +6,34 @@ exports._bound = function _bound(lower, upper, lowerOpen, upperOpen) {
}
};
exports._includes = function _includes(range, key) {
export function _includes(range, key) {
return range.includes(key);
};
exports._lower = function _lower(range) {
export function _lower(range) {
return range.lower;
};
exports._lowerBound = function _lowerBound(lower, open) {
export function _lowerBound(lower, open) {
return IDBKeyRange.lowerBound(lower, open);
};
exports._lowerOpen = function _lowerOpen(range) {
export function _lowerOpen(range) {
return range.lowerOpen;
};
exports._only = function _only(key) {
export function _only(key) {
return IDBKeyRange.only(key);
};
exports._upper = function _upper(range) {
export function _upper(range) {
return range.upper;
};
exports._upperBound = function _upperBound(upper, open) {
export function _upperBound(upper, open) {
return IDBKeyRange.upperBound(upper, open);
};
exports._upperOpen = function _upperOpen(range) {
export function _upperOpen(range) {
return range.upperOpen;
};

View File

@ -19,18 +19,17 @@ module Database.IndexedDB.IDBKeyRange
, upperOpen
) where
import Prelude (($), (>>>), map)
import Prelude (($), (>>>), map)
import Data.Foreign (Foreign)
import Data.Function.Uncurried as Fn
import Data.Function.Uncurried (Fn2, Fn4)
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe)
import Foreign (Foreign)
import Data.Function.Uncurried as Fn
import Data.Function.Uncurried (Fn2, Fn4)
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe)
import Database.IndexedDB.Core (class IDBKeyRange, KeyRange)
import Database.IndexedDB.Core (class IDBKeyRange, KeyRange)
import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, toKey, unsafeFromKey)
--------------------
-- TYPES
--
@ -38,55 +37,54 @@ import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, toKey, unsafeFromK
-- | Type alias for open
type Open = Boolean
--------------------
-- CONSTRUCTORS
--
-- | Returns a new IDBKeyRange spanning only key.
only
:: forall a. (IDBKey a)
=> a
-> KeyRange
:: forall a
. (IDBKey a)
=> a
-> KeyRange
only key =
_only (unsafeFromKey $ toKey key)
-- | Returns a new IDBKeyRange starting at key with no upper bound.
-- | If `Open` is `true`, key is not included in the range.
lowerBound
:: forall a. (IDBKey a)
=> a
-> Open
-> KeyRange
:: forall a
. (IDBKey a)
=> a
-> Open
-> KeyRange
lowerBound key open =
Fn.runFn2 _lowerBound (unsafeFromKey $ toKey key) open
-- | Returns a new IDBKeyRange with no lower bound and ending at key.
-- | If `Open` is `true`, key is not included in the range.
upperBound
:: forall a. (IDBKey a)
=> a
-> Open
-> KeyRange
:: forall a
. (IDBKey a)
=> a
-> Open
-> KeyRange
upperBound key open =
Fn.runFn2 _upperBound (unsafeFromKey $ toKey key) open
-- | Returns a new IDBKeyRange spanning from `lower` to `upper`.
-- | If `lowerOpen` is `true`, `lower` is not included in the range.
-- | If `upperOpen` is `true`, `upper` is not included in the range.
-- |
-- | It throws a `DataError` if the bound is invalid.
bound
:: forall key. (IDBKey key)
=> { lower :: key, upper :: key, lowerOpen :: Boolean, upperOpen :: Boolean }
-> Maybe KeyRange
:: forall key
. (IDBKey key)
=> { lower :: key, upper :: key, lowerOpen :: Boolean, upperOpen :: Boolean }
-> Maybe KeyRange
bound { lower: key1, upper: key2, lowerOpen: open1, upperOpen: open2 } =
toMaybe
$ Fn.runFn4 _bound (unsafeFromKey $ toKey key1) (unsafeFromKey $ toKey key2) open1 open2
$ Fn.runFn4 _bound (unsafeFromKey $ toKey key1) (unsafeFromKey $ toKey key2) open1 open2
--------------------
-- INTERFACE
@ -94,14 +92,15 @@ bound { lower: key1, upper: key2, lowerOpen: open1, upperOpen: open2 } =
-- | Returns true if key is included in the range, and false otherwise.
includes
:: forall key range. (IDBKey key) => (IDBKeyRange range)
:: forall key range
. (IDBKey key)
=> (IDBKeyRange range)
=> range
-> key
-> Boolean
includes range =
toKey >>> unsafeFromKey >>> Fn.runFn2 _includes range
--------------------
-- ATTRIBUTES
--
@ -112,7 +111,6 @@ lower
lower =
_lower >>> toMaybe >>> map toKey
-- | Returns upper bound if any.
upper
:: KeyRange
@ -120,7 +118,6 @@ upper
upper =
_upper >>> toMaybe >>> map toKey
-- | Returns true if the lower open flag is set, false otherwise.
lowerOpen
:: KeyRange
@ -128,7 +125,6 @@ lowerOpen
lowerOpen =
_lowerOpen
-- | Returns true if the upper open flag is set, false otherwise.
upperOpen
:: KeyRange
@ -136,7 +132,6 @@ upperOpen
upperOpen =
_upperOpen
--------------------
-- FFI
--
@ -148,35 +143,28 @@ foreign import _only
foreign import _lowerBound
:: Fn2 Foreign Boolean KeyRange
foreign import _upperBound
:: Fn2 Foreign Boolean KeyRange
foreign import _bound
:: Fn4 Foreign Foreign Boolean Boolean (Nullable KeyRange)
foreign import _includes
:: forall range
. Fn2 range Foreign Boolean
. Fn2 range Foreign Boolean
foreign import _lower
:: KeyRange
-> Nullable Foreign
foreign import _upper
:: KeyRange
-> Nullable Foreign
foreign import _lowerOpen
:: KeyRange
-> Boolean
foreign import _upperOpen
:: KeyRange
-> Boolean

View File

@ -15,7 +15,7 @@ const toArray = function toArray(xs) {
};
exports._add = function _add(store, value, key) {
export function _add(store, value, key) {
return function aff(error, success) {
try {
const request = store.add(value, key || undefined);
@ -31,11 +31,11 @@ exports._add = function _add(store, value, key) {
};
};
exports._autoIncrement = function _autoIncrement(store) {
export function _autoIncrement(store) {
return store.autoIncrement;
};
exports._clear = function _clear(store) {
export function _clear(store) {
return function aff(error, success) {
try {
const request = store.clear();
@ -51,7 +51,7 @@ exports._clear = function _clear(store) {
};
};
exports._createIndex = function _createIndex(store, name, path, params) {
export function _createIndex(store, name, path, params) {
return function aff(error, success) {
var keyPath;
@ -81,7 +81,7 @@ exports._createIndex = function _createIndex(store, name, path, params) {
};
};
exports._deleteIndex = function _deleteIndex(store, name) {
export function _deleteIndex(store, name) {
return function aff(error, success) {
try {
store.deleteIndex(name);
@ -96,7 +96,7 @@ exports._deleteIndex = function _deleteIndex(store, name) {
};
};
exports._delete = function _delete(store, query) {
export function _delete(store, query) {
return function aff(error, success) {
try {
const request = store.delete(query);
@ -112,7 +112,7 @@ exports._delete = function _delete(store, query) {
};
};
exports._index = function _index(store, name) {
export function _index(store, name) {
return function aff(error, success) {
try {
const index = store.index(name);
@ -127,11 +127,11 @@ exports._index = function _index(store, name) {
};
};
exports._indexNames = function _indexNames(store) {
export function _indexNames(store) {
return toArray(store.indexNames);
};
exports._keyPath = function _keyPath(store) {
export function _keyPath(store) {
const path = store.keyPath;
if (Array.isArray(path)) {
@ -145,11 +145,11 @@ exports._keyPath = function _keyPath(store) {
return [];
};
exports._name = function _name(store) {
export function _name(store) {
return store.name;
};
exports._put = function _put(store, value, key) {
export function _put(store, value, key) {
return function aff(error, success) {
try {
const request = store.put(value, key || undefined);
@ -165,6 +165,6 @@ exports._put = function _put(store, value, key) {
};
};
exports._transaction = function _transaction(store) {
export function _transaction(store) {
return store.transaction;
};

View File

@ -25,19 +25,18 @@ module Database.IndexedDB.IDBObjectStore
, module Database.IndexedDB.IDBIndex
) where
import Prelude (Unit, map, ($), (<$>), (>>>), (<<<))
import Control.Monad.Aff (Aff)
import Control.Monad.Aff.Compat (EffFnAff, fromEffFnAff)
import Data.Foreign (Foreign)
import Data.Function.Uncurried as Fn
import Data.Function.Uncurried (Fn2, Fn3, Fn4)
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toNullable)
import Database.IndexedDB.Core
import Database.IndexedDB.IDBIndex (count, get, getAllKeys, getKey, openCursor, openKeyCursor)
import Data.Function.Uncurried (Fn2, Fn3, Fn4)
import Data.Function.Uncurried as Fn
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toNullable)
import Database.IndexedDB.IDBIndex (count, get, getAllKeys, getKey, openCursor, openKeyCursor)
import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, unsafeFromKey, toKey)
import Effect.Aff (Aff)
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
import Foreign (Foreign)
import Prelude (Unit, map, ($), (<$>), (>>>), (<<<))
--------------------
-- TYPES
@ -46,7 +45,6 @@ import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key, unsafeFromKey, toK
-- | Type alias for IndexName
type IndexName = String
-- | Flags to set on the index.
-- |
-- | An index has a `unique` flag. When this flag is set, the index enforces that no
@ -61,18 +59,16 @@ type IndexName = String
-- | If the `multiEntry` flag is true, then the one record is added to the index for each
-- | of the subkeys.
type IndexParameters =
{ unique :: Boolean
{ unique :: Boolean
, multiEntry :: Boolean
}
defaultParameters :: IndexParameters
defaultParameters =
{ unique : false
, multiEntry : false
{ unique: false
, multiEntry: false
}
--------------------
-- INTERFACES
--
@ -85,23 +81,24 @@ defaultParameters =
-- | If add() is used, and if a record with the key already exists the request will fail,
-- | with a "ConstraintError" DOMException.
add
:: forall e key val store. (IDBKey key) => (IDBObjectStore store)
:: forall key val store
. (IDBKey key)
=> (IDBObjectStore store)
=> store
-> val
-> Maybe key
-> Aff (idb :: IDB | e) Key
-> Aff Key
add store value key =
map toKey $ fromEffFnAff $ Fn.runFn3 _add store value (toNullable $ (toKey >>> unsafeFromKey) <$> key)
map toKey $ fromEffectFnAff $ Fn.runFn3 _add store value (toNullable $ (toKey >>> unsafeFromKey) <$> key)
-- | Deletes all records in store.
clear
:: forall e store. (IDBObjectStore store)
:: forall store
. (IDBObjectStore store)
=> store
-> Aff (idb :: IDB | e) Unit
-> Aff Unit
clear =
fromEffFnAff <<< _clear
fromEffectFnAff <<< _clear
-- | Creates a new index in store with the given name, keyPath and options and
-- | returns a new IDBIndex. If the keyPath and options define constraints that
@ -110,47 +107,47 @@ clear =
-- |
-- | Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
createIndex
:: forall e store. (IDBObjectStore store)
:: forall store
. (IDBObjectStore store)
=> store
-> IndexName
-> KeyPath
-> IndexParameters
-> Aff (idb :: IDB | e) Index
-> Aff Index
createIndex store name' path params =
fromEffFnAff $ Fn.runFn4 _createIndex store name' path params
fromEffectFnAff $ Fn.runFn4 _createIndex store name' path params
-- | Deletes records in store with the given key or in the given key range in query.
delete
:: forall e store. (IDBObjectStore store)
:: forall store
. (IDBObjectStore store)
=> store
-> KeyRange
-> Aff (idb :: IDB | e) Unit
-> Aff Unit
delete store range =
fromEffFnAff $ Fn.runFn2 _delete store range
fromEffectFnAff $ Fn.runFn2 _delete store range
-- | Deletes the index in store with the given name.
-- |
-- | Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
deleteIndex
:: forall e store. (IDBObjectStore store)
:: forall store
. (IDBObjectStore store)
=> store
-> IndexName
-> Aff (idb :: IDB | e) Unit
-> Aff Unit
deleteIndex store name' =
fromEffFnAff $ Fn.runFn2 _deleteIndex store name'
fromEffectFnAff $ Fn.runFn2 _deleteIndex store name'
-- | Returns an IDBIndex for the index named name in store.
index
:: forall e store. (IDBObjectStore store)
:: forall store
. (IDBObjectStore store)
=> store
-> IndexName
-> Aff (idb :: IDB | e) Index
-> Aff Index
index store name' =
fromEffFnAff $ Fn.runFn2 _index store name'
fromEffectFnAff $ Fn.runFn2 _index store name'
-- | Adds or updates a record in store with the given value and key.
-- |
@ -159,14 +156,15 @@ index store name' =
-- |
-- | If put() is used, any existing record with the key will be replaced.
put
:: forall e val key store. (IDBKey key) => (IDBObjectStore store)
:: forall val key store
. (IDBKey key)
=> (IDBObjectStore store)
=> store
-> val
-> Maybe key
-> Aff (idb :: IDB | e) Key
-> Aff Key
put store value key =
map toKey $ fromEffFnAff $ Fn.runFn3 _put store value (toNullable $ (toKey >>> unsafeFromKey) <$> key)
map toKey $ fromEffectFnAff $ Fn.runFn3 _put store value (toNullable $ (toKey >>> unsafeFromKey) <$> key)
--------------------
-- ATTRIBUTES
@ -178,7 +176,6 @@ autoIncrement
autoIncrement =
_autoIncrement
--| Returns a list of the names of indexes in the store.
indexNames
:: ObjectStore
@ -186,7 +183,6 @@ indexNames
indexNames =
_indexNames
-- | Returns the key path of the store, or empty array if none
keyPath
:: ObjectStore
@ -194,7 +190,6 @@ keyPath
keyPath =
_keyPath
-- | Returns the name of the store.
name
:: ObjectStore
@ -202,7 +197,6 @@ name
name =
_name
-- | Returns the associated transaction.
transaction
:: ObjectStore
@ -210,65 +204,53 @@ transaction
transaction =
_transaction
--------------------
-- FFI
--
foreign import _add
:: forall e val store
. Fn3 store val (Nullable Foreign) (EffFnAff (idb :: IDB | e) Foreign)
:: forall val store
. Fn3 store val (Nullable Foreign) (EffectFnAff Foreign)
foreign import _autoIncrement
:: ObjectStore
-> Boolean
foreign import _clear
:: forall e store
. store
-> EffFnAff (idb :: IDB | e) Unit
:: forall store
. store
-> EffectFnAff Unit
foreign import _createIndex
:: forall e store
. Fn4 store String (Array String) { unique :: Boolean, multiEntry :: Boolean } (EffFnAff (idb :: IDB | e) Index)
:: forall store
. Fn4 store String (Array String) { unique :: Boolean, multiEntry :: Boolean } (EffectFnAff Index)
foreign import _delete
:: forall e store
. Fn2 store KeyRange (EffFnAff (idb :: IDB | e) Unit)
:: forall store
. Fn2 store KeyRange (EffectFnAff Unit)
foreign import _deleteIndex
:: forall e store
. Fn2 store String (EffFnAff (idb :: IDB | e) Unit)
:: forall store
. Fn2 store String (EffectFnAff Unit)
foreign import _index
:: forall e store
. Fn2 store String (EffFnAff (idb :: IDB | e) Index)
:: forall store
. Fn2 store String (EffectFnAff Index)
foreign import _indexNames
:: ObjectStore
-> Array String
foreign import _keyPath
:: ObjectStore
-> Array String
foreign import _name
:: ObjectStore
-> String
foreign import _put
:: forall e val store
. Fn3 store val (Nullable Foreign) (EffFnAff (idb :: IDB | e) Foreign)
:: forall val store
. Fn3 store val (Nullable Foreign) (EffectFnAff Foreign)
foreign import _transaction
:: ObjectStore

View File

@ -1,4 +1,4 @@
exports._abort = function _abort(tx) {
export function _abort(tx) {
return function aff(error, success) {
try {
tx.abort();
@ -13,17 +13,17 @@ exports._abort = function _abort(tx) {
};
};
exports._db = function _db(tx) {
export function _db(tx) {
return tx.db;
};
exports._error = function _error(tx) {
export function _error(tx) {
return tx.error == null
? null
: tx.error;
};
exports._mode = function _mode(ReadOnly, ReadWrite, VersionChange, tx) {
export function _mode(ReadOnly, ReadWrite, VersionChange, tx) {
if (tx.mode === 'readwrite') {
return ReadWrite;
} else if (tx.mode === 'versionchange') {
@ -33,7 +33,7 @@ exports._mode = function _mode(ReadOnly, ReadWrite, VersionChange, tx) {
return ReadOnly;
};
exports._objectStore = function _objectStore(tx, name) {
export function _objectStore(tx, name) {
return function aff(error, success) {
try {
const store = tx.objectStore(name);
@ -48,11 +48,11 @@ exports._objectStore = function _objectStore(tx, name) {
};
};
exports._objectStoreNames = function _objectStoreNames(tx) {
export function _objectStoreNames(tx) {
return tx.objectStoreNames;
};
exports._onAbort = function _onAbort(tx, f) {
export function _onAbort(tx, f) {
return function aff(error, success) {
tx.onabort = function onabort() {
f();
@ -65,7 +65,7 @@ exports._onAbort = function _onAbort(tx, f) {
};
};
exports._onComplete = function _onComplete(tx, f) {
export function _onComplete(tx, f) {
return function aff(error, success) {
tx.oncomplete = function oncomplete() {
f();
@ -78,7 +78,7 @@ exports._onComplete = function _onComplete(tx, f) {
};
};
exports._onError = function _onError(tx, f) {
export function _onError(tx, f) {
return function aff(error, success) {
tx.onerror = function onerror(e) {
f(e.target.error)();

View File

@ -14,19 +14,17 @@ module Database.IndexedDB.IDBTransaction
, onError
) where
import Prelude (Unit, ($), (<<<), (>>>))
import Control.Monad.Aff (Aff)
import Control.Monad.Aff.Compat (EffFnAff, fromEffFnAff)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Exception (Error)
import Data.Function.Uncurried as Fn
import Data.Function.Uncurried (Fn2, Fn4)
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe)
import Database.IndexedDB.Core
import Data.Function.Uncurried (Fn2, Fn4)
import Data.Function.Uncurried as Fn
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe)
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
import Effect.Exception (Error)
import Prelude (Unit, ($), (<<<), (>>>))
--------------------
-- INTERFACES
@ -35,21 +33,22 @@ import Database.IndexedDB.Core
-- | Aborts the transaction. All pending requests will fail with a "AbortError"
-- | DOMException and all changes made to the database will be reverted.
abort
:: forall e tx. (IDBTransaction tx)
:: forall tx
. (IDBTransaction tx)
=> tx
-> Aff (idb :: IDB | e) Unit
-> Aff Unit
abort =
fromEffFnAff <<< _abort
fromEffectFnAff <<< _abort
-- | Returns an IDBObjectStore in the transaction's scope.
objectStore
:: forall e tx. (IDBTransaction tx)
:: forall tx
. (IDBTransaction tx)
=> tx
-> String
-> Aff (idb :: IDB | e) ObjectStore
-> Aff ObjectStore
objectStore tx name =
fromEffFnAff $ Fn.runFn2 _objectStore tx name
fromEffectFnAff $ Fn.runFn2 _objectStore tx name
--------------------
-- ATTRIBUTES
@ -62,7 +61,6 @@ db
db =
_db
-- | If the transaction was aborted, returns the error (a DOMException) providing the reason.
error
:: Transaction
@ -70,7 +68,6 @@ error
error =
_error >>> toMaybe
-- | Returns the mode the transaction was created with (`ReadOnly|ReadWrite`)
-- | , or `VersionChange` for an upgrade transaction.
mode
@ -79,7 +76,6 @@ mode
mode =
Fn.runFn4 _mode ReadOnly ReadWrite VersionChange
-- | Returns a list of the names of object stores in the transactions scope.
-- | For an upgrade transaction this is all object stores in the database.
objectStoreNames
@ -88,85 +84,70 @@ objectStoreNames
objectStoreNames =
_objectStoreNames
--------------------
-- EVENT HANDLERS
--
-- | Event handler for the `abort` event.
onAbort
:: forall e e'
. Transaction
-> Eff ( | e') Unit
-> Aff (idb :: IDB | e) Unit
:: Transaction
-> Effect Unit
-> Aff Unit
onAbort db' f =
fromEffFnAff $ Fn.runFn2 _onAbort db' f
fromEffectFnAff $ Fn.runFn2 _onAbort db' f
-- | Event handler for the `complete` event.
onComplete
:: forall e e'
. Transaction
-> Eff ( | e') Unit
-> Aff (idb :: IDB | e) Unit
:: Transaction
-> Effect Unit
-> Aff Unit
onComplete db' f =
fromEffFnAff $ Fn.runFn2 _onComplete db' f
fromEffectFnAff $ Fn.runFn2 _onComplete db' f
-- | Event handler for the `error` event.
onError
:: forall e e'
. Transaction
-> (Error -> Eff ( | e') Unit)
-> Aff (idb :: IDB | e) Unit
:: Transaction
-> (Error -> Effect Unit)
-> Aff Unit
onError db' f =
fromEffFnAff $ Fn.runFn2 _onError db' f
fromEffectFnAff $ Fn.runFn2 _onError db' f
--------------------
-- FFI
--
foreign import _abort
:: forall tx e
. tx
-> EffFnAff (idb :: IDB | e) Unit
:: forall tx
. tx
-> EffectFnAff Unit
foreign import _db
:: Transaction
-> Database
foreign import _error
:: Transaction
-> (Nullable Error)
foreign import _mode
:: Fn4 TransactionMode TransactionMode TransactionMode Transaction TransactionMode
foreign import _objectStoreNames
:: Transaction
-> Array String
foreign import _objectStore
:: forall tx e
. Fn2 tx String (EffFnAff (idb :: IDB | e) ObjectStore)
:: forall tx
. Fn2 tx String (EffectFnAff ObjectStore)
foreign import _onAbort
:: forall tx e e'
. Fn2 tx (Eff ( | e') Unit) (EffFnAff (idb :: IDB | e) Unit)
:: forall tx
. Fn2 tx (Effect Unit) (EffectFnAff Unit)
foreign import _onComplete
:: forall tx e e'
. Fn2 tx (Eff ( | e') Unit) (EffFnAff (idb :: IDB | e) Unit)
:: forall tx
. Fn2 tx (Effect Unit) (EffectFnAff Unit)
foreign import _onError
:: forall tx e e'
. Fn2 tx (Error -> Eff ( | e') Unit) (EffFnAff (idb :: IDB | e) Unit)
:: forall tx
. Fn2 tx (Error -> Effect Unit) (EffectFnAff Unit)

25
test.html Normal file
View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css">
</head>
<body>
<div id="mocha"></div>
<script src="https://unpkg.com/chai/chai.js"></script>
<script src="https://unpkg.com/mocha/mocha.js"></script>
<script class="mocha-init">
mocha.setup('bdd');
mocha.checkLeaks();
</script>
<script src="test.js"></script>
<script>
mocha.run()
</script>
</body>
</html>

File diff suppressed because it is too large Load Diff