Date:

Api NestJs

Creating an API with NestJS, TypeORM, PostgreSQL, Docker, and MinIO

Overview

In this article, we will create an API using NestJS, TypeORM, PostgreSQL, Docker, and MinIO. We will also define the structure of the data that the API will handle, which includes an array of objects with various properties such as id, name, image, images, description, lien, manuel, and videoT.

Setting up the Project

To start, let’s create a new NestJS project using the following command:

npx @nestjs/cli new my-api

Next, install the required dependencies:

npm install @nestjs/typeorm typeorm pg minio

Configuring TypeORM

Create a new file called typeorm.config.ts in the src folder and add the following code:

import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { join } from 'path';

export const typeOrmConfig: TypeOrmModuleOptions = {
  type: 'postgres',
  url: 'postgres://user:password@localhost:5432/mydb',
  entities: [join(__dirname, '..', '**/**.entity{.ts,.js}')],
  synchronize: true,
};

This configuration sets up TypeORM to connect to a PostgreSQL database using the postgres driver.

Defining the Entity

Create a new file called tracking.entity.ts in the src folder and add the following code:

import { Entity, Column, OneToMany } from 'typeorm';
import { Image } from './image.entity';

@Entity()
export class Tracking {
  @Column('integer')
  id: number;

  @Column('varchar')
  name: string;

  @Column('varchar')
  image: string;

  @OneToMany(() => Image, (image) => image.tracking)
  images: Image[];
}

This entity defines the structure of the data that the API will handle, which includes an array of images.

Defining the Image Entity

Create a new file called image.entity.ts in the src folder and add the following code:

import { Entity, Column, ManyToOne } from 'typeorm';
import { Tracking } from './tracking.entity';

@Entity()
export class Image {
  @Column('varchar')
  image: string;

  @Column('varchar')
  imgDes: string;

  @ManyToOne(() => Tracking, (tracking) => tracking.images)
  tracking: Tracking;
}

This entity defines the structure of the images that will be stored in the database.

Creating the API Endpoints

Create a new file called tracking.controller.ts in the src folder and add the following code:

import { Controller, Get, Post, Body } from '@nestjs/common';
import { TrackingService } from './tracking.service';
import { Tracking } from './tracking.entity';

@Controller('tracking')
export class TrackingController {
  constructor(private readonly trackingService: TrackingService) {}

  @Get()
  async getAll(): Promise<Tracking[]> {
    return await this.trackingService.getAll();
  }

  @Post()
  async create(@Body() tracking: Tracking): Promise<Tracking> {
    return await this.trackingService.create(tracking);
  }
}

This controller defines two endpoints: GET /tracking to retrieve all tracking data, and POST /tracking to create a new tracking data.

Implementing the Tracking Service

Create a new file called tracking.service.ts in the src folder and add the following code:

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Tracking } from './tracking.entity';

@Injectable()
export class TrackingService {
  constructor(
    @InjectRepository(Tracking)
    private readonly trackingRepository: Repository<Tracking>,
  ) {}

  async getAll(): Promise<Tracking[]> {
    return await this.trackingRepository.find();
  }

  async create(tracking: Tracking): Promise<Tracking> {
    return await this.trackingRepository.save(tracking);
  }
}

This service defines two methods: getAll to retrieve all tracking data, and create to create a new tracking data.

Testing the API

To test the API, start the Docker container using the following command:

docker-compose up -d

Then, use a tool like Postman to send a GET request to http://localhost:3000/tracking to retrieve all tracking data, and a POST request to http://localhost:3000/tracking with the following JSON data to create a new tracking data:

{
  "name": "TRACKING CASH ",
  "image": "images/T5.jpg",
  "images": [
    {
      "image": "images/T3.jpg",
      "imgDes": "connexion"
    }
  ],
  "description": "texte",
  "lien": "http://2003",
  "manuel": "/guide.pdf.crdownload",
  "videoT": ""
}

Conclusion

In this article, we have created an API using NestJS, TypeORM, PostgreSQL, Docker, and MinIO. We have defined the structure of the data that the API will handle, which includes an array of objects with various properties. We have also implemented the API endpoints and tested the API using Postman.

FAQs
Q: What is NestJS?

A: NestJS is a framework for building efficient, scalable Node.js server-side applications.

Q: What is TypeORM?

A: TypeORM is an Object-Relational Mapping (ORM) library for TypeScript and JavaScript.

Q: What is PostgreSQL?

A: PostgreSQL is a powerful, open-source relational database management system.

Q: What is MinIO?

A: MinIO is an object storage server that is designed for scalability, performance, and reliability.

Q: How do I deploy the API?

A: You can deploy the API using a containerization platform like Docker, or by using a cloud provider like AWS or Google Cloud.

Latest stories

Read More

LEAVE A REPLY

Please enter your comment!
Please enter your name here