
slate
Start here
Section titled “Start here”gleam add slateDETS is part of OTP, so slate runs on Gleam's Erlang target. It does not support the JavaScript target.
What it looks like
Section titled “What it looks like”This program stores a user's age in a file and reads it back. Your decoders check the stored data when you read it.
slate does not create parent directories, so create the directory first:
mkdir -p dataimport 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), ) set.lookup(users, key: "alice")}// Ok(42), and the table is closedGleam's use value <- function(...) syntax passes the rest of the block as a callback. Here, with_table supplies users and attempts to close the table when the callback returns. The decoders define the key and value types expected from disk; result.try stops early if the insert fails.
Because the age is in a file, it stays there after the node restarts. Closing the table is what makes this reliable: an abnormal node shutdown can lose pending writes and leave the file needing repair. The quick start runs this example and then reads the age in a new VM. The table cleanup guide explains cleanup and repair options.
When to use DETS
Section titled “When to use DETS”DETS is between a plain file and a full database:
| Approach | Setup | Persistence | Query capability |
|---|---|---|---|
| JSON file | File I/O and serialization | Yes | None |
| DETS (slate) | Included in OTP; add slate for Gleam | Yes | Key lookup, fold |
| SQLite | Embedded database library | Yes | Full SQL |
| Postgres | Database server | Yes | Full SQL |
| Mnesia | Included in OTP; configure a schema | Yes | Key lookup, query expressions |
Use DETS when you need persistent key lookup without a separate database service. This simplicity has limits: each table has a 2 GB file size limit, and disk-backed access is slower than ETS for frequent reads. See Limitations.
Pick a table type
Section titled “Pick a table type”The example above uses a set, because each user has one age. Other data has a different shape, so slate has three table types. They share a core API. Set and bag tables also provide insert_new, with different duplicate rules. Only set tables provide update_counter.
Next steps
Section titled “Next steps”Continue with the quick start to print a stored value, then read it again in a new BEAM VM.
