Skip to content

Quick start

Store two users' ages, print one age, then read it again in a new BEAM VM. Each run closes the table before it exits.

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

Replace the contents of your application's main module with this code:

import gleam/dynamic/decode
import gleam/int
import gleam/io
import gleam/result
import slate
import slate/set
pub fn main() -> Nil {
let lookup = {
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")
}
case lookup {
Ok(age) -> io.println("Alice's age: " <> int.to_string(age))
Error(error) -> io.println(slate.error_message(error))
}
}

use users <- set.with_table(...) passes the rest of the inner block 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. result.try returns early from the callback if an insert fails.

From your project directory, run:

Terminal window
gleam run

If the inserts, lookup, and close succeed, the program prints:

Alice's age: 42

The program prints an error message if a storage operation fails.

The first run has exited and closed its table. Replace your main module with this read-only version to check the saved data without inserting it again:

import gleam/dynamic/decode
import gleam/int
import gleam/io
import slate
import slate/set
pub fn main() -> Nil {
let lookup = {
use users <- set.with_table(
"data/users.dets",
repair: slate.NoRepair,
access: slate.ReadOnly,
key_decoder: decode.string,
value_decoder: decode.int,
)
set.lookup(users, key: "alice")
}
case lookup {
Ok(age) -> io.println("Alice's age: " <> int.to_string(age))
Error(error) -> io.println(slate.error_message(error))
}
}

Run gleam run again from the same project directory. This starts a new BEAM VM. You should see Alice's age: 42 again: the program read it from data/users.dets, not from the first VM's memory.

ReadOnly requires an existing file, and NoRepair reports a repair requirement without changing the 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.

If independent CLI processes share a file, use exclusive sessions for every read and write. The with_table calls above do not acquire an OS lock.