简体   繁体   English

TypeORM,manyToMany 我无法保存数组对象及其与一个请求的关系

[英]TypeORM, manyToMany I cannot save array object with its relations with one request

I have two different mutations that I have saved the coordinates in the database createCoordinate , createCoordinateMultiple .我有两个不同的突变,我将坐标保存在数据库createCoordinatecreateCoordinateMultiple 中 In the first, I can easily record a single object and get the response with its relations.首先,我可以轻松记录单个对象并通过其关系获得响应。

But I can't get the mutation I'm trying to write for multiple operations correctly.但是我无法正确地为多个操作编写突变。 Can you help me please ?你能帮我吗 ?

@Authorized('ADMIN')
    @Mutation(() => Coordinate)
    async createCoordinate(
        @Arg('coordinate') input: CreateCoordinateInput
    ): Promise<Coordinate> {
        const coordinate = new Coordinate();
        coordinate.lat = input.lat;
        coordinate.lng = input.lng;
        const insects = input.insectList.map(id => ({ id })) as Insect[];
        coordinate.insects = Promise.resolve(insects);
        const response = await this.coordinateRepo.save(coordinate);
        return response;
    }

    @Authorized('ADMIN')
    @Mutation(() => [Coordinate])
    async createCoordinateMultiple(
        @Arg('coordinates', () => [CreateCoordinateInput]) coordinates: CreateCoordinateInput[]
    ): Promise<Coordinate[]> {

        const response = await this.coordinateRepo.save(coordinates);

        const data = response.map(coordinate => ({
            id: coordinate.id,
            lat: coordinate.lat,
            lng: coordinate.lng,
            title: coordinate.title,
            description: coordinate.description,
            sourceId: coordinate.sourceId,
            image: coordinate.image,
            insects: coordinate.insectList.map(id => ({
                insectId: id,
                coordinateId: coordinate.id,
            })),
        }));

        console.log(data);
        return response;
    }

console.log(data) output; console.log(data) 输出;

[
  {
    id: 'c2f6b8f3-3d1a-4497-a492-ace7d58d217e',
    lat: 36.7812360767751,
    lng: 34.5795541012578,
    title: 'B - konum',
    description: 'B - bilgi',
    sourceId: '7c94ce2d-e5a6-4a07-a272-369b3b34a61b',
    image: '1.jpg',
    insects: [ [Object], [Object] ]
  },
  {
    id: 'b555f620-e9fd-4fdf-81ef-f355cb151969',
    lat: 36.7819407011663,
    lng: 34.580841561585,
    title: 'D - konum',
    description: 'D - bilgi',
    sourceId: '7c94ce2d-e5a6-4a07-a272-369b3b34a61b',
    image: '2.jpg',
    insects: [ [Object], [Object] ]
  }
]

https://github.com/typeorm/typeorm/issues/6713 https://github.com/typeorm/typeorm/issues/6713

I found a different solution.我找到了一个不同的解决方案。 Although I don't know a clean use.虽然我不知道干净的用途。 At least it works for now while I wait for the answer from TypeORM developers.至少在我等待TypeORM开发人员的回答时它现在有效

I'm creating another entity for the coordinate and insect relationship.我正在为坐标和昆虫关系创建另一个实体。

I then save the coordinate assets.然后我保存坐标资产。 I match the coordinate ids from the returned answer and the insect ids that need to be written later into an array object.我将返回答案中的坐标 id 与稍后需要写入数组对象的昆虫 id 进行匹配。

@Authorized('ADMIN')
    @Mutation(() => [Coordinate])
    async createCoordinateMultiple(
        @Arg('createCoordinateInputs', () => [CreateCoordinateInput]) inputs: CreateCoordinateInput[]
    ): Promise<Coordinate[]> {
        const response = await this.coordinateRepo.save(inputs);
        const coordinatesInsects = [];
        response.forEach(coordinate => {
            coordinate.insectList.forEach(insect => {
                coordinatesInsects.push({ coordinateId: coordinate.id, insectId: insect })
            })
        });
        await this.coordinateInsectRepo.save(coordinatesInsects);
        return response;
    }

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

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