Today's Hype Scores!

How we used the Nodejs Twilio library, NestJS, and Postgres to build an image sharing microservice: Part 1 of 3

This is a 3-part tutorial where we go over how to build a simple MMS image sharing service using Twilio, NestJS, and Postgres. In this first post of the 3-part tutorial series we go over how to connect your NestJS instance to a Docker container that is running Postgres

Intro

Hey everyone - in this series of posts we're going to show you how we set up a simple MMS image sharing service. This is similar to the service that we use at HypeRider to drive our daily newsletter

For those of you unfamiliar, we at HypeRider run a sentiment driven stock newsletter. We started by listening to Reddit and Twitter and reporting what people were saying about stocks. But since then, our newsletter has grown to support other things like NFTs, Cryptocurrency, and now our new "Meme Corner".

The founding apes and I wanted an easy way that we could take all the memes that we share between each other every day, and bring those to our users. We currently use this service to power our "Meme Corner" but we know that this can be expanded to other types of content as well.

We decided to use Twilio - a messaging API provider - to help us build this and we're going to show you how as well in this 3-part tutorial

Technical Summary

  • In this post we will review how to create a new NestJS service and connect it to a Postgres instance in Docker
  • Also as a reminder - this is a toy project and by no means should this be deployed as is to a "production" environment.
  • In general in this series we may assume you have some basic knowledge of some of the tools mentioned here
  • If you have questions or need more guidance - we still want to help you! Reach out here - bwalsh@hyperider.io if you want more help!

Pre-requisites

For all of the tutorials we will require:

Setting up NestJS and Postgres

NestJS is our framework of choice. Nest can be used as an extension on top of Express. For those of you that are concerned with pure performance, nest can also be used with Fastify but for sake of these tutorials we will be using Express.

We enjoy working with Nest given it's useful CLI tooling and familiar and repeatable patterns that it uses to implement the basics we need.

Install the Nest CLI

We will use the Nest CLI throughout this entire process to help us scaffold our components. You can use npm to install it with the command below:

npm install -g @nestjs/cli

Start a new nest project

If you were successful installing the Nest CLI you can then use it to create a new project

nest new my-twilio-image-service

Start a Docker instance of Postgres and create your DB structure

You can start a docker instance of postgres using the below command you should change the "mysecretpassword" to your preferred password. Remember this as we will use it later. You can also rename your docker container to something other than some-postgres if you prefer.

docker run --name some-postgres -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres

You will then need to modify your database to include a table to store the URLs to your images. How you connect and modify your database is up to you. We use DataGrip as our IDE of choice but you can use any basic SQL to do this. You can then find a basic DDL to use here:

  create table image
  (
  	id serial not null
  		constraint user_pk
  			primary key,
  	url text
  );

  alter table image owner to postgres;

  create unique index image_id_uindex
  	on image (id);

Install MassiveJS and configure NestJS to connect to Postgres

You can then move into the directory of your newly created Nest project - in the case of this tutorial it should be under my-twilio-image-service. Once there, you can run this command in your terminal to install the NestJS-massiveJS package.

npm install @nestjsplus/massive

While Nest has it's own opinions with it's own ORM (which we generally like!) - we chose MassiveJS for this example as it has a good balance of features and simplicity that makes it useful for POCs like this.

Once you've installed massive, you should modify your app.module in your new nest project to look like the code below.

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MassiveModule } from '@nestjsplus/massive';

@Module({
  imports: [
    MassiveModule.register({
      host: '127.0.0.1',
      port: 5432,
      database: 'postgres',
      user: 'postgres',
      password: 'mysecretpassword',
    })
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

And that should give you a new basic NestJS project - that we can then use to connect and write data to Postgres

Next steps: Install Twilio, save image urls, deploy and test

This concludes the first part of this tutorial. The (next part)[https://hyperider.io/blog/2022-07-30-how-we-used-the-nodejs-twilio-library-nestjs-and-postgres-to-build-an-image-sharing-microservice-part-2-of-3/] will go over the actual code needed in Nest to receive messages from Twilio and write them to our database. You can follow this link now to read the next part!

If you've enjoyed it and want to support us - go ahead and subscribe to our free newsletter below! Even if you don't enjoy stocks, there are always more features - both useful and funny - that we are always adding to it.

If you want more info, have some feedback for me, or just want to get in touch you can always email me at bwalsh@hyperider.io