SvelteKit - Create & Update

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
Search for a command to run...

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
No comments yet. Be the first to comment.
SvelteKit is a powerful framework for building web apps. Follow my series of posts to learn how to use its features and create fast and efficient web apps.
In this post, I will introduce you to SvelteKit – a powerful and easy to use framework for building server-side rendered web applications. I've used the predecessor to SvelteKit, Sapper but things have moved on quite a bit since then so let's see how...
Writing this up so I have somewhere to quickly grab basic Jest setup. This information is copied from the Jest Documentation Site compressed into just the parts I'm interested in. Create folders mkdir -p <project>/src cd <project> Install and init n...

In this blog post, we'll explore the process of setting up and configuring a Postgres database using Docker and seeding it with test data. This approach can be used for a variety of apps and can be easily adapted for your own needs. We'll cover all t...

Introduction In this post, I will explore how to create a REST API using Spring Boot and Kotlin. We will dive into the fundamentals of Spring Boot and Kotlin, explain how to set up the project, and guide you through the process of building the API st...

Introduction Welcome to the latest post in my series of Koan Battles using Kotlin and TypeScript. In this series, we'll complete a Koan in both languages and then discuss the differences between the two solutions. My goal is to help you gain a deeper...

Continuing from last time, we will explore the concepts behind Create and Update functionality and provide a step-by-step guide on how to implement it in your SvelteKit application. Let's get started!
In SvelteKit, the server-side code and client-side code are split between files. The server code is responsible for rendering the initial HTML for a page. The client code is responsible for handling user interactions and updating the page without requiring a full page refresh.
The server code for the page is stored +page.server.ts, the .server name is important and signifies that this code will only ever run on the server. We're safe to make database calls and even store API secrets here.
We call the Prisma function getTop3 to fetch the data from the database, this data is strongly typed when used in the svelte component page +page.svelte.
export const load = (async () => {
return await getTop3();
}) satisfies PageServerLoad<{ todos: Todo[] }>;
The +page.server.js file exports actions, which allows us to POST data to the server using a <form> element in the page component. We have create and update actions that we receive the form data and call the Prisma code.
export const actions = {
create: async ({ request }) => {
const data = await request.formData();
return createTop3((data.getAll('todo') as string[]) ?? []);
},
update: async ({ request }) => {
const data = await request.formData();
const id = Number(data.get('id'));
const complete = data.get('todo') === 'on';
return updateTodo(id, complete);
}
} satisfies Actions;
Nothing SvelteKit specific here, this is standard Prisma code used to fetch, create and update the database.
export async function getTop3(): Promise<{ todos: Todo[] }> {
const top3 = await prisma.top3.findFirst({
where: {
createdAt: {
equals: new Date()
}
},
include: {
todos: {
orderBy: {
id: 'asc'
}
}
}
});
return { todos: top3 ? top3.todos : [] };
}
export async function createTop3(data: string[]): Promise<Top3> {
return await prisma.top3.create({
data: {
todos: {
create: data.map((title) => ({ title, complete: false }))
}
}
});
}
export async function updateTodo(id: number, complete: boolean): Promise<Todo> {
return await prisma.todo.update({
where: { id },
data: {
complete
}
});
}
To call the create action from the page, we just need to add a standard HTML <form/>, no JavaScript is needed! We set its method to POST and the action to ?/create. This form data can then be processed in the +page.server.ts create action that we saw earlier.
<form method="POST" action="?/create">
<label> 1. </label>
<input type="text" name="todo" autoFocus />
<br />
<label> 2. </label>
<input type="text" name="todo" />
<br />
<label> 3. </label> <input type="text" name="todo" />
<br />
<button type="submit">Create</button>
</form>
To update the Todo data, again we just need to add a standard HTML <form/> and this time submit the form with the action ?/update. Notice the hidden input used to send the Todo id field to the server.
<div class="todo">
<form method="POST" action="?/update" bind:this={form}>
<input name="id" type="hidden" bind:value={todo.id} />
<input id={`todo-${todo.id}`} name="todo" type="checkbox"
bind:checked={todo.complete}
on:change={() => form.requestSubmit()} />
</form>
<label for={`todo-${todo.id}`}>
{todo.title}
</label>
</div>
This gives us everything we need to create and update our daily top 3 todo items:

Next time we’ll take a look at adding some nice styling using Tailwind CSS.
You can grab the code from the GitHub Repository.