Deploy a Nest application with Prisma database on Zeit Now for FREE

The Hangry Coder
3 min readNov 17, 2019

--

Deploy a Nest application with Prisma database to Zeit Now

This story is part of my TODO challenge: creating a TODO app with different frontend frameworks. But before I want to start on the frontend part, I wanted to have a working backend as well. So this story is about the backend part:

  • Backend language/framework
  • Database
  • Hosting

NestJS

I’ve choosen NestJS as my backend. It’s a strong typed NodeJS framework using TypeScript and they modeled it after Angular. As an Angular developer myself, I feel right at home with NestJS.

Prisma.io

My TODO application also needs a database. I’ve choosen Prisma.io for this. On the surface, Prisma looks like your typical ORM database. But the main advantages are

  • GraphQL ready
  • Free hosted database(s)

And the free hosted database is what I needed. It’s included in the free tier of Prisma Cloud.

Zeit Now

Now I stil need to host my backend. This is where Zeit Now comes in. Zeit is an all-in-one solution for static & JAMstack deployment for performance-obsessed teams. The main advantages of Zeit Now:

  • free deployment (no creditcard required)
  • automatic ssl ready
  • alias your custom domain(s) to the Zeit instance(s)
  • support for serverless functions (see their list of supported languages)

So Zeit supports NodeJS, and that is what I need because NestJS is NodeJS based.

Setting up NestJS and deploy to Zeit

First install NestJS and start a new application

Install NestJS and generate a new project

This will give us a standard NestJS project with a default controller. Next up is installing Zeit Now and deploy.

Install Now package

Now comes the “tricky” part: deploying. First navigate to your new NestJS application root folder. In order to deploy to Now you can just use

Deploying to Zeit Now

Just that simple. The Now command will look for a configuration file : now.json. If there is no configuration file, it will create an instance on Zeit Now and will use the foldername from where Now was called as instance name.

Before you will try this, I must tell you, this will fail for NestJS applications.

In order to make it work for NestJS you need to

  • create and configure now.json in the project root folder
  • build the NestJS application for production
  • deploy the build version to Now

Configure now.json

now.json

Build the NestJS application and deploy to Now

Now we can build the application and deploy to Now

build the NestJS application and deploy to Now

If everything went well, Now will give you the url of your endpoint, usally in the format of <application name>.<zeit username>.now.sh.

Creating and deploying the Prisma database

Navigate to your project root and let’s install the packages for Prisma

Installing the required packages for Prisma.io

Database model

Before we can use a database, we must create a database model. Create a file named database.prisma (you can use any filename ofcourse) and add the model

Test model

Configure Prisma

Next we can configure Prisma. We need to tell to wich endpoint the model should be deployed (and can be used). Create a file named prisma.yml

prisma.yml

As you can see we still need to define the endpoint:

  • <region> : this can be us1 or eu1
  • <prisma username> : login into your prisma dashboard and look for your username in the url : https://app.prisma.io/<username>/
  • <database name>: any database name you want to use
  • <stage>: this could be prod or dev

So for example: https://eu1.prisma.sh/foobar-723f27/testdatabase/dev

Deploying the Prisma databasemodel en generate the .ts files

Next we are ready to deploy the model to Prisma.io and generate the .ts files.

We need the generated .ts files in our NestJS application.

deploy the databasemodel and generate the .ts files

So after the prisma generate, we can import the Prisma client lib

TODO backend

You can checkout my backend I use for the TODO challenge:

Extra information

NestJS docs
Zeit docs
Prisma data model
Prisma client

That’s it for now.

You can find me on :
- linkedin : https://www.linkedin.com/in/fransjoleihitu/
- GitHub : https://github.com/fransyozef/
- Instagram : https://www.instagram.com/thehangrycoder/

--

--