Background
I have a Mongoose schema that defines a set of possible values a given object can have.
const mongoose = require("mongoose");
const COUNTRIES = ["ES", "PT", "US", "FR", "UK"];
const GENDERS = ["M", "F"];
const surveySchema = {
subject: { type: String, required: true },
country: { type: String, enum: COUNTRIES },
target: {
gender: { type: String, enum: GENDERS }
}
};
module.exports = new mongoose.Schema(surveySchema);;
module.exports.modSchema = surveySchema;
Why I don't like ENUM
I don't personally like ENUM
values because if I add another value to the ENUM
, I have to recompile the entire application again and deploy.
I guess that with an ENUM
such as gender, that will never change, this is not a problem.
However, with countries, my SQL side tells me I should store them because if you have a growing business, you are likely to expand to other countries.
Problem
My problem here is that I don't know how to tell Mongoose, at a schema level, that the only allowed values for the countries have to be ["ES", "PT", "US", "FR", "UK"]
.
I guess I could create a collection countries, but then I lack the knowledge on how I would connect them. Would I have to use async validators?
How would you deal with an ENUM
that can change?