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 and CRUD operations
crap.globalsGlobal definition and get/update operations
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

CRUD Availability

CRUD functions (crap.collections.find, .create, .update, .delete, crap.globals.get, .update) 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.