mirror of
https://github.com/ilyakooo0/purescript-indexedDB.git
synced 2024-09-11 06:55:48 +03:00
provide oldVersion metadata as part of the onUpgradeNeeded callback
This commit is contained in:
parent
51b48b643c
commit
c64384da82
@ -14,6 +14,9 @@ const noOp2 = function noOp2() {
|
|||||||
return noOp;
|
return noOp;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const noOp3 = function noOp3() {
|
||||||
|
return noOp2;
|
||||||
|
};
|
||||||
|
|
||||||
exports._deleteDatabase = function _deleteDatabase(name) {
|
exports._deleteDatabase = function _deleteDatabase(name) {
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
@ -46,7 +49,8 @@ exports._open = function _open(fromMaybe, name, mver, req) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
request.onupgradeneeded = function onUpgradeNeeded(e) {
|
request.onupgradeneeded = function onUpgradeNeeded(e) {
|
||||||
fromMaybe(noOp2)(req.onUpgradeNeeded)(e.target.result)(e.target.transaction)();
|
const meta = { oldVersion: e.oldVersion };
|
||||||
|
fromMaybe(noOp3)(req.onUpgradeNeeded)(e.target.result)(e.target.transaction)(meta)();
|
||||||
};
|
};
|
||||||
|
|
||||||
request.onerror = errorHandler(error);
|
request.onerror = errorHandler(error);
|
||||||
|
@ -30,7 +30,7 @@ import Database.IndexedDB.Core
|
|||||||
-- Type alias for binding listeners to an initial open action.
|
-- Type alias for binding listeners to an initial open action.
|
||||||
type Callbacks e =
|
type Callbacks e =
|
||||||
{ onBlocked :: Maybe (Eff (| e) Unit)
|
{ onBlocked :: Maybe (Eff (| e) Unit)
|
||||||
, onUpgradeNeeded :: Maybe (Database -> Transaction -> Eff (| e) Unit)
|
, onUpgradeNeeded :: Maybe (Database -> Transaction -> { oldVersion :: Int } -> Eff (| e) Unit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -87,23 +87,25 @@ main = runMocha do
|
|||||||
}
|
}
|
||||||
tearDown name version db
|
tearDown name version db
|
||||||
|
|
||||||
|
|
||||||
it "open + onUpgradeNeed" do
|
it "open + onUpgradeNeed" do
|
||||||
let name = "db-upgrade-needed"
|
let name = "db-upgrade-needed"
|
||||||
version = 1
|
version = 1
|
||||||
callback var db _ = do
|
callback (Tuple varName varVersion) db _ { oldVersion } = do
|
||||||
_ <- launchAff $ modifyVar (const $ IDBDatabase.name db) var
|
_ <- launchAff $ modifyVar (const $ IDBDatabase.name db) varName
|
||||||
|
_ <- launchAff $ modifyVar (const $ oldVersion) varVersion
|
||||||
pure unit
|
pure unit
|
||||||
var <- makeVar' "-"
|
varName <- makeVar' "-"
|
||||||
db <- IDBFactory.open name Nothing
|
varVersion <- makeVar' (-1)
|
||||||
{ onUpgradeNeeded : Just (callback var)
|
db <- IDBFactory.open name Nothing
|
||||||
|
{ onUpgradeNeeded : Just (callback (Tuple varName varVersion))
|
||||||
, onBlocked : Nothing
|
, onBlocked : Nothing
|
||||||
}
|
}
|
||||||
name <- peekVar var
|
name' <- peekVar varName
|
||||||
name `shouldEqual` name
|
version' <- peekVar varVersion
|
||||||
|
name' `shouldEqual` name
|
||||||
|
version' `shouldEqual` 0
|
||||||
tearDown name version db
|
tearDown name version db
|
||||||
|
|
||||||
|
|
||||||
it "open + onBlocked" do
|
it "open + onBlocked" do
|
||||||
let name = "db-blocked"
|
let name = "db-blocked"
|
||||||
version = 14
|
version = 14
|
||||||
@ -124,8 +126,8 @@ main = runMocha do
|
|||||||
{ onUpgradeNeeded : Nothing
|
{ onUpgradeNeeded : Nothing
|
||||||
, onBlocked : Just (callback var)
|
, onBlocked : Just (callback var)
|
||||||
}
|
}
|
||||||
name <- peekVar var
|
name' <- peekVar var
|
||||||
name `shouldEqual` name
|
name' `shouldEqual` name
|
||||||
tearDown name version db02
|
tearDown name version db02
|
||||||
|
|
||||||
describe "IDBKeyRange" do
|
describe "IDBKeyRange" do
|
||||||
@ -289,7 +291,7 @@ main = runMocha do
|
|||||||
pure unit
|
pure unit
|
||||||
|
|
||||||
setup storeParams = do
|
setup storeParams = do
|
||||||
let onUpgradeNeeded var db _ = launchAff' do
|
let onUpgradeNeeded var db _ _ = launchAff' do
|
||||||
store <- IDBDatabase.createObjectStore db "store" storeParams
|
store <- IDBDatabase.createObjectStore db "store" storeParams
|
||||||
_ <- putVar var { db, store }
|
_ <- putVar var { db, store }
|
||||||
pure unit
|
pure unit
|
||||||
@ -327,7 +329,7 @@ main = runMocha do
|
|||||||
tearDown db
|
tearDown db
|
||||||
|
|
||||||
it "deleteObjectStore" do
|
it "deleteObjectStore" do
|
||||||
let onUpgradeNeeded var db _ = launchAff' do
|
let onUpgradeNeeded var db _ _ = launchAff' do
|
||||||
_ <- IDBDatabase.deleteObjectStore db "store"
|
_ <- IDBDatabase.deleteObjectStore db "store"
|
||||||
putVar var true
|
putVar var true
|
||||||
|
|
||||||
@ -350,7 +352,7 @@ main = runMocha do
|
|||||||
pure unit
|
pure unit
|
||||||
|
|
||||||
setup { storeParams, onUpgradeNeeded } = do
|
setup { storeParams, onUpgradeNeeded } = do
|
||||||
let onUpgradeNeeded' var db _ = launchAff' do
|
let onUpgradeNeeded' var db _ _ = launchAff' do
|
||||||
store <- IDBDatabase.createObjectStore db "store" storeParams
|
store <- IDBDatabase.createObjectStore db "store" storeParams
|
||||||
liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store)
|
liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store)
|
||||||
putVar var { db, store }
|
putVar var { db, store }
|
||||||
@ -506,7 +508,7 @@ main = runMocha do
|
|||||||
, onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
|
, onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
|
||||||
} -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
|
} -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
|
||||||
setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
|
setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
|
||||||
let onUpgradeNeeded' var db tx = launchAff' do
|
let onUpgradeNeeded' var db tx _ = launchAff' do
|
||||||
store <- IDBDatabase.createObjectStore db "store" storeParams
|
store <- IDBDatabase.createObjectStore db "store" storeParams
|
||||||
_ <- traverse (uncurry (IDBObjectStore.add store)) values
|
_ <- traverse (uncurry (IDBObjectStore.add store)) values
|
||||||
index <- IDBObjectStore.createIndex store "index" keyPath indexParams
|
index <- IDBObjectStore.createIndex store "index" keyPath indexParams
|
||||||
@ -844,7 +846,7 @@ main = runMocha do
|
|||||||
, onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
|
, onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
|
||||||
} -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
|
} -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
|
||||||
setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
|
setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
|
||||||
let onUpgradeNeeded' var db tx = launchAff' do
|
let onUpgradeNeeded' var db tx _ = launchAff' do
|
||||||
store <- IDBDatabase.createObjectStore db "store" storeParams
|
store <- IDBDatabase.createObjectStore db "store" storeParams
|
||||||
_ <- traverse (uncurry (IDBObjectStore.add store)) values
|
_ <- traverse (uncurry (IDBObjectStore.add store)) values
|
||||||
index <- IDBObjectStore.createIndex store "index" keyPath indexParams
|
index <- IDBObjectStore.createIndex store "index" keyPath indexParams
|
||||||
|
Loading…
Reference in New Issue
Block a user