A GORM-inspired ORM for Zig 0.17 with first-class PostgreSQL, MySQL/MariaDB, and SQLite drivers. PostgreSQL and MySQL use built-in pure-Zig wire protocol implementations; SQLite links libsqlite3.
- Pure-Zig PostgreSQL driver: protocol 3.0,
SCRAM-SHA-256/MD5/ cleartext auth; simple query and extended query protocols (Parse/Bind/Describe/Execute, parameterized against SQL injection) - Binary-format parameters: scalars (ints/floats/bools/timestamps) are sent in binary format with explicit type OIDs;
byteatravels raw — better performance and precision - Array & JSON columns: 1-D and multi-dimensional numeric/boolean arrays (
[]const i64,[][]const i64, ...), text arrays ([]const []const u8= text[]), NULL elements via optional element types ([]const ?f64), pluszg.Json(jsonb) andzg.Bytes(bytea) - Binary-format results: result sets are decoded from PostgreSQL's binary format (arrays, timestamps, jsonb, numeric scalars...) — opt out via
Config.binary_results = false - Comptime model reflection: plain Zig structs are models — no macros, no codegen, zero runtime registration
- Convention over configuration: camelCase → snake_case,
idas the default primary key, auto-filledcreated_at/updated_at - Extended query builder: joins, subqueries, CTE/recursive CTE, unions,
DISTINCT ON, raw CASE/window/JSON/array expressions, hints, named parameters, scopes, dry-run,EXPLAIN, andRETURNING - Production pool and execution controls: prepared-statement LRU caches, query/acquire/network/transaction deadlines, PostgreSQL cancellation, connection lifetime/idle limits, background cleanup, prewarming, health checks, and pool statistics
- CRUD, upsert, optimistic locks, and transactions: chunked batches, configurable
ON CONFLICT, arbitrary/batchRETURNING, composite primary keys, savepoints, and explicit bounded transaction retries - Streaming rows: model decoding and CSV export begin at the first row, keep large-result memory bounded, propagate iterator errors, and release connections after early termination
- Schema management: additive
autoMigrate, versioned migrations with advisory locking/checksums/dirty-state validation, schema diffs, type/rename/drop operations, constraints, and composite/partial/expression indexes - Associations:
belongs_to,has_one,has_many, many-to-many and polymorphic declarations; nested and join preloading; association append/replace/delete/clear/count; cascade and auto-save support - Custom codecs:
zgormEncode/zgormDecodeplus UUID, decimal, IP/CIDR, range, geometry, enum, and JSON helpers - Observability: structured query events, fingerprints, SQLSTATE/error classes, OpenTelemetry-compatible span hooks, Prometheus metrics, slow-query aggregation, prepared-cache rates, and pool metrics
- Routing: primary/replica read-write routing, round-robin replicas, tenant and shard maps, health-based failover
- Plugins: global query/CRUD callbacks plus tracing, metrics, AES-256-GCM encryption, tenant filtering, audit, pagination, and cache-invalidation helpers
- Three first-class drivers: MySQL/MariaDB supports
mysql_native_password,caching_sha2_password(TLS or RSA full authentication), server prepared statements, pools, and timeouts; SQLite supports bound parameters, pools, busy/query timeouts, WAL, and foreign-key configuration. Both share typed CRUD/upsert, optimistic locking, hooks, associations/preload, query composition, additive migrations, versioned migrations, plugins, routing, observability, and explicit transaction retries. - Errors carry context: server errors map to typed errors (
UniqueViolation,ForeignKeyViolation,SerializationFailure, ...), with SQLSTATE, message and constraint details available vialastError
- Zig 0.17.0
- PostgreSQL 10+ (14+ recommended;
trust/scram-sha-256/md5password auth supported) libsqlite3, linked by your own build, when usingzg.drivers.sqlite
Add zgorm as a dependency (build.zig.zon):
zig fetch --save <git-url-or-tarball>// build.zig
const zgorm = b.dependency("zgorm", .{
.target = target,
.optimize = optimize,
}).module("zgorm");
const exe = b.addExecutable(.{
.name = "app",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{.{ .name = "zgorm", .module = zgorm }},
}),
});
// Only when using zg.drivers.sqlite. The module itself links no C library.
exe.root_module.linkSystemLibrary("sqlite3", .{});The PostgreSQL and MySQL drivers are pure Zig, so a build that imports only those cross-compiles with no C library.
Models can embed zg.Model (GORM gorm.Model equivalent) to get id,
created_at, updated_at, and deleted_at columns automatically. All three
timestamp columns use PostgreSQL timestamptz; embedded fields are flattened
into the table at comptime:
pub const Post = struct {
base: zg.Model = .{}, // id, created_at, updated_at, deleted_at
title: []const u8 = "",
pub const zgorm = .{ .table = "posts" };
};
// post.base.id is filled back on insert; timestamps are auto-stampedPlain models without embedding work exactly the same way:
const std = @import("std");
const zg = @import("zgorm");
pub const User = struct {
id: ?i64 = null, // optional integer PK => bigserial, filled back on insert
name: []const u8 = "",
email: ?[]const u8 = null, // optional => NULLable column
age: i32 = 0,
active: bool = true,
balance: f64 = 0,
status: Status = .draft, // enums are stored as their name (text)
created_at: ?zg.Time = null, // name match => auto timestamp
updated_at: ?zg.Time = null,
pub const Status = enum { draft, review, published };
pub const zgorm = .{ // optional per-model config
.table = "users", // defaults to the type name in snake_case
.unique = .{"email"}, // unique index (used by autoMigrate)
.indexes = .{"age"}, // plain index
// .primary_key = "email",
// .columns = .{ .isActive = "active" }, // field -> column override
// .ignore = .{.transient}, // ignored fields
};
};Note: once a model declares
pub const zgorm, reference library types inside that struct through an import alias (e.g.const zg = @import("zgorm")and then?zg.Time), otherwise you get an ambiguous-reference compile error.
pub fn main(init: std.process.Init) !void {
const gpa = init.gpa;
const io = init.io;
// DSN: URL or key=value form
const db = try zg.Db.connectDsn(gpa, io,
"postgres://user:pass@127.0.0.1:5432/app?sslmode=disable", 8);
defer db.deinit();
try db.autoMigrate(User);
// Insert (auto-increment id filled back, timestamps auto-stamped)
var u = User{ .name = "Alice", .email = "alice@example.com", .age = 30 };
try db.insert(&u);
// Find by primary key (returns ?User; strings are allocated with gpa,
// release them with zg.free)
var found = (try db.find(User, u.id.?)).?;
defer zg.free(User, found, gpa);
// Update / save (inserts when the primary key is empty)
found.age += 1;
_ = try db.update(&found);
// Query builder: placeholders are renumbered automatically
const adults = try zg.query(User, db)
.where("age >= $1", .{18})
.whereEq("active", true)
.whereIn("status", .{ .published, .review })
.order_by("created_at DESC")
.limit(10)
.all(gpa);
defer zg.freeSlice(User, adults, gpa);
const n = try zg.query(User, db).where("age > $1", .{20}).count();
const exists = try zg.query(User, db).whereEq("name", "Alice").exists();
// Batch insert (auto-chunked)
var posts = [_]Post{ .{ .title = "a" }, .{ .title = "b" } };
try db.insertBatch(&posts); // posts[i].id filled back
// Conditional update / delete
_ = try db.updateWhere(User, .{ .active = false }, "age > $1", .{100});
_ = try db.deleteWhere(User, "age < $1", .{5});
// Transaction
{
var tx = try db.begin();
defer tx.rollback(); // no-op if commit succeeded
var ghost = User{ .name = "Ghost" };
try tx.insert(&ghost);
try tx.commit();
}
// Delete
_ = try db.delete(&u);
}- PostgreSQL
findandfirstuse the session allocator. After retrieving a model, usedefer zg.free(User, user, s.gpa)in the same scope. all(gpa)uses the supplied allocator.defer zg.freeSlice(User, users, gpa)releases the models, their strings and their preloaded children. Keep the original string and slice fields until cleanup, or use an arena for the results.- A terminal operation releases the query builder. Results from
exec,execRawanddryRuneach have their owndeinit().
Set the allocator on a session for this request. Its find and first results and any rows fetched with all(arena) then share one lifetime. The connection pool keeps the allocator supplied to Db.
var arena_state = std.heap.ArenaAllocator.init(gpa);
defer arena_state.deinit();
const arena = arena_state.allocator();
var s = db.session();
s.gpa = arena;
if (try s.find(User, id)) |user| {
std.debug.print("{s}\n", .{user.name});
}
const adults = try s.query(User).where("age >= $1", .{18}).all(arena);
for (adults) |user| std.debug.print("{s}\n", .{user.name});
var rows = try s.query(User).whereEq("active", true).stream(arena);
defer rows.deinit();
if (try rows.next()) |user| std.debug.print("{s}\n", .{user.name});stream(arena) allocates decoded rows in the arena. The iterator holds a database connection until it reaches the end or you call rows.deinit(). Place defer rows.deinit() immediately after opening it, including when you only read the first row. Use all decoded values before arena_state.deinit().
Run the example in example/arena.zig with zig build run-arena -Ddsn=....
postgres://user:password@host:5432/dbname?sslmode=disable
postgresql://user@10.0.0.1:5433/app
host=/var/run/postgresql user=pg dbname=app # unix socket directory
Parameters: host, port, user, password, dbname/database, sslmode, application_name.
sslmode: disable (default) | require (encrypted, no cert check) | verify_ca | verify_full (chain + hostname verification).
Preload paths nest with dots and are validated at compile time:
const authors = try zg.query(Author, db).preload("posts.comments").all(gpa);
defer zg.freeSlice(Author, authors, gpa); // releases preloaded children too
// authors[0].posts[i].comments[j] are filledpub const Author = struct {
base: zg.Model = .{},
name: []const u8 = "",
posts: []Post = &.{}, // inferred has_many
};
pub const Post = struct {
base: zg.Model = .{},
author_id: ?i64 = null,
title: []const u8 = "",
author: ?Author = null, // inferred belongs_to
};
// Default inference:
// Author.posts -> Post.author_id
// Post.author -> Post.author_id
const authors = try zg.query(Author, db).preload("posts").all(gpa);
defer zg.freeSlice(Author, authors, gpa);
const posts = try zg.query(Post, db).preload("author").all(gpa);
defer zg.freeSlice(Post, posts, gpa);
// Explicit configuration when naming does not follow convention:
pub const ExplicitAuthor = struct {
base: zg.Model = .{},
posts: []Post = &.{},
pub const posts_relation = zg.hasManyWith(
Post,
zg.AssociationOptions{
.foreign_key = "owner_id",
},
);
};
pub const User = struct {
// ...
pub fn beforeCreate(self: *User, s: *zg.Session) zg.Error!void {
self.name = std.fmt.allocPrint(...); // mutate fields
}
pub fn afterFind(self: *User, s: *zg.Session) zg.Error!void { ... }
};Available hooks: beforeSave, beforeCreate, afterCreate, beforeUpdate, afterUpdate, beforeDelete, afterDelete, afterFind (declare them on the model, all optional; return zg.Error!void, use error.HookAborted to abort the operation).
zgorm ships with a default stderr logger (GORM-style): failed queries log at
error level, queries slower than the threshold at warn, and everything traces
at info. Default level is warn with a 200ms slow-query threshold, so normal
usage stays quiet. Its level tags use ANSI colors; custom sinks receive plain messages.
// Trace every query:
db.setLogger(.{ .level = .info });
// Silence:
db.setLogger(zg.Logger.discard);
// Custom sink (e.g. your log framework):
const MyLogger = struct {
fn log(ctx: ?*anyopaque, level: zg.LogLevel, msg: []const u8) void {
// `msg` contains elapsed time, row count, inlined SQL and params
my_log_framework.write(level, msg);
}
};
db.setLogger(.{ .level = .info, .logFn = MyLogger.log, .context = null });
// Tune the slow-query threshold:
db.setLogger(.{ .level = .warn, .slow_threshold_ms = 500 });Logged lines inline the parameter values into the SQL (with NULL and
quoting handled), so they can be copied straight into psql.
Models with a deleted_at column (?zg.Time or ?i64, also included in zg.Model) get GORM-style soft deletes. The default filter applies to every orWhere branch and each level of a preload. Calling unscoped() includes deleted parents and children.
_ = try db.delete(&user); // UPDATE ... SET deleted_at = now()
if (try db.find(User, id)) |visible| {
defer zg.free(User, visible, gpa);
std.debug.print("{s}\n", .{visible.name});
}
_ = try zg.query(User, db).count(); // filtered too; update/deleteWhere as well
_ = try zg.query(User, db).unscoped().count(); // include soft-deleted rows
if (try db.findUnscoped(User, id)) |archived| {
defer zg.free(User, archived, gpa);
std.debug.print("{s}\n", .{archived.name});
}
_ = try db.updateUnscoped(&user); // update it too
_ = try db.restore(&user); // clear deleted_at by primary key
_ = try db.deleteHard(&user); // real DELETE
_ = try db.deleteWhereHard(User, "age < $1", .{5});var tx = try db.begin();
defer tx.rollback();
var sp = try tx.begin(); // SAVEPOINT zgorm_sp_1
var u2 = User{ .name = "maybe" };
try sp.insert(&u2);
sp.rollback(); // undoes only this savepoint; use commit() to RELEASE
try tx.commit();var m = zg.Migrator.init(db);
defer m.deinit();
m.add(&.{
.{ .version = 1, .name = "create_users", .up = "CREATE TABLE users (...)", .down = "DROP TABLE users" },
.{ .version = 2, .name = "add_email", .up = "ALTER TABLE users ADD COLUMN email text" },
});
_ = try m.applyAll(); // applies pending migrations in ascending order, one txn each
try m.rollbackLast(); // rolls the latest version back with its down scriptThe tracking table stores version, name, checksum, dirty, and applied_at. applyAll serializes runners with a PostgreSQL advisory lock and rejects changed or dirty migrations.
var metrics: zg.PluginMetrics = .{};
try db.addPlugin(metrics.plugin());
const Retry = struct {
fn run(user: *User, tx: *zg.Tx) zg.Error!void {
try tx.insert(user);
}
};
var user = User{ .name = "retry-safe" };
try db.transactionRetry(
.{ .max_attempts = 4, .base_delay_ms = 10, .max_delay_ms = 500 },
&user,
Retry.run,
);Only transactionRetry replays a transaction, and only for serialization,
deadlock, and optionally temporary connection failures. Ordinary CRUD and
begin never replay writes.
Tenant filtering and pagination remain explicit query transformations:
var q = zg.query(User, db);
q = (zg.TenantFilterPlugin{}).apply(q, tenant_id);
q = (zg.PaginationPlugin{ .page = 0, .per_page = 50 }).apply(q);
const page = try q.all(gpa);
defer zg.freeSlice(User, page, gpa);const sqlite = try zg.drivers.sqlite.Db.open(gpa, ":memory:");
defer sqlite.deinit();
var sqlite_db = zg.drivers.sqlite.orm(sqlite);
try sqlite_db.autoMigrate(User);
var local_user = User{ .name = "Ada" };
try sqlite_db.insert(&local_user);
const local_rows = try sqlite_db.whereEq(User, "name", "Ada", gpa);
defer zg.freeSlice(User, local_rows, gpa);
const mysql = try zg.drivers.mysql.Conn.connect(gpa, io, .{
.host = "127.0.0.1", .port = 3306,
.user = "app", .password = "secret", .database = "app",
});
defer mysql.deinit();
var mysql_db = zg.drivers.mysql.orm(mysql);
try mysql_db.autoMigrate(User);
var remote_user = User{ .name = "Grace" };
try mysql_db.insert(&remote_user);The MySQL/MariaDB and SQLite ORM has the same shared typed CRUD/upsert,
optimistic-locking, lifecycle-hook, association/preload, query-builder,
additive/versioned-migration, pool, plugin, routing, observability, and explicit
transaction-retry APIs. Dialect-specific SQL remains explicit: PostgreSQL alone
supports DISTINCT ON, advisory migration locks, destructive schema diffs, and
binary streaming; MySQL emulates mutation RETURNING, while SQLite requires
libsqlite3.
For MySQL/MariaDB batches that need generated IDs, insertBatch inserts each row in the same transaction and reads its ID from the server. This uses more database round trips and works with a custom auto-increment step. For updateReturning and deleteReturning, ordering, limit and offset select the primary keys that the mutation will affect.
MySQL requires a mapped primary key for these operations. A primary-key rewrite or a selection with aggregates, CTEs, unions, distinct or selectRaw returns InvalidQuery before writing.
db.insert(&user) catch |err| {
switch (err) {
error.UniqueViolation => { /* lastError(db).code == "23505" */ },
error.SerializationFailure => retry(),
else => {
const e = zg.lastError(db); // severity/code/message/detail/constraint...
},
}
};zig build # build library + example
zig build test # unit tests
zig build run -Ddsn='postgres://...' # run the example
zig build test-integration -Ddsn='postgres://...' # integration tests (needs a real PG)
zig build test-drivers # SQLite
zig build test-drivers -Dmysql-port=3307 # SQLite + live MySQL/MariaDB
zig build -Doptimize=ReleaseFast # release buildIntegration coverage includes CRUD and batches, transactions/savepoints/retries, migrations and schema diffs, prepared caches, deadlines/cancellation, streaming, observability, routing, plugins, composite keys, upsert/returning, custom codecs, and associations. test-drivers exercises SQLite in memory and optionally a live MySQL/MariaDB server.
- Wire formats: parameters are binary for scalars (int8/float8/bool/timestamptz/bytea/text with explicit OIDs) and text for arrays/JSON/enums; results are decoded from PostgreSQL's binary format by default (
Config.binary_results = falseto opt out for exotic column types). - Preloading: any depth via dotted paths; string/array values of preloaded rows are deep-copied per parent. Multi-dimensional arrays are supported for numeric/boolean element types; text arrays are 1-D.
- autoMigrate remains additive. Destructive/type-changing work is explicit through
Migrator(alterColumnType,dropColumn,renameColumn, constraints and advanced indexes). - SQL injection surface: values are always parameterized;
order_by,group_by,havingandselectcolumn names are string-interpolated (same as GORM) — never pass user input there. - Concurrency:
Db/the connection pool are thread-safe (std.Io.Mutex/Condition); a singlepq.Connand aSessionwith an in-flight operation are not. - Full-column updates:
updatewrites every column of the model (GORMSavesemantics); useupdateWherefor partial updates.
- Binary format + array/JSONB column types (v0.2.0)
- Associations (belongs_to/has_many) & preloading (v0.2.0)
- Model hooks (BeforeSave etc.) (v0.2.0)
- Savepoints / nested transactions (v0.2.0)
- Migration version management (schema_migrations) (v0.2.0)
- Nested preloading (v0.3.0)
- Binary-format result decoding (v0.3.0)
- Multi-dimensional arrays / full NULL array elements (v0.3.0)
- MySQL/SQLite direct drivers and typed CRUD ORM
- Production execution, routing, plugins, codecs, advanced migrations, and explicit transaction retries
zgorm is inspired by GORM, especially its approachable model conventions, chainable query API, hooks, associations, preloading, soft deletes, and migration ergonomics. Thanks to the GORM maintainers and contributors for building and evolving an excellent ORM for the Go ecosystem.
MIT