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

slate supports Gleam's Erlang target. DETS is part of OTP; slate does not support the JavaScript target.

Open a table, insert a value, and look it up. Reads use your decoders to check the stored data.

Create the parent directory before you run the example:

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.

Stored data persists across node restarts. An abnormal node shutdown can lose pending writes and leave the file needing repair. The quick start explains this example. See the table cleanup guide for cleanup and repair options.

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. Each table has a 2 GB file size limit. Disk-backed access is slower than ETS for frequent reads. See Limitations.

The table modules share a core API. Set and bag tables also provide insert_new, with different duplicate rules. Only set tables provide update_counter.

slate follows Semantic Versioning. Continue with the quick start to create a table, write values, and read them back.