Skip to Content
The Public InterfaceSeptember 12, 2026

The Public Interface

May 1, 2023

Write Once, Run Anywhere

Write one schema in JavaScript and query data on the edge with Drizzle-ORM, SvelteKit, and Vercel Storage.

By Ross Robinosvelte / drizzle / orm

At the time of this writing, Drizzle has not reached 1.0. Check out tools like Prisma for a production ready solution.

Updated May 9th, 2024

Many projects set out with the goal of writing once, and running anywhere. Recently, there has been a trend of combining front-end and backend into the same project with tools like Next.js or React Server Components. Drizzle-ORM helps developers get closer to this promise at the database layer by allowing users to write their schema and query their data in JavaScript.

Skip to tutorial

Maintaining consistency between application code and database schema can be a challenging task, especially considering the involvement of multiple languages at each layer of the architecture. Developers want to have a single, unified source of truth for data representation that can be utilized across both their database and applications. This approach simplifies the process of ensuring that data being inserted from the application adheres to the database schema, while also ensuring that data retrieved from the database is accurately displayed within the application.

Incredible ORM tools like Prisma enable this workflow for developers. Prisma provides it’s own language to define a schema in a .prisma file, create database migrations, and generate types for the client. Another common practice is to generate types from the by introspecting a database schema, I wrote about how you can do this with Supabase here.

The “serverless” infrastructure has transformed the way we approach computing and development. Along with “serverless”, the edge runtime has emerged as a faster alternative to lambda functions. Ideally, new applications can run in either environment.

Drizzle-ORM is a project similar to Prisma, with a few notable distinctions:

Drizzle takes developers one step closer to the write once, run anywhere workflow.

Here’s how to use Drizzle-ORM in a SvelteKit project with Vercel’s Postgres Storage. Drizzle makes it easier to keep your services modular and swap out the database provider, check out the Planetscale integration for a similar developer experience.

Here we can see how creating a schema is very similar to writing SQL. The benefits of writing it in TypeScript are that Drizzle will handle any migrations when the schema is changed, and we also get automatic types for any query made.

out is the path to the output directory, schema is the path to the schema file, dialect is the postgresql since we are using Vercel Postgres, and breakpoints is whether to execute each SQL statement in the migration individually. These can all be customized based on your preferences.

You can see the output in the drizzle directory containing a 0000_...sql file. This file contains the statements to keep your database in sync with your schema.

You have now created a table! Each time you change the src/lib/db/schema.ts file, run npm run migrate. This will generate a new migration (0001_...sql) taking into account all of the changes made. The drizzle folder serves as a history of all of your migrations made.

Here’s where the magic happens, all we need to do is pass Vercel’s sql as a argument into drizzle. We can give this file a .server postfix to ensure it is only utilized on the server.

Since the data isn’t critical to our users, let’s stream it by returning a promise in the load function.

Here we achieve full type safety from the database to the server to the client through the combination of Drizzle-ORM and SvelteKit’s load functions–all without manually writing any types (be sure to run vercel dev again to generate types for this load function).

Finally, we can render our data to the page in src/routes/+page.svelte.

Deploy your changes by pushing your changes to GitHub and verify your build in the Vercel Dashboard.

Drizzle-ORM simplifies the process of writing code once and running it on any platform, by allowing developers to write in JavaScript and execute it on the edge runtime. Drizzle-ORM provides the convenience of using SQL-like syntax, and ensures reusability across various database providers. Developers can confidently invest their time in mastering a versatile and efficient tool.

Thanks for reading!

For an example of this project, check out my personal site here. Also, check out the drizzle-zod module to infer Zod schemas from Drizzle schemas.…

Continue reading