mirror of
https://github.com/ilyakooo0/purescript-indexedDB.git
synced 2024-09-11 06:55:48 +03:00
move open and deleteDatabase in IDBFactory module
This commit is contained in:
parent
08b42d717d
commit
86afd03dbe
@ -3,9 +3,6 @@ const Core = require('Database.IndexedDB.Core');
|
||||
const $Core = require('Database.IndexedDB.Core/foreign');
|
||||
|
||||
const toArray = $Core.toArray;
|
||||
const noOp2 = $Core.noOp2;
|
||||
const errorHandler = $Core.errorHandler;
|
||||
const eventHandler = $Core.eventHandler;
|
||||
|
||||
|
||||
exports.close = function close(db) {
|
||||
@ -44,18 +41,6 @@ exports._deleteObjectStore = function _deleteObjectStore(db, name) {
|
||||
};
|
||||
};
|
||||
|
||||
exports.deleteDatabase = function deleteDatabase(name) {
|
||||
return function aff(success, error) {
|
||||
const request = indexedDB.deleteDatabase(name);
|
||||
|
||||
request.onsuccess = function onSuccess(e) {
|
||||
success(e.oldVersion);
|
||||
};
|
||||
|
||||
request.onerror = errorHandler(error);
|
||||
};
|
||||
};
|
||||
|
||||
exports.name = function name(db) {
|
||||
return db.name;
|
||||
};
|
||||
@ -64,21 +49,6 @@ exports.objectStoreNames = function objectStoreNames(db) {
|
||||
return toArray(db.objectStoreNames);
|
||||
};
|
||||
|
||||
exports._open = function _open(name, mver, req) {
|
||||
const ver = Maybe.fromMaybe(undefined)(mver);
|
||||
|
||||
return function aff(success, error) {
|
||||
const request = indexedDB.open(name, ver);
|
||||
request.onsuccess = function onSuccess(e) {
|
||||
success(e.target.result);
|
||||
};
|
||||
|
||||
request.onerror = errorHandler(error);
|
||||
request.onblocked = eventHandler(Maybe.fromMaybe(noOp2)(req.onBlocked));
|
||||
request.onupgradeneeded = eventHandler(Maybe.fromMaybe(noOp2)(req.onUpgradeNeeded));
|
||||
};
|
||||
};
|
||||
|
||||
exports._transaction = function _transaction(db, stores, mode) {
|
||||
return function eff() {
|
||||
var mode_;
|
||||
|
@ -13,12 +13,6 @@ import Data.Maybe(Maybe)
|
||||
import Database.IndexedDB.Core
|
||||
|
||||
|
||||
type IDBOpenRequest eff =
|
||||
{ onBlocked :: Maybe (IDBDatabase -> Eff (idb :: INDEXED_DB | eff) Unit)
|
||||
, onUpgradeNeeded :: Maybe (IDBDatabase -> Eff (idb :: INDEXED_DB | eff) Unit)
|
||||
}
|
||||
|
||||
|
||||
foreign import close :: forall eff. IDBDatabase -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit
|
||||
|
||||
|
||||
@ -34,21 +28,12 @@ deleteObjectStore db name =
|
||||
Fn.runFn2 _deleteObjectStore db name
|
||||
|
||||
|
||||
foreign import deleteDatabase :: forall eff. String -> Aff (idb :: INDEXED_DB | eff) Int
|
||||
|
||||
|
||||
foreign import name :: IDBDatabase -> String
|
||||
|
||||
|
||||
foreign import objectStoreNames :: IDBDatabase -> Array String
|
||||
|
||||
|
||||
foreign import _open :: forall eff. Fn3 String (Maybe Int) (IDBOpenRequest eff) (Aff (idb :: INDEXED_DB | eff) IDBDatabase)
|
||||
open :: forall eff . String -> Maybe Int -> IDBOpenRequest eff -> Aff (idb :: INDEXED_DB | eff) IDBDatabase
|
||||
open name mver req =
|
||||
Fn.runFn3 _open name mver req
|
||||
|
||||
|
||||
foreign import _transaction :: forall eff. Fn3 IDBDatabase (Array String) IDBTransactionMode (Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) IDBTransaction)
|
||||
transaction :: forall eff. IDBDatabase -> Array String -> IDBTransactionMode -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) IDBTransaction
|
||||
transaction db stores mode =
|
||||
|
34
src/Database/IndexedDB/IDBFactory.js
Normal file
34
src/Database/IndexedDB/IDBFactory.js
Normal file
@ -0,0 +1,34 @@
|
||||
const Maybe = require('Data.Maybe');
|
||||
const $Core = require('Database.IndexedDB.Core/foreign');
|
||||
|
||||
const noOp2 = $Core.noOp2;
|
||||
const errorHandler = $Core.errorHandler;
|
||||
const eventHandler = $Core.eventHandler;
|
||||
|
||||
|
||||
exports.deleteDatabase = function deleteDatabase(name) {
|
||||
return function aff(success, error) {
|
||||
const request = indexedDB.deleteDatabase(name);
|
||||
|
||||
request.onsuccess = function onSuccess(e) {
|
||||
success(e.oldVersion);
|
||||
};
|
||||
|
||||
request.onerror = errorHandler(error);
|
||||
};
|
||||
};
|
||||
|
||||
exports._open = function _open(name, mver, req) {
|
||||
const ver = Maybe.fromMaybe(undefined)(mver);
|
||||
|
||||
return function aff(success, error) {
|
||||
const request = indexedDB.open(name, ver);
|
||||
request.onsuccess = function onSuccess(e) {
|
||||
success(e.target.result);
|
||||
};
|
||||
|
||||
request.onerror = errorHandler(error);
|
||||
request.onblocked = eventHandler(Maybe.fromMaybe(noOp2)(req.onBlocked));
|
||||
request.onupgradeneeded = eventHandler(Maybe.fromMaybe(noOp2)(req.onUpgradeNeeded));
|
||||
};
|
||||
};
|
27
src/Database/IndexedDB/IDBFactory.purs
Normal file
27
src/Database/IndexedDB/IDBFactory.purs
Normal file
@ -0,0 +1,27 @@
|
||||
module Database.IndexedDB.IDBFactory where
|
||||
|
||||
import Prelude
|
||||
|
||||
import Control.Monad.Aff(Aff)
|
||||
import Control.Monad.Eff(kind Effect, Eff)
|
||||
import Control.Monad.Eff.Exception(EXCEPTION)
|
||||
import Data.Function.Uncurried as Fn
|
||||
import Data.Function.Uncurried(Fn3)
|
||||
import Data.Maybe(Maybe)
|
||||
|
||||
import Database.IndexedDB.Core
|
||||
|
||||
|
||||
type IDBOpenRequest eff =
|
||||
{ onBlocked :: Maybe (IDBDatabase -> Eff (idb :: INDEXED_DB | eff) Unit)
|
||||
, onUpgradeNeeded :: Maybe (IDBDatabase -> Eff (idb :: INDEXED_DB | eff) Unit)
|
||||
}
|
||||
|
||||
|
||||
foreign import deleteDatabase :: forall eff. String -> Aff (idb :: INDEXED_DB | eff) Int
|
||||
|
||||
|
||||
foreign import _open :: forall eff. Fn3 String (Maybe Int) (IDBOpenRequest eff) (Aff (idb :: INDEXED_DB | eff) IDBDatabase)
|
||||
open :: forall eff . String -> Maybe Int -> IDBOpenRequest eff -> Aff (idb :: INDEXED_DB | eff) IDBDatabase
|
||||
open name mver req =
|
||||
Fn.runFn3 _open name mver req
|
Loading…
Reference in New Issue
Block a user