简体   繁体   中英

How to get specific field from nested table relation in adonisjs

THIS IS MY CODE QUERY

i want to get only some specific field from every table:

 const project = await Project.query()
      .where("user_id", user_id)
      .where('id', project_id)
      .with('items.file.sheets.markup.comment.attachment').first()

but i get this response there is lot of extra fields

"status": true,
    "message": "Here is detail of Your Project",
    "data": {
        "id": 1,
        "name": "Spacey",
        "description": "222 please change some points",
        "created_at": "2021-03-24 17:18:56",
        "updated_at": "2021-03-24 19:12:09",
        "deleted_at": null,
        "items": [
            {
                "id": 1,
                "project_id": 1,
                "assignee_id": 1,
                "status": null,
                "type": "markup",
                "description": "my  ",
                "version": 4,
                "visibility": "1",
                "due_date": "2019-12-31",
                "priority": "high",
                "resolved_on": null,
                "created_by": 2,
                "resolved_by": null,
                "updated_by": null,
                "created_at": "2021-03-24 17:19:02",
                "updated_at": "2021-03-24 17:19:02",
                "deleted_at": null,
                "file": [
                    {
                        "id": 1,
                        "actual_name": "10Feb2021.pdf",
                        "original_ext": "pdf",
                        "random_name": "1616588342707_8063",
                        "original_local_path": "files/2/1/1/1616588342707_8063.pdf",
                        "image_url": "https://resolve-dev-backend.s3.us-east- "
                        "media_type": "profile",
                        "item_id": 1,
                        "user_id": 2,
                        "created_at": "2021-03-24 17:19:05",
                        "updated_at": "2021-03-24 17:19:05",
                        "created_by": 2,
                        "updated_by": null,
                        "deleted_at": null,
                        "sheets": [
                            {
                                "id": 1,
                                "actual_name": "convertpdftojpg.png",
                                "original_ext": "png",
                                "random_name": "1616588346705_117",
                                "original_local_path": "sheets/2/1/1/1616588346705_117.png",
                                "image_url": "https://resolve-dev-backend.s3.us-east-"
                                "type": "Sheet",
                                "item_id": 1,
                                "file_id": 1,
                                "user_id": 2,
                                "created_at": "2021-03-24 17:19:09",
                                "updated_at": "2021-03-24 17:19:09",
                                "created_by": 2,
                                "updated_by": null,
                                "deleted_at": null,
                                "markup": [
                                    {
                                        "id": 1,
                                        "item_id": 1,
                                        "sheet_id": 1,
                                        "assignee_id": null,
                                        "editor_details": "{\"a\": \"b\", \"c\": \"d\"}",
                                        "visibility": "private",
                                        "image_url": "{\"a\": \"b\", \"c\": \"d\"}",
                                        "priority": "low",
                                        "resolved_by": 2,
                                        "resolved_on": "2021-03-",
                                        "due_date": null,
                                        "created_by": 2,
                                        "created_at": "2021-03-24 18:30:21",
                                        "updated_at": "2021-03-24 19:12:39",
                                        "deleted_at": null,
                                        "comment": []
                                    }
                                ]
                            }
                        ]
                    }
                ],

but i don't need project_id etc like some field in every table for
example i need only these field from file table and want to get specific field from every table in my query but i get above output response in my postman

                     ```
                    "id": 1,
                    "actual_name": "10 -Rehan Shakeel - Feb2021.pdf",
                    "image_url": "https://resolve-dev/file/2/1/1/1616588346705_117.pdf",
                    "media_type": "profile",
                    "created_at": "2021-03-24 17:19:05",
                    "updated_at": "2021-03-24 17:19:05",
                    "deleted_at": null,
                        ```
                    

Try to add the select statement with the columns inside an array

const project = await Project.query()
      .select(['tableyouwant.id', 'actual_name', 'image_url', 'media_type', ..........])
      .where('id', project_id)
      .with('items.file.sheets.markup.comment.attachment').first()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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