Hello :) mostly more work on zyn this week, I got carried away...

zyn is the application framework I'm building that I'll use for Occasionly. This is an unintended devlog, and the last zyn-only one.

A real database

zyn comes with managed Postgres that Just Works, and this week I added forward-only migrations — no rollbacks, no take-backs. In dev you have zyn db reset to nuke the DB and start over, but otherwise you go in one direction. And there's no DSL, it's just SQL. Also added 2 CLI helpers — zyn g migration NAME to create an empty migration, or zyn g table NAME for a helping hand:

CREATE TABLE public."users" (
    id uuid NOT NULL DEFAULT uuidv7() PRIMARY KEY,
    created_at timestamptz NOT NULL DEFAULT now(),
    updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TRIGGER zyn_users_set_updated_at
    BEFORE UPDATE ON public."users"
    FOR EACH ROW
    EXECUTE FUNCTION public.zyn_set_updated_at();

And zyn db migrate to do the honours. This dumps the database schema to schema.sql and refreshes models.zig, a minimum viable ORM. Running the above generates this:

// This file is automatically generated from the PostgreSQL catalog.
// Do not edit by hand.

pub const User = struct {
    id: []const u8,
    created_at: i64,
    updated_at: i64,
};

Access to the database comes through app.db, available in route actions, middleware, hooks and policies...

Routing hooks and policies

For route actions I've added hooks, which can run before one, many or all routes. They control execution with executor.next(), so you can run code afterwards as well:

const models = @import("models.zig");

// ....

fn load_current_user(app: *zyn.App, req: *zyn.Request, _: *zyn.Response, executor: *zyn.Executor) !void {
    const session_id = try req.cookies().encrypted().get("session_id", .{}) orelse return executor.next();

    const auth = (try app.db.one(
        struct { session: models.Session, user: models.User },
        \\ SELECT sessions.*, users.*
        \\ FROM sessions
        \\ JOIN users ON users.id = sessions.user_id
        \\ WHERE sessions.id = ?;
    ,
        .{session_id},
    )) orelse return executor.next();

    req.locals.auth = .{
        .user = auth.user,
        .session = auth.session,
    };

    try executor.next();
}

We'll see how that "ORM" scales... I like it so far; it's simple, elegant, and doesn't try to re-create SQL in Zig. Just use SQL!

I also added policies, an authorisation layer to guard route access. They take the app config, request object and return either allow, unauthenticated, forbidden or not_found:

pub fn update_project_policy(app: *zyn.App, req: *zyn.Request) !zyn.PolicyDecision {
    const auth = req.locals.auth orelse return .unauthenticated;

    const project_id = req.param("id") orelse return .not_found;
    const project = (try app.db.one(
        models.Project,
        "SELECT * FROM projects WHERE id = ?",
        .{project_id},
    )) orelse return .not_found;

    // Conceal projects belonging to another organization.
    if (project.organization_id != auth.user.organization_id) {
        return .not_found;
    }

    // The project is visible, but this user cannot update it.
    if (auth.user.role != .admin) {
        return .forbidden;
    }

    return .allow;
}

Used like so:

const zyn = @import("zyn");

pub const defaults: zyn.RouteDefaults = &.{.hook(load_current_user)};

pub const table: zyn.RouteTable = &.{
    .patch("/projects/:id", update_project, &.{.policy(update_project_policy)}),
};

And the routing table can include other tables, so you can apply a hook or policy to an entire group:

const boats = @import("actions/boats.zig");

pub const table: zyn.RouteTable = &.{
    // ....
    .mount("/boats", boat_routes, &.{.policy(is_skipper)}),
};

const boat_routes: zyn.RouteTable = &.{
    .get("/", boats.index, &.{}),
    .post("/", boats.create, &.{}),
    .get("/new", boats.new, &.{}),
    .get("/:id", boats.show, &.{}),
    .patch("/:id", boats.update, &.{}),
    .delete("/:id", boats.delete, &.{}),
    .get("/:id/edit", boats.edit, &.{}),
};

And there's now a zyn routes command:

And to actually get occasionly underway, I wanted a) agent-accessible docs and b) a way to run tests, so zyn docs and zyn test now exist.

And finally, zyn init to start building this calendar...

Until next Thursday,

- Nick