Loopback 4: how to get the correct id in the response when creating an entity

38 Views Asked by At

I have been unable to correctly get ids out when creating entity via a CRUD API in Loopback 4.

I have a simple model that look like this :

@model({settings: 
  {strict: true,
  idInjection: false}
})
export class Tournament extends Entity {
  @property({
    type: 'string',
    required: true,
  })
  Nom: string;

  @property({
    type: 'string',
    id: true,
    defaultFn: 'uuidv4'
  })
  ID?: string;

  constructor(data?: Partial<Tournament>) {
    super(data);
  }
}

and a controller with this endpoint :

@post('/tournaments')
@response(200, {
  description: 'Tournament model instance',
  content: {'application/json': {schema: getModelSchemaRef(Tournament)}},
})
async create(
  @requestBody({
    content: {
      'application/json': {
        schema: getModelSchemaRef(Tournament, {
          title: 'NewTournament',
          exclude: ['ID'],
        }),
      },
    },
  })
  tournament: Omit<Tournament, 'ID'>,
): Promise<Tournament> {
  return this.tournamentRepository.create(tournament);
}

Nothing fancy, mostly auto-generated as all of this is pretty new to me.

When I'm creating a tournament via my endpoint, the ID I get back is a number instead of the uuid I want. However, in my DB (SQLite if this matters), UUIDs are correctly saved.

How do I fix this ?

0

There are 0 best solutions below