In this blog, I write about Web Development, Blockchain, and DevOps. I am a Docker Captain, Public Speaker, and Developer Advocate at daily.dev. Check my YouTube channel.
By the end of this article, you will understand how to build a FULL CRUD API app, including a simple frontend to use it, using the following technologies:
Next.js
TypeScript
Tailwind CSS
tRPC
Prisma
Postgres
Docker
They are MANY technologies, but we'll keep the example as basic as possible to make it understandable. We will also use Zod, a validation library.
If you prefer a video version:
All the code is available for free on GitHub (link in video description).
Let's get started.
🏁 Intro
Here is a schema of the architecture of the application we are going to create:
We will create five endpoints for basic CRUD operations:
Create Read all Read one Update Delete
We will create the application using Create T3 App, and we will connect this application to a Postgres instance running in a docker container.
👣 Steps
We will go with a step-by-step guide so that you can follow along.
Here are the steps:
Prerequisites
Project creation and dependency installation
Run the Postgres database with Docker
Configure Prisma and the database schema
Write the tRPC procedures
Configure the handlers in the index.tsx file
write the simple frontend App using Tailwind
💡 Prerequisites
Requirements:
Node.js
Docker
Any editor (I'll use VS Code)
You can check Node and Docker versions by typing:
node --version
docker --version
I suggest using the latest versions.
🚀 Create a new project using Create T3 App
To create our app, we will use "Create T3 App". It's a CLI tool to create a new project with all the necessary technologies.
npm create t3-app@latest
Select all except nextauth (use the spacebar to select/deselect)
This usually takes a couple of minutes.
Then step into the directory:
cd my-t3-app
Now open the project using any IDE. If you use VS Code, you can type:
This file will be added to the .gitignore and not be pushed to the public repository, so if you cloned the project, you would have to create this file.
Now open and edit the prisma/schema.prisma file replacing it with the following:
// 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"
previewFeatures = ["jsonProtocol"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
//User with an id, name and email as strings
model User {
id String @id @default(uuid())
name String
email String
}
We replace the provider with postgresql and the model with a User with:
an id
a name
an email
All fields are strings.
Now, to update the schema in the DB, you can type:
npx prisma migrate dev --name init
And to test if everything is working correctly, we can use Prisma Studio, a tool that comes with Prisma. Type:
This project is intended as an example so you can use it as a starting point for your projects. If you want to contribute, feel free to open a PR on GitHub (link in the video description).
__
🏁 Conclusion
We built a CRUD API using the following technologies:
Next.js
TypeScript
Tailwind CSS
tRPC
Prisma
Postgres
Docker
If you prefer a video version:
All the code is available for free on GitHub (link in video description).