<commit_before>var nohm = require('nohm').Nohm;

function loadModelError(msg, code){
  this.name = 'loadModelError';
  if (typeof(msg) === 'string') {
    this.message = msg;
  } else {
    this.data = msg;
    this.message = 'custom';
  }
  this.code = code || 500;
  Error.call(this, msg);
}

loadModelError.prototype.__proto__ = Error.prototype;

module.exports = function loadModel(model_name, param_name, allow_string_search) {
  param_name = param_name || 'id';
  return function (req, res, next){
    var id = req.param(param_name);
    var model = nohm.factory(model_name);
    if ( ! req.loaded) {
      req.loaded = {};
    }
    var _load = function (id) {
      model.load(id, function (err) {
        if (err) {
          next(new loadModelError('not_found', 404));
        } else {
          req.loaded[model_name] = model;
          next();
        }
      });
    };
    
    if (id && !!id.match(/^[\d]*$/)) {
      _load(id);
    } else if (id && allow_string_search) {
      model.find({name:id}, function (err, ids) {
        if (err || ids.length === 0) {
          next(new loadModelError('not_found', 404));
        } else if (ids.length > 1) {
          next(new loadModelError('multiple_matches', 400));
        } else {
          _load(ids[0]);
        }
      });
    } else {
      next(new loadModelError('needs_id_param', 400));
    }<commit_msg>Allow loadModel to work with non-integer ids<commit_after>var nohm = require('nohm').Nohm;

function loadModelError(msg, code){
  this.name = 'loadModelError';
  if (typeof(msg) === 'string') {
    this.message = msg;
  } else {
    this.data = msg;
    this.message = 'custom';
  }
  this.code = code || 500;
  Error.call(this, msg);
}

loadModelError.prototype.__proto__ = Error.prototype;

module.exports = function loadModel(model_name, param_name, do_string_search) {
  param_name = param_name || 'id';
  return function (req, res, next){
    var id = req.param(param_name);
    var model = nohm.factory(model_name);
    if ( ! req.loaded) {
      req.loaded = {};
    }
    var _load = function (id) {
      model.load(id, function (err) {
        if (err) {
          next(new loadModelError('not_found', 404));
        } else {
          req.loaded[model_name] = model;
          next();
        }
      });
    };
    
    if (id && !do_string_search) {
      _load(id);
    } else if (id && do_string_search) {
      model.find({name:id}, function (err, ids) {
        if (err || ids.length === 0) {
          next(new loadModelError('not_found', 404));
        } else if (ids.length > 1) {
          next(new loadModelError('multiple_matches', 400));
        } else {
          _load(ids[0]);
        }
      });
    } else {
      next(new loadModelError('needs_id_param', 400));
    }
{'fraction_non_alphanumeric': 0.1583122971510999, 'fraction_numerical': 0.01298232960692391, 'mean_word_length': 2.39119804400978, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1507779', 'n_tokens_mistral': 963, 'n_tokens_neox': 924, 'n_words': 196}