Skip to content

Instantly share code, notes, and snippets.

@beeekind
Created September 23, 2015 23:36
Show Gist options
  • Save beeekind/d9e10811b8381912723c to your computer and use it in GitHub Desktop.
Save beeekind/d9e10811b8381912723c to your computer and use it in GitHub Desktop.
Sexy isomorphic nodeJS function for instantiating any model with explicit fields (to prevent mass injection attacks) and a custom association with our User model. version 3
"use strict";
/** @module createInstanceWithFieldsAndConnection */
var capitalizeFirstLetter = require("../utils/capitalizeFirstLetter");
var models = require("../wrapper").getSingleton();
var async = require("async");
/**
* Isomorphic action for persisting an object or series of objects of model {{name}}, only accepting explicit fields, and
* forming a connection between the new instance and the user who created it. Finally return using a
* callback all the created models and model connections.
*
* @param params {object|Array} - object or objects to be instantiated and persisted
* @param userInstance {object} - user model instance
* @param name {string} - name of the model being created
* @param {Array} [fields] - fields to be accepted when creating the models (defaults to allowing all fields)
* @param callback {function} - callback function, returning the newly created instance and its connection to the user
*/
module.exports = function(params, userInstance, name, fields, callback){
// function parameter validations, including making fields param entirely optional
if (!callback || typeof callback !== "function") { throw new Error("Missing callback parameter"); }
if (!params || typeof params !== "object") { throw new Error("Invalid params parameter"); }
if (!userInstance || typeof userInstance !== "object") { throw new Error("Invalid userInstance parameter"); }
if (!name || typeof name !== "string"){ throw new Error("Invalid name parameter"); }
if (typeof fields === "function") { callback = fields; fields = null; }
name = capitalizeFirstLetter(name); // ensure its uppercased to match our model definitions
if (!(name in models)) { throw new Error("Model " + name + " is not defined"); }
var modelKey = name + "Id"; // the key we will later use to build a connection
// decide if we want to create one or create multiple
var isMultiple = false;
if (Array.isArray(params)) { isMultiple = true; }
else { params = [params]; } // if singular, convert to array format for bulkCreate call
// build the first query
var options = {};
if (fields && Array.isArray(fields)){ options.fields = fields; }
var modelInstances;
async.series({
modelInstances: function(done){
models[name].bulkCreate(params, options)
.then(function(results){
modelInstances = results;
done(null, results);
})
.catch(function(err){
done(err);
});
},
connections: function(done){
var connections = [];
// create a connection between the user and the single instance
if (!isMultiple) {
var params = {};
params["UserId"] = userInstance.get('id');
params[modelKey] = modelInstances[0].get('id');
params["permissions"] = 70;
connections.push(params);
}
// create a connection between the user and each instance
else {
modelInstances.forEach(function(modelInstance){
var params = {};
params["UserId"] = userInstance.get('id');
params[modelKey] = modelInstance.get('id');
params["permissions"] = 70;
connections.push(params);
});
}
models.Connection.bulkCreate(connections)
.then(function(connections){
done(null, connections);
})
.catch(function(err){
done(err);
});
}
}, callback);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment