Current section
Files
Jump to
Current section
Files
README.md
# idb[](https://hex.pm/packages/idb)[](https://hexdocs.pm/idb/)[](https://github.com/ilhamft/idb/blob/main/LICENSE)[](https://gleam.run/news/v0.16-gleam-compiles-to-javascript/)Gleam bindings for the [IndexedDB](https://developer.mozilla.org/docs/Web/API/IndexedDB_API) API.This library is still a work in progress, so not all IndexedDB features are included yet.## InstallationAdd to your Gleam project:```shgleam add idb```## Usage### Getting data```gleamimport gleam/dynamic/decodeimport gleam/resultimport idbimport idb/rangepub fn main() -> Nil { use db <- idb.try_connect("MyDatabase", 1, []) use tx <- result.try( idb.start_transaction(db, ["cats"], idb.ReadOnly, fn(_) { Nil }), ) use store <- result.map(idb.get_store(tx, "cats")) use maybe_data <- idb.get_one(store, range.only(idb.key_int(1))) use data <- result.try(maybe_data |> result.map_error(WrapIdbError)) let cat = data |> decode.run(cat_decoder()) |> result.map_error(WrapDecodeError) case cat { Error(_) -> echo "errored" Ok(cat) -> echo "hello " <> cat.name } todo as "do something with the cat"}```### Handling database upgrade```gleamimport gleam/resultimport idbimport idb/eventimport idb/indeximport idb/storepub fn main() -> Nil { use db <- idb.try_connect("MyDatabase", 2, [ event.OnUpgrade(handle_upgrade), ]) todo as "do something with the db"}fn handle_upgrade(tx: idb.Transaction, old_version: Int, new_version: Int) { case old_version, new_version { 1, 2 -> { use _ <- result.try( tx |> idb.delete_store("cats"), ) use cat_store <- result.try( tx |> idb.create_store("cats", store.OutOfLineKey), ) use _ <- result.try( cat_store |> idb.create_index("name_index", index.UniqueKey("name")), ) use _ <- result.map( cat_store |> idb.create_index("age_index", index.Key("age")), ) Nil } _, _ -> Ok(Nil) }}```Documentations can be found at <https://hexdocs.pm/idb>.