Skip to content

Instantly share code, notes, and snippets.

View rskhan167's full-sized avatar

Rasool Khan rskhan167

View GitHub Profile
@rskhan167
rskhan167 / app.module.ts
Last active September 19, 2021 11:37
Nestjs Series
import { Module } from '@nestjs/common';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [MikroOrmModule.forRoot()],
controllers: [AppController],
providers: [AppService],
})
@rskhan167
rskhan167 / mikro-orm.config.ts
Created September 19, 2021 11:38
Nestjs Series
import { Logger } from '@nestjs/common';
import { Options } from '@mikro-orm/core';
import { SqlHighlighter } from '@mikro-orm/sql-highlighter';
import { TsMorphMetadataProvider } from '@mikro-orm/reflection';
const logger = new Logger('MikroORM');
const config = {
entities: ['dist/**/*.entity.js'],
entitiesTs: ['src/**/*.entity.ts'],
dbName: process.env.DBNAME || 'movie-review',
@rskhan167
rskhan167 / user.entity.ts
Created September 19, 2021 11:39
Nestjs Series
import { Property, Entity, Unique, PrimaryKey } from '@mikro-orm/core';
import { IsEmail } from 'class-validator';
@Entity()
export class User {
@PrimaryKey()
id!: number;
@Property()
name: string;
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");',
);
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) {}
import { IsNotEmpty, IsString, IsOptional } from 'class-validator';
export class CreateUserDto {
@IsNotEmpty()
@IsString()
name: string;
@IsNotEmpty()
@IsString()
email: string;
@rskhan167
rskhan167 / user.service.ts
Created September 19, 2021 13:24
Nestjs Series
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(
@rskhan167
rskhan167 / main.ts
Created September 19, 2021 13:34
Nestjs Series
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();
@rskhan167
rskhan167 / app.module.ts
Created September 27, 2021 09:19
Nest series
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 October 13, 2021 15:00
Nestjs Series
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')