Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

NamespaceDescription
crap.collectionsCollection definition, CRUD operations, and per-collection typing factories (crap.collections.<slug>.{hook,field_hook,condition,access,auth_strategy,row_label})
crap.globalsGlobal definition, get/update, and per-global typing factories
crap.anyCross-collection typing factories — crap.any.{collection_hook,field_hook,access,auth_strategy,job_handler,row_label,display_condition}
crap.fieldsField factory functions (crap.fields.text(), etc.)
crap.hooksGlobal hook registration
crap.jobsJob definition
crap.logStructured logging
crap.utilUtility functions
crap.authPassword hashing and verification (Argon2id)
crap.envRead-only environment variable access
crap.httpOutbound HTTP requests (blocking)
crap.configRead-only access to crap.toml values
crap.localeLocale configuration queries
crap.emailSend email via configured SMTP
crap.cryptoCryptographic utilities (HMAC, random bytes, hashing)
crap.schemaRuntime schema introspection
crap.richtextCustom 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_validate hooks — Yes
  • before_change hooks — Yes
  • before_delete hooks — Yes
  • after_change hooks — Yes (runs inside the same transaction via run_hooks_with_conn)
  • after_delete hooks — Yes (runs inside the same transaction via run_hooks_with_conn)
  • after_read hooks — No (no transaction)
  • before_read hooks — 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:

  1. Startup VM — a single VM that loads collection/global definitions and runs init.lua. Used only during initialization, then discarded.
  2. 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 the crap.* API with CRUD functions registered.

All VMs have the config directory on their package path, so require("hooks.posts") works in both stages.