How do we create a Schema at runtime in Mongoose ODM?

给你一囗甜甜゛ 提交于 2019-12-06 15:02:45

问题


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?

来源:https://stackoverflow.com/questions/18060709/how-do-we-create-a-schema-at-runtime-in-mongoose-odm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!