<commit_before>/*!
 * Bookshelf-Returning
 *
 * Copyright 2016-2017 <NAME>
 * Released under the MIT license
 * https://github.com/joshswan/bookshelf-returning/blob/master/LICENSE
 */

const Promise = require('bluebird');
const assign = require('lodash/assign');
const extend = require('lodash/extend');

module.exports = (bookshelf) => {
  const BaseModel = bookshelf.Model;

  bookshelf.Model = BaseModel.extend({
    initialize() {
      this.on('saving', (model, attrs, options) => {
        if (options.returning) {
          options.query.returning(options.returning);
        }
      });

      this.on('saved', (model, resp, options) => {
        if (options.returning && typeof resp[0] === 'object') {
          // Assign returned values to attributes
          assign(this.attributes, this.parse(resp[0]));

          // Update model's ID
          this.id = this.attributes.id || null;
        }
      });
    },
    // Override sync insert method to respect options.returning
    sync(...args) {
      const sync = BaseModel.prototype.sync.apply(this, args);

      sync.insert = Promise.method(function syncInsert() {
        const syncing = this.syncing;
        const attributes = syncing.format(extend(Object.create(null), syncing.attributes));

        return this.query.insert(attributes, this.options.returning || syncing.idAttribute);
      });

      return sync;
    },
  });
};
<commit_msg>Throw NoRowsUpdatedError for update queries with require option and nothing returned<commit_after>/*!
 * Bookshelf-Returning
 *
 * Copyright 2016-2017 <NAME>
 * Released under the MIT license
 * https://github.com/joshswan/bookshelf-returning/blob/master/LICENSE
 */

const Promise = require('bluebird');
const assign = require('lodash/assign');
const extend = require('lodash/extend');

module.exports = (bookshelf) => {
  const BaseModel = bookshelf.Model;

  bookshelf.Model = BaseModel.extend({
    initialize() {
      this.on('saving', (model, attrs, options) => {
        if (options.returning) {
          options.query.returning(options.returning);
        }
      });

      this.on('saved', (model, resp, options) => {
        if (options.returning) {
          // Throw NoRowsUpdatedError for updates with require option and nothing returned
          if (options.method === 'update' && options.require !== false && !resp.length) {
            throw new this.constructor.NoRowsUpdatedError('No Rows Updated');
          }

          if (typeof resp[0] === 'object') {
            // Assign returned values to attributes
            assign(this.attributes, this.parse(resp[0]));

            // Update model's ID
            this.id = this.attributes.id || null;
          }
        }
      });
    },
    // Override sync insert method to respect options.returning
    sync(...args) {
      const sync = BaseModel.prototype.sync.apply(this, args);

      sync.insert = Promise.method(function syncInsert() {
        const syncing = this.syncing;
        const attributes = syncing.format(extend(Object.create(null), syncing.attributes));

        return this.query.insert(attributes, this.options.returning || syncing.idAttribute);
      });

      return sync;
    },
  });
};

{'fraction_non_alphanumeric': 0.13768342179206994, 'fraction_numerical': 0.0062441461130190445, 'mean_word_length': 2.661714285714286, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '2413789', 'n_tokens_mistral': 930, 'n_tokens_neox': 886, 'n_words': 202}