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

Column overrides do not work with RETURNING * #373

Open
maximdvoynishnikov opened this issue Jan 28, 2025 · 0 comments
Open

Column overrides do not work with RETURNING * #373

maximdvoynishnikov opened this issue Jan 28, 2025 · 0 comments

Comments

@maximdvoynishnikov
Copy link

Describe the bug
When writing any (not SELECT) queries with RETURNING statement in the end, the generated type does not respect column overrides.

To Reproduce
Simple table schema

create table dummy (
    id uuid primary key default gen_random_uuid(),
    name text not null,
    created_at timestamp with time zone NOT NULL default now(),
    metadata jsonb NOT NULL default '{}'
);

safeql.config.ts

export default defineConfig({
  connections: {
    databaseUrl,
    targets: [
      {
        // The name of the sql tag that should be checked:
        tag: 'sql',
        // Postgres.js type should be an array, so we add an extra "[]" after the generated type:
        transform: '{type}[]',
      },
    ],
    overrides: {
      columns: {
        'dummy.metadata': 'MetadataType',
      },
      types: {},
    },
  },
});

Repository class

export type MetadataType = Record<string, string>;

export class DummyRepositoryRecord {
  id!: string;
  name!: string;
  created_at!: Date;
  metadata!: MetadataType;
}

export type DummyType = {
  id: string;
  name: string;
  created_at: Date;
  metadata: MetadataType;
};

@Injectable()
export class DummyRepository {
  constructor(@Inject(SQL_INJECT_TOKEN) private sql: Sql) {}

  async get(): Promise<void> {
    await this.sql<DummyRepositoryRecord[]>`
        SELECT *
        FROM dummy
    `;
  }

  async getWithType(): Promise<void> {
    await this.sql<DummyType[]>`
        SELECT *
        FROM dummy
    `;
  }

  async create(name: string): Promise<void> {
    await this.sql<DummyType[]>`
        INSERT INTO dummy (name)
        VALUES (${name})
        RETURNING *
    `;
  }

  async update(id: string, name: string): Promise<void> {
    await this.sql<DummyType[]>`
        UPDATE dummy
        SET name = ${name}
        where id = ${id}::uuid
        RETURNING *
    `;
  }

  async delete(id: string): Promise<void> {
    await this.sql<DummyType[]>`
        DELETE
        FROM dummy
        WHERE id = ${id}::uuid RETURNING *`;
  }
}

Expected behavior
I expect the DummyType generic not to be overriden by linting.

Screenshots

Image

getWithType is the only method, which seems not to have any typescript related issues.
Image

The state after linting has been executed:
Image

Interesting fact. Classes types do not work as well, I mean, strictly speaking, that's probably not the intended usage of the library. Would be nice to hear a statement from the maintainers on it.
Image

Desktop (please complete the following information):

  • OS: [macOS 15.2]
  • PostgreSQL version [16]
  • Version [3.6.6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant