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
_idproperty is of type ObjectId4, notString.
Therefore some type casting might be needed when comparing values with strings.

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
LogSchema - Log: Schema for documents in the
logcollection - Chatbot: Schema for documents in the
chatbotcollection - AudioBubble: Schema for documents in the
audiobubblescollection - PersonalScript: Schema for documents in the
personalScriptscollection - CommunityScript: Schema for documents in the
communityScriptscollection
- Message: Schema used for the
-
classroom.js
- Classroom: Schema for documents in the
classroomcollection
- Classroom: Schema for documents in the
-
engagement.synthesis.js
- props: Schema for documents in the
engagement.playSynthesiscollection
- props: Schema for documents in the
-
event.js
- Event: Schema for documents in the
engagementcollection
- Event: Schema for documents in the
-
gramadoir.js
- QuillHighlightTag: Schema used for the
GramadoirCacheSchema - GramadoirCache: Schema for documents in the
gramadoir.cachecollection (Possibly DEPRECATED) - GramadoirCacheLink: Schema used for the
GramadoirStoryHistorySchema - GramadoirStoryHistory: Schema for documents in the
gramadoir.story.historycollection (Possibly DEPRECATED)
- QuillHighlightTag: Schema used for the
-
message.js
- Message: Schema for documents in the
messagecollection
- Message: Schema for documents in the
-
profile.js
- Profile: Schema for documents in the
profilecollection
- Profile: Schema for documents in the
-
recording.js
- VoiceRecording: Schema for documents in the
recordingscollection
- VoiceRecording: Schema for documents in the
-
story.js
- Schema for documents in the
storycollection
- Schema for documents in the
-
storygrammarerrors.js
- StoryGrammarErrors: Schema for documents in the
storygrammarerrorscollection
- StoryGrammarErrors: Schema for documents in the
-
teacherCode.js
- TeacherCode: Schema for documents in the
teacherCodescollection
- TeacherCode: Schema for documents in the
-
user.js
- verificationSchema: Schema used for the
userSchema - resetPasswordSchema: Schema used for the
userSchema - userSchema: Schema for documents in the
usercollection
- verificationSchema: Schema used for the
-
userGrammarCounts.js
- UserGrammarCounts: Schema for documents in the
userGrammarCountscollection
- UserGrammarCounts: Schema for documents in the
-
api/endpoints_functions/gramadoir/getUniqueErrorTypeCounts.js
- SentenceError: Schema used for
UniqueStoryErrors - UniqueStoryErrors: Schema for documents in the
uniquestoryerrorscollection
- SentenceError: Schema used for
Footnotes
-
Mongoose: https://mongoosejs.com/docs/ ↩
-
Mongoose Schemas: https://mongoosejs.com/docs/guide.html ↩
-
Authentication Controller documentation ↩
-
ObjectId in Mongoose: https://mongoosejs.com/docs/schematypes.html#objectids . ↩