src/databaseUtil.js
/**
 * Given a set of name/value pairs, this function will generate a list of
 * `WHERE ... LIKE` clauses.
 *
 * For each `columnName` (key) / `prefix` (value) pair in `criteria`, a
 * string `cond` is generated such that `cond` is a `WHERE ... LIKE` clause
 * that attempts to match rows where `prefix` is the prefix of a string in
 * the `columnName` column. The function returns a list of all conditions.
 *
 * Please note that JavaScript-style names should be used for keys in `criteria`.
 * They will be automatically converted to SQL-style names.
 *
 * Example: If `criteria` is
 * ```
 * {firstName: 'Ja', lastName: 'Br'}
 * ```
 * and `args` is
 * ```
 * []
 * ```
 * the output should be
 * ```
 * {
 *    conditions: [
 *      "\"first_name\" LIKE $1 || '%'",
 *      "\"last_name\" LIKE $2 || '%'"
 *    ],
 *    args: [
 *      'Ja',
 *      'Br'
 *    ]
 * }
 * ```
 *
 * @memberof module:databaseUtil
 *
 * @param {Object} criteria
 *    An object where each key is a column name given in JavaScript-style
 *    and each value is a prefix of a string to match on that column.
 *
 * @param {Array<Any>} args
 *    A parameterized query arguments array to build upon. This array will
 *    not be mutated. A copy of the array with the new elements added will
 *    be returned in the return object.
 *
 * @returns {Object}
 * - `conditions` - An array of strings where each element is a condition.
 * - `args` - The new array of arguments. Includes the elements from the parameter `args`.
 */
Given a set of name/value pairs, this function will generate a list of
`WHERE ... LIKE` clauses.



Please note that JavaScript-style names should be used for keys in `criteria`.
They will be automatically converted to SQL-style names.



@memberof module:databaseUtil

@param {Object} criteria
An object where each key is a column name given in JavaScript-style
and each value is a prefix of a string to match on that column.

@param {Array} args
A parameterized query arguments array to build upon. This array will
not be mutated. A copy of the array with the new elements added will
be returned in the return object.

@returns {Object}
- `conditions` - An array of strings where each element is a condition.
- `args` - The new array of arguments. Includes the elements from the parameter `args`.
Given a set of name / value pairs this function will generate a list of ` WHERE ... LIKE ` clauses . Please note that JavaScript - style names should be used for keys in ` criteria ` . They will be automatically converted to SQL - style names . @memberof module : databaseUtil @param { Object } criteria An object where each key is a column name given in JavaScript - style and each value is a prefix of a string to match on that column . @param { Array } args A parameterized query arguments array to build upon . This array will not be mutated . A copy of the array with the new elements added will be returned in the return object . @returns { Object } - ` conditions ` - An array of strings where each element is a condition . - ` args ` - The new array of arguments . Includes the elements from the parameter ` args ` .
function genWhereLikePrefixConditions(criteria, args) {
  var sqlField = '',
      argNumber,
      condStr = '',
      conditions = [];
  if (!args)
    args = [];
  else
    args = _.cloneDeep(args);
  argNumber = args.length;
  _.forEach(criteria, function(value, field) {
    if (_.isString(value)) {        
      sqlField = jsName2SqlName(field);
      condStr = sprintf("\"%s\" LIKE $%i || '%%'", sqlField, ++argNumber);
      conditions.push(condStr);
      args.push(value);
    }
  });
  return {
    conditions: conditions,
    args: args
  };
}
function genWhereLikePrefixConditions ( criteria , args ) { var sqlField = '' , argNumber , condStr = '' , conditions = [ ] ; if ( ! args ) args = [ ] ; else args = _ . cloneDeep ( args ) ; argNumber = args . length ; _ . forEach ( criteria , function ( value , field ) { if ( _ . isString ( value ) ) { sqlField = jsName2SqlName ( field ) ; condStr = sprintf ( "\"%s\" LIKE $%i || '%%'" , sqlField , ++ argNumber ) ; conditions . push ( condStr ) ; args . push ( value ) ; } } ) ; return { conditions : conditions , args : args } ; }
Given a set of name/value pairs, this function will generate a list of
 `WHERE ... LIKE` clauses.
Given a set of name / value pairs this function will generate a list of ` WHERE ... LIKE ` clauses .