Search code examples
sails.jsdatabase-schemajsonschemaapi-designsails-mongo

exam app model structure for sailsjs API


I am building a mobile app for exam practice. I need an API endpoint for the exam details and questions. I need help with structuring my data in sails.js. I have this JSON structure from firebase in mind.

The endpoint GET operation should return this:

         {
            "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
                }
              ]
            }      
    }

where waec is the exam, 1990 is the year, and chemistry is the subject.

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

I have generated API using the sails generate command. but I don't know how to structure and query my data. How do I structure my schema? i have a sails model structure like this

   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'
                  }
                ]
              }
            ]
          }
        ]
      }
    }
  }

Solution

  • You are not using the Model correctly. Think that each Model.js is a table and each attribute is a column.

    So, your Exam.js can be like this:

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

    And on the controller you will define how to display the data from db. You can learn more about Sails Models and Controllers on the docs:

    Example project: