TriPSs/nestjs-query

Issue with Remove Relation/set relation to null in Many to One relation

Opened this issue · 0 comments

Describe the bug
QueryService setRelation and removeRelation don't work as expected for ManyToOne relationships.

Have you read the Contributing Guidelines?

yes

To Reproduce
if you have a many to one relation on an entity, you can set it via code like this:

event = await this.eventService.setRelation(
          'company',
          input.id,
          input.update.companyId,
        );

however, there is no convenient way to remove the relationship. I would think you could set it to null, but this does not work:

event = await this.eventService.setRelation(
          'company',
          input.id,
          null,
        );

and for some reason, removeRelation still requires the ID to be removed, which while that makes sense if the relationshp was many to many and I was plucking on out of a collection, in a many to one, we are just trying to clear the single value, so removeRelation should not need a relationId.

Consider the following, simple, clean code:

 if ('companyId' in inputEvent) {
      event = await this.eventService.setRelation(
        'company',
        event.id,
        inputEvent.companyId,
      );
    }

if setRelation supported a null companyId to clear the relationship, this is all I'd need to support setting and removing the relationship.

so.. options:
setRelation - could take a null for relationId and remove the relationship (similar in nature to the collection approach where you can just set the new collection).
removeRelation - remove the relationId parameter and just remove whatever relationship may exist

Desktop (please complete the following information):

  • Node Version 18
  • Nestjs-query Version 5.1.0-alpha.1