How to create multiple database entries and have id of first inserted entry as parentId field for the rest entries?

huangapple go评论49阅读模式
英文:

How to create multiple database entries and have id of first inserted entry as parentId field for the rest entries?

问题

[entry1: {parentId: null}, entry2: {parentId: 'idOfInsertedEntry1'}, entry3: {parentId: 'idOfInsertedEntry1'}]

我需要在数据库中创建多个条目(我有一个数据对象数组)。第一个条目应该是主条目,其他条目应该是第一个条目的子条目。

我无法使用批量创建,因为在过程中无法获取第一个插入条目的ID。如何实现这个目标?

英文:

I need to create multiple entries in db (I have array of data objects). The first entry should be the main, other entries should be the children of the first.

[entry1: {parentId: null}, entry2: {parentId: 'idOfInsertedEntry1'}, entry3: {parentId: 'idOfInsertedEntry1'}]

I can't use bulk creation as I can't get the id of first inserted entry during the process.
How to achieve the goal?

答案1

得分: 1

以下是您要求的翻译内容:

"There is no easy way, you are going to need a custom route and a custom controller, and then splice.."

"没有简单的方法,您需要一个自定义路由和一个自定义控制器,然后再拼接..."

"I've would also not recommend to use parentId, use parent as relation to own content type. ParentId give headaches when used with data transfer."

"我也不建议使用 parentId,使用 parent 作为与自己的内容类型的关联。parentId 在数据传输时会引起问题。"

"so if you want to store something locally (entry specific) in strapi content type you could define a Service component, add it to your main entry as repeatable component (if you want search out of the box) or dynamicZone (if you want to have polymorphic but no search out of the box)."

"因此,如果您想在 Strapi 内容类型中存储某些内容(特定于条目),您可以定义一个 Service 组件,将其添加到主要条目中作为可重复组件(如果您想要立即进行搜索)或 dynamicZone(如果您想要多态但不需要立即搜索)。"

"then example payload of mutation would be like this:"

"然后,变异的示例负载将如下所示:"

"and you would have this services bound specifically to that order. if you query you would do something like"

"然后,您将这些服务专门绑定到该订单。如果您进行查询,可以执行类似以下的操作"

"parent and child
you do in your schema:"

"父母和子女
您在架构中执行:"

{
...
"parent": {
"type": "relation",
"relation": "oneToOne",
"target": "api::order.order"
}
...
}

{
...
"parent": {
"type": "relation",
"relation": "oneToOne",
"target": "api::order.order"
}
...
}

"so then you can chain them like tree branches"

"然后您可以像树枝一样将它们链接在一起"

英文:

There is no easy way, you are going to need a custom route and a custom controller, and then splice..

const [root, ...childs] = entries;

const rootEntry = await strapi.entityService.create('api::...', { data: root });

const childsEntries = await Promise.all(
  children.map(async (child) => 
    await strapi.entityService.create('api::...', { data: {...child, parentId:rootEntry.id }})
));

return [ rootEntry, ...childsEntries ];

I've would also not recommend to use parentId, use parent as relation to own content type. ParentId give headaches when used with data transfer.

upd

so if you want to store something locally (entry specific) in strapi content type you could define a Service component, add it to your main entry as repeatable component (if you want search out of the box) or dynamicZone (if you want to have polymorphic but no search out of the box).

then example payload of mutation would be like this:

{
  data: {
    ...someStuff,
    services: [
    { ...someServiceStuff1 },
    { ...someServiceStuff2 } 
    ]
  }

and you would have this services bound specifically to that order. if you query you would do something like

{ where : { services: { param: someServiceParam } } }

parent and child

you do in your schema:

{
 ...,
"parent": {
      "type": "relation",
      "relation": "oneToOne",
      "target": "api::order.order"
    }
  ...
},

so then you can chain them like tree branches

huangapple
  • 本文由 发表于 2023年5月11日 05:01:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定