ts-safeql / safeql

Validate and auto-generate TypeScript types from raw SQL queries in PostgreSQL.

Home Page:https://safeql.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

working with prisma & enums breaks formatting and/or types

okaisti opened this issue · comments

Describe the bug

// This works
prisma.$queryRaw<{ id: number }[]>`
  SELECT id from machine
`

// This also works

interface Demo {
id: number
}

prisma.$queryRaw<Demo[]>`
  SELECT id from machine
`

// This does not work, because the return type declaration spans multiple lines
prisma.$queryRaw<{
  id: number
}[]>`
  SELECT id from machine
`

now adding safeql to the equation, breaks, when working with enums for some reason

...
          interface Demo {
            task_definition_class_type:
              | 'BASIC'
              | 'WORK_PACKAGE'
              | 'WORK_ITEM'
              | 'ADDITIONAL'
              | 'ROADBLOCK'
              | 'TASK'
              | 'QUALITY'
              | 'LEGACY_PREREQUISITE'
              | null;
          }

          return client.$queryRaw<Demo[]>`
            SELECT
                td.class_type as task_definition_class_type
            FROM table1 ti
            LEFT JOIN table2 td ON ti.task_definition_id = td.id;
        `

results in safeql error:

Query has incorrect type annotation.
	Expected: { task_definition_class_type: "BASIC" | "WORK_PACKAGE" | "WORK_ITEM" | "TASK" | "QUALITY" | "ADDITIONAL" | "ROADBLOCK" | "LEGACY_PREREQUISITE" | null; }
	Actual: { task_definition_class_type: 'BASIC' | 'WORK_PACKAGE' | 'WORK_ITEM' | 'ADDITIONAL' | 'ROADBLOCK' | 'TASK' | 'QUALITY' | 'LEGACY_PREREQUISITE' | null; }[]eslint[@ts-safeql/check-sql](https://github.com/ts-safeql/safeql)

tried to change the order of the union keys -> didn't work either. only fix is to add a //prettier-ignore for the line, so somewhat inconvenient

this works, but again, breaks prisma syntax highlighting

          return client.$queryRaw<
            {
              task_definition_class_type:
                | 'BASIC'
                | 'WORK_PACKAGE'
                | 'WORK_ITEM'
                | 'ADDITIONAL'
                | 'ROADBLOCK'
                | 'TASK'
                | 'QUALITY'
                | 'LEGACY_PREREQUISITE'
                | null;
            }[]>`
            SELECT
                td.class_type as task_definition_class_type
            FROM table1 ti
            LEFT JOIN table2 td ON ti.task_definition_id = td.id;
        `

The lack of syntax highlighting after multi-line generic arguments is a problem with VS Code and TextMate grammars not being able to span across multiple lines:

However, a workaround exists for many syntax highlighting extensions (such as SQL tagged template literals by @frigus02), to use a comment (such as /* sql */) between the generic arguments list and the template string (after the > and before the `):

I believe I fixed the issue with Query has incorrect type annotation in the pre-released version 3.0.0

You could test it by installing the pre-release version (npm i -D @ts-safeql/eslint-plugin@3.0.0.next-4).

Feedback from you would be highly appreciated 🙏

3.0.0-next.4 works 🙌