Quick start
This guide creates a typed table, writes two values, reads one back, and closes the table automatically.
1. Add slate to your project
Section titled “1. Add slate to your project”gleam add slate2. Open a table and store data
Section titled “2. Open a table and store data”Create the data directory in your project before you run the example. slate creates table files, but it does not create parent directories.
mkdir -p dataUse this code in your application's main module:
import gleam/dynamic/decodeimport gleam/resultimport slateimport 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 closeduse 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.
3. Data persists across restarts
Section titled “3. Data persists across restarts”import gleam/dynamic/decodeimport slateimport 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.
Next steps
Section titled “Next steps”- Use set tables for one value per key
- Use bag tables for multiple distinct values per key
- Use duplicate bag tables to store repeated key-value pairs
- Use
with_tablefor short-lived operations that close on return