import _ from 'lodash';


export const unexistedUID = '00000000-0000-0000-C000-000000000046';

export function initObject(classDef, attrs, id, params) {
  return new classDef({ ...attrs, id, ...params });
}

export function prepareModelPayload(payload, namesMapping, valuesMapping) {
  const result = {};
  const keys = _.intersection(Object.keys(payload), Object.keys(namesMapping));

  for (const key of keys) {
    const mappedKey = namesMapping[key];
    const mappedVal = valuesMapping[key] ? valuesMapping[key](payload[key]) : payload[key];
    result[mappedKey] = mappedVal;
  }

  return result;
}

@@ -17,16 +17,89 @@
 odash';%0A
+import pgFormat from 'pg-format';%0A%0Aimport %7B List %7D from '../open-lists';%0A
 %0A%0Aexport
@@ -668,12 +668,1178 @@
 n result;%0A%7D%0A
+%0A// These helpers allow to use the IN operator with the empty list of values.%0A// 'IN %3Cempty list%3E' always returns 'false' and 'NOT IN %3Cempty list%3E' always returns 'true'.%0A// We don't escape 'field' here because pgFormat escaping doesn't work properly with dot-joined%0A// identifiers (as in 'table.field').%0A%0A// export const sqlIn = (field, list) =%3E list.length === 0 ? 'false' : pgFormat(%60$%7Bfield%7D in (%25L)%60, list);%0A// export const sqlNotIn = (field, list) =%3E list.length === 0 ? 'true' : pgFormat(%60$%7Bfield%7D not in (%25L)%60, list);%0A%0Aexport function sqlIn(field, list) %7B%0A  list = List.from(list);%0A%0A  if (list.isEmpty()) %7B%0A    return 'false';%0A  %7D else if (list.isEverything()) %7B%0A    return 'true';%0A  %7D%0A%0A  return pgFormat(%60$%7Bfield%7D $%7Blist.inclusive ? 'in' : 'not in'%7D (%25L)%60, list.items);%0A%7D%0A%0Aexport function sqlNotIn(field, list) %7B%0A  return sqlIn(field, List.difference(List.everything(), list));%0A%7D%0A%0Aexport function sqlIntarrayIn(field, list) %7B%0A  list = new List(list);%0A%0A  if (list.isEmpty()) %7B%0A    return 'false';%0A  %7D else if (list.isEverything()) %7B%0A    return 'true';%0A  %7D%0A%0A  return pgFormat(%60($%7Blist.inclusive ? '' : 'not '%7D$%7Bfield%7D && %25L)%60, %60%7B$%7Blist.items.join(',')%7D%7D%60);%0A%7D%0A

Add helpers functions to generate SQL for 'field [NOT] IN somelist'