I'm creating a Mongoose model for a web service. I have a secondary index on facultyId
because majority of the reads would want to search by facultyId
. I've included some statics
on the model in order to make it simple for people to query.
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
const GAPFApplicationSchema = new Schema({
facultyId: { type: Number, index: true },
created: Number, // use UNIX time stamps for all dates
lastModified: Number,
status: { type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] },
attachedDocuments: [
{
name: String,
link: String,
attachedDate: Number
}
]
});
GAPFApplicationSchema.static("findByFacultyId", function(facultyId, callback) {
return this.findOne()
.where("facultyId")
.equals(facultyId);
});
GAPFApplicationSchema.static("submit", function(gapf, callback) {
return this.findOneAndUpdate(
{ facultyId: gapf.facultyId },
gapf,
{ upsert: true }, // create it if doesn't exist
callback
);
});
const GAPFApplication = mongoose.model(
"GAPFApplication",
GAPFApplicationSchema
);
export { GAPFApplication };
I haven't used Mongoose before, so I was hoping to get some review on the approach taken here. The code is pretty short, so I didn't think there would be much to address.