schemats icon indicating copy to clipboard operation
schemats copied to clipboard

Name conflict in generated types

Open bradleyayers opened this issue 7 years ago • 12 comments

I have a table document with a column type of type document_type:

CREATE TYPE document_type AS ENUM (
    'NOTE',
    'PAGE'
);
CREATE TABLE document (
    id uuid NOT NULL,
    document_type document_type NOT NULL
);

This causes a name conflict in the generate types:

export type document_type = "NOTE" | "PAGE";

// ...

export namespace documentFields {
  export type id = string;
  export type document_type = document_type;
  //          ^^^^^^^^^^^^^
  // Type alias 'note_type' circularly references itself
}

bradleyayers avatar Feb 21 '18 05:02 bradleyayers

To avoid that, I think enums could be generated in PascalCase (after all it's the common practice with typescript) and/or with Enum prefix, something like :

export type EnumDocumentType = "NOTE" | "PAGE";

export namespace documentFields {
  export type id = string;
  export type document_type = EnumDocumentType;
}

abenhamdine avatar Feb 21 '18 09:02 abenhamdine

I like that proposal, nice and simple!

bradleyayers avatar Feb 21 '18 17:02 bradleyayers

It will be a breaking change though, because someone could have imported an enum and used it in its code.

abenhamdine avatar Feb 21 '18 18:02 abenhamdine

Perhaps an option/flag then?

bradleyayers avatar Feb 21 '18 18:02 bradleyayers

@xiamx what do you think on it ? Personnaly, I'm -1 on putting this behind a flag because options complexify config and code.

It seems better to include it in next major version (while I doubt many users have enums and import them).

abenhamdine avatar Feb 21 '18 18:02 abenhamdine

Yes, preferring a major version bump to have Enum in PascalCase. We need a minimal impact solution to handle name collision for the current major version.

xiamx avatar Feb 21 '18 20:02 xiamx

Emitting exports for both snake and camel, but using camel in the interfaces should work

bradleyayers avatar Feb 21 '18 21:02 bradleyayers

Nice, that should provide a smooth transition.

xiamx avatar Feb 21 '18 21:02 xiamx

Whats the status on this?

hinderberg avatar Feb 18 '19 11:02 hinderberg

I'm using a forked version that I have checked into my project with this fix applied.

bradleyayers avatar Feb 18 '19 11:02 bradleyayers

Thats to bad! Renaming my enum type while waithing for this. Forking too many libs allready! ;)

hinderberg avatar Feb 18 '19 11:02 hinderberg

Why not export the enums and all table related interfaces under namespace with the name of the table.

felixmosh avatar Mar 14 '19 18:03 felixmosh