function resolveTableMappings()

in database-jones/Adapter/api/UserContext.js [311:407]


function resolveTableMappings(userContext, factory, session, tableMappings, onMappingsResolvedCallback) {
  var mappings = [];
  var mappingBeingResolved = 0;
  var currentTableMapping, currentTableMappingType, currentTableMappingName, currentTableName;
  var message;

  function resolveTableMappingsOnTableHandler(err, tableHandler) {
    if(udebug.is_detail()) {
      udebug.log('UserContext.resolveTableMappinsgOnTableHandler got err', err, 'tableHandler', tableHandler,
        '\nfor', mappingBeingResolved + 1, 'of', mappings.length, '\n', mappings[mappingBeingResolved]);
    }
    if (err) {
      userContext.firstError = userContext.firstError || err;
      // what were we resolving?
      currentTableMapping = mappings[mappingBeingResolved];
      currentTableMappingType = typeof currentTableMapping;
      currentTableName = currentTableMapping;
      message = currentTableName;
      if (currentTableMappingType === 'function') {
        currentTableMappingName = currentTableMapping.prototype.constructor.name;
        if (currentTableMapping.prototype.jones !== undefined) {
          currentTableName = currentTableMapping.prototype.jones.mapping.table;
          message = currentTableName + ' for domain object ' + currentTableMappingName;
        }
      }
      userContext.appendErrorMessage('Error resolving table ' + message + ': ' + util.inspect(err));
    }
    if (++mappingBeingResolved === mappings.length || mappingBeingResolved > 20) {
      onMappingsResolvedCallback();
    } else {
      // get the table handler for the next one, and so on until all are done
      getTableHandler(userContext, mappings[mappingBeingResolved], session, resolveTableMappingsOnTableHandler);
    }
  }

  function typeofTableMapping(mapping) {
    var type = typeof mapping;
    if (type == 'object') {
      if (Array.isArray(mapping)) {
        type = 'array';
      } else if (tableMappings.constructor && tableMappings.constructor.name === 'TableMapping') {
        type = 'tablemapping';
      } else {
        type = 'illegal';
      }
    }
    return type;
  }

  // resolveTableMappings begins here

  var tableMappingsType = typeofTableMapping(tableMappings);
  var tableMapping;
  var tableMappingType;
  var m;
  switch (tableMappingsType) {
  case 'string':
    mappings.push(tableMappings);
    break;
  case 'function':
    mappings.push(tableMappings);
    break;
  case 'array':
    if (tableMappings.length) {
      for (m = 0; m < tableMappings.length; ++m) {
        tableMapping = tableMappings[m];
        tableMappingType = typeofTableMapping(tableMapping);
        if (tableMappingType === 'function' ||
            tableMappingType === 'string' ||
            tableMappingType === 'tableMapping') {
          mappings.push(tableMapping);
        } else {
          userContext.appendErrorMessage('unknown table mapping' + util.inspect(tableMapping));
        }
      }
    } else {
      userContext.appendErrorMessage('unknown table mappings' + util.inspect(tableMappings));
    }
    break;
  case 'tablemapping':
    mappings.push(tableMappings);
    break;
  case 'illegal':
    break;
  default:
    userContext.appendErrorMessage('unknown table mappings' + util.inspect(tableMappings));
    break;
  }
  if (mappings.length === 0) {
    if(udebug.is_detail()) { udebug.log('resolveTableMappingsOnSession no mappings!'); }
    onMappingsResolvedCallback();
  } else {
    // get table handler for the first; the callback will then do the next one...
    if(udebug.is_detail()) { udebug.log('getSessionFactory resolving mappings:', mappings); }
    getTableHandler(userContext, mappings[0], session, resolveTableMappingsOnTableHandler);
  }
}