Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list of available Mongoose Discriminators?

Given a situation where you have a User Scheme that you use to create a base model called User. And then for user roles, you use mongoose discriminators to create inherited models called Admin, Employee and Client. Is there a way to programmatically determine how many discriminations/inheritances/roles of the User model are available, as well as the available names?

My question in terms of code:

File: models/user.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var options = {discriminatorKey: 'role'};

var userSchema = mongoose.Schema({
  name: String,
  email: String,
  password: String,
},options);

var User = mongoose.model('User', userSchema);

var Client = User.discriminator("Client", mongoose.Schema({
  Address : String,
  Tax_identification : String,
  Phone_number : String,
  Internal_Remarks : String,
  CRM_status : String,
  Recent_contact : String,
}));
var Employee = User.discriminator("Employee",mongoose.Schema({
  Staff_Id: String,
}));

module.exports = {User: User, Client: Client, Employee: Employee };

File: controllers/usersController.js

var User = require('../models/user.js').User;


module.exports = {
  registerRoutes: function(app){
     app.get('user/create',this.userCreateCallback)
  },

  userCreateCallback: function(req,res){

    //Get Available User Roles - The function below doesn't exist, 
    //Just what I hypothetically want to achieve:

    User.geAvailableDiscriminators(function(err,roles){

      res.render('user/create',{roles:roles})      

    });

  }
};

I hope I managed to express what I want to do. Alternative approaches are also welcome.

like image 814
Milazi Avatar asked Dec 31 '17 18:12

Milazi


People also ask

How do you use a mongoose discriminator?

The discriminatorKey option tells mongoose to add a path to the schema called 'kind' and use it to track which type of document this is. For instance, suppose you declared two discriminators, ClickedLinkEvent and PurchasedEvent , as shown below. // ClickedLinkEvent var clickedEventSchema = new mongoose.

Does Mongoose model create collection?

Mongoose by default does not create any collection for the model in the database until any documents are created. The createCollection() method is used to create a collection explicitly.

Does Mongoose auto generate ID?

_id field is auto generated by Mongoose and gets attached to the Model, and at the time of saving/inserting the document into MongoDB, MongoDB will use that unique _id field which was generated by Mongoose.

What is autoIndex in mongoose?

Also note, that autoIndex does not mean that you create an index for every field. Instead, it means: Mongoose automatically calls createIndex for each defined index in your schema.


1 Answers

Since v4.11.13, mongoose model has model.discriminators which is an array of models, keyed on the name of the discriminator model.

In your case if you do console.log(User.discriminators) you will get:

{
  Client: {
    ....
  },
  Employee: {
  }
}

As far as I can see, this is not documented anywhere.

Line 158 in lib.helpers.model.discriminators.js is where this is created.

like image 176
LondonRob Avatar answered Oct 16 '22 15:10

LondonRob