Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE]: Allow specifying types for sql raw statements #4087

Open
1 task done
Sparticuz opened this issue Feb 7, 2025 · 0 comments
Open
1 task done

[FEATURE]: Allow specifying types for sql raw statements #4087

Sparticuz opened this issue Feb 7, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@Sparticuz
Copy link

Sparticuz commented Feb 7, 2025

Feature hasn't been suggested before.

  • I have verified this feature I'm about to request hasn't been suggested before.

Describe the enhancement you want to request

How do I specify the types for a raw sql statement? I have the following code, but I have to override the types to unknown before I can get the types out.

interface SQLResults {
  numResponses: number;
  opsTeam: number;
}
const csFeedbackMTD = sql<SQLResults>`
  SELECT a.ops_team AS opsTeam, COUNT(DISTINCT(a.customer_fk)) AS numResponses
  FROM (
    SELECT ${eventsTable.customer}, ${eventsTable.opsTeam}
    FROM ${eventsTable}
    WHERE
      ${eventsTable.type} IN(8, 14) AND
      ${eventsTable.effectiveDate} BETWEEN ${format(startDate, "yyyy-MM-dd")} AND ${format(endDate, "yyyy-MM-dd")}
    UNION ALL
    SELECT ${eventsTable.customer}, ${eventsTable.opsTeam}
    FROM ${eventsTable}
    WHERE
      ${eventsTable.type} = 6 AND
      ${eventsTable.scoredByEntity} = 1 AND
      ${eventsTable.effectiveDate} BETWEEN ${format(startDate, "yyyy-MM-dd")} AND ${format(endDate, "yyyy-MM-dd")}
    ) a
  LEFT OUTER JOIN ${customersTable} ON a.customer_fk = ${customersTable.id}
  WHERE
    ${customersTable.startDate} < DATE_SUB(CURDATE(),INTERVAL 10 DAY)
  AND
    a.ops_team IS NOT NULL
  GROUP BY a.ops_team`;

  // This type override is wrong, this is because the database.execute type is wrong
  const [result] = (await database.execute(csFeedbackMTD)) as unknown as [SQLResults[]];
  // I've also tries this, but it isn't working either
  //const [result] = await database.execute<SQLResults>(csFeedbackMTD);
@Sparticuz Sparticuz added the enhancement New feature or request label Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant