When I get an API call, i have to install to about 4,5 tables, since an entry has child items which in turn have child items. Since I have to pass data from the first insert to the second (using the parent ids) I end up having too many functions (like function insertTable1, insertTable1Kids, insertTable1KidsKids) so on..
Is this good design? It's bothering me that I'm using so many functions.. Is there a better way to handle this?
Use closures and the async
module.
npm install async
Your code would look something like this...
var async = require('async');
exports.your_route = function(req, res) {
var parent_id = req.query.parent_id;
var calls = [];
calls.push(function(callback) {
// insertTable1 - use parent_id
callback(false);
});
calls.push(function(callback) {
// insertTable1Kids - use parent_id
callback(false);
});
calls.push(function(callback) {
// insertTable1KidsKids - use parent_id
callback(false);
});
calls.push(function(callback) {
// return response
res.send("tabels for parent ID: "+parent_id+" created);
callback(false);
});
async.series(calls);
};