This section explores common patterns and best practices for working with data in Nuxt Crouton. Learn how to build forms, tables, relationships, and integrate with Drizzle ORM.
The Patterns section provides practical guidance for common use cases:
File: 1.relations.md
Master relationships between collections:
ref-target)Example:
# posts.yml
fields:
- name: author
type: reference
ref-target: users
- name: categories
type: reference
ref-target: categories
multiple: true
File: 2.forms.md
Form patterns and customization:
Key Composables:
useCollectionForm() - Form state and CRUD operationsuseFormValidation() - Custom validation logicFile: 3.tables.md
Table patterns and composition:
Key Composables:
useCollectionTable() - Table data and paginationuseCollectionQuery() - Advanced filtering and searchFile: drizzle.md
Working directly with Drizzle ORM:
Example:
import { db } from '~/server/database/db'
import { posts } from '~/server/database/schema'
import { eq } from 'drizzle-orm'
// Custom query
const publishedPosts = await db.select()
.from(posts)
.where(eq(posts.published, true))
.orderBy(posts.createdAt)
File: 5.list-layouts.md
Alternative layouts for displaying collections:
const { create } = useCollection('posts')
await create({
title: 'My Post',
author: userId, // Reference to users collection
categories: [categoryId1, categoryId2] // Multiple references
})
const { data } = await useCollectionQuery('posts', {
include: ['author', 'categories'], // Include related data
filters: {
'author.name': 'John Doe'
}
})
<script setup lang="ts">
const { formData, save } = useCollectionForm('products')
</script>
<template>
<CroutonForm v-model="formData" @save="save">
<!-- Override price field with custom component -->
<template #field-price>
<CustomPriceField v-model="formData.price" />
</template>
</CroutonForm>
</template>
<script setup lang="ts">
const { data, pagination } = useCollectionTable('products')
</script>
<template>
<CroutonTable :data="data" :pagination="pagination">
<!-- Override price column -->
<template #column-price="{ row }">
<span class="font-bold text-green-600">
${{ row.price.toFixed(2) }}
</span>
</template>
</CroutonTable>
</template>
useCollectionForm() and useCollectionTable() for state managementAfter mastering patterns:
Before diving into patterns:
For related technologies: