How to get multiple rows from a one-to-many relationship in the findOne method in Nestjs+Typeform?

43 views Asked by At

I want to import medias as a multi-row

[1].

const feed = await this.repository.findOne({
            where: {
                id: feedId,
            },
            select: {
                id: true,
                contents: true,
                group: {
                    id: true,
                    groupName: true,
                    groupDescription: true,
                },
                member: {
                    id: true,
                    username: true,
                },
                medias:{
                    url:true,
                    position:true
                }
            },

            relations: {
                group: true,
                member: true,
                medias: true,
            },
        });

This will fetch a single row

[2].

const feed = await this.repository.findOne({
            where: {
                id: feedId,
            },
            select: {
                id: true,
                contents: true,
                group: {
                    id: true,
                    groupName: true,
                    groupDescription: true,
                },
                member: {
                    id: true,
                    username: true,
                },
                
            },

            relations: {
                group: true,
                member: true,
                medias: true,
            },
        });

This will import medias as a multiline, but will import all fields.

I'm practicing writing without using querybuilder and just using the built-in function findOne, but for the life of me I can't find a way to get only certain fields from multiple rows. Can anyone help me?

[1] Result

"data": {
        ....
        "medias": [
            {
                "url": "url1",
                "position": "0"
            }
        ]
    }

[2] Result

"data": {
        ...
        "medias": [
            {
                "id": "id1",
                "createdAt": "2023-10-18T23:11:25.458Z",
                "updatedAt": "2023-10-18T23:11:25.458Z",
                "feedId": "feedid",
                "url": "url1",
                "position": "0"
            },
            {
                "id": "id2",
                "createdAt": "2023-10-18T23:11:25.466Z",
                "updatedAt": "2023-10-18T23:11:25.466Z",
                "feedId": "feedid",
                "url": "url2",
                "position": "1"
            }
        ]
    }

I just looked at the documentation and tried again https://github.com/typeorm/typeorm/blob/master/docs/eager-and-lazy-relations.md

@OneToMany(() => FeedMediaEntity, (fm) => fm.feed, {
        eager: true,
    })
medias?: FeedMediaEntity[];
select: {
                ...
                medias: {
                    url: true,
                    position: true,
                },
            },

[Result]

"data": {
        ...
        "medias": [
            {
                "id": "id1",
                "createdAt": "2023-10-18T23:11:25.458Z",
                "updatedAt": "2023-10-18T23:11:25.458Z",
                "feedId": "feedid",
                "url": "url1",
                "position": "0"
            },
            {
                "id": "id2",
                "createdAt": "2023-10-18T23:11:25.466Z",
                "updatedAt": "2023-10-18T23:11:25.466Z",
                "feedId": "feedid",
                "url": "url2",
                "position": "1"
            }
        ]
    }

Doesn't work :(

0

There are 0 answers