nerak repo

This commit is contained in:
2026-07-13 17:19:33 -05:00
parent 3d2c18cb29
commit 0e8c9854bf
2 changed files with 54 additions and 54 deletions
+13 -13
View File
@@ -101,10 +101,10 @@ On success promotes `input:name` → app scope. On failure writes `error:name` a
By order: `{ctx_key, regex, err_msg}`. Named: `.opt` (skip if absent), `.def` (default when absent). `regex` is a regex string or a built-in macro. Define custom macros: `#define n_zipcode "^\\d{5}$"`.
```c
input(
{"email", n_email, "must be a valid email"},
{"title", n_not_empty, "cannot be empty"},
{"page", n_int, "must be a number", .def = "1"},
{"filter", "^(active|done)$", "must be 'active' or 'done'", .opt = true}
{.ctx_key = "email", .regex = n_email, .err_msg = "must be a valid email"},
{.ctx_key = "title", .regex = n_not_empty, .err_msg = "cannot be empty"},
{.ctx_key = "page", .regex = n_int, .err_msg = "must be a number", .def = "1"},
{.ctx_key = "filter", .regex = "^(active|done)$", .err_msg = "must be 'active' or 'done'", .opt = true}
)
```
Built-in validators: `n_not_empty n_alpha n_alphanum n_slug n_no_html` · `n_int n_positive n_float n_percent` · `n_email n_uuid n_user` · `n_date n_time n_datetime` · `n_url n_ipv4 n_hex_color` · `n_zip n_phone n_cron` · `n_token n_base64` · `n_bool n_yes_no n_on_off`.
@@ -115,11 +115,11 @@ For non-regex checks (uniqueness, cross-field), pair with a query + `run()` call
By order per item: `{db, q, ctx_key}`. `ctx_key` optional (omit for inserts without `RETURNING`). Named: `.err_on_empty = true` (404 if zero rows), `.if_ctx`/`.not_ctx` (per item).
```c
sqlite_query(
{"todos_db", "get_todos", "todos_data"},
{"todos_db", "get_todo", "todo", .err_on_empty = true},
{"todos_db", "get_urgent","urgent", .if_ctx = "show_urgent"}
{.db = "todos_db", .q = "get_todos", .ctx_key = "todos_data"},
{.db = "todos_db", .q = "get_todo", .ctx_key = "todo", .err_on_empty = true},
{.db = "todos_db", .q = "get_urgent", .ctx_key = "urgent", .if_ctx = "show_urgent"}
)
sqlite_query({"todos_db", "create_todo"}) // no result captured
sqlite_query({.db = "todos_db", .q = "create_todo"}) // no result captured
```
SQL file uses bound params: `select id, title from todos where id = {{id}};` and `insert into todos(title) values({{title}});`.
@@ -127,7 +127,7 @@ SQL file uses bound params: `select id, title from todos where id = {{id}};` and
By order: `join(parent_ctx_key, parent_field_key, child_ctx_key, child_field_key)`. Optional `.parent_join_key` (new field name on parent records; defaults to the child table name). After it, each parent record gains a field holding its matched child records.
```c
// before: { blog:[{id,...}], comments:[{id,blog_id,...}] }
join("blog", "id", "comments", "blog_id")
join(.parent_ctx_key = "blog", .parent_field_key = "id", .child_ctx_key = "comments", .child_field_key = "blog_id")
// after: { blog:[{id,..., comments:[{...}]}] } -> template: {{#blog}}...{{#comments}}{{body}}{{/comments}}{{/blog}}
```
@@ -167,7 +167,7 @@ run(.call = assign_opponents)
### sse — push a Server-Sent Event
With `.chan` (by order, first value, supports interpolation) broadcasts to all clients on the channel; without it, returns to the requester. Named: `.event` (event line), `.data` (array of strings, one per data line), `.comment` (comment/keep-alive line).
```c
sse("todos:{{user_id}}", .event = "todo_updated", .data = {"id: {{todo_id}}", "title: {{title}}"})
sse(.chan = "todos:{{user_id}}", .event = "todo_updated", .data = {"id: {{todo_id}}", "title: {{title}}"})
```
### render — mustache / mdm / json
@@ -176,7 +176,7 @@ sse("todos:{{user_id}}", .event = "todo_updated", .data = {"id: {{todo_id}}", "t
### respond — send the response
By order: `respond(ctx_key)`. Named: `.status` (default `n_ok`; values `n_ok` 200, `n_created` 201, `n_redirect` 302, `n_bad_request` 400, `n_not_authorized` 401, `n_not_found` 404, `n_error` 500), `.mime` (override; same values as resource `.mime`).
```c
mustache("404","not_found_s"), respond("not_found_s", .status = n_not_found)
mustache("404","not_found_s"), respond(.ctx_key = "not_found_s", .status = n_not_found)
```
### headers / cookies — set response headers/cookies
@@ -224,7 +224,7 @@ respond("page_s", .not_ctx = "is_htmx")
`.map = "table"` runs a step once per row, ALL ROWS CONCURRENTLY; the row's fields land in scope as bare `{{interpolations}}`. `.map_key = "key"` (pairs with `.map`) exposes the current row as a single-row table under `key` (useful for `mustache`). With a `ctx_key`, results collect into a table aligned with the input (one entry per row).
```c
fetch({.url = "https://api.users.dev/{{id}}", .ctx_key = "profiles", .map = "users"}) // per-row fan-out
fetch({n_get, "https://api.users.dev/{{id}}", "profiles", .map = "users"}) // per-row fan-out
mustache("todo","todo_s", .map = "todos", .map_key = "todo_d") // render per row, collect into todo_s
```
@@ -262,7 +262,7 @@ A task is a named, reusable pipeline defined inside a module with `task("name",
// reusable, run inline via run_task("recount_todos"):
task("recount_todos", { sqlite_query({"todos_db","recount"}) }, .accepts = {"user_id"});
// durable background job via dispatch("notify_new_todo") (needs #include <dispatch.h>):
task("notify_new_todo", { fetch({.url = "https://api.push.dev/notify", .method = n_post, .json = "{\"text\":\"New todo: {{title}}\"}"}) }, .accepts = {"title"});
task("notify_new_todo", { fetch({n_post, "https://api.push.dev/notify", .json = "{\"text\":\"New todo: {{title}}\"}"}) }, .accepts = {"title"});
// recurring on a schedule, no caller:
task("daily_digest", { sqlite_query({"todos_db","digest"}), emit("digest_ready") }, .cron = "0 8 * * *");
```