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

Failed to infer field type for MySQL ENUM field #12

Closed
muniter opened this issue Jan 16, 2023 · 3 comments
Closed

Failed to infer field type for MySQL ENUM field #12

muniter opened this issue Jan 16, 2023 · 3 comments

Comments

@muniter
Copy link

muniter commented Jan 16, 2023

Trying to generate classes from a table with a ENUM field in MySQL throws an error.

@lorefnon
Copy link
Owner

lorefnon commented Jan 16, 2023

Hi @muniter, you are perhaps the first person to use the codegen utility with mysql.

I'll update the regex patterns for the primitive types like char, decimal etc. however for composite types like enum or json you will need to define a type adapter and pass it to the generator.

The tool we rely on to dump the schema (tbls) does not provide us with enough info to infer the enum completely. I had opened an issue with them sometime back.

@juanluispaz
Copy link
Collaborator

juanluispaz commented Jan 16, 2023

Hi,

The generator, by default, only map types that clear, unambiguous and safe type in JS/TS, for the other types you will need to map the corresponding type using a field mapping in a similar way the default ones are defined. Example:

const options: GeneratorOpts = {
  schemaPath: './db/schema.yaml',
  connectionSourcePath: './src/db.connection.ts',
  outputDirPath: './src/db',
  fieldMappings: [
    {
      columnType: /^numeric$/,
      generatedField: { type: { dbType: { name: "stringDouble" } } }
    }
  ],
  export: {
    tableInstances: true,
    rowTypes: true,
    extractedColumns: true
  }
}

const generator = new Generator(options);
await generator.generate();

For more complex types you will need more complex rules, where there are not a generic type representation, like ENUM, JSON, etc. you will need to follow the ones described here

@muniter
Copy link
Author

muniter commented Jan 16, 2023

Hello, thank you very much for the project, I hope the multiple issues wasn't taken as noise!

Thanks for the quick response!

@juanluispaz Noted, makes perfect sense. Will close the issues on the complex types.

@muniter muniter closed this as completed Jan 16, 2023
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

3 participants