Skip to content

Instantly share code, notes, and snippets.

@nicobytes
Last active February 20, 2023 11:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicobytes/b1ce813729476ca4747435f65a9d41dc to your computer and use it in GitHub Desktop.
Save nicobytes/b1ce813729476ca4747435f65a9d41dc to your computer and use it in GitHub Desktop.

Check => https://gist.github.com/nicobytes/7fb83f8feab37ef7b31923cdeb4e20c8 https://app.quickdatabasediagrams.com/#/

1: Entities

// src/tasks/entities/user.entity.ts
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class User{
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  email: string;

  @Column()
  password: string;
}

// src/tasks/entities/profile.entity.ts
import { Entity, Column, PrimaryGeneratedColumn, OneToOne } from 'typeorm';

import { User } from './user.entity';

@Entity()
export class Profile {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column({ name: 'last_name' })
  lastName: string;
}

// src/tasks/task.module.ts

@Module({
  imports: [TypeOrmModule.forFeature([Task, User, Profile])],
  providers: [TasksService, UserService],
  controllers: [TasksController, UserController],
})
export class TasksModule {}

2: 1:1

// src/tasks/entities/user.entity.ts
@OneToOne(() => Profile)
@JoinColumn({ name: 'profile_id' })
profile: Profile;

// src/tasks/entities/profile.entity.ts
@OneToOne(() => User, (user) => user.profile)
 user: User;

3: Run Migrations

npm run migrations:generate -- new-tables
npm run migrations:run

4: Service

// nest g service tasks/services/user --flat
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './../entities/user.entity';
import { Profile } from './../entities/profile.entity';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User) private userRepo: Repository<User>,
    @InjectRepository(Profile) private profileRepo: Repository<Profile>,
  ) {}

  findAll() {
    return this.userRepo.find({
      relations: ['profile'],
    });
  }

  async create(body: any) {
    const profile = new Profile();
    profile.name = body.name;
    profile.lastName = body.lastName;
    const newProfile = await this.profileRepo.save(profile);

    const user = new User();
    user.email = body.email;
    user.password = body.password;
    user.profile = newProfile;
    return this.userRepo.save(user);
  }
}

5: Controller

// nest g co tasks/controllers/user --flat

import { Controller, Get, Post, Body } from '@nestjs/common';

import { UserService } from './../services/user.service';

@Controller('api/users')
export class UserController {
  constructor(private userService: UserService) {}

  @Get()
  findAll() {
    return this.userService.findAll();
  }

  @Post()
  create(@Body() body: any) {
    return this.userService.create(body);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment