sveltekit-patterns

SvelteKit patterns for devhub-crm. Use for remote functions (query, form, command), routing, and server-side logic.

⭐6stars🔀0forks📁View on GitHub🕐Updated Jan 1, 2026⬇️2downloads

When & Why to Use This Skill

This Claude skill provides standardized SvelteKit patterns specifically optimized for the devhub-crm ecosystem. It streamlines the development of remote functions—including queries, forms, and commands—while managing complex routing and server-side logic. By enforcing core principles such as Valibot validation, N+1 prevention through batching, and mandatory security filters, it ensures that web applications are built with consistent, scalable, and secure architectural standards.

Use Cases

  • Standardizing Data Access: Implement the 'query' pattern to fetch database records efficiently, utilizing built-in batching to optimize performance and prevent N+1 query problems.
  • Streamlining Form Logic: Develop secure, validated form mutations that handle user input via Valibot schemas and manage post-submission redirects and state refreshes automatically.
  • Executing Server-Side Commands: Use the 'command' pattern for non-form mutations, ensuring that UI state remains synchronized through mandatory refresh calls and consistent return values.
  • Enforcing Security and Best Practices: Automatically apply security filters like user_id isolation and follow established file-based routing and database patterns to maintain code integrity.
namesveltekit-patterns
descriptionSvelteKit patterns for devhub-crm. Use for remote functions (query, form, command), routing, and server-side logic.

SvelteKit Patterns

Quick Start

// Query: Read data
export const get_contacts = query(() =>
	db.prepare('SELECT * FROM contacts').all(),
);

// Form: Validated mutation with redirect
export const create = form(
	v.object({ name: v.string() }),
	async ({ name }) => {
		db.prepare('INSERT INTO contacts ...').run(id, name);
		redirect(303, '/contacts');
	},
);

// Command: Mutation with refresh
export const delete_contact = command(v.string(), async (id) => {
	db.prepare('DELETE FROM contacts WHERE id = ?').run(id);
	await get_contacts().refresh();
	return { success: true };
});

Core Principles

  • Types: query (read), form (mutations + redirects), command (mutations only)
  • Validation: Use valibot schemas for all inputs
  • Security: Always include user_id in WHERE clauses
  • Batching: Use query.batch() for N+1 prevention
  • Refresh: Call .refresh() in form/command handlers
  • Use .current: Store queries in variables, check .current === undefined for initial load
  • No manual keys: Never use refresh_key++ or {#key} blocks
  • Return values: Commands must return { success: true }

Reference Files