Crud: Load eager relation data in GET request

Created on 27 Aug 2019  路  3Comments  路  Source: nestjsx/crud

@Crud({
  model: {
    type: Gate,
  },
})
@ApiUseTags('Gate')
@Controller('gate')
export class GateController {
  constructor(public service: GateService) {
  }
}
@Injectable()
export class GateService extends TypeOrmCrudService<Gate> {
  constructor(@InjectRepository(Gate) repo) {
    super(repo);
  }
}
@Entity()
export class Gate {
  @ApiModelProperty()
  @PrimaryGeneratedColumn()
  id: number;

  @ApiModelProperty()
  @Column()
  name: string;

  @ManyToOne(type => School, school => school.gates, {
    eager: true,
  })
  @JoinColumn({ name: 'school_id' })
  school: School;
}

How to get out put as a eager.

question

All 3 comments

Yo! Thanks for the question!
You can load eager relations like so

@Crud({
  model: {
    type: Gate,
  },
  query: {
    joins: {
      school: {
        eager: true,
      },
    },
  },
})
@Crud({
  model: {
    type: Gate,
  },
  query: {
    joins: {
      school: {
        eager: true,
      },
    },
  },
})

Thank you for the help! It works.
Another problem is, how to give swagger support for this. its show sample output as like =>
[ { "id": 0, "title": "string", } ]
And eager between 3 relational classes not load.

typeorm is going to deprecate eager and lazy loading
see https://github.com/typeorm/typeorm/issues/3251

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MeMeMax picture MeMeMax  路  4Comments

tbrannam picture tbrannam  路  3Comments

jbjhjm picture jbjhjm  路  4Comments

lalalalaluk picture lalalalaluk  路  4Comments

terapepo picture terapepo  路  3Comments