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, 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
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
- Message: Schema used for the
-
classroom.js
- Classroom: Schema for documents in the
classroom
collection
- Classroom: Schema for documents in the
-
engagement.synthesis.js
- props: Schema for documents in the
engagement.playSynthesis
collection
- props: Schema for documents in the
-
event.js
- Event: Schema for documents in the
engagement
collection
- Event: Schema for documents in the
-
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)
- QuillHighlightTag: Schema used for the
-
message.js
- Message: Schema for documents in the
message
collection
- Message: Schema for documents in the
-
profile.js
- Profile: Schema for documents in the
profile
collection
- Profile: Schema for documents in the
-
recording.js
- VoiceRecording: Schema for documents in the
recordings
collection
- VoiceRecording: Schema for documents in the
-
story.js
- Schema for documents in the
story
collection
- Schema for documents in the
-
storygrammarerrors.js
- StoryGrammarErrors: Schema for documents in the
storygrammarerrors
collection
- StoryGrammarErrors: Schema for documents in the
-
teacherCode.js
- TeacherCode: Schema for documents in the
teacherCodes
collection
- 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
user
collection
- verificationSchema: Schema used for the
-
userGrammarCounts.js
- UserGrammarCounts: Schema for documents in the
userGrammarCounts
collection
- UserGrammarCounts: Schema for documents in the
-
api/endpoints_functions/gramadoir/getUniqueErrorTypeCounts.js
- SentenceError: Schema used for
UniqueStoryErrors
- UniqueStoryErrors: Schema for documents in the
uniquestoryerrors
collection
- 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 . ↩