2

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?

shennan
  • 10,798
  • 5
  • 44
  • 79
  • 1
    You may go schema-less. Even mongoose-less if you decide to use node-mongodb-native. You will find it easier to use Mongoose for your own application logic, and the mongodb driver mongoose comes with for the dynamic CMS. I am not sure how much freedom you want to give to your users since you want them to define their schemas though... – randunel Aug 05 '13 at 14:34
  • @randunel Thanks for your comment. I hope I can sustain your interest in answering by giving a bit more to chew on. It sounds like your response to question `1)` is essentially no. And perhaps question `2)` 's response is that I should leverage the driver from mongoose to do this instead. Your comment has made me focus on what I want a little more. It's made me realise that I'm essentially after a two-tiered admin panel: one for the developer to create models and another for the client to utilise them (populate them with content). A very small working example will warrant an accepted answer :) – shennan Aug 05 '13 at 15:19
  • @randunel Obviously I'm not expecting a working CMS example. Just a brief mongoose-less (or mongoose driver-only) example. Perhaps a preference of which would be better and why... – shennan Aug 05 '13 at 15:35
  • 1
    I understand what you mean, but it's a bit more complex than an example. There are several possibilities in designing such an application, and it depends on what you want it to do, in the end. If it is something trivial, you can simply add an attribute to Mongoose like Schema.Types.Mixed and add everything the user defines as a JSON in there. You could have a separate collection for each user, or all-in-one. Database design is a very important step when building an application, and the ones involved know best what they need :) – randunel Aug 05 '13 at 17:00
  • @randunel Fair enough. Thanks for your input. – shennan Aug 05 '13 at 17:28

0 Answers0