PureScript FFI bindings for the idb library, providing a simple Aff-based API for IndexedDB.
open :: String -> Int -> Array String -> Aff IDBDatabase
get :: IDBDatabase -> String -> String -> Aff (Maybe Foreign)
put :: IDBDatabase -> String -> String -> Foreign -> Aff Unit
delete :: IDBDatabase -> String -> String -> Aff Unit
clear :: IDBDatabase -> String -> Aff UnitgetAll :: IDBDatabase -> String -> Aff (Array Foreign)
getAllKeys :: IDBDatabase -> String -> Aff (Array String)
getAllEntries :: IDBDatabase -> String -> Aff (Array (Tuple String Foreign))getAllEntries reads keys and values in a single readonly transaction, guaranteeing consistency.
withWriteTransaction :: IDBDatabase -> String -> (TxStore -> Effect Unit) -> Aff Unit
txPut :: TxStore -> String -> Foreign -> Effect Unit
txDelete :: TxStore -> String -> Effect Unit
txClear :: TxStore -> Effect UnitThe callback is Effect (not Aff) to prevent the transaction from auto-committing between async operations.
Built on withWriteTransaction:
putBatch :: IDBDatabase -> String -> Array (Tuple String Foreign) -> Aff Unit
clearAndPutBatch :: IDBDatabase -> String -> Array (Tuple String Foreign) -> Aff Unit
deleteBatch :: IDBDatabase -> String -> Array String -> Aff Unitimport Web.IDB as IDB
import Data.Tuple (Tuple(..))
import Foreign (unsafeToForeign, unsafeFromForeign)
main :: Effect Unit
main = launchAff_ do
db <- IDB.open "my-db" 1 ["items"]
-- Single key
IDB.put db "items" "key1" (unsafeToForeign "hello")
result <- IDB.get db "items" "key1"
-- result :: Maybe Foreign
-- Batch write (single transaction)
IDB.putBatch db "items"
[ Tuple "a" (unsafeToForeign "val-a")
, Tuple "b" (unsafeToForeign "val-b")
]
-- Mixed put + delete in one atomic transaction
IDB.withWriteTransaction db "items" \tx -> do
IDB.txPut tx "new-key" (unsafeToForeign "value")
IDB.txDelete tx "old-key"Requires PureScript (purs) on PATH.
bun install
bunx spago build
bunx spago testTests use fake-indexeddb to provide an in-memory IndexedDB implementation in Node.js.
bunx spago test