hansontechsolutions.com

Integrating Turso and Drizzle: A Comprehensive Guide

Written on

Chapter 1: Introduction to Turso and Drizzle

Setting up Turso and Drizzle has never been easier. After PlanetScale discontinued its free tier, many developers, myself included, sought alternative database solutions that work seamlessly with Drizzle ORM. This led me to Turso DB, a scalable SQLite-based database that not only fills the gaps left by PlanetScale but also offers enhanced compatibility with Drizzle, easy setup, and additional features.

Let’s get started with the setup process!

Table of Contents

  1. Setting Up a Next.js 14 Project
  2. Installing Dependencies
  3. Configuring Turso
  4. Integrating Turso with Drizzle
  5. Testing in Next.js

Section 1.1: Setting Up a Next.js 14 Project

To initiate a Next.js project, follow the instructions available on the official website. Typically, you will execute the following command in your terminal:

npx create-next-app@latest

Section 1.2: Installing Dependencies

Open your terminal in the project directory and run one of the following commands to install the required dependencies:

npm install drizzle-orm drizzle-kit @libsql/client dotenv

yarn add drizzle-orm drizzle-kit @libsql/client dotenv

pnpm add drizzle-orm drizzle-kit @libsql/client dotenv

bun add drizzle-orm drizzle-kit @libsql/client dotenv

Section 1.3: Configuring Turso

Before diving into Next.js, we first need to:

  1. Create a Turso database.
  2. Acquire the necessary environment variables.

Open your UNIX terminal and run the following command (note: this will not work on Windows):

For Windows users, you will need to install WSL to run a Linux environment.

For additional guidance, refer to the article linked below:

  1. After Turso is installed, authenticate yourself to create and manage databases:

turso auth signup

For WSL users, you can use the headless option to simplify authentication:

turso auth signup --headless

  1. Once authenticated, create a new database with the following command:

turso db create [database-name]

You can list your created databases with:

turso db list

  1. Generate the required environment variables, including the database URL and authentication token:

turso db list # Copy the "URL" value.

turso db tokens create [database-name] -e # Prevent token expiry.

  1. In your Next.js project, create a file named .env.local and paste your environment variables:

# Turso

TURSO_DATABASE_URL=

TURSO_AUTH_TOKEN=

Chapter 2: Integrating Turso with Drizzle

To integrate Turso with Drizzle, create a drizzle-config.ts file in the root of your project containing the following code:

// drizzle.config.ts

import dotenv from "dotenv"

import type { Config } from "drizzle-kit"

dotenv.config({ path: ".env.local" })

export default {

schema: "./src/db/schema.ts",

out: "./src/db/migrations",

dbCredentials: {

url: process.env.TURSO_DATABASE_URL!,

authToken: process.env.TURSO_AUTH_TOKEN!,

},

driver: "turso",

} satisfies Config

Next, create the database instance for use throughout your Next.js application:

// src/db/index.ts

import { createClient } from "@libsql/client"

import { drizzle } from "drizzle-orm/libsql"

const turso = createClient({

url: process.env.TURSO_DATABASE_URL!,

authToken: process.env.TURSO_AUTH_TOKEN,

})

export const db = drizzle(turso)

export type DbClient = typeof db

Optionally, you may update your package.json to include scripts for managing database migrations:

"scripts": {

"generate": "drizzle-kit generate:sqlite",

"push": "drizzle-kit push:sqlite",

"drop": "drizzle-kit drop"

},

Chapter 3: Testing in Next.js

Congratulations! You have successfully configured Turso and Drizzle within a Next.js application. Let’s create a simple posts schema to test our setup:

// src/db/schema.ts

import { randomUUID } from "crypto"

import { sqliteTable, text } from "drizzle-orm/sqlite-core"

export const posts = sqliteTable("posts", {

id: text("id", { length: 255 })

.notNull()

.primaryKey()

.$defaultFn(() => randomUUID()),

title: text("title").notNull(),

content: text("content").notNull(),

})

Next, develop server actions to handle retrieving and adding posts:

// src/app/_actions/postActions.ts

"use server"

import { db } from "@/db"

import { posts } from "@/db/schema"

import { InferSelectModel } from "drizzle-orm"

import { revalidatePath } from "next/cache"

export type Post = InferSelectModel

export const getPosts = async () => {

return await db.select().from(posts)

}

export const addPost = async ({ title, content }: Post) => {

await db.insert(posts).values({ title, content })

revalidatePath("/")

}

Finally, create the frontend code to display and submit posts:

import { getPosts, addPost, Post } from "./_actions/postActions"

export default async function Home() {

async function submitPost(formData: FormData) {

"use server"

const title = formData.get("title")

const content = formData.get("content")

await addPost({ title, content } as Post)

}

return (

<div>

<h1>Posts:</h1>

{(await getPosts()).map((post) => (

<div key={post.id}>

<h2>Title: {post.title}</h2>

<p>Content: {post.content}</p>

</div>

))}

<form onSubmit={submitPost}>

<input name="title" placeholder="Title" required />

<textarea name="content" placeholder="Content" required />

<button type="submit">Add Post</button>

</form>

</div>

)

}

Here’s a visual representation of the application:

Example application showcasing posts and submission form

If you found this guide helpful, don’t forget to subscribe, like, and connect with me! 🌐

References

Chapter 4: Additional Resources

Explore the easiest way to set up a database in Next.js 14 with Turso and Drizzle in this informative video.

Learn how to master Drizzle ORM in just 60 minutes with this quick tutorial.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

52 Intriguing Insights About Sexuality You Might Not Know

Discover 52 captivating facts about sexuality that challenge common perceptions and deepen understanding.

Rethinking the 40-Hour Workweek: Finding True Fulfillment

Explore why the traditional 40-hour workweek may hinder your fulfillment and how to create a more meaningful work-life balance.

Decoding Speech from Brain Waves: New Frontiers in AI

A groundbreaking AI model decodes speech from brain activity, offering new communication avenues for those with disabilities.

Mastering Web Scraping: A Comprehensive Guide Using Python

Discover effective methods for web scraping with Python, including practical examples and tools like Beautiful Soup and Selenium.

Emerging AI Trends: Transforming the Landscape in 2022

Discover the key AI trends for 2022 that will shape the future of technology and business.

Unlocking Procrastination: Embracing Our Inner Lazy Genius

Discover how embracing procrastination can lead to productivity and self-acceptance through innovative tools and perspectives.

Harnessing ChatGPT: A New Era for Side Hustles

Discover how ChatGPT is transforming the side hustle landscape and how you can leverage it for your own success.

# Are Blue Light Products a Hoax? Insights from Scientific Research

Investigating the validity of blue light blocking products based on scientific research.