--- alwaysApply: true --- # Svelte Remote Functions (Experimental) This project uses SvelteKit's experimental `remoteFunctions` feature. Follow these rules when working with server-side logic and data fetching. ## 1. Definition & Structure - Logic MUST be defined in `*.remote.ts` files co-located with the route. - Import `form`, `query`, and `command` from `$app/server`. - Import `z` from `zod/v4`. ## 2. Creating Remote Functions ### Queries (Data Fetching) Use `query` for reading data. It behaves like a GET request. ```typescript import { query } from '$app/server'; import z from 'zod/v4'; export const getData = query(z.object({ id: z.string() }), async ({ id }) => { return await db.select(...); }); ``` ### Commands (Mutations) Use `command` for direct logic execution (RPC style), typically for actions like delete, toggle, or specific updates. ```typescript import { command } from '$app/server'; export const deleteItem = command(z.string(), async (id) => { await db.delete(...); }); ``` ### Forms (Form Submissions) Use `form` for handling HTML form submissions with progressive enhancement and field validation. ```typescript import { form } from '$app/server'; export const updateItem = form(schema, async (data) => { await db.update(...); return { success: true }; }); ``` ## 3. Frontend Usage ### Reactivity with Queries Queries can be called inside `$derived` to create reactive promises that update when their arguments change. ```svelte {#await itemsPromise then items}