How should I create refs on the go?

13 views
Skip to first unread message

i...@bodokaiser.io

unread,
Mar 25, 2014, 3:04:16 PM3/25/14
to mongoo...@googlegroups.com
I have a schema called "material":

  var schema = new mongoose.Schema({
    name
: String,
    supplier
: {
     
ref: 'Supplier',
      type
: mongoose.Schema.ObjectId
   
},
    categories
: [
     
String
   
]
 
});

A client does a POST to a REST API:

{
 
"name": "Foobar",
 
"supplier": {
   
"name": "The Company",
   
"email": "t...@company.com"
 
},
 
"categories": [
   
"product"
 
]
}

Unfortunately the provided "supplier" document does not exist. So it should be created on save of the Material Model.

However seems for me mongoose only supports saving existing references.

So what to do?
  1. Add save middleware to create Supplier instance
  2. Change REST API
  3. Anything else?
What is the recommend approach in this case?

Best,
Bo
Reply all
Reply to author
Forward
0 new messages