How do we create a Schema at runtime in Mongoose O

2019-04-16 02:10发布

问题:

It's my understanding that Mongoose Schema's are defined at some point in the initiating stages of a Node app. These will obviously be hard coded, like so:

var SomeSchema = new Schema({

    some_string:String,
    some_number:Number

});

If I'm creating a CMS whereby the administrator (via a pretty web GUI) could add their own models, with their own pre-defined types (sometimes custom types like e-mail) the app would need to create these Schema's on the fly. Sometimes adding to existing Schemas (presumably using Mongoose's add() method, and sometimes creating new Schemas all-together.

I've done some initial searching but it seems not many people have this requirement. Is this functionality, therefore, a gross misunderstanding of what Mongoose was built for? Was Mongoose built as a closed ODM without any real ability to leverage runtime Schema creation?

Additionally, if I was to go ahead and force this type of functionality, would the prospect of dynamic Schema creation be a potential bottleneck for the speed of my application?

To better summarise my question so as not to attract accusations of debate:

1) Is there a way to create Mongoose Schema's on the fly?

2) Is this programmatically expensive, and is there a better way of achieving my goals of delegating Model creation to a GUI within the administration panel of my CMS?