Skip to content
Chalk drawing on a slate chalkboard of a database cylinder under a magnifying glass, with the slate logo above it.

slate

Persistent key-value storage for Gleam, built on Erlang DETS. No separate database service is required.
Terminal window
gleam add slate

DETS is part of OTP, so slate runs on Gleam's Erlang target. It does not support the JavaScript target.

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:

Terminal window
mkdir -p data
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),
)
set.lookup(users, key: "alice")
}
// Ok(42), and the table is closed

Gleam'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.

DETS is between a plain file and a full database:

Storage approaches at a glance
ApproachSetupPersistenceQuery capability
JSON fileFile I/O and serializationYesNone
DETS (slate)Included in OTP; add slate for GleamYesKey lookup, fold
SQLiteEmbedded database libraryYesFull SQL
PostgresDatabase serverYesFull SQL
MnesiaIncluded in OTP; configure a schemaYesKey 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.

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.

Continue with the quick start to print a stored value, then read it again in a new BEAM VM.