ts-safeql / safeql

Validate and auto-generate TypeScript types from raw SQL queries in PostgreSQL.
https://safeql.dev
MIT License
1.35k stars 22 forks source link

Transform type as nested object #170

Closed ropbastos closed 6 months ago

ropbastos commented 1 year ago

Is your feature request related to a problem? Please describe. I'm trying to introduce this plugin to a code base that uses Knex and is formatted with Prettier. However, the way this extension formats the type annotation conflicts with what Prettier expects.

Using this rule:

targets: [{ wrapper: 'knex.raw', transform: '{ rows: {type}[] }' }],

SafeQL formats the type like this:

return (
            // eslint-disable-next-line prettier/prettier
            await this.knex.raw<{ rows: { s3Path: string; name: string; type: string; }[] }>(sql`
            SELECT

If I remove the // eslint-disable-next-line prettier/prettier the line is then formatted to:

return (
            await this.knex.raw<{ rows: { s3Path: string; name: string; type: string }[] }>(sql`
            SELECT

Which gives me the following error:

Query has incorrect type annotation.
    Expected: { rows: { s3Path: string; name: string; type: string; }[]; }
    Actual: { rows: { s3Path: string; name: string; type: string; }[] }eslint[@ts-safeql/check-sql](https://github.com/ts-safeql/safeql)

Describe the solution you'd like It would be nice if this plugin would generate types that are conformant to Prettier expectations, as Prettier is widely used.

Additional context Here are my ESLint and Prettier configs:

{
  "printWidth": 120,
  "tabWidth": 4,
  "semi": false,
  "singleQuote": true,
  "trailingComma": "all"
}
require('dotenv').config()

module.exports = {
    parser: '@typescript-eslint/parser',
    parserOptions: {
        project: 'tsconfig.json',
        sourceType: 'module',
    },
    plugins: ['@typescript-eslint/eslint-plugin', '@ts-safeql/eslint-plugin'],
    extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'],
    root: true,
    env: {
        node: true,
        jest: true,
    },
    ignorePatterns: ['.eslintrc.js'],
    rules: {
        '@typescript-eslint/interface-name-prefix': 'off',
        '@typescript-eslint/explicit-function-return-type': 'off',
        '@typescript-eslint/explicit-module-boundary-types': 'off',
        'max-len': [
            'warn',
            {
                code: 120,
                ignoreStrings: true,
            },
        ],
        '@ts-safeql/check-sql': [
            'error',
            {
                connections: {
                    databaseUrl: process.env.CRM_DATABASE_URL,
                    targets: [{ wrapper: 'knex.raw', transform: '{ rows: {type}[] }' }],
                },
            },
        ],
    },
}

Thank you very much for this awesome plugin!

Newbie012 commented 1 year ago

I actually don't use prettier. I do it manually for performance reasons. This indeed seems like a funny issue. Thanks for posting that! I'll raise a fix soon ✌️

Newbie012 commented 1 year ago

It's a bit more complex than I initially thought. The part that is responsible for the comparison needs to be improved. The source of the issue here is that you're transforming the type to a nested object (which should be okay).

Newbie012 commented 10 months ago

fixed in 3.0.0-next.4

I'll close this issue when 3.0.0 will be released.

Newbie012 commented 6 months ago

fixed in 3.0.0