javascript - Adonis.js populate all with related tables data
问题描述
I use MySql, all relationships have been configured correctly in models and schemas:
I have 3 tables: 1. posts ( main ) 2. categories ( 1 post has 1 cat, 1 cat belongs to many posts ) 3. tags ( many-to-many )
Categories and tags both have "post_id" column relating to "id" column in posts table.
What is the best way to get all related data ( post data + post category data + post tags data ) ? ( for now I know I can get something like:
const post = await Post.find(params.id)
const category = await post.categories().fetch()
etc for every related table
I'm sure there must be better way.
I'd like result to be:
{ post data - name, text ...,
"category": { category data - name, text ... },
"tags": [ { tag1 data }, { tag2 data }
] }
解决方案
const wantedData = await Post.query().where('id', params.id).with('categories').with('tags').fetch()
推荐阅读
- wordpress - 在woocommerce产品(后端)中更改产品描述所见即所得的位置
- python - TypeError: Request url must be str or unicode, got list:
- scala - lagom 服务调用中的名称调用?
- javascript - 为 prod 构建不要从组件中获取 styleUrls
- scheme - a-plus-abs-b: 无效函数: (if (> b 0) + -)
- vba - 如何通过 API 从访问表单 vba 发送阿拉伯语短信
- javascript - 附加/重新定义函数?
- c - Writing and reading messages continuously in ipc with pipes in C
- docusignapi - 如何查明文件是否已签名
- reactjs - 将 Typescript 应用程序中的远程控制台与 Office 加载项一起使用