prepareTableNamesToTables(tableNames) { this.tableNamesToTables = R.pipe( R.unnest, R.groupBy(n => n[0]), R.map(groupedNameToDef => groupedNameToDef.map(nameToDef => nameToDef[1])) )( tableNames.map(tableName => { const [schema, table] = this.parseTableName(tableName); const tableDefinition = this.resolveTableDefinition(tableName); const definition = { schema, table, tableDefinition, tableName }; const tableizeName = inflection.tableize(table); const parts = tableizeName.split('_'); const tableNamesFromParts = R.range(0, parts.length - 1).map(toDrop => inflection.tableize(R.drop(toDrop, parts).join('_'))); const names = R.uniq([table, tableizeName].concat(tableNamesFromParts)); return names.map(n => [n, definition]); }) ); }
// eslint-disable-next-line consistent-return resolveTableName(tableName) { let tableParts; if (Array.isArray(tableName)) { tableParts = tableName; } else { tableParts = tableName.match(/(["`].*?["`]|[^`".]+)+(?=\s*|\s*$)/g); } if (tableParts.length === 2) { this.scaffoldingSchema.resolveTableDefinition(tableName); return tableName; } else if (tableParts.length === 1) { const schema = Object.keys(this.dbSchema).find( tableSchema => this.dbSchema[tableSchema][tableName] || this.dbSchema[tableSchema][inflection.tableize(tableName)] ); if (!schema) { throw new UserError(`Can't find any table with '${tableName}' name`); } if (this.dbSchema[schema][tableName]) { return `${schema}.${tableName}`; } if (this.dbSchema[schema][inflection.tableize(tableName)]) { return `${schema}.${inflection.tableize(tableName)}`; } } else { throw new UserError('Table names should be in <table> or <schema>.<table> format'); } }
const withoutId = column.name.replace(new RegExp(idRegex, 'i'), ''); const tablesToJoin = this.tableNamesToTables[withoutId] || this.tableNamesToTables[inflection.tableize(withoutId)];