npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

pinisidev-nestjs-dto-generator

v1.0.2

Published

A powerful DTO generator for NestJS entities.

Readme

NestJS DTO Generator

npm version License: MIT

Sebuah tool CLI yang powerful untuk secara otomatis menghasilkan Data Transfer Objects (DTOs) dari class Entity Anda di proyek NestJS. Ucapkan selamat tinggal pada pembuatan DTO manual yang membosankan dan rawan kesalahan. Cukup hiasi entity Anda, jalankan satu perintah, dan biarkan generator melakukan sisanya.

Generator ini secara cerdas akan membuat DTO untuk operasi Create, Update (dengan PartialType), dan Response, lengkap dengan decorator validasi dari class-validator.

Fitur Utama

  • Otomatisasi Penuh: Generate Create, Update, dan Response DTO dari satu sumber kebenaran (class Entity).
  • Validasi Cerdas: Secara otomatis menambahkan decorator class-validator (@IsString, @IsEmail, @IsInt, @IsOptional, dll.) berdasarkan tipe data properti.
  • Konfigurasi Fleksibel: Kontrol properti mana yang muncul di DTO tertentu menggunakan decorator @DtoProperty yang intuitif.
  • Struktur Rapi: Menempatkan file DTO yang dihasilkan di dalam struktur folder modul yang sudah ada (src/modules/{namaModul}/dto/).
  • Integrasi Mulus: Bekerja dengan baik dengan entity dari berbagai ORM seperti TypeORM.

Instalasi

Install package menggunakan npm atau yarn:

npm install your-package-name --save-dev

atau

yarn add your-package-name --dev

Catatan: Disarankan untuk menginstalnya sebagai dev dependency karena ini adalah tool untuk development.

Setup

Untuk kemudahan penggunaan, tambahkan skrip berikut ke dalam file package.json proyek NestJS Anda.

package.json

{
  "scripts": {
    // ... skrip Anda yang lain
    "dto:gen": "generate-dtos --path ."
  }
}

Perintah ini akan menjalankan generator dan menganalisis semua file *.entity.ts di dalam direktori src/ proyek Anda.


Panduan Penggunaan

Penggunaan generator ini sangat mudah dan hanya terdiri dari dua langkah.

Langkah 1: Hiasi Class Entity Anda

Impor dan gunakan decorator yang disediakan di dalam file entity Anda.

Contoh: src/modules/users/entities/user.entity.ts

import {
  GenerateDto,
  GenerateResponseDto,
  DtoProperty,
} from "your-package-name";
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";

// Menandai class ini untuk pembuatan Create/Update DTO
@GenerateDto()
// Menandai class ini untuk pembuatan Response DTO
@GenerateResponseDto()
@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ unique: true })
  @DtoProperty({ type: "email" }) // Menambahkan validasi @IsEmail()
  email: string;

  @Column()
  @DtoProperty({ exclude: ["response"] }) // Sembunyikan properti ini dari Response DTO
  passwordHash: string;

  @Column()
  name: string;

  @Column({ nullable: true })
  bio?: string; // Properti opsional akan otomatis mendapatkan @IsOptional()

  @Column()
  @DtoProperty({ scope: ["create"] }) // Properti ini hanya akan muncul di CreateUserDto
  invitationCode: string;
}

Langkah 2: Jalankan Generator

Jalankan skrip yang telah Anda tambahkan di package.json.

npm run dto:gen

Hasil

Generator akan secara otomatis membuat file-file berikut:

src/modules/users/dto/ ├── create-user.dto.ts ├── update-user.dto.ts └── response-user.dto.ts

Contoh Isi create-user.dto.ts:

import {
  IsString,
  IsInt,
  IsOptional,
  IsEmail,
  IsBoolean,
  IsDateString,
} from "class-validator";

export class CreateUserDto {
  @IsEmail()
  email: string;

  @IsString()
  passwordHash: string;

  @IsString()
  name: string;

  @IsOptional()
  @IsString()
  bio?: string;

  @IsString()
  invitationCode: string;
}

Konfigurasi Decorator

@GenerateDto()

Decorator level class yang menandai sebuah entity untuk diproses oleh generator. Akan menghasilkan DTO untuk operasi create dan update.

@GenerateResponseDto()

Decorator level class opsional. Jika digunakan, generator akan membuat DTO Response tambahan yang ideal untuk data yang dikirim kembali oleh API.

@DtoProperty(options?: DtoPropertyOptions)

Decorator level properti untuk kontrol yang lebih mendalam.

Opsi: | Properti | Tipe | Deskripsi | | :--- | :--- | :--- | | type | 'email', 'password', 'url', dll. | (Opsional) Memberikan petunjuk untuk validasi yang lebih spesifik. Saat ini mendukung 'email'. | | scope | ('create' \| 'update')[] | (Opsional) Daftar DTO di mana properti ini hanya akan disertakan. Jika tidak disetel, properti akan disertakan di semua DTO (kecuali di-exclude). | | exclude| ('response')[] | (Opsional) Daftar DTO di mana properti ini akan dihilangkan. Sangat berguna untuk menyembunyikan data sensitif seperti password dari Response DTO. |


Lisensi

Didistribusikan di bawah Lisensi MIT. Lihat LICENSE untuk informasi lebih lanjut.