Skip to content

Commit 39c81d0

Browse files
github-actions[bot]svelte-docs-bot[bot]Rich-Harris
authored andcommitted
Sync kit docs (sveltejs#1528)
sync kit docs Co-authored-by: svelte-docs-bot[bot] <196124396+svelte-docs-bot[bot]@users.noreply.github.com> Co-authored-by: Rich Harris <[email protected]>
1 parent 710d9ac commit 39c81d0

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

apps/svelte.dev/content/docs/kit/20-core-concepts/60-remote-functions.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,59 @@ Any query can be re-fetched via its `refresh` method, which retrieves the latest
173173
174174
> [!NOTE] Queries are cached while they're on the page, meaning `getPosts() === getPosts()`. This means you don't need a reference like `const posts = getPosts()` in order to update the query.
175175
176+
## query.batch
177+
178+
`query.batch` works like `query` except that it batches requests that happen within the same macrotask. This solves the so-called n+1 problem: rather than each query resulting in a separate database call (for example), simultaneous queries are grouped together.
179+
180+
On the server, the callback receives an array of the arguments the function was called with. It must return a function of the form `(input: Input, index: number) => Output`. SvelteKit will then call this with each of the input arguments to resolve the individual calls with their results.
181+
182+
```js
183+
/// file: weather.remote.js
184+
// @filename: ambient.d.ts
185+
declare module '$lib/server/database' {
186+
export function sql(strings: TemplateStringsArray, ...values: any[]): Promise<any[]>;
187+
}
188+
// @filename: index.js
189+
// ---cut---
190+
import * as v from 'valibot';
191+
import { query } from '$app/server';
192+
import * as db from '$lib/server/database';
193+
194+
export const getWeather = query.batch(v.string(), async (cities) => {
195+
const weather = await db.sql`
196+
SELECT * FROM weather
197+
WHERE city = ANY(${cities})
198+
`;
199+
const lookup = new Map(weather.map(w => [w.city, w]));
200+
201+
return (city) => lookup.get(city);
202+
});
203+
```
204+
205+
```svelte
206+
<!--- file: Weather.svelte --->
207+
<script>
208+
import CityWeather from './CityWeather.svelte';
209+
import { getWeather } from './weather.remote.js';
210+
211+
let { cities } = $props();
212+
let limit = $state(5);
213+
</script>
214+
215+
<h2>Weather</h2>
216+
217+
{#each cities.slice(0, limit) as city}
218+
<h3>{city.name}</h3>
219+
<CityWeather weather={await getWeather(city.id)} />
220+
{/each}
221+
222+
{#if cities.length > limit}
223+
<button onclick={() => limit += 5}>
224+
Load more
225+
</button>
226+
{/if}
227+
```
228+
176229
## form
177230
178231
The `form` function makes it easy to write data to the server. It takes a callback that receives the current [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData)...

apps/svelte.dev/content/docs/kit/98-reference/20-$app-server.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,49 @@ function read(asset: string): Response;
263263

264264

265265

266+
## query
267+
268+
<div class="ts-block">
269+
270+
```dts
271+
namespace query {
272+
/**
273+
* Creates a batch query function that collects multiple calls and executes them in a single request
274+
*
275+
* See [Remote functions](https://svelte.dev/docs/kit/remote-functions#query.batch) for full documentation.
276+
*
277+
* @since 2.35
278+
*/
279+
function batch<Input, Output>(
280+
validate: 'unchecked',
281+
fn: (
282+
args: Input[]
283+
) => MaybePromise<(arg: Input, idx: number) => Output>
284+
): RemoteQueryFunction<Input, Output>;
285+
/**
286+
* Creates a batch query function that collects multiple calls and executes them in a single request
287+
*
288+
* See [Remote functions](https://svelte.dev/docs/kit/remote-functions#query.batch) for full documentation.
289+
*
290+
* @since 2.35
291+
*/
292+
function batch<Schema extends StandardSchemaV1, Output>(
293+
schema: Schema,
294+
fn: (
295+
args: StandardSchemaV1.InferOutput<Schema>[]
296+
) => MaybePromise<
297+
(
298+
arg: StandardSchemaV1.InferOutput<Schema>,
299+
idx: number
300+
) => Output
301+
>
302+
): RemoteQueryFunction<
303+
StandardSchemaV1.InferInput<Schema>,
304+
Output
305+
>;
306+
}
307+
```
308+
309+
</div>
310+
266311

0 commit comments

Comments
 (0)