避免在BookshelfJS中过多的对象嵌套

时间:2017-11-05 18:59:27

标签: node.js postgresql reactjs orm bookshelf.js

我正在使用BookshelfJS作为RESTful ExpressJS API的ORM。我一直在使用模型关系来连接具有特定API调用所需的其他详细信息的表。有时这些关系会深入3到4个模型,每个关系都会创建一个新的嵌套对象。这意味着我正在创建这样的JSON结构:

{
  "id": 45,
  "credit_check": {
    "id": 1,
    "creditor_id": 1,
    "is_complete": true,
    "creditor": {
      "id": 1,
      "business_name": "Burritotopia"
    },
  },
  "questions": [
    {
      "id": 44,
      "question_id": 2,
      "order": 1,
      "choice_response": [],
      "question": {
        "id": 2,
        "text": "Which best describes your credit status?",
        "type": "multipleChoice",
        "choices": [
          {
            "id": 1,
            "question_id": 2,
            "text": "Below Average"
          },
          {
            "id": 2,
            "question_id": 2,
            "text": "Average"
          }
        ]
      }
    }
  ]
}

我在前端使用React,我宁愿在服务器上进行任何数据按摩。更不用说,深层嵌套的对象在React中很难处理。我真的希望将object.credit_check.creditor.business_name等属性仅显示为object.business_name,将object.questions[0].question.text显示为object.questions[0].text

使用BookshelfJS有一种简单或首选的方法来实现这种类型的操作吗?

0 个答案:

没有答案