如何将联接的表放在单独的JSON密钥下

时间:2018-11-28 11:47:37

标签: mysql node.js mariadb

我是一个前端开发人员,试图创建带有连接表的外观正确的JSON文件。我有两个桌子:

JOBS
---------------
id | title

COMPANIES
---------------
id | name

我想要实现的是一个看起来像这样的JSON:

{
  "id": jobId,
  "title: jobTitle,
  "company": {
    "id": companyId,
    "name": companyName
  }
}

我正在使用:

  • Node.js
  • MySQL 5.0.12
  • MariaDB 10.1.35

如何在不映射所有对象的情况下实现它,所以实际上数据库为我提供了已经格式化的数据?

编辑 现在,我正在这样做:

jobs.map(obj => {
      obj.skills = JSON.parse(obj.skills)
      obj.fund = JSON.parse(obj.fund)
      obj.contract_type = JSON.parse(obj.contract_type)
      obj.currency = JSON.parse(obj.currency)
      obj.company = {
        name: obj.name,
        logo: obj.logo
      },
      obj.project = {
        staff: obj.staff,
        breakdown: JSON.parse(obj.breakdown),
        methodology: JSON.parse(obj.methodology),
        stack: JSON.parse(obj.stack),
        stage: obj.stage,
        project_type: obj.project_type,
        required_skills: JSON.parse(obj.required_skills),
        perks: JSON.parse(obj.perks)

      }

      delete obj.name
      delete obj.logo
      delete obj.staff
      delete obj.methodology
      delete obj.stack
      delete obj.stage
      delete obj.project_type
      delete obj.required_skills
      delete obj.perks
      delete obj.breakdown

      return obj
    })

0 个答案:

没有答案