sveltekit-patterns

Implement SvelteKit query, form, and command remote functions with valibot validation.

6|Updated Aug 8, 2023
One-click install
npx skills add https://github.com/spences10/devhub-crm --skill sveltekit-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sveltekit-patterns
Source: https://github.com/spences10/devhub-crm/tree/main/.claude/skills/sveltekit-patterns
Command: npx skills add https://github.com/spences10/devhub-crm --skill sveltekit-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires valibot, and includes scripts (resource) and references (resource) and assets (resource) components.

What problem does it solve?

This Skill provides essential SvelteKit patterns for building full-stack applications, focusing on efficient and type-safe communication between client and server using remote functions. It addresses common challenges like data fetching, form submissions, mutations, and reactive UI updates, enabling developers to build robust and highly interactive applications with less boilerplate.

Core Features & Use Cases

  • Remote Functions (query, form, command): Defines clear patterns for read-only data fetching, validated form submissions with redirects, and AJAX-style mutations with return values.
  • Reactive UI Updates: Emphasizes the .current property pattern to achieve smooth, in-place UI updates without jarring page reloads, preserving user context during data refreshes.
  • Data Validation & Security: Integrates valibot for input validation and highlights the importance of row-level security in all server-side operations.
  • N+1 Prevention: Demonstrates query.batch() for optimizing data fetching and preventing redundant database queries, improving application performance.
  • Use Case: Build a contact management application where users can view, create, update, and delete contacts. This Skill provides the patterns for defining remote functions for each operation, ensuring data is fetched efficiently, forms are validated, and the UI updates smoothly without full page refreshes.

Quick Start

For data fetching, use export const get_data = query(() => ...);. For forms with redirects, use export const create_item = form(schema, async () => redirect(303, '/path'));. For AJAX mutations, use export const update_item = command(schema, async () => ({ success: true }));. For smooth UI updates, store queries in variables (const my_query = get_data();) and access data via my_query.current.

Frequently Asked Questions about sveltekit-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fetch data and handle form submissions in SvelteKit without full page reloads?

SvelteKit patterns use remote functions to manage data fetching and form submissions efficiently. Use `query()` for read-only data fetching, `form()` for validated submissions with redirects, and `command()` for AJAX mutations. Access data via the `.current` property to enable smooth in-place UI updates without page refreshes.

How do I validate user input on the server side in SvelteKit?

Server-side input validation in SvelteKit uses valibot schemas integrated into `form()` and `command()` remote functions. Define validation schemas to enforce type safety and security constraints before processing data, ensuring all inputs are validated before database operations execute.

What's the best way to prevent N+1 queries when fetching related data in SvelteKit?

Prevent N+1 queries by using `query.batch()` to batch multiple data-fetching operations into a single request. This optimization reduces redundant database queries and improves application performance when loading related or dependent data in SvelteKit forms and pages.

Can I use SvelteKit remote functions with row-level security for multi-tenant applications?

Yes, SvelteKit remote functions support row-level security by implementing user-scoped queries with `user_id` validation on the server. This ensures users only access data they're authorized to view, enabling secure multi-tenant or user-scoped data fetching patterns.

How do I handle transactional database operations across multiple mutations in SvelteKit?

SvelteKit patterns support transactional database operations through batched or coordinated remote function calls. Define server logic in `command()` functions that wrap multiple mutations in database transactions, ensuring data consistency across create, update, and delete operations.

Do I need to set up separate endpoints for queries and mutations in SvelteKit?

No. SvelteKit patterns consolidate all remote communication through a single pattern-based interface. Use `query()` for reads, `form()` for validated writes with redirects, and `command()` for AJAX mutations—eliminating the need to manage separate REST or RPC endpoints manually.