Skip to content

Update relationship attribute having a @BelongsTo() decorator #51

Closed
@thnico

Description

@thnico

I have two models with OneToMany relationship:

@JsonApiModelConfig({
    type: 'posts'
})
export class Post extends JsonApiModel {

    @Attribute()
    title: string;

    @HasMany()
    comments: Comment[];
}

@JsonApiModelConfig({
    type: 'comments'
})
export class Comment extends JsonApiModel {

    @Attribute()
    title: string;

    @BelongsTo()
    post: Post;
}

I'm getting one Comment and I update the Post it's belonging to:

this.datastore.findRecord(Comment, '1').subscribe(
      (comment: Comment) => {
        comment.post = anotherExistingPost;
        comment.save().subscribe();
      }
);

When I save, the Post my Comment is belonging to is not updated.

Actually my request that is being sent to the server doesn't contain anything to update:

{
  "data": {
    "type": "comments",
    "id": "1",
    "attributes": {
      "id": "1"
    }
  }
}

For what I can see in the Library code, the save method is only taking in consideration the properties with @Attribute() decoration.
Unfortunately, my post property of Comment has a @BelongsTo() decoration.

JsonApiModel.prototype.save = function (params, headers) {
    var attributesMetadata = Reflect.getMetadata('Attribute', this);
    return this._datastore.saveRecord(attributesMetadata, this, params, headers);
};

Then what should be done so that I can update the Post my Comment is belonging to ?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions