Lua API
The crap global table is the entry point for all CMS operations in Lua. It’s available in init.lua, collection definitions, and hook functions.
Namespace
| Namespace | Description |
|---|---|
crap.collections | Collection definition, CRUD operations, and per-collection typing factories (crap.collections.<slug>.{hook,field_hook,condition,access,auth_strategy,row_label}) |
crap.globals | Global definition, get/update, and per-global typing factories |
crap.any | Cross-collection typing factories — crap.any.{collection_hook,field_hook,access,auth_strategy,job_handler,row_label,display_condition} |
crap.fields | Field factory functions (crap.fields.text(), etc.) |
crap.hooks | Global hook registration |
crap.jobs | Job definition |
crap.log | Structured logging |
crap.util | Utility functions |
crap.auth | Password hashing and verification (Argon2id) |
crap.env | Read-only environment variable access |
crap.http | Outbound HTTP requests (blocking) |
crap.config | Read-only access to crap.toml values |
crap.locale | Locale configuration queries |
crap.email | Send email via configured SMTP |
crap.crypto | Cryptographic utilities (HMAC, random bytes, hashing) |
crap.schema | Runtime schema introspection |
crap.richtext | Custom rich text node registration |
Typed hook factories
Every callable a Lua user writes — collection hooks, field hooks, access functions, auth strategies, job handlers, row labels, display conditions — should be wrapped in a typing factory so the editor can infer the parameter types of the function body.
-- Per-collection: ctx narrows to crap.hook.Posts
return crap.collections.posts.hook(function(context)
-- context.data.title, context.operation, etc. all typed
return context
end)
-- Per-collection per-field: value narrows to the field's type
return crap.collections.posts.field_hook("title", function(value, context)
-- value: string (from posts.title's text field)
return value:lower()
end)
-- Cross-collection generic (hook that runs on multiple collections)
return crap.any.field_hook(function(value, context)
-- value: any, context: crap.FieldHookContext
return value
end)
-- Access function (uniform context across all collections)
return crap.any.access(function(context)
return context.user ~= nil
end)
The factories are pure pass-throughs at runtime (f(fn) = fn); they
exist solely so LuaLS can propagate the typed parameter slot into
your function body’s locals. Requires "type.inferParamType": true
in your .luarc.json — included in the init scaffold by default.
See crap.collections and crap.globals
for the full per-slug factory surface; crap.any
for the cross-collection helpers.
Two ways to address a collection
Every defined collection is reachable in two forms:
- Per-collection accessor (recommended for static slugs):
crap.collections.<slug>.find(query),crap.collections.<slug>.create(data), … Slug is bound, return values fully typed for editor autocomplete. - Slug-keyed dispatch (required for dynamic slugs):
crap.collections.find(slug, query),crap.collections.create(slug, data), … Same semantics, slug passed at call time. Use when the collection varies at runtime (auth strategies, generic plugins, migration loops).
Globals follow the same pattern: crap.globals.<slug>.{get,update}(...)
versus crap.globals.{get,update}(slug, ...).
See crap.collections and crap.globals
for the full surface of each accessor.
CRUD Availability
CRUD functions (whether called as crap.collections.<slug>.find(...) or
crap.collections.find(slug, ...), and crap.globals.<slug>.{get,update}(...)
or the slug-keyed equivalents) are only available inside hooks with
transaction context:
before_validatehooks — Yesbefore_changehooks — Yesbefore_deletehooks — Yesafter_changehooks — Yes (runs inside the same transaction viarun_hooks_with_conn)after_deletehooks — Yes (runs inside the same transaction viarun_hooks_with_conn)after_readhooks — No (no transaction)before_readhooks — No (no transaction)- Collection definition files — No
Calling CRUD functions outside of transaction context results in an error:
crap.collections CRUD functions are only available inside hooks
with transaction context (before_change, before_delete, etc.)
Lua VM Architecture
Crap CMS uses two stages of Lua execution:
- Startup VM — a single VM that loads collection/global definitions and runs
init.lua. Used only during initialization, then discarded. - HookRunner pool — a pool of Lua VMs for runtime hook execution (size configured via
hooks.vm_pool_size). Each VM gets its own copy of thecrap.*API with CRUD functions registered.
All VMs have the config directory on their package path, so require("hooks.posts") works in both stages.