Giter Site home page Giter Site logo

belajar-nest's Introduction

Belajar NestJS

Nest framework TypeScript starter repository.

Introduction

Install Nest CLI

# Install Nest CLI
$ npm i -g @nestjs/cli

# Membuat kerangka project
$ nest new project-name

Files generated app.controller.ts A basic controller with a single route. app.controller.spec.ts The unit tests for the controller. app.module.ts The root module of the application. app.service.ts A basic service with a single method. main.ts The entry file of the application which uses the core function NestFactory to create a Nest application instance.

Generate Module

# Generate module dengan menggunakan resource (CRUD)
$ nest g resource todo

# Muncul pilihan: REST API, GraphQL, Microservice, Websockets
# Contoh kasus menggunakan REST API

Menambahkan adaptor koneksi database (Sequelize)

$ npm install --save @nestjs/sequelize sequelize sequelize-typescript mysql2
$ npm install --save-dev @types/sequelize

Database connection & migration setup

create file .sequelizerc file in the root folder and dd the following code

const path = require('path');
module.exports = {
   'seeders-path': path.resolve('src/database', 'seeders'),
   'migrations-path': path.resolve('src/database', 'migrations'),
   'config': path.resolve('src/database', 'config.json'),
}

dan buat secara manual folder berdasarkan struktur kode di atas, kemudian install / eksekusi perintah berikut

$ npm install --save-dev sequelize-cli
$ npx sequelize-cli init

apabila folder models ter-create, silahkan hapus saja.

Generate migration file
$ npx sequelize-cli migration:generate --name TodoTableCreate

Untuk membuat shortcut command, anda dapat melakukan registrasi di file package.json dan masukkan code pada properti script "migration:generate": "nest build && npx sequelize-cli migration:generate --name" Sehingga perintah dari

$ npx sequelize-cli migration:generate TodoTableCreate

menjadi

# gunakan yarn
$ yarn migration:generate TodoTableCreate

# gunakan npm
$ npm run migration:generate TodoTableCreate

Make DB Table menggunakan migration

module.exports = {
  /**
   *
   * @param {import('sequelize/types').QueryInterface} queryInterface
   * @param {import('sequelize-typescript').DataType} Sequelize
   * @returns
   */
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('Todos', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      description: {
        type: Sequelize.STRING(100),
        allowNull: false,
      },
      status: {
        type: Sequelize.ENUM('PENDING', 'PROGRESS', 'DONE'),
      },
      createdAt: Sequelize.DATEONLY,
      updatedAt: Sequelize.DATEONLY,
    });
  },

  /**
   *
   * @param {import('sequelize/types').QueryInterface} queryInterface
   * @returns
   */
  async down(queryInterface) {
    await queryInterface.dropTable('Todos');
  },
};

Running the app

# development with watch mode 
$ npm run start:dev

# production mode
$ npm run start:prod

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.