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

crap.schema

Schema introspection API. Provides read-only access to collection and global definitions loaded from Lua files. Available everywhere the crap global is accessible.

crap.schema.get_collection(slug)

Get a collection’s full schema definition. Returns a table or nil if not found.

local schema = crap.schema.get_collection("posts")
if schema then
    print(schema.slug)           -- "posts"
    print(schema.timestamps)     -- true
    print(schema.has_auth)       -- false
    print(#schema.fields)        -- number of fields
    for _, field in ipairs(schema.fields) do
        print(field.name, field.type, field.required)
    end
end

Return Value

FieldTypeDescription
slugstringCollection slug.
labelstable{ singular?, plural? } display names.
timestampsbooleanWhether created_at/updated_at are enabled.
has_authbooleanWhether authentication is enabled.
has_uploadbooleanWhether file uploads are enabled.
has_versionsbooleanWhether versioning is enabled.
has_draftsbooleanWhether draft/publish workflow is enabled (versioned + drafts).
fieldstable[]Array of field definitions (see below).

Field Schema

Each field table contains:

FieldTypeDescription
namestringField name.
typestringField type (text, number, relationship, etc.).
requiredbooleanWhether the field is required.
localizedbooleanWhether the field has per-locale values.
uniquebooleanWhether the field has a unique constraint.
relationshiptable?{ collection, has_many } for relationship fields.
optionstable[]?{ label, value } for select fields.
fieldstable[]?Sub-fields for array/group types (recursive).
blockstable[]?Block definitions for blocks type.

crap.schema.get_global(slug)

Get a global’s schema definition. Same return shape as get_collection.

local schema = crap.schema.get_global("site_settings")

crap.schema.list_collections()

List all registered collections with their slugs and labels.

local collections = crap.schema.list_collections()
for _, c in ipairs(collections) do
    print(c.slug, c.labels.singular)
end

crap.schema.list_globals()

List all registered globals with their slugs and labels.

local globals = crap.schema.list_globals()
for _, g in ipairs(globals) do
    print(g.slug, g.labels.singular)
end