๐Ÿ˜œ

์ญˆ๋‚˜์•„๋น  ๋ธ”๋กœ๊ทธ

JUNA
STUDIO

How to use NextJS + Prisma + PlanetScale

๋ฐœํ–‰์ผ: Feb, 2025
์กฐํšŒ์ˆ˜: 14
๋‹จ์–ด์ˆ˜: 297

Table of Contents

PlanetScale Account Creation

First, go to the PlanetScale (https://planetscale.com/) website and complete the account creation process.


PlanetScale CLI Installation

Next, to install the PlanetScale CLI, open a PowerShell terminal window and enter the following command to install scoop. (Reference: https://scoop.sh/)

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser # Optional: Needed to run a remote script the first time
irm get.scoop.sh | iex

After installing scoop, install pscale. (Reference: https://github.com/planetscale/cli#installation)

scoop bucket add pscale https://github.com/planetscale/scoop-bucket.git
scoop install pscale mysql

NextJS Project Setup

Now that the PlanetScale CLI is installed, create a new NextJS project to test it.

npx create-next-app@latest

Enter the project folder and install the prisma package.

npm i prisma -D
npm i @prisma/client

Enter the prisma initialization command.

npx prisma init

After running the initialization command, the schema.prisma and .env files will be created.

Schema and .env files

Prisma Setup

Install the vscode extension to conveniently edit the schema.

VSCode extension

Define the schema of the table to be used in the schema.prisma file as shown below. For detailed instructions on how to define the prisma schema, please refer to https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#model-field-scalar-types.

Change the db provider in the default schema from postgresql to mysql.

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model Test {
  id   Int    @id @default(autoincrement())
  name String
}

PlanetScale Database Connection

To connect to PlanetScale, enter the following command to log in to the PlanetScale site.

pscale auth login

After logging in, check the login completion message in the terminal window as shown below.

Login success message

Now, to connect to the database created in PlanetScale, enter the following command. (For example, if the DB name is mytestdb)

pscale connect mytestdb
DB connection command

Change the DATABASE_URL in the .env file as follows.

DATABASE_URL="mysql://127.0.0.1:3306/mytestdb"

Enter the following command to create the table model defined in schema.prisma in the DB.

npx prisma db push
DB push command

Once the push is successfully completed, you can check that a new table has been created on the PlanetScale website.

New table created

Click on Tables on the screen above to check the created table schema.

Table schema

Once you have completed the above steps, you can now read and write to the DB created in PlanetScale from NextJS.


Tags: #PlanetScale#NextJS#Prisma#Installation Guide#Database Connection

JavaScript/TypeScript > ์ด ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ํฌ์ŠคํŒ…

JUNA BLOG VISITORS
Today
7
 (
updown
-7
)
Total
657
 (
updown
+7
)

ยฉ 2025 juniyunapapa@gmail.com.