Blazing Fast Next.js with Drizzle ORM and Turso Database
Want to build performant, scalable, and type-safe Next.js applications with a production ready database? Look no further than the powerful combination of Drizzle ORM and Turso , SQLite for Production.
Introduction
Drizzle ORM is a type-safe and performant ORM for TypeScript and JavaScript. It offers a clean and intuitive API for interacting with your database, making it easier to write and maintain your application's data logic. Turso, on the other hand, is a serverless database platform that provides a scalable and cost-effective solution for deploying your applications.
Why Choose This Stack?
- Next.js: The go-to framework for building modern web applications, offering server-side rendering, automatic code splitting, and a robust routing system. Learn more about Next.js
- Drizzle ORM: A type-safe and performant ORM for TypeScript and JavaScript, simplifying database interactions and ensuring code clarity. Explore Drizzle ORM
- Turso: A serverless database platform that provides a scalable and cost-effective solution for deploying your applications. Turso's edge SQLite database offers lightning-fast performance and seamless integration with Next.js. Discover Turso
- Bun: A fast, all-in-one JavaScript runtime that offers a powerful alternative to Node.js. Learn more about Bun
Setup
Let's get started with setting up Drizzle ORM with Turso in your Next.js application.
1. Create a Next.js Project
If you don't have an existing Next.js project, create one using the following command:
Replace my-drizzle-turso-app
with your desired project name.
2. Install Dependencies
Install the necessary dependencies for Drizzle ORM, Turso, and SQLite:
3. Spin Up Your Turso Database
Sign up or Log in: Head over to Turso and create a free account or log in if you already have one.
Signup or Login to Turso CLI
Signup:
Login:
Create a New Database
Create a new database using the following command:
Get turso credentials
Database URL:
Authentication token:
4. Setup Environment Variables
Update your .env
file with the connection URL and authentication token:
5. Connect Drizzle ORM to Your Database
Create a file src/db/db.ts
and set up your database configuration:
6. Define Database Schema
Create a file src/db/schema.ts
to define your database schema:
7. Setup Drizzle Configuration
Create a drizzle.config.ts
file in the root of your project:
8. Generate and Apply Migrations
Add this script to your package.json
file:
Generate migrations using Drizzle Kit:
Apply the migrations:
Open the Drizzle Studio:
Deploy to Vercel
Deploy your Next.js application, ensuring you set the TURSO_DATABASE_URL
and TURSO_AUTH_TOKEN
environment variables in your deployment settings.
Conclusion
Congratulations! You've successfully integrated Drizzle ORM and Turso into your Next.js application. You can now build feature-rich and performant applications with ease, leveraging the power of type-safe database interactions and a serverless edge database.