kokobob.com

Seeding Neon Database with Drizzle in Next.js: A Comprehensive Guide

Written on

Chapter 1: Introduction to Database Seeding

In web development, it's often essential to populate a database with initial data before launching an application. This is particularly true for user roles, color options, or country lists. With Drizzle ORM, the process of seeding a Neon database becomes seamless. Let’s explore how to achieve this.

If you're looking to establish a connection to Neon, I highly recommend checking out this tutorial as it serves as a continuation of the current discussion.

Section 1.1: Setting Up Your Seed Script

Creating a seed script is a straightforward process. First, you need to define the database schema and then create an instance of the database. Once these steps are completed, you'll have the capability to manage the database effectively.

To insert data, you'll utilize the insert and values methods. In the insert method, specify the table schema where you want to add data, and in the values method, provide the actual data to be inserted.

import "dotenv/config";

import { drizzle } from "drizzle-orm/neon-http";

import { neon } from "@neondatabase/serverless";

import * as schema from "../db/schema";

const sql = neon(process.env.DATABASE_URL!);

// @ts-ignore

const db = drizzle(sql, { schema });

async function main() {

try {

console.log("Seed process initiated...");

await db.insert(schema.todos).values([

{

id: 1,

label: "Walk the dog",

},

{

id: 2,

label: "Grocery shopping",

},

]);

console.log("Seeding completed...");

} catch (error) {

console.error(error);

throw new Error("Seeding error...");

}

}

main();

Section 1.2: Troubleshooting Common Issues

If you encounter issues while running your seed script, don't worry! One solution is to use Bun, which has built-in support for TypeScript. However, as Bun is still experimental for Windows users, I suggest using the tsx package, which allows you to execute TypeScript files without additional configuration.

npm i tsx

tsx ./scripts/seed.ts

This should execute your seed script correctly, and you can verify that the data has been seeded in Drizzle Studio.

The first video provides a comprehensive tutorial on using Next.js, Drizzle ORM, PostgreSQL, and Vercel, which will enhance your understanding of the tools involved.

Chapter 2: Final Thoughts

If you found this guide helpful and want to join a growing community of like-minded developers, consider following our journey. Your insights and feedback are always appreciated, so feel free to share your thoughts!

In Plain English 🚀

Thank you for being part of the In Plain English community! Don't forget to follow us on various platforms for more valuable content.

The second video walks you through building a Next.js project and deploying it using Vercel, Neon, Drizzle, TailwindCSS, and FlowBite, which can greatly expand your web development skills.

Share the page:

Twitter Facebook Reddit LinkIn

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

Recent Post:

Navigating the Native vs Cross-Platform App Development Debate

A comparative analysis of native and cross-platform app development, exploring their unique advantages and challenges.

# Embrace Life's Journey: Shift Into High Gear for Growth

Discover how to enhance your life journey by shifting into a higher gear with actionable steps for personal growth and motivation.

Reimagining Healthcare: The Role of AI in Medicine's Future

Explore how AI is reshaping healthcare, from diagnostics to treatment, and the public's perception of its role in medicine.