0

I have two entities called User and Ad and the relation is 1:M, when I need to create a new Ad, I need to pass the announcer_id together.

Ad.ts

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';

import User from './User';

@Entity('ads')
class Ad {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  announcer_id: string;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'announcer_id' })
  announcer: User;
}

export default Ad;

User.ts

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';

@Entity('users')
class Ad {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  name: string;

  @Column()
  email: string;

  @Column()
  password: string;

  @CreateDateColumn()
  created_at: Date;

  @UpdateDateColumn()
  updated_at: Date;
}

export default Ad;

CreateAdService.ts

import { getCustomRepository } from 'typeorm';
import Ad from '../models/Ad';

import AdsRepository from '../repositories/AdsRepository';

interface IRequest {
  announcer_id: string;
  title: string;
  description: string;
  price: number;
  n_room: number;
  n_bathroom: number;
  garage: boolean;
}

class CreateAdService {
  public async execute({
    announcer_id,
    title,
    description,
    price,
    n_room,
    n_bathroom,
    garage,
  }: IRequest): Promise<Ad> {
    const adsRepository = getCustomRepository(AdsRepository);

    const priceNegative = adsRepository.isNegative(price);

    if (priceNegative) {
      throw new Error("You can't create an announcement with negative price.");
    }

    const ad = adsRepository.create({
      announcer_id,
      title,
      description,
      price,
      n_room,
      n_bathroom,
      garage,
    });

    await adsRepository.save(ad);

    return ad;
  }
}

export default CreateAdService;

The Error

{
  "error": "Cyclic dependency: \"Ad\""
}
Vagner Wentz
  • 391
  • 1
  • 7
  • 29
  • I tried creating the same project and no errors has been thrown. – Carlo Corradini Jul 07 '20 at 09:22
  • Seeing from the code there is no cycle dependency . Are you sure the problem is from the code above? – Carlo Corradini Jul 07 '20 at 09:24
  • For better readability move create Ad method inside the Ad custom repository so you can create one calling *await getCustomRepository(AdsRepository).create(request: IRequest);* – Carlo Corradini Jul 07 '20 at 09:28
  • I suggest you to add eslint and more constraints using *eslint - prettier* and some typescript compiler using *tsconfig*. [Here](https://github.com/carlocorradini/racer-server) is my full project with a (pedantic) error checking/testing and many more. PS: add *export default class* (it's more concise) and the *!* when you declare class parameters (see [this](https://stackoverflow.com/questions/42273853/in-typescript-what-is-the-exclamation-mark-bang-operator-when-dereferenci)). – Carlo Corradini Jul 07 '20 at 09:34

0 Answers0