Skip to content

Instantly share code, notes, and snippets.

Avatar

Rasool Khan rskhan167

View GitHub Profile
@rskhan167
rskhan167 / user.controller.ts
Last active Oct 13, 2021
Nestjs Series - Part 3
View user.controller.ts
import { Controller, Post, Body, Get, Param } from '@nestjs/common';
import { ApiOkResponse } from '@nestjs/swagger';
import { CreateUserDto } from './dtos/create-user.dto';
import { UserService } from './user.service';
import { User } from './entities/user.entity';
@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}
View nest-cli.json
{
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"plugins": [
{
"name": "@nestjs/swagger/plugin",
"options": {
"dtoFileNameSuffix": [".entity.ts", ".dto.ts"],
"controllerFileNameSuffix": [".controller.ts"]
@rskhan167
rskhan167 / main.ts
Created Oct 13, 2021
Nestjs Series
View main.ts
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('api');
const swaggerConfig = new DocumentBuilder()
.setTitle('NestJS Tutorial')
View app.module.ts
import { Module } from '@nestjs/common';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserModule } from './user/user.module';
@Module({
imports: [MikroOrmModule.forRoot(), UserModule],
controllers: [AppController],
@rskhan167
rskhan167 / main.ts
Created Sep 19, 2021
Nestjs Series
View main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('api');
await app.listen(3000);
}
bootstrap();
View user.service.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@mikro-orm/nestjs';
import { User } from './entities/user.entity';
import { CreateUserDto } from './dtos/create-user.dto';
import { UserRepository } from './user.repository';
@Injectable()
export class UserService {
constructor(
View create-user.dto.ts
import { IsNotEmpty, IsString, IsOptional } from 'class-validator';
export class CreateUserDto {
@IsNotEmpty()
@IsString()
name: string;
@IsNotEmpty()
@IsString()
email: string;
View user.controller.ts
import { Controller, Post, Body } from '@nestjs/common';
import { CreateUserDto } from './dtos/create-user.dto';
import { UserService } from './user.service';
import { User } from './entities/user.entity';
@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}
View Migration20210808123050.ts
import { Migration } from '@mikro-orm/migrations';
export class Migration20210808123050 extends Migration {
async up(): Promise<void> {
this.addSql(
'create table "user" ("id" serial primary key, "name" varchar(255) not null, "email" varchar(255) not null, "password" varchar(255) not null, "profile_image" varchar(255) null, "created_at" timestamptz(0) not null, "updated_at" timestamptz(0) not null);',
);
this.addSql(
'alter table "user" add constraint "user_email_unique" unique ("email");',
);
View user.entity.ts
import { Property, Entity, Unique, PrimaryKey } from '@mikro-orm/core';
import { IsEmail } from 'class-validator';
@Entity()
export class User {
@PrimaryKey()
id!: number;
@Property()
name: string;