sailsjs 的考试应用程序模型结构 API
exam app model structure for sailsjs API
我正在构建一个用于考试练习的移动应用程序。我需要一个 API 端点来获取考试详情和问题。我需要帮助来构建 sails.js 中的数据。我想到了这个来自 firebase 的 JSON 结构。
端点 GET 操作应该 return 这个:
{
"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'
}
]
}
]
}
]
}
}
}
您没有正确使用模型。认为每个 Model.js 是一个 table 并且每个属性是一列。
所以,你的 Exam.js 可以是这样的:
// api/models/Exam.js
module.exports = {
attributes: {
name: {
type: 'string',
},
year: {
type: 'integer'
},
subject: {
type: 'string',
},
question: {
type: 'string',
},
answers: {
type: 'array',
}
}
};
并且在控制器上,您将定义如何显示来自数据库的数据。
您可以在文档中了解有关 Sails 模型和控制器的更多信息:
- http://sailsjs.com/documentation/concepts/controllers
- http://sailsjs.com/documentation/concepts/models-and-orm/models
示例项目:
我正在构建一个用于考试练习的移动应用程序。我需要一个 API 端点来获取考试详情和问题。我需要帮助来构建 sails.js 中的数据。我想到了这个来自 firebase 的 JSON 结构。
端点 GET 操作应该 return 这个:
{
"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'
}
]
}
]
}
]
}
}
}
您没有正确使用模型。认为每个 Model.js 是一个 table 并且每个属性是一列。
所以,你的 Exam.js 可以是这样的:
// api/models/Exam.js
module.exports = {
attributes: {
name: {
type: 'string',
},
year: {
type: 'integer'
},
subject: {
type: 'string',
},
question: {
type: 'string',
},
answers: {
type: 'array',
}
}
};
并且在控制器上,您将定义如何显示来自数据库的数据。 您可以在文档中了解有关 Sails 模型和控制器的更多信息:
- http://sailsjs.com/documentation/concepts/controllers
- http://sailsjs.com/documentation/concepts/models-and-orm/models
示例项目: