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.
- Static library for embedding in Zig projects
- Generator tool to convert
.zormschema files to Zig code - Supports both SQLite and PostgreSQL backends
- Easily extensible and integrates with Zig build system
Add ZORM to your build.zig.zon using :
zig fetch --save git+https://github.com/Tony-ArtZ/zormconst 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:
- Add Generator Artifact to your build step:
const generator = zorm_dep.artifact("zorm-generator");
b.installArtifact(generator);- Run the generator with your schema and output paths:
./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.
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?
}
- The
[config]section specifies the backend (postgresorsqlite). - Each
modeldefines a table with fields and attributes (e.g.,@id,@unique). - Optional fields use
?(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", .{});
}- The example above uses SQLite, but you can switch to PostgreSQL by changing the backend and connection string.
- The generated schema file provides types and metadata for your models.
- You can switch to PostgreSQL by using
const PG = zorm.pg.PG;and updating the backend initialization and connection string. - The generated schema file will provide types like
UserandUserMeta.
- See
examples/example.zigfor a complete example. - See
examples/query_builder_example.zigfor a sample query builder example. - See
examples/schema.zormfor a sample schema definition.
- ZORM is a work in progress. Features and APIs may change.
- Contributions and feedback are welcome!
MIT
