<commit_before>var _ = require('lodash');
var async = require('async');

module.exports = function(Meal) {
  /**
   * Create a new meal along with associated relations
   * @param {object} fullMeal The meal object with an array of objects representing the mealfoods
   * @param {Function(Error, object)} callback
   */

  Meal.fullMeal = function(fullMeal, callback) {
    var mealToCreate = _.omit(fullMeal, 'mealfoods');
    var mealfoodsToCreate = fullMeal.mealfoods;

    var models = Meal.app.models;

    Meal.create(mealToCreate, function(err, meal) {
      if (err) {
        return callback(err);
      }

      async.each(mealfoodsToCreate, function(mealfoodToCreate, cb) {
        mealfoodToCreate.mealId = meal.id;
        models.mealfood.create(mealfoodToCreate, cb);
      }, function done(err) {
        if (err) {
          return callback(err);
        }
        callback(null, meal);
      });
    });
  };
};<commit_msg>Update full-meal endpoint to use a transaction<commit_after>var _ = require('lodash');
var async = require('async');

module.exports = function(Meal) {
  /**
   * Create a new meal along with associated relations
   * @param {object} fullMeal The meal object with an array of objects representing the mealfoods
   * @param {Function(Error, object)} callback
   */

  Meal.fullMeal = function(fullMeal, callback) {
    var mealToCreate = _.omit(fullMeal, 'mealfoods');
    var mealfoodsToCreate = fullMeal.mealfoods;

    var models = Meal.app.models;

    Meal.beginTransaction({isolationLevel: Meal.Transaction.READ_COMMITTED}, function(err, tx) {
      if (err) return callback(err);

      var options = {transaction: tx};

      Meal.create(mealToCreate, options, function(err, meal) {
        if (err) return callback(err);

        async.each(mealfoodsToCreate, function(mealfoodToCreate, cb) {
          mealfoodToCreate.mealId = meal.id;
          models.mealfood.create(mealfoodToCreate, options, cb);
        }, function done(err) {
          if (err) return callback(err);

          tx.commit(function(err) {
            if (err) return callback(err);

            callback(null, meal);
          });
        });
      });
    });
  };
};
{'fraction_non_alphanumeric': 0.12197350388305161, 'fraction_numerical': 0.0, 'mean_word_length': 2.924731182795699, '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': '2932272', 'n_tokens_mistral': 690, 'n_tokens_neox': 673, 'n_words': 161}