An API wrapper around indexedDB
Go to file
2023-03-02 15:52:04 +03:00
.github rework README 2017-06-30 23:47:30 +02:00
src/Database/IndexedDB Migrated to PureScript 15 2023-01-02 12:48:03 +03:00
test Cleaned things up 2023-03-02 15:52:04 +03:00
.eslintignore review test cases and add some for IDBIndex 2017-06-30 18:39:57 +02:00
.eslintrc add IDBKeyRange API bindings 2017-06-29 15:41:30 +02:00
.gitignore Migrated to PureScript 15 2023-01-02 12:48:03 +03:00
.travis.yml prepare CI environment 2017-07-06 12:26:28 +02:00
index.js Cleaned things up 2023-03-02 15:52:04 +03:00
LICENSE Initial commit 2017-06-16 15:12:20 +02:00
package.json Cleaned things up 2023-03-02 15:52:04 +03:00
packages.dhall Cleaned things up 2023-03-02 15:52:04 +03:00
README.md upgrade to latest purescript-aff 2018-03-13 20:36:20 +01:00
spago.dhall Cleaned things up 2023-03-02 15:52:04 +03:00
yarn.lock Cleaned things up 2023-03-02 15:52:04 +03:00

PureScript IndexedDB Build Status

This package offers complete bindings and type-safety upon the IndexedDB API.

Overview

The IDBCore and IDBFactory are the two entry points required to create and connect to an indexed database. From there, modules are divided such that each of them covers a specific IDB interface.

They are designed to be used as qualified imports such that each method gets prefixed with a menaingful namespace (e.g IDBIndex.get, IDBObjectStore.openCursor ...)

Here's a quick example of what it look likes.

module Main where

import Prelude

import Control.Monad.Aff                 (Aff, launchAff_)
import Control.Monad.Aff.Console         (CONSOLE, log)
import Control.Monad.Eff.Exception       (EXCEPTION)
import Control.Monad.Eff                 (Eff)
import Data.Maybe                        (Maybe(..), maybe)

import Database.IndexedDB.Core
import Database.IndexedDB.IDBFactory      as IDBFactory
import Database.IndexedDB.IDBDatabase     as IDBDatabase
import Database.IndexedDB.IDBObjectStore  as IDBObjectStore
import Database.IndexedDB.IDBIndex        as IDBIndex
import Database.IndexedDB.IDBTransaction  as IDBTransaction
import Database.IndexedDB.IDBKeyRange     as IDBKeyRange


main :: Eff (idb :: IDB, exception :: EXCEPTION, console :: CONSOLE) Unit
main = launchAff_ do
  db <- IDBFactory.open "db" Nothing { onBlocked       : Nothing
                                     , onUpgradeNeeded : Just onUpgradeNeeded
                                     }

  tx    <- IDBDatabase.transaction db ["store"] ReadOnly
  store <- IDBTransaction.objectStore tx "store"
  (val :: Maybe String) <-  IDBObjectStore.get store (IDBKeyRange.only 1)
  log $ maybe "not found" id val


onUpgradeNeeded :: forall e. Database -> Transaction -> { oldVersion :: Int } -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit
onUpgradeNeeded db _ _ = launchAff_ do
  store <- IDBDatabase.createObjectStore db "store" IDBDatabase.defaultParameters
  _     <- IDBObjectStore.add store "patate" (Just 1)
  _     <- IDBObjectStore.add store { property: 42 } (Just 2)
  _     <- IDBObjectStore.createIndex store "index" ["property"] IDBObjectStore.defaultParameters
  pure unit

Notes

Errors

Errors normally thrown by the IDB* interfaces are wrapped in the Aff Monad as Error where the name corresponds to the error's name (e.g. "InvalidStateError"). Pattern matching can therefore be done on any error to handle specific errors thrown by the API.

Examples

The test folder contains a great amount of examples showing practical usage of the IDB* interfaces. Do not hesitate to have a peek should you wonder how to use one of the module. The wrapper tries to keep as much as possible an API consistent with the original IndexedDB API. Hence, it should be quite straightforward to translate any JavaScript example to a PureScript one.

Changelog

v3.0.0

  • callback to onUpgradeNeeded event now provide a record with the database old version.

v2.0.0

  • review interface implementation (use of opaque classes to improve readability without compromising the reusability). The API doesn't really change from a user perspective though.

  • make the Key more opaque (by having an IDBKey instance for Foreign types)

  • Upgrade purescript-exceptions to 3.1.0 and leverage the new name accessor

v1.0.0

  • Indexed Database API 2.0 totally covered apart from
    • index.getAll method (and the associated one for the IDBObjectStore)
    • binary keys

Documentation

Module documentation is published on Pursuit.

Testing

Tested in the cloud on multiple browsers and operating systems thanks to BrowserStack

IE / Edge Chrome Firefox Safari Opera Android iOS Safari
- >= 57 >= 51 - >= 46 - -

browserstack