This section explains how Nuxt Crouton's code generation system works. The CLI tools transform your collection schemas into fully functional CRUD interfaces with minimal configuration.
The Generation section covers everything about the code generation workflow:
crouton-generate CLIFile: cli-commands.md
Master the Nuxt Crouton CLI:
npx crouton-generate - Generate collections from schemasFile: 2.schema-format.md
Learn the YAML schema format for defining collections:
Field Types:
Schema Example:
name: products
description: Product catalog
icon: i-lucide-shopping-bag
fields:
- name: title
type: text
required: true
- name: price
type: number
validation:
min: 0
- name: category
type: select
options:
- electronics
- clothing
- books
File: 3.multi-collection.md
Work with multiple collections efficiently:
collections/ directoryFile: 4.cli-reference.md
Legacy CLI reference (may be outdated - refer to 5.cli-reference.md for latest).
File: 5.cli-reference.md
Complete reference of all CLI commands and options:
The typical workflow for generating collections:
schemas/[name]-schema.jsonnpx crouton-generate <layer> <collection> --fields-file schemas/[name]-schema.jsonlayers/[name]/When you run npx crouton-generate, Nuxt Crouton creates:
CroutonForm.vue - Auto-generated form componentCroutonTable.vue - Table/list view componentCroutonModal.vue - Modal for create/edit operationsuseCollectionForm() - Form state managementuseCollectionTable() - Table data and paginationuseCollection() - CRUD operationsuseCollectionQuery() - Advanced queryingGET /api/[collection] - List endpointGET /api/[collection]/[id] - Get single itemPOST /api/[collection] - Create endpointPUT /api/[collection]/[id] - Update endpointDELETE /api/[collection]/[id] - Delete endpointGenerated list endpoints (GET /api/[collection]) support ?ids=xxx by default for fetching specific items by ID. To add custom filtering (e.g., filtering by a parent entity like eventId), you need to extend both the query function and API handler.
1. Add a query function in server/database/queries.ts:
export async function getProductsByEventId(teamId: string, eventId: string) {
const db = useDB()
return await db
.select({ ...tables.products })
.from(tables.products)
.where(
and(
eq(tables.products.teamId, teamId),
eq(tables.products.eventId, eventId)
)
)
}
2. Handle the query param in index.get.ts:
import { getAllProducts, getProductsByIds, getProductsByEventId } from '...'
export default defineEventHandler(async (event) => {
const { team } = await resolveTeamAndCheckMembership(event)
const query = getQuery(event)
if (query.ids) {
return await getProductsByIds(team.id, String(query.ids).split(','))
}
// Custom filter: eventId
if (query.eventId) {
return await getProductsByEventId(team.id, String(query.eventId))
}
return await getAllProducts(team.id)
})
3. Pass the filter from the client using useCollectionQuery:
const eventId = computed(() => event.value?.id)
const { items } = await useCollectionQuery('products', {
query: computed(() => ({ eventId: eventId.value }))
})
The query object is automatically serialized to URL parameters (e.g., ?eventId=abc123) and the API endpoint reads them via getQuery(event).
After understanding generation:
Before working with generation:
For related concepts: