kristiandupont / kanel

Generate Typescript types from Postgres
https://kristiandupont.github.io/kanel/
MIT License
867 stars 60 forks source link

Make identifier aliasing optional #580

Open alecmev opened 3 months ago

alecmev commented 3 months ago

I found a couple of mentions of the intention of making branding optional, like https://github.com/kristiandupont/kanel/issues/433 and https://github.com/kristiandupont/kanel/discussions/444. It doesn't look like it happened yet, but supplying a custom generateIdentifierType is easy enough:

{
  // ...
  generateIdentifierType: (column, details, config) => {
    const name = kanel.escapeIdentifier(
      pascalCase(details.name) + pascalCase(column.name),
    );

    const configWithoutGenerateIdentifierType = { ...config };
    delete configWithoutGenerateIdentifierType.generateIdentifierType;

    const innerType = kanel.resolveType(
      column,
      details,
      configWithoutGenerateIdentifierType,
    );

    return {
      declarationType: 'typeDeclaration',
      name,
      exportAs: 'named',
      typeDefinition: [
        typeof innerType === 'string' ? innerType : innerType.name,
      ],
      typeImports: typeof innerType === 'string' ? [] : innerType.typeImports,
      comment: [`Identifier type for ${details.schemaName}.${details.name}`],
    };
  },
}

However, I would also like to take this a step further and drop identifier aliasing altogether, so that instead of this:

export type UsersId = string;
export default interface UsersTable {
  id: ColumnType<UsersId, UsersId | undefined, UsersId>;
}

It's just this:

export default interface UsersTable {
  id: ColumnType<string, string | undefined, string>;
}

Type-wise this doesn't get in the way, so this is a low-priority request, of course, but it would be nice to get rid of *Id entries in code completion tooltips. This can probably be accomplished with a plugin, but the number of edge cases feels intimidating.

kristiandupont commented 3 months ago

Yeah, this is something I probably need to do since it a popular and reasonable request. The problem is, as you are identifying, that there are many edge cases and combinations possible, so it's not quite as trivial as it sounds.