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

Import Custom Types #131

Closed
pdesjardins-jumptrading opened this issue Aug 22, 2023 · 1 comment
Closed

Import Custom Types #131

pdesjardins-jumptrading opened this issue Aug 22, 2023 · 1 comment

Comments

@pdesjardins-jumptrading

Description

It would be extremely convenient to have in the configuration a place to add a list of import that are injected at the top of the generated .d.ts file.

Issue

Currently, using the sql-ts configuration file, a developer can define a typeMap. However, if the mapping point into a custom type, it gets converted to any.

For example:

{
  "client": "mysql2",
  "connection": {
    // Retracted
  },
  "schemas": ["retracted"],
  "filename": "./src/autogenerated/out/dbTypes.d",
  "interfaceNameFormat": "Db${table}",
  "tableNameCasing": "pascal",
  "singularTableNames": false,
  "typeMap": {
    "boolean": ["tinyint"],
    "number": ["int", "mediumint", "smallint"],
    "Date": ["datetime"],
    "DateOnly": ["date"],
    "string": ["bigint", "bigint unsigned"]
  }
}

In that case, the DateOnly is a very basic type that is a number but it is a descriptive way used in the system.

Potential Solutions

Similar to how The Guild CodeGen for GraphQL is having its configurations, to have a way to map the type into another one (or file).

In the configuration json file a new entry like:

"customScalarMap" :{
    "DateOnly":  "./src/myScalarsFolder/myScalarFile#DateOnly"
}

Desired Result

The desired result is to have at the top of the generated d.ts file imports for custom type. In that case:

import {DateOnly} from "./............"

This way, the DateOnly would point to number instead of any.

Similar Past Issue

This issue from few years ago was raised. A follow up comment talked about using template but I couldn't see any clear documentation about how to add an import.

@rmp135
Copy link
Owner

rmp135 commented Mar 16, 2024

To add the import to the template you would copy the template file, lets say it's called ./template-custom.handlebars relative to your working dir.

At the very top add:

import {DateOnly} from "./blah.d"

// Rest of the original template remains here.

Then, add this to your config.

{
    "template": "./template-custom.handlebars"
}

Your custom config will then be used, with the import at the top.

I'm curious as to this:

However, if the mapping point into a custom type, it gets converted to any.

I've just tested using the type map with a custom type and it correctly mapped to DateOnly (although it errors unless a valid type import is added).

@rmp135 rmp135 closed this as completed Oct 27, 2024
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

2 participants