Skip to content

Quick start

This guide creates a typed table, writes two values, reads one back, and closes the table automatically.

Terminal window
gleam add slate

Create the data directory in your project before you run the example. slate creates table files, but it does not create parent directories.

Terminal window
mkdir -p data

Use this code in your application's main module:

import gleam/dynamic/decode
import gleam/result
import slate
import slate/set
pub fn main() {
use users <- set.with_table(
"data/users.dets",
repair: slate.AutoRepair,
access: slate.ReadWrite,
key_decoder: decode.string,
value_decoder: decode.int,
)
use Nil <- result.try(set.insert(users, "alice", 42))
use Nil <- result.try(set.insert(users, "bob", 37))
set.lookup(users, key: "alice")
}
// Ok(42), and the table is closed

use users <- set.with_table(...) passes the rest of main as a callback. with_table opens the file before it calls the callback and attempts to close it when the callback returns. The key decoder accepts strings; the value decoder accepts integers. Reads return a decode error if the stored data does not match the expected types.

main returns Ok(42) if the operations and close succeed. gleam run does not print this return value.

import gleam/dynamic/decode
import slate
import slate/set
pub fn write() {
use table <- set.with_table(
"data/state.dets",
repair: slate.AutoRepair,
access: slate.ReadWrite,
key_decoder: decode.string,
value_decoder: decode.int,
)
set.insert(table, "counter", 42)
}
pub fn read() {
use table <- set.with_table(
"data/state.dets",
repair: slate.AutoRepair,
access: slate.ReadWrite,
key_decoder: decode.string,
value_decoder: decode.int,
)
set.lookup(table, key: "counter")
}

Call write and confirm that it returns Ok(Nil). Stop the node, then call read from the same working directory. It returns Ok(42) from the same file.

An abnormal node shutdown can lose pending writes and leave the file needing repair. See the table cleanup guide for cleanup and repair options.