⚡ Zig Guide LiveUnofficialbut fully verified
✓ Zig 0.17.0-dev.2122+3e15e99e6What's newOn an older Zig?

Databases

Three ways into a database from Zig, at three different levels. SQLite through its C API, which is the shortest path to durable storage and a good look at how Zig links C. The PostgreSQL wire protocol written out by hand, startup message through row description, because the protocol is simpler than its client libraries suggest. And Redis RESP, which is small enough to parse in one page. For a query layer on top of these, see the ORM chapters under Projects.

  1. A database driver is mostly a codec. Once the framing is written out, a query is a message you send and a stream of messages you read until the server says it is ready again.
  2. Postgres and Redis both frame every message with a type and a length, and both are simpler to implement than to install a client library for. The complexity in a real driver is pooling, auth and types, not the protocol.
  3. Protocol code that reads from a Reader rather than a socket can be tested with a string literal, which is why two of these three recipes run in your browser.
  4. SQLite is a C library, so this is also the shortest realistic example of translate-c, linking, and turning C error codes into Zig errors.

3 chapters.