Recall that every MongoDB collection is encapsulated by a Typescript class with the same name.
As we implemented this encapsulation, we observed that there was common functionality that could be abstracted out into a set of four superclasses:
- BaseCollection. All classes (with the single exception of the UserCollection class) inherit either directly or indirectly from BaseCollection. This class provides the methods and fields used to manage a MongoDB collection that are common across RadGrad.
- BaseTypeCollection. This class extends BaseCollection with methods to support MongoDB collections that specify "types" in RadGrad. Currently, there are two such types: InterestTypes and OpportunityTypes.
- BaseSlugCollection. This class extends BaseCollection with methods to support the MongoDB collections whose documents must include a "slug". For more details, see the section on slugs.
- BaseProfileCollection. This class extends BaseSlugCollection with the methods common to the implementation of the collections to support RadGrad roles: AdvisorProfileCollection, FacultyProfileCollection, MentorProfileCollection, and StudentProfileCollection.
Here is the resulting class hierarchy:
There is one exceptional class: UserCollection, which does not inherit from any of the Base classes. This is because the UserCollection class encapsulates a "special" MongoDB collection managed by the Meteor.Accounts package. The behavior of this collection is sufficiently different from regular user-defined collections that we did not feel it was appropriate to make it a subclass of any other class.
All collections that extend BaseCollection are required to implement five methods:
- define: The
definemethod creates a document in the collection. It returns the
_idof the resulting document. The
definemethod takes and object as its one parameter. The values in the object that refer to other documents should use slugs. This supports the loading from a database fixture. For example the
CourseInstancesdefine method looks like
The course, academicTerm, and student are slugs that refer to a Course, AcademicTerm, and StudentProfile.
updatemethod supports changing certain fields of a document. Some fields are immutable by design. The
updatemethod takes two parameters, the docID or slug that identifies which document to update, and an object containing the fields to change. For example the
CourseInstancesupdate method allows you to change the academicTerm, verified, fromRegistrar, grade, creditHrs, note, ice and retired fields. Once you define a CourseInstance the student and course are fixed. All the fields are optional.
removeItmethod removes a single document from the collection. This may have side affects depending on the relationships between collections. The method takes one parameter, the docID or slug, that defines which document to remove. The
removeItmethod may throw a Meteor exception if there are other documents refering to it. For example the
Courses.removeItmethod checks for CourseInstances.
Feeds that refer to the removed course are also removed.
- dumpOne: The
dumpOnemethod creates a JSON object representing the document. RadGrad uses the dump object to restore the document. Restoring an object is calling define using the dump object. This means that the dump object should have slugs not IDs. The
dumpOnemethod has one parameter the docID to dump. For example the
- checkIntegrity: The
checkIntegritymethod checks all the collection's documents ensuring that any ids refer to documents in other collections. The method returns an array of any errors/problems. If the id doesn't refer to another document then a message is pushed onto the returned array. Here's the