简体   繁体   中英

MongoDB/mongoose | Extending Schema functionality

I'am trying to give a Schema some custom fields, that describes its behaviour. I want to use it for some authentication, by giving a minumum "level", what someone needs to read its value. The plan is to declare it just like making a field required, so I would write "minAuth: someLevel" in the field declaration.

I already tried this and it gives no error, but I cant really find the value anywhere.

Does somebody know, where I can access a Schemas fields properties?

So if your schema looks something like this:

YourSchema = new mongoose.Schema({
  name: {type: 'String', minLevel: 1}
})
module.exports = mongoose.model('You', YourSchema);

Then you can import it, and use the schema variables like

const You = require("/path/to/schema/you")
// Get all fields from the schema
let path = You.schema.paths
  for ( const [index, PropertyName] of path) {
    const PropertyDescription = schema.paths[index]
    let Level = PropertyDescription.options.minLevel
  }


// Or you can go directly to it if you know the index of it

You.schema.paths[TheIndexYouKnow].options.NameOfYourCustomVariable

I hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM