<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ömer Berkan Çalık</title>
    <description>The latest articles on DEV Community by Ömer Berkan Çalık (@omercalik).</description>
    <link>https://dev.to/omercalik</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F716653%2F37ff1ec4-a2cf-4126-adaa-b4dbbf83bad7.jpeg</url>
      <title>DEV Community: Ömer Berkan Çalık</title>
      <link>https://dev.to/omercalik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/omercalik"/>
    <language>en</language>
    <item>
      <title>Connecting Heroku Postgres with NestJS Backend</title>
      <dc:creator>Ömer Berkan Çalık</dc:creator>
      <pubDate>Fri, 01 Oct 2021 19:39:23 +0000</pubDate>
      <link>https://dev.to/omercalik/connecting-heroku-postgres-with-nestjs-backend-3p1j</link>
      <guid>https://dev.to/omercalik/connecting-heroku-postgres-with-nestjs-backend-3p1j</guid>
      <description>&lt;p&gt;Hi all! 👋&lt;/p&gt;

&lt;p&gt;This is my first article on here. In this article I'll show you how you can connect a postgres database to your backend api, and how to deploy it on heroku. This is not a tutorial for NestJS so i assume that you know at least the basic.&lt;/p&gt;

&lt;p&gt;If you found this article helpful make sure you follow me on &lt;a href="https://twitter.com/omerberkancalik" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;. I am planning to share resources and tips on my twitter.&lt;/p&gt;

&lt;p&gt;First we need to install Nestjs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -g @nestjs/cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then go to the directory which you want to create the project and type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nest new project-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the project in your favorite code editor&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqlr9y9o2fb7gagnq1mfc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqlr9y9o2fb7gagnq1mfc.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Type in below lines to terminal to generate modules,collections and services.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nest generate module users
nest generate controller users
nest generate service users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that nest will create the necessary files for us.&lt;/p&gt;

&lt;p&gt;Go to Heroku and create a new app.After you created the app go to overview tab and click configure add-ons.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkjpnr1n2wzg56g7480q9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkjpnr1n2wzg56g7480q9.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Search for Heroku Postgres and select it.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff0u6jhpgwjeh276cmd72.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff0u6jhpgwjeh276cmd72.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After you add the database click on it. You will be redirected to database dashboard. Go to setting tab and copy the URI.&lt;/p&gt;

&lt;p&gt;Create a .env file in the root directory and type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASE_URL=your_database_url
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then install nestjs config dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i --save @nestjs/config
npm install --save @nestjs/typeorm typeorm pg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then go to your app.module.ts file and add the following lines. Your app.module.ts file should look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ConfigModule } from '@nestjs/config';

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [
    UsersModule,
    ConfigModule.forRoot(),
    TypeOrmModule.forRoot({
      url: process.env.DATABASE_URL,
      type: 'postgres',
      ssl: {
        rejectUnauthorized: false,
      },
      entities: ['dist/**/*.entity{.ts,.js}'],
      synchronize: true, // This for development
      autoLoadEntities: true,
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to create a User entity and inject it to our service.&lt;br&gt;
Create a file called "user.entity.ts" in the users folder.&lt;br&gt;
It should look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity('users')
export class User{
  @PrimaryGeneratedColumn()
  id?: number;

  @Column()
  username: string;

  @Column()
  password: string;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now go to  "users.module.ts" file and add the following line to your "@Module" decorator so it should look like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { User } from './user.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';

@Module({
  imports: [TypeOrmModule.forFeature([User])],
  controllers: [UsersController],
  providers: [UsersService],
})
export class UsersModule {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then go to "users.service.ts" file and edit it like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { User } from './user.entity';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

@Injectable()
export class UsersService {
  constructor(
    @InjectRepository(User)
    private usersRepository: Repository&amp;lt;User&amp;gt;,
  ) {}

  getUsers(): Promise&amp;lt;User[]&amp;gt; {
    return this.usersRepository.find();
  }

  addUser(user): Promise&amp;lt;User&amp;gt; {
    this.usersRepository.insert(user);
    return user;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally go to "users.controller.ts" file and paste the below code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { UsersService } from './users.service';
import { Body, Controller, Get, Post } from '@nestjs/common';

type User = {
  id?: number;
  username: string;
  password: string;
};

@Controller('users')
export class UsersController {
  constructor(private readonly userService: UsersService) {}

  @Get()
  getUsers() {
    return this.userService.getUsers();
  }

  @Post()
  addUser(@Body() user: User) {
    return this.userService.addUser(user);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We created our really simple API. Now we need to make some changes for deployment.&lt;/p&gt;

&lt;p&gt;First create a "Procfile" without extensions in the root folder. And paste the below code inside it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web: npm run start:prod
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vjupl3mmv4kxf4m27kn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vjupl3mmv4kxf4m27kn.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And finally go to your "main.ts" file inside the src folder and edit it like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const PORT = process.env.PORT || 5000;
  await app.listen(PORT);
}
bootstrap();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are ready to deploy our api on Heroku! First go ahead and create a github repository and push your code to it(Dont forget to add your ".env" file to gitignore). Then go to your already created app and click deploy tab. Click GitHub and search for your repository and select it. Enable Automatic Deploys if you want it to deploy your code whenever you push to your main branch. And finally click deploy below.&lt;/p&gt;

&lt;p&gt;Now lets test our api on Insomnia.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F29388hwd28cu8ugz7il7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F29388hwd28cu8ugz7il7.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
(URL should be your own app's url!)&lt;/p&gt;

&lt;p&gt;Now that we add the user. Let's see if we can get it from database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fju8ijt4f2vjgj0gu7i0f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fju8ijt4f2vjgj0gu7i0f.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see we successfully get our user. After that you can try to implement deleting or updating users.&lt;/p&gt;

&lt;p&gt;Thanks for the reading. I hope you found it useful. If you have any questions feel free to ask. Bye 👋&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>postgres</category>
      <category>heroku</category>
      <category>nestjs</category>
    </item>
  </channel>
</rss>
