Fullstack Blog with Next.js, Typescript and Prisma
With Railway Integration
Without the Railway integration
This is a starter that shows how to implement a fullstack app in TypeScript with Next.js with the following stack:
Before you deploy the application to Vercel, ensure you
Note that the app uses a mix of server-side rendering with
getServerSideProps
getStaticProps
Clone this repository:
git clone git@github.com:prisma/prisma-nextjs-blog.git
Install npm dependencies:
cd prisma-nextjs-blognpm install
If you're using Docker on your computer, the following script to set up PostgreSQL database using the
docker-compose.yml
npm run db:up
Run the following command to create your PostgreSQL database. This also creates the
User
Post
Account
Session
VerificationToken
prisma/schema.prisma
npx prisma migrate dev --name init
When
npx prisma migrate dev
prisma/seed.ts
In order to get this example to work, you need to configure the GitHub authentication provider from NextAuth.js.
Configuring the GitHub authentication providerFirst, log into your GitHub account.
Then, navigate to Settings, then open to Developer Settings, then switch to OAuth Apps.
Clicking on the Register a new application button will redirect you to a registration form to fill out some information for your app. The Authorization callback URL should be the Next.js
/api/auth
An important thing to note here is that the Authorization callback URL field only supports a single URL, unlike e.g. Auth0, which allows you to add additional callback URLs separated with a comma. This means if you want to deploy your app later with a production URL, you will need to set up a new GitHub OAuth app.
Click on the Register application button, and then you will be able to find your newly generated Client ID and Client Secret. Copy and paste this info into the
The resulting section in the
.env
# GitHub oAuthGITHUB_ID=6bafeb321963449bdf51GITHUB_SECRET=509298c32faa283f28679ad6de6f86b2472e1bff
npm run dev
The app is now running, navigate to
Evolving the application typically requires three steps:
For the following example scenario, assume you want to add a "profile" feature to the app where users can create a profile and write a short bio about themselves.
The first step is to add a new table, e.g. called
Profile
// schema.prismamodel Post {id Int @default(autoincrement()) @idtitle Stringcontent String?published Boolean @default(false)author User? @relation(fields: [authorId], references: [id])authorId Int}model User {id Int @default(autoincrement()) @idname String?email String @uniqueposts Post[]+ profile Profile?}+model Profile {+ id Int @default(autoincrement()) @id+ bio String?+ userId Int @unique+ user User @relation(fields: [userId], references: [id])+}
Once you've updated your data model, you can execute the changes against your database with the following command:
npx prisma migrate dev
You can now use your
PrismaClient
Profile
Create a new user with a new profile
const profile = await prisma.profile.create({data: {bio: "Hello World",user: {connect: { email: "alice@prisma.io" },},},});
Update the profile of an existing user
const user = await prisma.user.create({data: {email: "john@prisma.io",name: "John",profile: {create: {bio: "Hello World",},},},});
const userWithUpdatedProfile = await prisma.user.update({where: { email: "alice@prisma.io" },data: {profile: {update: {bio: "Hello Friends",},},},});
Once you have added a new endpoint to the API (e.g.
/api/profile
/POST
/PUT
GET
profile.tsx
pages
In the application code, you can access the new endpoint via
fetch
If you want to try this example with another database than SQLite, you can adjust the the database connection in
datasource
Learn more about the different connection configurations in the docs.
For PostgreSQL, the connection URL has the following structure:
datasource db {provider = "postgresql"url = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA"}
Here is an example connection string with a local PostgreSQL database:
datasource db {provider = "postgresql"url = "postgresql://janedoe:mypassword@localhost:5432/notesapi?schema=public"}
For MySQL, the connection URL has the following structure:
datasource db {provider = "mysql"url = "mysql://USER:PASSWORD@HOST:PORT/DATABASE"}
Here is an example connection string with a local MySQL database:
datasource db {provider = "mysql"url = "mysql://janedoe:mypassword@localhost:3306/notesapi"}
Here is an example connection string with a local Microsoft SQL Server database:
datasource db {provider = "sqlserver"url = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;"}
Here is an example connection string with a local MongoDB database:
datasource db {provider = "mongodb"url = "mongodb://USERNAME:PASSWORD@HOST/DATABASE?authSource=admin&retryWrites=true&w=majority"}
Because MongoDB is currently in Preview, you need to specify the
previewFeatures
generator
generator client {provider = "prisma-client-js"previewFeatures = ["mongodb"]}
Fullstack Blog with Next.js, Typescript and Prisma
With Railway Integration
Without the Railway integration
This is a starter that shows how to implement a fullstack app in TypeScript with Next.js with the following stack:
Before you deploy the application to Vercel, ensure you
Note that the app uses a mix of server-side rendering with
getServerSideProps
getStaticProps
Clone this repository:
git clone git@github.com:prisma/prisma-nextjs-blog.git
Install npm dependencies:
cd prisma-nextjs-blognpm install
If you're using Docker on your computer, the following script to set up PostgreSQL database using the
docker-compose.yml
npm run db:up
Run the following command to create your PostgreSQL database. This also creates the
User
Post
Account
Session
VerificationToken
prisma/schema.prisma
npx prisma migrate dev --name init
When
npx prisma migrate dev
prisma/seed.ts
In order to get this example to work, you need to configure the GitHub authentication provider from NextAuth.js.
Configuring the GitHub authentication providerFirst, log into your GitHub account.
Then, navigate to Settings, then open to Developer Settings, then switch to OAuth Apps.
Clicking on the Register a new application button will redirect you to a registration form to fill out some information for your app. The Authorization callback URL should be the Next.js
/api/auth
An important thing to note here is that the Authorization callback URL field only supports a single URL, unlike e.g. Auth0, which allows you to add additional callback URLs separated with a comma. This means if you want to deploy your app later with a production URL, you will need to set up a new GitHub OAuth app.
Click on the Register application button, and then you will be able to find your newly generated Client ID and Client Secret. Copy and paste this info into the
The resulting section in the
.env
# GitHub oAuthGITHUB_ID=6bafeb321963449bdf51GITHUB_SECRET=509298c32faa283f28679ad6de6f86b2472e1bff
npm run dev
The app is now running, navigate to
Evolving the application typically requires three steps:
For the following example scenario, assume you want to add a "profile" feature to the app where users can create a profile and write a short bio about themselves.
The first step is to add a new table, e.g. called
Profile
// schema.prismamodel Post {id Int @default(autoincrement()) @idtitle Stringcontent String?published Boolean @default(false)author User? @relation(fields: [authorId], references: [id])authorId Int}model User {id Int @default(autoincrement()) @idname String?email String @uniqueposts Post[]+ profile Profile?}+model Profile {+ id Int @default(autoincrement()) @id+ bio String?+ userId Int @unique+ user User @relation(fields: [userId], references: [id])+}
Once you've updated your data model, you can execute the changes against your database with the following command:
npx prisma migrate dev
You can now use your
PrismaClient
Profile
Create a new user with a new profile
const profile = await prisma.profile.create({data: {bio: "Hello World",user: {connect: { email: "alice@prisma.io" },},},});
Update the profile of an existing user
const user = await prisma.user.create({data: {email: "john@prisma.io",name: "John",profile: {create: {bio: "Hello World",},},},});
const userWithUpdatedProfile = await prisma.user.update({where: { email: "alice@prisma.io" },data: {profile: {update: {bio: "Hello Friends",},},},});
Once you have added a new endpoint to the API (e.g.
/api/profile
/POST
/PUT
GET
profile.tsx
pages
In the application code, you can access the new endpoint via
fetch
If you want to try this example with another database than SQLite, you can adjust the the database connection in
datasource
Learn more about the different connection configurations in the docs.
For PostgreSQL, the connection URL has the following structure:
datasource db {provider = "postgresql"url = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA"}
Here is an example connection string with a local PostgreSQL database:
datasource db {provider = "postgresql"url = "postgresql://janedoe:mypassword@localhost:5432/notesapi?schema=public"}
For MySQL, the connection URL has the following structure:
datasource db {provider = "mysql"url = "mysql://USER:PASSWORD@HOST:PORT/DATABASE"}
Here is an example connection string with a local MySQL database:
datasource db {provider = "mysql"url = "mysql://janedoe:mypassword@localhost:3306/notesapi"}
Here is an example connection string with a local Microsoft SQL Server database:
datasource db {provider = "sqlserver"url = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;"}
Here is an example connection string with a local MongoDB database:
datasource db {provider = "mongodb"url = "mongodb://USERNAME:PASSWORD@HOST/DATABASE?authSource=admin&retryWrites=true&w=majority"}
Because MongoDB is currently in Preview, you need to specify the
previewFeatures
generator
generator client {provider = "prisma-client-js"previewFeatures = ["mongodb"]}