exam app model structure for sailsjs API

Course Queries Competitions/Entrance Exams 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Competitions/Entrance Exams related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

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: "H2SO4",
                 isValid: false
                 },
                {
                 option: "H3", 
                 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: "H2SO4",
                 isValid: false
                 },
                {
                 option: "H3", 
                 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: "H2SO4",
                 isValid: false
                 },
                {
                 option: "H3", 
                 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: "H2SO4",
                 isValid: false
                 },
                {
                 option: "H3", 
                 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'
                  }
                ]
              }
            ]
          }
        ]
      }
    }
  }
profilepic.png
manpreet 2 years ago

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:


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.