Generation

Generator Commands

Learn how to use Nuxt Crouton generator commands to create collections

Nuxt Crouton provides CLI commands to generate collections quickly. You can generate single collections or use a configuration file to generate multiple collections at once.

# Basic syntax
npx crouton-generate <layer> <collection> --fields-file <schema-file>

# Example
npx crouton-generate shop products --fields-file ./schemas/product-schema.json

Single Collection

Use the basic command to generate a single collection. The --fields-file option (required) specifies the path to your schema JSON file. Additional options include --force to overwrite files, --no-db to skip database generation, and --dry-run to preview changes.

npx crouton-generate shop products --fields-file ./schemas/product-schema.json

The generator creates several files in your project:

layers/[layer]/
  ├── components/
  │   └── [collection]/
  │       ├── List.vue       # Table/list view
  │       ├── Form.vue       # Create/edit form
  │       └── Table.vue      # Table component
  ├── composables/
  │   └── use[Collection].ts # Validation, columns, defaults
  └── types/
      └── [collection].ts    # TypeScript types

Multi-Collection Configuration

For larger projects with multiple collections, create a configuration file and generate everything at once:

# Generate from config
npx crouton-generate config ./crouton.config.js

# With options
npx crouton-generate config ./crouton.config.js --force --preview

Config File Format

// crouton.config.js
export default {
  collections: [
    { name: 'products', fieldsFile: './schemas/product-schema.json' },
    { name: 'categories', fieldsFile: './schemas/category-schema.json' },
  ],
  targets: [
    {
      layer: 'shop',
      collections: ['products', 'categories']
    }
  ],
  dialect: 'sqlite',
  flags: {
    useTeamUtility: false,   // Enable team-based multi-tenancy
    useMetadata: true,       // Add createdAt/updatedAt timestamps
    force: false,
    noTranslations: false,
    noDb: false
  }
}

Configuration Flags

useTeamUtility (boolean, default: false)

Enables team-based multi-tenancy features. When set to true:

Database schema changes:

  • Automatically adds teamId field (required, text) to all collections
  • Automatically adds userId field (required, text) to all collections

API endpoint changes:

  • Generates simplified endpoints with automatic team-based authentication
  • Automatically injects teamId and userId from the authenticated user's session
  • All database queries are automatically scoped to the current user's team
  • Adds resolveTeamAndCheckMembership middleware to all endpoints

When to use:

  • Multi-tenant SaaS applications
  • Apps where data must be isolated by organization/team
  • Apps using Nuxt Crouton's built-in team authentication

When NOT to use:

  • Single-tenant applications
  • Apps with custom authentication strategies
  • Apps where you need manual control over user/team associations
Important: Do NOT define teamId or userId in your schema JSON files when this flag is enabled. The generator adds them automatically, and manual definitions will cause duplicate key errors.

See Team-Based Authentication for usage examples.

useMetadata (boolean, default: true)

Automatically adds timestamp fields to track record creation and updates. When set to true:

Database schema changes:

  • Automatically adds createdAt timestamp field (auto-populated on record creation)
  • Automatically adds updatedAt timestamp field (auto-updated on record modification)

Database behavior:

  • createdAt is set automatically when a record is created
  • updatedAt is set automatically whenever a record is modified
  • Both fields use the database's native timestamp type

When to disable (false):

  • You want to implement custom timestamp tracking
  • You're integrating with an existing database schema
  • You need different timestamp field names or behavior
Important: Do NOT define createdAt or updatedAt in your schema JSON files when this flag is enabled. The generator adds them automatically, and manual definitions will cause duplicate key errors.

Example: Generate Multiple Collections

# Create config file
cat > crouton.config.js << 'EOF'
export default {
  collections: [
    { name: 'products', fieldsFile: './schemas/product-schema.json' },
    { name: 'categories', fieldsFile: './schemas/category-schema.json' },
    { name: 'orders', fieldsFile: './schemas/order-schema.json' },
  ],
  targets: [
    {
      layer: 'shop',
      collections: ['products', 'categories', 'orders']
    }
  ],
  dialect: 'sqlite'
}
EOF

# Generate all collections at once
npx crouton-generate config ./crouton.config.js

Helper Commands

Initialize Example Schema

Create an example schema file to get started:

crouton-generate init

# Output: Creates crouton-schema.json with example fields

Custom output path:

crouton-generate init --output=./schemas/product-schema.json

Generated Schema:

{
  "id": {
    "type": "string",
    "meta": { "primaryKey": true }
  },
  "name": {
    "type": "string",
    "meta": { "required": true, "maxLength": 255 }
  },
  "description": {
    "type": "text"
  },
  "price": {
    "type": "decimal",
    "meta": { "precision": 10, "scale": 2 }
  },
  "inStock": {
    "type": "boolean"
  },
  "createdAt": {
    "type": "date"
  }
}

After creation, you can generate a collection:

crouton-generate shop products --fields-file=crouton-schema.json

Install Required Modules

Install Nuxt Crouton and dependencies:

crouton-generate install

This installs:

  • @friendlyinternet/nuxt-crouton
  • Required peer dependencies
  • Updates nuxt.config.ts with extends

Manual installation:

pnpm add @friendlyinternet/nuxt-crouton

Then update nuxt.config.ts:

export default defineNuxtConfig({
  extends: ['@friendlyinternet/nuxt-crouton']
})

Rollback Commands

See Rollback & Undo Guide for complete documentation on removing collections.

Quick reference:

# Remove single collection
crouton-rollback <layer> <collection>

# Remove entire layer
crouton-rollback-bulk --layer=<name>

# Interactive removal
crouton-rollback-interactive

Complete CLI Flags Reference

Generation Flags

FlagTypeDefaultDescription
--fields-filestringrequiredPath to JSON schema file
--dialectstringsqliteDatabase dialect: sqlite or pg
--configstring-Use config file instead of CLI args
--dry-runbooleanfalsePreview mode - Show what would be generated without creating files
--forcebooleanfalseForce generation even if files exist (overwrites)
--no-translationsbooleanfalseSkip translation field generation
--no-dbbooleanfalseSkip database schema generation
--auto-relationsbooleanfalseAdd relation stub comments in generated code

Preview Mode (--dry-run)

See exactly what will be generated before creating any files:

crouton-generate shop products --fields-file=product-schema.json --dry-run

# Output:
📋 Preview: Would generate the following files:

layers/shop/
  ├── components/products/
   ├── List.vue (new)
   ├── Form.vue (new)
   └── Table.vue (new)
  ├── composables/
   └── useProducts.ts (new)
  └── types/
      └── products.ts (new)

Total: 5 files (5 new)

Would also update:
  - app.config.ts (add products collection)

Proceed? (y/n)

Use when:

  • First time generating a collection
  • Unsure about file placement
  • Checking if files will be overwritten
  • Testing a new schema structure

Force Mode (--force)

Overwrite existing files without prompting:

crouton-generate shop products --fields-file=product-schema.json --force

⚠️ Warning: This will overwrite any customizations you made to generated files.

Safe workflow:

# 1. Check what would be overwritten
crouton-generate shop products --fields-file=product-schema.json --dry-run

# 2. Commit your changes
git add .
git commit -m "Save customizations before regenerate"

# 3. Force regenerate
crouton-generate shop products --fields-file=product-schema.json --force

Skip Translations (--no-translations)

Generate without i18n support:

crouton-generate shop products --fields-file=product-schema.json --no-translations

Useful when:

  • Building a single-language app
  • Adding translations later
  • Faster generation for testing

Skip Database (--no-db)

Generate UI components only, no database schema:

crouton-generate shop products --fields-file=product-schema.json --no-db

Useful when:

  • Using an existing database
  • Only need frontend components
  • Database is managed separately

Auto Relations (--auto-relations)

Add commented relation stubs in generated code:

crouton-generate shop products --fields-file=product-schema.json --auto-relations

Generates comments like:

// TODO: Add relation
// export const productsRelations = relations(products, ({ one }) => ({
//   category: one(categories, {
//     fields: [products.categoryId],
//     references: [categories.id]
//   })
// }))

Useful when:

  • Planning to add Drizzle relations later
  • Want reminders about relation opportunities
  • Learning relation patterns

Config File Options

When using --config or config command, flags are set in the config file:

// crouton.config.js
export default {
  dialect: 'sqlite',
  flags: {
    useTeamUtility: false,    // Team-based multi-tenancy
    useMetadata: true,        // Timestamp fields (createdAt/updatedAt)
    force: false,
    noTranslations: false,
    noDb: false,
    autoRelations: true,
    dryRun: false
  }
}

CLI flags override config file settings:

# Config has force: false, but CLI overrides to true
crouton-generate config ./crouton.config.js --force

Common Flag Combinations

Safe First Generation:

crouton-generate shop products --fields-file=schema.json --dry-run
# Review output, then run without --dry-run

Multi-Tenant SaaS Application:

# Config with team utility enabled
crouton-generate config ./crouton.config.js
# With useTeamUtility: true in config

Single-Tenant Application:

# Config without team features
crouton-generate config ./crouton.config.js
# With useTeamUtility: false in config

Quick Testing (No DB):

crouton-generate shop products --fields-file=schema.json --no-db --no-translations

Full Featured Generation:

crouton-generate shop products --fields-file=schema.json --auto-relations

Force Regenerate:

crouton-generate shop products --fields-file=schema.json --force

Next Steps