Tony-ArtZ/zorm
A Zig ORM with custom schema support
Note: ZORM is a work in progress (WIP).
For schema syntax highlighting and LSP support, check out the ZORM VSCode Extension and the Github repo.
ZORM is a Zig ORM library with a custom schema file support. You can define your schema in a readable .zorm file and let the zorm-generator generate the necessary typings for you.
Currently ZORM only supports Postgres and Sqlite3. More functionality and backends are being worked on.
.zorm schema files to Zig codeAdd ZORM to your build.zig.zon using :
zig fetch --save git+https://github.com/Tony-ArtZ/zorm
const zorm_dep = b.dependency("zorm", .{
.target = target,
.optimize = optimize,
});
const zorm_mod = zorm_dep.module("zorm");
const exe = b.addExecutable(.{
.name = "zorm_test",
.root_module = exe_mod,
});
exe.root_module.addImport("zorm", zorm_mod);
You have two options to generate Zig code from your .zorm schema:
const generator = zorm_dep.artifact("zorm-generator");
b.installArtifact(generator);
./zig-out/bin/zorm-generator <input_schema_path> <output_path?>
Import and call the generator in your Zig code:
const zorm = @import("zorm");
try zorm.generator.generateSchema(
allocator, // std.mem.Allocator
schema_path, // []const u8
output_path // []const u8
);
Important: When building your executable, make sure to link the appropriate C library headers for your backend (SQLite or PostgreSQL). For SQLite, link
-lsqlite3; for PostgreSQL, link-lpqand ensure the headers are available. This is required for successful compilation and runtime.
schema.zorm)Define your data models and backend in a .zorm file. For example:
[config]
backend postgres
model User {
id Int @id
name String
email String @unique
age Int?
}
[config] section specifies the backend (postgres or sqlite).model defines a table with fields and attributes (e.g., @id, @unique).? (e.g., Int?).See examples/schema.zorm for a complete example.
See the examples/ directory for a full working example. Here is a minimal usage:
const std = @import("std");
const zorm = @import("zorm");
const schema = @import("generated_schema.zig"); // Generated from schema.zorm
const SQLITE = zorm.SQLITE;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Initialize SQLite backend
var db = SQLITE.init(allocator);
defer db.disconnect();
try db.connect("test.db");
try db.createTable(schema.UserMeta);
const user = schema.User{
.id = "1",
.name = "John Doe",
.email = "[email protected]",
.age = "30",
};
try db.insert(schema.User, user);
std.debug.print("User inserted successfully!\n", .{});
}
const PG = zorm.pg.PG; and updating the backend initialization and connection string.User and UserMeta.examples/example.zig for a complete example.examples/query_builder_example.zig for a sample query builder example.examples/schema.zorm for a sample schema definition.MIT