Published on November 24, 2023 •
Updated on June 17, 2024
Drizzle ORM is a lightweight, fast, and easy-to-use ORM for Node.js, written in TypeScript. It supports all major databases, including MySQL, PostgreSQL, SQLite, and MongoDB. It’s also a good alternative to Prisma. In this article, we’ll learn how to use Drizzle ORM with SvelteKit.
First, we’ll need to create a new SvelteKit project.
Select the appropriate options and make sure to enable TypeScript.
We can now install Drizzle ORM and its dependencies.
We’ll use a https://planetscale.com/ database for this tutorial, but you can use any database you want. Just make sure to install the appropriate driver.
Here’s what you need to install based on your database:
We first need to set up our database credentials. Create a .env file in the root of your project and add the following:
Next, we’ll need to create 2 files in our project: src/lib/server/db.ts and src/lib/server/schema.ts. The first file will contain our database connection, and the second will contain our database schema.
Let’s start with src/lib/server/db.ts. Add the following code to the file:
This code is pretty straightforward. We import the drizzle function from drizzle-orm/planetscale-serverless, which is the driver we’ll be using. And we import the connect function from @planetscale/database, which is provided by the Planetscale SDK. We then create a connection to our database using the connect function and pass it to the drizzle function to create our database object.
Also, notice that we’re importing our database credentials from the .env file via imports 🤯 that’s a cool feature of SvelteKit!
Next, let’s create our database schema. We’ll make a simple todo app because that’s what everyone does when they’re learning a new framework 😅
Create a src/lib/server/schema.ts file and add the following code:
Did you also notice that this schema is written in TypeScript? That’s another cool feature of Drizzle ORM! We can write our schema in TypeScript, and Drizzle ORM will automatically generate the SQL for us. This is a huge improvement over Prisma, which requires us to write our schema in a custom DSL. Here’s the SQL that Drizzle ORM generates for us:
Now, that we’re done with our database schema, we need to push it to our database. We can do this with the tool provided by Drizzle called drizzle-kit.
Install it by running the following command:
Next, we need to create a drizzle.config.ts file at the root of our project. Add the following code to the file:
Now update the package.json file with the following scripts:
The db:push script will push our schema to the database, and the db:studio script will open the Drizzle Studio, which is a GUI for managing our database.
Now, run the following command to push our schema to the database:
You should see the following output:
Now, run the following command to open the Drizzle Studio:
Woohoo! We’ve successfully set up our database and pushed our schema to it. Now, let’s create our SvelteKit app.
Creating the SvelteKit app
First, run the dev server if you haven’t already:
We’ll create a simple to-do app with only 1 page. Create a src/routes/+page.svelte file and add the following code:
Add this to the <head> of app.html so that we don’t need to write CSS.
Now, let’s add some functionality to our app. We’ll use Svelte server actions to handle our form submission. Create a src/routes/+page.server.ts file and add the following code:
We’ll then update our frontend to show the message returned by the server action. Update the src/routes/+page.svelte file with the following code:
Now, try adding a todo and check Drizzle Studio. You should see that the todo has been added to the database.
We then can add a load function to load all the todos from the database. Add the following code to src/routes/+page.server.ts:
And update the src/routes/+page.svelte file with the following code:
Now, we can update and delete todos. Add the following actions to the src/routes/+page.server.ts file:
Now test it out! You should be able to add, update, and delete todos. You can also check Drizzle Studio to see the changes in the database.
Conclusion
Now that you’ve learned how to use Drizzle ORM with SvelteKit, you can use it in your own projects. You can also check out the Github Repo as well as the Demo for this project.
Here are the sources I used to write this article: