Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

exam app model structure for sailsjs API

Course Queries Competitions/Entrance Exams

Max. 2000 characters
Replies

usr_profile.png

User

( 5 months 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: "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'
                  }
                ]
              }
            ]
          }
        ]
      }
    }
  }

usr_profile.png

User

( 5 months 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:

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif