I once shared that I have migrated my database from Active Record using ruby on rails to a Prisma ORM and Node Js server powered by NextJS. This tutorial will create a simple API for Tesla vehicles to help you get started. Prisma is a powerful database toolkit that simplifies working with databases by providing a modern, type-safe query builder.
Table of Contents
Prerequisites
Before starting, ensure you have the following:
- Node.js installed (Version 14 or higher)
- A PostgreSQL database (you can create a free database with Vercel or Neon DB)
Step by Step Guide
Step 1: Setting Up Your Node.js Project
The first step is to initialize a new NodeJS project in the terminal. This project will be the foundation for the REST API you will build in this tutorial. To do this, you must first create a folder and a typescript project.
After creating a new NodeJS project, we need to install other prerequisites such as typescript and Prisma CLI.
Lastly, we need to add a tsconfig.json file to make sure Typescript works correctly. You can create the file manually or use nano tsconfig.json and add the following code.
Step 2 — Setting Up Prisma with PostgreSQL
The second step is to initialize Prisma and create our vehicle model to map with the database. We first need to add our database URL to the .env file so that Prisma knows how to connect with the database.
Now update the .env file to add your database url from Vercel, NeonDB, or another database provider. If you are using Ubuntu, you can also run PostgreSQL locally using pgadmin. If you are on a pooled database you also need a NON-POOLING url to be used for seeding.
Make sure to change the database credentials to the ones you specified in your hosted database or localhost database. Once you’re done, save and exit the file and initialize Prisma by running. npx prisma init.
Step 3 — Defining Your Data Model and Creating Database Tables
Now that your project is initialized, it's time to define your data model. Navigate to the prisma/schema.prisma file in your project directory. Here, you'll define your database schema using Prisma's intuitive schema language.
We are defining just one model for the Vehicle with fields for model, price, picture, and year, but you can add a user model if you like. Save the changes and close the file.
With these models in place, you can now create the corresponding tables in the database using Prisma Migrate. In your terminal, run the following command:
This command generates a new migration file based on your data model changes. The SQL migration file in the prisma/migrations/20241209084626_init/migration.sql directory has the following statements that were executed against the database.
Step 5: Generating a Prisma client
It is often a good practice to create a global Prisma client to prevent recreating a new instance every time. To do this, we initialize Prisma Client using npx prisma generate and create a new folder where we declare a Prisma client and export the client for use in other files.
Now create a file named prisma.ts under the Prisma folder and add the following code:
Step 6: Seeding the Database
Prisma allows us to seed the database if we have some seed data. To seed your database, you need to add a seed.ts file and add our seed data. In a larger project, you might need to edit the package.json to add a seed script.
Next, run the seed script in the terminal by running the command:
Step 7: Create CRUD Operations
The next step is to initiate a ExpressJs server to interact with the database and perform CRUD operations.
First, create a index.ts file to add our server code.
In the src/index.ts file, add the following code to create the API routes.
Test the server by running the following command
You can modify the routes to add try catch blocks for error handling. You can also edit the package.json file to make it easier to run the server by adding a run script. Update the package.json file to add the following code.
Now you can start the development server using npm as follows:
Conclusion
In this article, you created a REST API server with several different routes to create, read, update, and delete user and post data for a sample blogging application. Inside the API routes, you use the Prisma Client to send the respective queries to your database.
As next steps, you can implement additional API routes or extend your database schema using Prisma Migrate. Visit the Prisma documentation to learn about different aspects of Prisma and explore some ready-to-run example projects using tools such as GraphQL or grPC APIs in the prisma-examples repository.
