简体   繁体   English

如何返回新创建的实体及其与 Nest.js/TypeORM 的关系?

[英]How can I return a newly created entity along with it's relationship with Nest.js/TypeORM?

I am having trouble trying to figure out the best way to return a newly created entity along with its relationship(s).我在试图找出返回新创建的实体及其关系的最佳方法时遇到了麻烦。

Alone, each route that I have is working properly.独自一人,我拥有的每条路线都在正常工作。 Meaning, all CRUD operations work great.这意味着,所有 CRUD 操作都运行良好。 If I do a GET on the entity, I am also getting the relationship back as expected.如果我对实体执行GET ,我也会按预期恢复关系。

The issue I am stumbling with is, when a new entity is created--I am returning that entity.我遇到的问题是,当创建一个新实体时——我正在返回该实体。 For example:例如:

const foo = this.create();

foo.relationshipId = relationshipId;
foo.bar = bar;
foo.baz = baz;
foo.uuid = uuidv4();

try {
    await foo.save();
} catch (error) {
    // this.logger.error(`Failed to create the foo: ${error.stack}`);

    throw new InternalServerErrorException();
}

return foo;

If I log what foo is, I get back something that looks like this:如果我记录foo是什么,我会得到如下所示的内容:

foo: {
    relationshipId: 1,
    bar: 'example',
    baz: 'example',
    uuid: '123-asdf-example'
}

What I need is to also include the actual related model/entitiy.我需要的是还包括实际的相关模型/实体。 Something like this:像这样的东西:

foo: {
    relationshipId: 1,
    related: {
        some: 'property',
        another: 'example',
    },
    bar: 'example',
    baz: 'example',
    uuid: '123-asdf-example'
}

If I do a "regular" GET for that entity I do get the related entity back with it (exactly as the example above).如果我为该实体执行“常规” GET ,我得到相关实体(与上面的示例完全相同)。 It's only at the create method I am not returning the relationship.只是在create方法中我没有返回关系。

How can I return the newly created entity along with the relationship?如何将新创建的实体与关系一起返回? Do I need to do a GET on the new entity?我需要对新实体进行GET操作吗? Is there a better way to do this?有一个更好的方法吗?

Thank you for any suggestions!感谢您的任何建议!

UPDATE/SOLUTION更新/解决方案

Here is what ended up working for me (and also fits perfectly to what @Schutt suggested).这是最终为我工作的东西(也完全符合@Schutt 的建议)。 This is my foo.repository.ts file:这是我的foo.repository.ts文件:

...

const foo = this.create();

foo.bar = bar;
foo.uuid = uuidv4();

try {
    await foo.save();
} catch (error) {
    // this.logger.error(`Failed to create the foo: ${error.stack}`);

    throw new InternalServerErrorException();
}

return await this.findOne({
    where: { id: foo.id },
    relations: ['related'],
});

I am now getting the newly created entity as well as the related entitiy.我现在正在获取新创建的实体以及相关实体。 In my frontend, I can display something like this now:在我的前端,我现在可以显示如下内容:

{{ foo.relation.name }}

I don't think that it is possible to load the relationship while you .save() your entity.我认为在.save()实体时无法加载关系。

You have to re-fetch the entity.您必须重新获取实体。 (ie with foo.findOne(..) there you can specify the relations property and this will then load your relation automatically) (即使用foo.findOne(..)你可以指定relations属性,然后这将自动加载你的关系)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM