Creating a Modern REST API Using Prisma, TypeScript, Express and Docker - Part 1

Creating a Modern REST API Using Prisma, TypeScript, Express and Docker - Part 1

The what and why

·

3 min read

Motivation

I tend to always come up with ideas for new projects, things to study, or work to do. With every task I come across, I always ask myself this question - "What is the motivation?". In this multi-part blog series, I will not only answer this question but I will also outline how I developed a REST API using Prisma, Express, Typescript and Docker and how you can build this project from scratch.

This series acts as a guide and also an outline for how to build a modern REST API using this tech stack in a way I find useful - like a conversation. While it is easy to create a bullet list for my readers of well-described steps to building this project, I find documentation and blog posts like this lack a ton of context and further thoughts and concerns that come with every step of your development journey. I believe explaining, showing and telling are a lot more effective than giving a to-do list of tasks to take.

So why this stack? As I have seen Prisma grow, I have been very intrigued by building a REST API using this technology and taking the next step in my development journey. While REST APIs are still prevalent, it seems the tools, environments and technologies for developers to create these APIs continues to grow. I decided I wanted to learn Prisma as my main driver, leverage Docker for my deployment and code maintainability needs and continue the new trend in Type safe REST APIs using TypeScript.

This is what I made.

Tech Stack

  • Prisma - Type safe ORM for SQL-based databases and MongoDB

  • Postgres - A modern SQL database

  • Express - Micro web framework for building web servers and applications

  • Typescript - Typed superset of Javascript

  • TS-Jest & Supertest - Testing library for JS/TS projects coupled with Supertest for API tests

  • Zod - Type safe input Schema validator

  • Docker - Tool for packaging software into small units called containers for easy deployment and code maintainability

There are other tools, libraries and functionality in the codebase that is not listed in the list above. Given they are supporting libraries and functionality, they do not find themselves in this list as they are not fundamental to the stack as a whole.

Application

I wanted to take the Prisma CRUD example in their docs and augment it to work with Postgres deployed on Docker, a Typescript codebase, MVC architecture and very importantly - authentication and authorization. While their docs show how to use the Prisma Client and make calls to the underlying database, there is no step-by-step guide in the base docs for creating a REST API using the technologies I listed above. Because of this, I developed this project.

The API I created allows CRUD operations for Users and Posts for a basic social media CRUD app that allows users to create posts. Posts can be related to a user and users can perform CRUD operations on both posts and themselves. I have also developed APIs for authentication and authorization while having request payload validation with Zod.

While this is a very contrived example, it is a very good way to learn Prisma and CRUD and is also a perfect starter project users can build off of. All applications need users and authentication which makes this application useful in many ways.

While part 1 is explaining the what and why of this project, parts 2 and forward will focus on how to build this. If you are not interested in how to build the project, you can go to my Github and follow the directions in the README for how to clone, initialize and run the project. This is a template repo which means it is a starter repo for users to get going with this stack and then expand upon - like CRA for React apps.