Shivam
Shivam

Reputation: 2248

Set Enum Array based of another Enum array in the same Model - Mongoose

I am trying to build my listings model in a way where it restricts users to only select a subcategory if it exists in the parent array list. I can do this when I am building the API end point fine but I wanted to see if its possible to do this within the Model itself.

here is an example:

If(a user selects parent category household from the parent enum category array) .then(set ENUM array based of the parent category)

Code Reference:

Model:

var categories  = require(settings.PROJECT_DIR + 'controllers/data/categories');

var ListingSchema = mongoose.Schema({
    data: {
        category: {
            parent: {
                type: String,
                enum: categories.parent(),
                required: true
            },
            sub: {
                type: String,
                enum: categories.sub("household"), // should be this.data.category.parent instead of hard coded value
                required: true
            }
        },
    }
}

The Categories Array:

module.exports.categories = [
    { "household"  : ["repair", "carpentry", "housecleaning", "electrical", "handymen", "movers", "plumbing", "gardening/pool"] },
    { "automotive" : ["autobody", "mechanics", "towing"] },
    { "creative"   : ["art", "cooking", "film", "graphic_design", "marketing", "music", "writing"] },
    { "tech"       : ["tech", "repair", "web_design", "web_development"] },
    { "events"     : ["artist", "florist", "musician", "photography", "planning", "venue"] },
    { "legal"      : ["accounting", "advising", "investments", "consulting", "real_estate"] },
    { "health"     : ["beauty", "fitness", "nutrition", "therapeutic"] },
    { "pets"       : ["grooming", "sitter", "trainer", "walker"] }
];

// @returns parent categories
module.exports.parent = function() {
    var self = this;
    var parentCategories = [];

    for(var i in self.categories) {
        parentCategories.push( Object.keys(self.categories[i])[0] );
    }

    return parentCategories;
}

// @returns sub categories based of parent input
module.exports.sub = function(parent) {
    var self = this;
    var subCategories = [];

    for(var i in self.categories) {
        if(self.categories[i][parent]) {
            return self.categories[i][parent];
        } 
    }
}

Upvotes: 1

Views: 297

Answers (1)

Dani Akash
Dani Akash

Reputation: 7096

It is not possible in the model.

mongoose.Schema() methods creates a Schema object based on the parameter supplied to it. And the created object is stored in ListingSchema. Since the object is created the first time mongoose.Schema() method gets executed, you cannot modify the values dynamically after that. Which means the value accepted by enum cannot be changed once it's set.

The best approach is to add all the values in a single array then set it as the value of enum and do the sub category restriction somewhere else.

Upvotes: 1

Related Questions