Skip to main content

Models

Goal

The models folder contains all the Mongoose1 Schema2 definitions for the different documents in the database. It allows us to map documents stored in MongoDB to JavaScript objects that can be manipulated and sent to the frontend.

Current Setup

The models folder can be found at AnScealai/api/models. This folder contains JavaScript files that define different documents (object entries) in the database. For organisational purposes, most of these files correspond to a certain collection in the database. For example, models/story.js defines the Schema for the Story documents stored in the story collection of the DB (some files contain more than one Schema).

Each schema maps to a document in Mongo by defining the shape of the document within a collection. These models are imported in the routes files in order to read and write documents to the database. In addition to defining the data structure, Schemas also define any methods associated with the documents. Some of these methods are already defined, such as the findOne() and findById() functions used in the routes files to retrieve documents from the database.

Other functions we can define ourselves. An example of this can be found in the User schema defined at AnScealai/api/models/user.js, where the methods are used for user authentication. This means that any document retrieved from the DB that belongs to this schema will have those methods available to use. More detail on the implementation of these user methods can be found in the Authentication Controller documentation3 .

Usage

Mongoose schemas are defined by creating a new Schema object. The keys for the object correspond to the keys of the documents we want to store in the database. Each key has a defined type (String, Number, Date, etc.) and any other optional properties, such as for indexing and default values. Mongoose adds an automatic _id property to each document in the DB when it is created, so even though this value is used in the routes for manipulating the data, it does not need to be defined as part of the schema.

Note: This auto-generated _id property is of type ObjectId4, not String.
Therefore some type casting might be needed when comparing values with strings.

models-image-01

Example of defining a Schema for a VoiceRecording document in the recordings collection of the database


Defined Schemas

The following Schemas are currently defined in the files of the models folder (except for the last one, which is defined above the endpoint that uses it):

  • chatbot.js

    • Message: Schema used for the Log Schema
    • Log: Schema for documents in the log collection
    • Chatbot: Schema for documents in the chatbot collection
    • AudioBubble: Schema for documents in the audiobubbles collection
    • PersonalScript: Schema for documents in the personalScripts collection
    • CommunityScript: Schema for documents in the communityScripts collection
  • classroom.js

    • Classroom: Schema for documents in the classroom collection
  • engagement.synthesis.js

    • props: Schema for documents in the engagement.playSynthesis collection
  • event.js

    • Event: Schema for documents in the engagement collection
  • gramadoir.js

    • QuillHighlightTag: Schema used for the GramadoirCache Schema
    • GramadoirCache: Schema for documents in the gramadoir.cache collection (Possibly DEPRECATED)
    • GramadoirCacheLink: Schema used for the GramadoirStoryHistory Schema
    • GramadoirStoryHistory: Schema for documents in the gramadoir.story.history collection (Possibly DEPRECATED)
  • message.js

    • Message: Schema for documents in the message collection
  • profile.js

    • Profile: Schema for documents in the profile collection
  • recording.js

    • VoiceRecording: Schema for documents in the recordings collection
  • story.js

    • Schema for documents in the story collection
  • storygrammarerrors.js

    • StoryGrammarErrors: Schema for documents in the storygrammarerrors collection
  • teacherCode.js

    • TeacherCode: Schema for documents in the teacherCodes collection
  • user.js

    • verificationSchema: Schema used for the userSchema
    • resetPasswordSchema: Schema used for the userSchema
    • userSchema: Schema for documents in the user collection
  • userGrammarCounts.js

    • UserGrammarCounts: Schema for documents in the userGrammarCounts collection
  • api/endpoints_functions/gramadoir/getUniqueErrorTypeCounts.js

    • SentenceError: Schema used for UniqueStoryErrors
    • UniqueStoryErrors: Schema for documents in the uniquestoryerrors collection

Footnotes

  1. Mongoose: https://mongoosejs.com/docs/

  2. Mongoose Schemas: https://mongoosejs.com/docs/guide.html

  3. Authentication Controller documentation

  4. ObjectId in Mongoose: https://mongoosejs.com/docs/schematypes.html#objectids .