implement direct accessors for IDBObjectStore + fix createObjectStore impl

This commit is contained in:
KtorZ 2017-06-23 14:28:42 +02:00
parent 43f28e480b
commit 846ad4dd1b
No known key found for this signature in database
GPG Key ID: 3F72E8BC2894C015
4 changed files with 76 additions and 10 deletions

View File

@ -14,15 +14,27 @@ exports.close = function close(db) {
};
};
exports._createObjectStore = function _createObjectStore(fromMaybe, db, name, opts) {
const keyPath = fromMaybe(undefined)(opts.keyPath);
const autoIncrement = opts.autoIncrement;
exports._createObjectStore = function _createObjectStore(db, name, opts) {
return function eff() {
var keyPath;
try {
// NOTE 1: createObjectStore throws when given an empty array
// NOTE 2: keyPath supports strings and sequence of strings, however
// a string hasn't the same meaning as a sequence of strings
switch (opts.keyPath.length) {
case 0:
keyPath = undefined;
break;
case 1:
keyPath = opts.keyPath[0];
break;
default:
keyPath = opts.keyPath;
}
return db.createObjectStore(name, {
autoIncrement: opts.autoIncrement,
keyPath: keyPath,
autoIncrement: autoIncrement,
});
} catch (e) {
throw new Error(e.name);

View File

@ -6,8 +6,8 @@ import Control.Monad.Aff(Aff)
import Control.Monad.Eff(Eff)
import Control.Monad.Eff.Exception(EXCEPTION)
import Data.Function.Uncurried as Fn
import Data.Function.Uncurried(Fn2, Fn3, Fn4)
import Data.Maybe(Maybe, fromMaybe)
import Data.Function.Uncurried(Fn2, Fn3)
import Data.Maybe(Maybe)
import Database.IndexedDB.Core
@ -15,10 +15,10 @@ import Database.IndexedDB.Core
foreign import close :: forall eff. IDBDatabase -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit
foreign import _createObjectStore :: forall a eff. Fn4 (a -> Maybe a -> a) IDBDatabase String { keyPath :: Maybe String, autoIncrement :: Boolean } (Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) IDBObjectStore)
createObjectStore :: forall eff. IDBDatabase -> String -> { keyPath :: Maybe String, autoIncrement :: Boolean } -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) IDBObjectStore
foreign import _createObjectStore :: forall a eff. Fn3 IDBDatabase String { keyPath :: Array String, autoIncrement :: Boolean } (Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) IDBObjectStore)
createObjectStore :: forall eff. IDBDatabase -> String -> { keyPath :: Array String, autoIncrement :: Boolean } -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) IDBObjectStore
createObjectStore db name opts =
Fn.runFn4 _createObjectStore fromMaybe db name opts
Fn.runFn3 _createObjectStore db name opts
foreign import _deleteObjectStore :: forall eff. Fn2 IDBDatabase String (Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) IDBObjectStore)

View File

@ -0,0 +1,30 @@
const $Core = require('Database.IndexedDB.Core/foreign');
const toArray = $Core.toArray;
exports.autoIncrement = function autoIncrement(store) {
return store.autoIncrement;
};
exports.indexNames = function indexNames(store) {
return toArray(store.indexNames);
};
exports.keyPath = function keyPath(store) {
const path = store.keyPath;
if (Array.isArray(path)) {
return path;
}
if (typeof path === 'string') {
return path.split(':');
}
return [];
};
exports.name = function name(store) {
return store.name;
};

View File

@ -0,0 +1,24 @@
module Database.IndexedDB.IDBObjectStore where
import Prelude
import Control.Monad.Aff(Aff)
import Control.Monad.Eff(Eff)
import Control.Monad.Eff.Exception(EXCEPTION)
import Data.Function.Uncurried as Fn
import Data.Function.Uncurried(Fn2, Fn3, Fn4)
import Data.Maybe(Maybe, fromMaybe)
import Database.IndexedDB.Core
foreign import autoIncrement :: IDBObjectStore -> Boolean
foreign import indexNames :: IDBObjectStore -> Array String
foreign import keyPath :: IDBObjectStore -> Array String
foreign import name :: IDBObjectStore -> String