sailsjs API的考试app模型结构

时间:2017-02-08 09:09:25

标签: sails.js database-schema jsonschema api-design sails-mongo

我正在为考试练习构建一个移动应用程序。我需要一个API端点来查看考试详情和问题。我需要帮助在sails.js中构建我的数据。我记得来自firebase的这个JSON结构。

端点GET操作应该返回:

         {
            "8h9iuhiuhi89h98h": {
              "exam": "waec",
              "year": "1990",
              "subject": "chemistry",
              "question": "Which of these is not an acid",
              "answers": [
                {
                 option: "NaCl",
                 isValid: true
                },
                {
                 option: "H<sub>2</sub>SO<sub>4</sub>",
                 isValid: false
                 },
                {
                 option: "H<sub>3</sub>", 
                 isValid: false
                },
                {
                 option: "HCl",
                 isValid: false
                }
              ]
            },
            "8h9iuhiuhi89h98h": {
              "exam": "waec",
              "year": "1990",
              "subject": "chemistry",
              "question": "Which of these is not an acid",
              "answers": [
                {
                 option: "NaCl",
                 isValid: true
                },
                {
                 option: "H<sub>2</sub>SO<sub>4</sub>",
                 isValid: false
                 },
                {
                 option: "H<sub>3</sub>", 
                 isValid: false
                },
                {
                 option: "HCl",
                 isValid: false
                }
              ]
            },
"8h9iuhiuhi89h98h": {
              "exam": "waec",
              "year": "1990",
              "subject": "chemistry",
              "question": "Which of these is not an acid",
              "answers": [
                {
                 option: "NaCl",
                 isValid: true
                },
                {
                 option: "H<sub>2</sub>SO<sub>4</sub>",
                 isValid: false
                 },
                {
                 option: "H<sub>3</sub>", 
                 isValid: false
                },
                {
                 option: "HCl",
                 isValid: false
                }
              ]
            },
            "8h9iuhiuhi89h98h": {
              "exam": "waec",
              "year": "1990",
              "subject": "chemistry",
              "question": "Which of these is not an acid",
              "answers": [
                {
                 option: "NaCl",
                 isValid: true
                },
                {
                 option: "H<sub>2</sub>SO<sub>4</sub>",
                 isValid: false
                 },
                {
                 option: "H<sub>3</sub>", 
                 isValid: false
                },
                {
                 option: "HCl",
                 isValid: false
                }
              ]
            }      
    }

凡是waec考试,1990年是年,化学是主题。

http://someappurl.com/api/exam/{exam}/{year}/{subject}

我使用sails generate命令生成了API。但我不知道如何构建和查询我的数据。如何构建我的架构?  我有像这样的风帆模型结构

   attributes: {
    exams: {
      exam_name: 'string',
      years: {
        exam_year: 'string',
        subjects:[
          {
            subject_name: 'string',
            questions: [
              {
                serial_no: 'string',
                text: 'string',
                answers:[
                  {
                    option: 'string',
                    is_valid: 'boolean'
                  }
                ]
              }
            ]
          }
        ]
      }
    }
  }

1 个答案:

答案 0 :(得分:0)

您没有正确使用模型。认为每个Model.js都是一个表,每个属性都是一列。

所以,你的Exam.js可以是这样的:

// api/models/Exam.js
module.exports = {
  attributes: {
    name: {
      type: 'string',
    },
    year: {
      type: 'integer'
    },
    subject: {
      type: 'string',
    },
    question: {
      type: 'string',
    },
    answers: {
      type: 'array',
    }
  }
};

在控制器上,您将定义如何显示db中的数据。 您可以在文档中了解有关Sails模型和控制器的更多信息:

示例项目: