<commit_before>const debug = require('debug')('feathers-knex-transaction');

const start = (options) => {
  return hook => new Promise(resolve => {
    if (!hook.service.Model || typeof hook.service.Model.transaction !== 'function') {
      return resolve(hook);
    }

    if (hook.params.transaction) {
      hook.params.transaction.count += 1;
      return resolve(hook);
    }

    hook.service.Model.transaction(trx => {
      const id = Date.now();
      hook.params.transaction = {
        trx,
        id,
        count: 0
      };
      debug('started a new transaction %s', id);
      return resolve(hook);
    });
  });
};

const end = (options) => {
  return hook => {
    if (hook.params.transaction) {
      const { trx, id, count } = hook.params.transaction;

      if (count > 0) {
        hook.params.transaction.count -= 1;
        return Promise.resolve(hook);
      }

      hook.params.transaction = undefined;

      return trx.commit()
        .then(() => debug('finished transaction %s with success', id))
        .then(hook);
    }
    return hook;
  };
};

const rollback = (options) => {
  return hook => {
    if (hook.params.transaction) {
      const { trx, id } = hook.params.transaction;
      return trx.rollback()
        .then(() => debug('rolling back transaction %s', id))
        .then(hook);
    }
    return hook;
  };
};

module.exports = {
  transaction: {
    start,
    end,
    rollback
  }
};<commit_msg>Pass function to then() (#137)<commit_after>const debug = require('debug')('feathers-knex-transaction');

const start = (options) => {
  return hook => new Promise(resolve => {
    if (!hook.service.Model || typeof hook.service.Model.transaction !== 'function') {
      return resolve(hook);
    }

    if (hook.params.transaction) {
      hook.params.transaction.count += 1;
      return resolve(hook);
    }

    hook.service.Model.transaction(trx => {
      const id = Date.now();
      hook.params.transaction = {
        trx,
        id,
        count: 0
      };
      debug('started a new transaction %s', id);
      return resolve(hook);
    });
  });
};

const end = (options) => {
  return hook => {
    if (hook.params.transaction) {
      const { trx, id, count } = hook.params.transaction;

      if (count > 0) {
        hook.params.transaction.count -= 1;
        return Promise.resolve(hook);
      }

      hook.params.transaction = undefined;

      return trx.commit()
        .then(() => debug('finished transaction %s with success', id))
        .then(() => hook);
    }
    return hook;
  };
};

const rollback = (options) => {
  return hook => {
    if (hook.params.transaction) {
      const { trx, id } = hook.params.transaction;
      return trx.rollback()
        .then(() => debug('rolling back transaction %s', id))
        .then(() => hook);
    }
    return hook;
  };
};

module.exports = {
  transaction: {
    start,
    end,
    rollback
  }
};
{'fraction_non_alphanumeric': 0.15807442813246841, 'fraction_numerical': 0.0037555479685899623, 'mean_word_length': 2.2591768631813127, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '4171917', 'n_tokens_mistral': 911, 'n_tokens_neox': 888, 'n_words': 206}