Usage of Pick in JHipster client entities

Viewed 63

I'm trying to understand the usage of Pick/Omit in generated client entities in JHipster. For instance, I have this generated:

export interface IHouse {
  id: number;
  creationDate?: dayjs.Dayjs | null;
  room?: Pick<IRoom, 'id'> | null;
}

And IRoom looks like this:

export interface IRoom{
  id: number;
  code?: string | null;
}

I need to display room's code from house instance, but I can't because of this Pick structure. I don't understand the value of this utility type here. Can someone enlight me on this please?

1 Answers

Pick<IRoom, 'id'> removes every property from IRoom type except the 'id'.

Others fields are not used, so they are irrelevant to relationships and omitted. This matches the backend with DTO behavior passing only the id and the label field.

If you customized the ui with the code field, just customize the type by adding code to the Pick fields like:

export interface IHouse {
  id: number;
  creationDate?: dayjs.Dayjs | null;
  room?: Pick<IRoom, 'id', 'code'> | null;
}

Or remove the Pick:

export interface IHouse {
  id: number;
  creationDate?: dayjs.Dayjs | null;
  room?: IRoom | null;
}

Missing label field

The example jdl is:

entity Room {
  code String
}

entity House {
  creationDate Date
}

relationship ManyToOne {
  House to Room
}

Adding a label to the relationship will generate like you want:

relationship ManyToOne {
  House{room(code)} to Room
}

Result:

export interface IHouse {
  id: number;
  creationDate?: dayjs.Dayjs | null;
  room?: Pick<IRoom, 'id', 'code'> | null;
}

Rest api without DTO

Given the jdl:

entity Room {
  code String
}

entity House {
  creationDate Date
}

relationship ManyToOne {
  House{room} Room{house}
}

Serializing the an entity would generate a circular exception due to the bi-directional relationship.

To avoid the circular exception, the backend is generated ignoring relationships like:

class House {
  JsonIgnoreProperties({'house', ...others relationships})
  Room room;
}

In typescript:

type House {
  room?: Omit<Room, 'house', ...others relationships>
}

Only the label field is relevant to the generated ui, we are using the Pick approach to keep templates simpler.

This can be easily customized using local blueprint.

Related