How to create dynamic Schemas...

3,171 views
Skip to first unread message

dkimbell13

unread,
Nov 19, 2011, 10:43:11 PM11/19/11
to Mongoose Node.JS ORM
I am trying to pass a JSON representation of a Schema via AJAX to a
Express server which then builds the schema and inserts an JSON
representation of an instance of that Schema into MongoDB. Lost yet?

Pretty much I am trying to create a REST interface using mongoose as
middleware, but using the same Schema, Model, Instance structure as
mongoose on the Web Side to CRUD data into a MongoDB.

So...
Given this simple JSON representation of a Schema...

"Schema":[{
"Name":"Documents",
"data":[
{"name":"author","type":"String"},
{"name":"title","type":"String"},
{"name":"body","type":"String"}
]
}]

how would I create a Schema, without using eval()...

I can create the Schema's with the correct names but the object passed
to Schema function has to have key values and SchemaTypes, not key
values and string representations of SchemaTypes...

So my question, if your not all the way confused is...

How do I build and object similar to

{ author:String,
title :String,
body :String}

using the JSON data above...


{ author:"String",
title :"String",
body :"String"}

DOES NOT WORK

String is not the same as "String", and I have not ideal how to
instantiate a String SchemaType using the string "String"

Thanks for your help,

Dom

Tim Oxley

unread,
Nov 20, 2011, 8:01:09 PM11/20/11
to mongoo...@googlegroups.com
So simply map "String" to the appropriate class after the initial JSON import.
eg something like:


var import = function(obj) {
  switch (obj.type) {
    case "String":
      obj.type = String
      break
    case "Number"
      obj.type = Number
      break
    /// etc
  }
  return obj
}

…and then open source it.

dkimbell13

unread,
Nov 20, 2011, 8:21:53 PM11/20/11
to Mongoose Node.JS ORM
Yeah, I thought of that, I just hoped there would be a more elegant
way...

something like obj.type = mongoose.Types["String"]...

or something to that effect...

and yes, this will be open sourced...

Dom

George Antoniadis

unread,
Sep 21, 2012, 2:48:20 PM9/21/12
to mongoo...@googlegroups.com, dkimb...@gmail.com
Where did you end up with this? :)
Had any progress?

Nag Kiran

unread,
Apr 7, 2013, 3:04:00 PM4/7/13
to mongoo...@googlegroups.com, dkimb...@gmail.com
Have you made any progress on this ? I am also having the same problem :)

Justin Spears

unread,
Apr 8, 2013, 12:32:36 PM4/8/13
to mongoo...@googlegroups.com
You can take a look how I did it here.

I believe you can get to the types, but they are natural javascript types, unless well
their not, then you can look them up in the mongoose types context.


--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages