rsnext/examples/with-mysql
Steven 4466ba436b
chore(examples): use default prettier for examples/templates (#60530)
## Description
This PR ensures that the default prettier config is used for examples
and templates.

This config is compatible with `prettier@3` as well (upgrading prettier
is bigger change that can be a future PR).

## Changes
- Updated `.prettierrc.json` in root with `"trailingComma": "es5"` (will
be needed upgrading to prettier@3)
- Added `examples/.prettierrc.json` with default config (this will
change every example)
- Added `packages/create-next-app/templates/.prettierrc.json` with
default config (this will change every template)

## Related

- Fixes #54402
- Closes #54409
2024-01-11 16:01:44 -07:00
..
components chore(examples): use default prettier for examples/templates (#60530) 2024-01-11 16:01:44 -07:00
lib chore(examples): use default prettier for examples/templates (#60530) 2024-01-11 16:01:44 -07:00
pages chore(examples): use default prettier for examples/templates (#60530) 2024-01-11 16:01:44 -07:00
prisma chore(examples): use default prettier for examples/templates (#60530) 2024-01-11 16:01:44 -07:00
public Some minor adjustments to MySQL example. (#34792) 2022-02-24 20:47:58 -08:00
styles Update MySQL example. (#34784) 2022-02-25 03:41:41 +00:00
.env.example Update MySQL example. (#34784) 2022-02-25 03:41:41 +00:00
.gitignore Add .yarn/install-state.gz to .gitignore (#56637) 2023-10-18 16:34:48 +00:00
next.config.js chore(examples): use default prettier for examples/templates (#60530) 2024-01-11 16:01:44 -07:00
package.json BREAKING CHANGE: Remove React 17 (#41629) 2022-10-21 22:20:36 +00:00
postcss.config.js chore(examples): use default prettier for examples/templates (#60530) 2024-01-11 16:01:44 -07:00
README.md update example Deploy button URLs (#48842) 2023-04-26 13:31:44 -04:00
tailwind.config.js chore(examples): use default prettier for examples/templates (#60530) 2024-01-11 16:01:44 -07:00

Next.js + MySQL

This is a Next.js project that uses Prisma to connect to a PlanetScale MySQL database and Tailwind CSS for styling.

Demo

https://next-mysql.vercel.app

Prerequisites

pscale auth login

Set up the database

Create a new database with the following command:

pscale database create <DATABASE_NAME>

A branch, main, was automatically created when you created your database, so you can use that for BRANCH_NAME in the steps below.

Set up the starter Next.js app

Execute create-next-app with npm, Yarn, or pnpm to bootstrap the example:

npx create-next-app --example with-mysql nextjs-mysql
# or
yarn create next-app --example with-mysql nextjs-mysql
# or
pnpm create next-app --example with-mysql nextjs-mysql

Next, you'll need to create a database username and password through the CLI to connect to your application. If you'd prefer to use the dashboard for this step, you can find those instructions in the Connection Strings documentation and then come back here to finish setup.

First, create your .env file by renaming the .env.example file to .env:

mv .env.example .env

Next, using the PlanetScale CLI, create a new username and password for the branch of your database:

pscale password create <DATABASE_NAME> <BRANCH_NAME> <PASSWORD_NAME>

The PASSWORD_NAME value represents the name of the username and password being generated. You can have multiple credentials for a branch, so this gives you a way to categorize them. To manage your passwords in the dashboard, go to your database overview page, click "Settings", and then click "Passwords".

Take note of the values returned to you, as you won't be able to see this password again.

Password production-password was successfully created.
Please save the values below as they will not be shown again

  NAME                  USERNAME       ACCESS HOST URL                     ROLE               PLAIN TEXT
 --------------------- -------------- ----------------------------------- ------------------ -------------------------------------------------------
  production-password   xxxxxxxxxxxxx   xxxxxx.us-east-2.psdb.cloud   Can Read & Write   pscale_pw_xxxxxxx

You'll use these properties to construct your connection string, which will be the value for DATABASE_URL in your .env file. Update the DATABASE_URL property with your connection string in the following format:

mysql://<USERNAME>:<PLAIN_TEXT_PASSWORD>@<ACCESS_HOST_URL>/<DATABASE_NAME>?sslaccept=strict

Push the database schema to your PlanetScale database using Prisma.

npx prisma db push

Run the seed script to populate your database with Product and Category data.

npm run seed

Run the App

Run the app with following command:

npm run dev

Open your browser at localhost:3000 to see the running application.

Deploy your own

After you've got your application running locally, it's time to deploy it. To do so, you'll need to promote your database branch (main by default) to be the production branch (read the branching documentation for more information).

pscale branch promote <DATABASE_NAME> <BRANCH_NAME>

Now that your branch has been promoted to production, you can either use the existing password you generated earlier for running locally or create a new password. Regardless, you'll need a password in the deployment steps below.

Deploy the example using Vercel:

Deploy with Vercel

Make sure to update the DATABASE_URL variable during this setup process.