mirror of
https://github.com/ilyakooo0/purescript-indexedDB.git
synced 2024-09-11 06:55:48 +03:00
implement direct accessors for IDBObjectStore + fix createObjectStore impl
This commit is contained in:
parent
43f28e480b
commit
846ad4dd1b
@ -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);
|
||||
|
@ -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)
|
||||
|
30
src/Database/IndexedDB/IDBObjectStore.js
Normal file
30
src/Database/IndexedDB/IDBObjectStore.js
Normal 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;
|
||||
};
|
24
src/Database/IndexedDB/IDBObjectStore.purs
Normal file
24
src/Database/IndexedDB/IDBObjectStore.purs
Normal 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
|
Loading…
Reference in New Issue
Block a user