Giter Site home page Giter Site logo

planetscale / nextjs-conf-2021 Goto Github PK

View Code? Open in Web Editor NEW
34.0 4.0 9.0 8.7 MB

The code from "Databases as Code with PlanetScale and Prisma" talk at Next.js Conf 2021

Home Page: https://youtu.be/5JpKZfPx-1k

JavaScript 62.01% CSS 37.99%
nextjs planetscale prisma databases

nextjs-conf-2021's Introduction

Using Next.js with PlanetScale and Prisma

Important update: This talk contains an outdated workflow of using prisma migrate dev with a shadow branch to update your database schema. We now recommend using prisma db push with no shadow branches. See the documentation on how to do migrations to your schema here:

This repo corresponds to the "Databases as Code with PlanetScale and Prisma" talk from Next.js Conf 2021.

This is a Next.js project bootstrapped with create-next-app.

In the demo from the talk, we created and edited an API route, http://localhost:3000/api/stars. This endpoint can be edited in pages/api/stars.js. The pages/api directory is mapped to /api/*. Files in this directory are treated as API routes instead of React pages.

Set up

Below are the steps I went through in the talk during Next.js Conf. You will likely find the talk to include a few more details around each step. I have created the following steps to give a brief idea for what is needed to go to production.

Prerequisite: You need to have the Prisma and PlanetScale CLIs installed

  1. In PlanetScale, create a star-app database
  2. In your database's Settings page, check "Automatically copy migration data" and select "Prisma"
  3. Create an initial-setup and shadow database branches from main branch

Note: This talk contains an outdated workflow of using prisma migrate dev with a shadow branch to update your database schema. We now recommend using prisma db push with no shadow branches. See the documentation on how to do migrations to your schema here:

  1. Locally, run:
npx create-next-app@latest --use-npm
  1. Once this is complete, run:
npm install @prisma\client
  1. To create the files needed to use Prisma, run:
npx prisma init
  1. Edit the prisma/schema.prisma file:
datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
  shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
  referentialIntegrity = "prisma"
}

generator client {
  provider = "prisma-client-js"
  previewFeatures = ["referentialIntegrity"]
}

model Star {
  id            Int       @default(autoincrement()) @id
  createdAt     DateTime  @default(now())
  updatedAt     DateTime  @updatedAt
  name          String    @db.VarChar(255)
  consellation  String    @db.VarChar(255)
}
  1. Edit the .env file:
DATABASE_URL="mysql://[email protected]:3309/star-app"
SHADOW_DATABASE_URL="mysql://[email protected]:3310/star-app"
  1. Next, we will use pscale CLI to locally proxy into our PlanetScale database. In a two different terminal tabs, run:
pscale connect star-app initial-setup --port 3309
pscale connect star-app shadow --port 3310
  1. In a different terminal, run the following to create the initial data model and do your first Prisma migrate:
prisma migrate dev --name init

You will notice a prisma/migrations folder as well as the schema in your initial-setup branch in PlanetScale. 11. Create lib/prisma.js file:

import { PrismaClient } from '@prisma/client'

const prisma = global.prisma || new PrismaClient();

if (process.env.NODE_ENV === "development") global.prisma = prisma;

export default prisma
  1. Create pages/api/stars.js file:
import prisma from "../../lib/prisma";

export default async function assetHandler(req, res) {
    const { method } = req;

    switch (method) {
        case "GET":
            try {
                const stars = await prisma.star.findMany();
                res.status(200).json(stars);
            } catch(e) {
                console.error("Request error", e);
                res.status(500).json({ error: "Error fetching posts" });
            }
            break;
        default:
            res.setHeader("Allow", ["GET"]);
            res.status(405).end(`Method ${method} Not Allowed`);
            break;
    }
}
  1. Add data using npx prisma studio, which will open a browser window. After you add data, then close Prisma Studio. You can also check to see that the data is in your intial-setup branch in PlanetScale.
  2. Run: npm run dev Navigate to http://localhost:3000/api/stars to see data returned from your API endpoint!
  3. You are now ready to open a deploy request in PlanetScale. Once you have opened a deploy request and merged it into your main branch, you are now ready to deploy to Vercel! Remember to add data again to your main database branch; otherwise, your API route will return no data.

Deploy on Vercel

Warning: You need to make sure to follow the steps from the "Databases as Code with PlanetScale and Prisma" talk at Next.js Conf before deploying to Vercel.

Deploy this application quickly to Vercel using the following Deploy button:

Deploy with Vercel

In order to connect your database to your application deployed in Vercel, you will need to set the DATABASE_URL variable. This URL is how your Vercel application is able to securely connect to PlanetScale. The URL is in the following format: mysql://[username]:[password]@[host]/star-app?sslaccept=strict

You can create a password in PlanetScale and generate this URL in the Connect modal in the database branch of your choice, select "Prisma" to get the Prisma specific URL:

Connect modal in PlanetScale app showing passwords

Note: If you see a 404 page after your build is successful, go into your app's Vercel Settings > General > Build & Development Settings and make sure that Next.js is the selected framework.

nextjs-conf-2021's People

Contributors

ayrton avatar tbarn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nextjs-conf-2021's Issues

Connection Pools and connection limits

Hi, I'm interested to move to PlanetScale for production and move away from hosted relational databases, my only question regarding this migration is connection limits. and how on serverless environment like Nextjs and Vercel this issue is addressed from planetScale ? i couldn't find any documentation on this topic.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.