<commit_before>'use strict';

const { replaceIdByPrimaryKey } = require('../utils/primary-key');
const { executeBeforeHook, executeAfterHook } = require('../utils/hooks');

module.exports = function createQuery(opts) {
  const { model, connectorQuery } = opts;

  return {
    get model() {
      return model;
    },

    get orm() {
      return model.orm;
    },

    get primaryKey() {
      return model.primaryKey;
    },

    get associations() {
      return model.associations;
    },

    /**
     * Run custom database logic
     */
    custom(mapping) {
      if (typeof mapping === 'function') {
        return mapping.bind(this, { model: this.model });
      }

      if (!mapping[this.orm]) {
        throw new Error(`Missing mapping for orm ${this.orm}`);
      }

      if (typeof mapping[this.orm] !== 'function') {
        throw new Error(`Custom queries must be functions received ${typeof mapping[this.orm]}`);
      }

      return mapping[this.model.orm].call(this, { model: this.model });
    },

    create: wrapQuery({ hook: 'create', model, connectorQuery }),
    update: wrapQuery({ hook: 'update', model, connectorQuery }),
    delete: wrapQuery({ hook: 'delete', model, connectorQuery }),
    find: wrapQuery({ hook: 'find', model, connectorQuery }),
    findOne: wrapQuery({ hook: 'findOne', model, connectorQuery }),
    count: wrapQuery({ hook: 'count', model, connectorQuery }),
    search: wrapQuery({ hook: 'search', model, connectorQuery }),
    countSearch: wrapQuery({ hook: 'countSearch', model, connectorQuery }),
  };
};

// wraps a connectorQuery call with:
// - param substitution
// - lifecycle hooks
const wrapQuery = ({ hook, model, connectorQuery }) => async (params, ...rest) => {
  // substite id for primaryKey value in params
  const newParams = replaceIdByPrimaryKey(params, model);

  // execute before hook
  await executeBeforeHook(hook, model, newParams, ...rest);

  // execute query
  const result = await connectorQuery[hook](newParams, ...rest);

  // execute after hook
  await executeAfterHook(hook, model, result);

  // return result
  return result;
};<commit_msg>Add arguments to after hook<commit_after>'use strict';

const { replaceIdByPrimaryKey } = require('../utils/primary-key');
const { executeBeforeHook, executeAfterHook } = require('../utils/hooks');

module.exports = function createQuery(opts) {
  const { model, connectorQuery } = opts;

  return {
    get model() {
      return model;
    },

    get orm() {
      return model.orm;
    },

    get primaryKey() {
      return model.primaryKey;
    },

    get associations() {
      return model.associations;
    },

    /**
     * Run custom database logic
     */
    custom(mapping) {
      if (typeof mapping === 'function') {
        return mapping.bind(this, { model: this.model });
      }

      if (!mapping[this.orm]) {
        throw new Error(`Missing mapping for orm ${this.orm}`);
      }

      if (typeof mapping[this.orm] !== 'function') {
        throw new Error(`Custom queries must be functions received ${typeof mapping[this.orm]}`);
      }

      return mapping[this.model.orm].call(this, { model: this.model });
    },

    create: wrapQuery({ hook: 'create', model, connectorQuery }),
    update: wrapQuery({ hook: 'update', model, connectorQuery }),
    delete: wrapQuery({ hook: 'delete', model, connectorQuery }),
    find: wrapQuery({ hook: 'find', model, connectorQuery }),
    findOne: wrapQuery({ hook: 'findOne', model, connectorQuery }),
    count: wrapQuery({ hook: 'count', model, connectorQuery }),
    search: wrapQuery({ hook: 'search', model, connectorQuery }),
    countSearch: wrapQuery({ hook: 'countSearch', model, connectorQuery }),
  };
};

// wraps a connectorQuery call with:
// - param substitution
// - lifecycle hooks
const wrapQuery = ({ hook, model, connectorQuery }) => async (params, ...rest) => {
  // substite id for primaryKey value in params
  const newParams = replaceIdByPrimaryKey(params, model);
  const queryArguments = [newParams, ...rest];

  // execute before hook
  await executeBeforeHook(hook, model, ...queryArguments);

  // execute query
  const result = await connectorQuery[hook](...queryArguments);

  // execute after hook with result and arguments
  await executeAfterHook(hook, model, result, ...queryArguments);

  // return result
  return result;
};
{'fraction_non_alphanumeric': 0.1457378551787351, 'fraction_numerical': 0.0, 'mean_word_length': 3.1930835734870318, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3221662', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1217, 'n_words': 361}