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

Transform type as nested object

ropbastos opened this issue · comments

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!

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 ✌️

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).

fixed in 3.0.0-next.4

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

fixed in 3.0.0