<commit_before>
// Data type constants
STRING	= VARCHAR = Sequelize.STRING;
TEXT	= Sequelize.TEXT;
FLOAT	= Sequelize.FLOAT;
BOOLEAN	= Sequelize.BOOLEAN;
DATE	= Sequelize.DATE;
INTEGER	= Sequelize.INTEGER;

// include default methods
var defaultInstanceMethods = {
	
}
var defaultClassMethods = {
	
	/**
	 * Retrieve models whose keyAttributes match all of the specified attributes,
	 * otherwise, create them (use Domain.createAll() if necessary)
	 * 
	 * Rather than using the non-standard Sequelize format, the callback function 
	 * is in the form of function (err,results) {}
	 */
	findOrCreate: function (attributes,keyAttributes,callback) {
		var Domain = global[this.name];
		var criteria = {};
		_.each(keyAttributes,function(keyAttributeName,index){
			criteria[keyAttributeName] = attributes[keyAttributeName];
		});
		Domain.find({where:criteria}).success(function(model){
			if (model) {
				callback(null,model);
			}
			else {
				Domain.createAll(attributes).success(_u.cbok(callback)).error(callback);
			}
		}).error(callback);
	},
	
	/**
	 * Create a model with the specified attributes
	 * If an attribute is a list, create a model for each item in the list
	 * 
	 * Rather than using the non-standard Sequelize format, the callback function 
	 * is in the form of function (err,results) {}
	 */
	createAll: function (attributes,callback) {
		var Domain = global[this.name];
		
		// TODO: nested loop, foreach attribute, foreach item in list
		createOne(attributes,callback);
		
		// Create a single model
		function createOne (theseAttrs,c) {
			Domain.create(theseAttrs).success(_u.cbok(c)).error(c);
		}
	}
}


// Prototype Model object
Model = function () {}
	
// Deep clone
Model.clone = function(property) {
	var newProp = _.clone(property);
	newProp.validate = {};
	_.each(property.validate,function(val,key) {
		newProp.validate[key]=val;
	});
	return newProp;
}	<commit_msg>Sails.createAll() static method added.  Allows multiple domain objects to be created at once using Sequelize's query chainer.  Uses [] notation from Sequelize.findAll, and is called as needed by Sails.findAndCreate().<commit_after>
// Data type constants
STRING	= VARCHAR = Sequelize.STRING;
TEXT	= Sequelize.TEXT;
FLOAT	= Sequelize.FLOAT;
BOOLEAN	= Sequelize.BOOLEAN;
DATE	= Sequelize.DATE;
INTEGER	= Sequelize.INTEGER;

// include default methods
var defaultInstanceMethods = {
	
}
var defaultClassMethods = {
	
	/**
	 * Retrieve models whose keyAttributes match all of the specified attributes,
	 * otherwise, create them (use Domain.createAll() if necessary)
	 * 
	 * Rather than using the non-standard Sequelize format, the callback function 
	 * is in the form of function (err,results) {}
	 */
	findOrCreate: function (attributes,keyAttributes,callback) {
		var Domain = global[this.name];
		var criteria = {};
		_.each(keyAttributes,function(keyAttributeName,index){
			criteria[keyAttributeName] = attributes[keyAttributeName];
		});
		Domain.find({
			where:criteria
		}).success(function(model){
			if (model) {
				callback(null,model);
			}
			else {
				if (_.any(attributes,function(val,key){return _.isArray(val);})) {
					Domain.createAll(attributes, callback);
				}
				else {
					Domain.create(attributes).success(_u.cbok(callback));
				}
			}
		}).error(callback);
	},
	
	/**
	 * Create a model with the specified attributes
	 * If an attribute is a list, create a model for each item in the list
	 * 
	 * Rather than using the non-standard Sequelize format, the callback function 
	 * is in the form of function (err,results) {}
	 */
	createAll: function (attributes,callback) {
		var Domain = global[this.name];
		
		var models = [];
		linearize(attributes,models);
		
		// Reduce into a set of attributes with only
		// one value per-attribute
		function linearize( attrs, attrSets ) {
			// Base case:
			// If there is one value per attribute, add this permutation to the result list
			if (_.all(attrs,function(v,k){
				return !_.isArray(v);
			})) {
				attrSets.push(attrs);
			}
			
			// Otherwise, recursively call this function on each of the value options
			else {
				_.each(attrs,function(attrVal,attrName) {
					
					// If this attribute is a list
					if (_.isArray(attrVal)) {	
						
						// recursively call linearize() with each value from the list
						_.each(attrVal,function(val,index) {
							var model = _.clone(attrs);
							model[attrName] = val;
							linearize(model,attrSets);
						});
					} 
					else {
						
					}
				});
			}
		}
		
		console.log("\n\n***** CREATEALL: ",this.name,"\n",models,"\n\n");
		
		
		var chainer = new Sequelize.Utils.QueryChainer;
		_.each(models,function(m,index) {
			chainer.add(Domain.create(m));
		});
		
		chainer.run().success(_u.cbok(callback)).error(callback);

//		createOne(attributes,callback);
//		
//		// Create a single model
//		function createOne (theseAttrs,c) {
//			Domain.create(theseAttrs).success(_u.cbok(c)).error(c);
//		}
	}
}


// Prototype Model object
Model = function () {}
	
// Deep clone
Model.clone = function(property) {
	var newProp = _.clone(property);
	newProp.validate = {};
	_.each(property.validate,function(val,key) {
		newProp.validate[key]=val;
	});
	return newProp;
}	
{'fraction_non_alphanumeric': 0.1264147323997698, 'fraction_numerical': 0.0, 'mean_word_length': 3.5576923076923075, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1008783', 'n_tokens_mistral': 1855, 'n_tokens_neox': 1585, 'n_words': 425}