
/* <%- lintDisableUnused %> */
// Metadata for forming raw SQL statements for GraphQL. (Can be re-generated.)
<%- tplTsOnly([
`import { App } from '../../app.interface'${sc}`,
`import { SqlResolverMap } from './graphql.interfaces'${sc}`,
``,
`export interface SqlMetadataOptions {`,
`  convertArgsToFeathers: any${sc}`,
`  convertArgsToOrderBy: any${sc}`,
`  convertArgsToWhere: any${sc}`,
`}`,
'',
]) -%>
<%- insertFragment('imports') %>
<%- insertFragment('init') %>

let moduleExports = function sqlMetadata<%- tplJsOrTs('(app, options)', '(app: App, options: SqlMetadataOptions)') %> {
  let { convertArgsToFeathers, convertArgsToOrderBy, convertArgsToWhere } = options<%- sc %>
  let makeOrderBy = convertArgsToOrderBy(options)<%- sc %>
  let makeWhere = convertArgsToWhere(options)<%- sc %>
  <%- insertFragment('func_init') %>

  let returns<%- tplTsOnly(': SqlResolverMap') -%> = {
<%# -%>
<%# --- forEach-1 starts below. Loop thru the schemas of GraphQL enabled services. -%>
<% Object.keys(mapping.graphqlSql).forEach(graphqlName => {
  __serviceName = mapping.graphqlSql[graphqlName].service;
  __graphql = feathersSpecs[__serviceName]._extensions.graphql;
  __add = __graphql.add;
-%>

    <%- graphqlName %>: {
      sqlTable: '<%- __graphql.sql.sqlTable %>',
      uniqueKey: '<%- __graphql.sql.uniqueKey %>',
      fields: {
<%# -%>
<%# --- forEach-2 starts below. Loop thru the fields to be renamed. -%>
<% Object.keys(__graphql.sql.sqlColumn).forEach(fieldName => { -%>
        <%- fieldName %>: {
          sqlColumn: '<%- __graphql.sql.sqlColumn[fieldName] %>',
          <%- insertFragment(`fields-${graphqlName}-${fieldName}`) %>
        },
<% }); -%>
<%# --- forEach-2 ends above. -%>
<%# -%>
<%# --- forEach-3 starts below. Loop thru the added fields. -%>
<% Object.keys(__add).forEach(fieldName => {
  __addField = __add[fieldName];
-%>

        // <%- fieldName %><%- __addField.args %>: <%- __addField.type %>
        <%- fieldName %>: {
<%# -%>
<%# --- if/else-1 starts below. -%>
<% if (__addField.serviceName) {
    __temp = [
        '          sqlJoin(ourTable, otherTable) { return `${ourTable}.'
            + __addField.relation.ourTableSql
            + ' = ${otherTable}.'
            + __addField.relation.otherTableSql
            + '`' + sc + ' },',
        `          orderBy(args, content) { return makeOrderBy(args, null)${sc} },`,
        `          where(table, args) { return makeWhere(table, args, '${__addField.relation.ourTableSql}', undefined)${sc} },`,
    ];
-%>
          <%- insertFragment(`fields-${graphqlName}-${fieldName}`, __temp) %>
<% } else { -%>
          <%- insertFragment(`fields-${graphqlName}-${fieldName}-non`, [
            `          sqlExpr: (tableName, args) => { throw Error('GraphQL fieldName ${graphqlName}.${fieldName} is not calculated.')${sc} },`,
]) %>
<%# --- if/else-1 ends above. -%>
<% } -%>
<%# --- forEach-3 ends above. -%>
        },
<% }); -%>
<%# --- forEach-1 ends above. -%>
        <%- insertFragment(`fields-${graphqlName}`) %>
      },
      <%- insertFragment(`type-${graphqlName}`) %>
    },
<% }); -%>

    Query: {
      fields: {
<%# -%>
<%# --- forEach-4 starts below. Loop thru the schemas of GraphQL enabled services. -%>
<% Object.keys(mapping.graphqlSql).forEach(graphqlName => {
  __serviceName = mapping.graphqlSql[graphqlName].service;
  __graphql = feathersSpecs[__serviceName]._extensions.graphql;
  __uniqueKey = __graphql.sql.uniqueKey;
  __temp = [
    `        // get${graphqlName}(query: JSON, params: JSON, key: JSON): ${graphqlName}`,
    `        get${graphqlName}: {`,
    `          orderBy: (args, content) => makeOrderBy(args, { ${__uniqueKey} : 1 }),`,
    `          where: (table, args) => makeWhere(table, args, '${__uniqueKey}'),`,
    '        },',
    '',
    `        // find${graphqlName}(query: JSON, params: JSON): [${graphqlName}!]`,
    `        find${graphqlName}: {`,
    `          orderBy: (args, content) => makeOrderBy(args, { ${__uniqueKey} : 1 }),`,
    `          where: (table, args) => makeWhere(table, args, '${__uniqueKey}'),`,
    '        },',
  ];
-%>

        <%- insertFragment(`query-${graphqlName}`, __temp) %>
<% }); -%>
<%# --- forEach-4 ends above. -%>
        <%- insertFragment('metadata_query_fields') %>
      },
      <%- insertFragment('metadata_query_more') %>
    },
  <%- insertFragment('metadata_more') %>
  }<%- sc %>

  <%- insertFragment('func_return') %>
  return returns<%- sc %>
}<%- sc %>

<%- insertFragment('more') %>

<%- insertFragment('exports') %>
<%- `${tplExport('moduleExports')}${sc}` %>

<%- insertFragment('funcs') %>
<%- insertFragment('end') %>
