I am trying to seed a MongoDB database with a one-to-many relationship using objectID's. Something like the following:
var postSchema = mongoose.Schema({
name: {type: String, required: true, unique: true},
description: {type: String},
userlikes: [{type: mongoose.Schema.Types.ObjectId, ref: 'Users'}]
});
A synchronous implementation of this would look something like this (pseudocode):
openConnection()
cleardb()
users = createUsers()
post = createPost(users)
Using Node I need to nest each of these calls which gets messy and makes re-use of objects difficult (I may want to create further dependent objects after). I have had a look at async but that doesn't appear to help too much in this case - I could use async.waterfall but that would give me something similar to the nested solution only flattened into an array.
When seeding related data it obviously helps if you can create data objects and then assume they exist for building subsequent data structures. Obviously this doesn't really happen with an asynchronous approach. Am I missing the point here?
I would assume that with node used so extensively for API's this would be a common challenge for setting up data for testing but I can't find an elegant solution documented anywhere.
Can anyone point me to an example implementation of how this problem is solved in a node setup?