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

Does typesOverrides work? #477

Closed
danvk opened this issue Jan 23, 2023 · 7 comments
Closed

Does typesOverrides work? #477

danvk opened this issue Jan 23, 2023 · 7 comments

Comments

@danvk
Copy link
Contributor

danvk commented Jan 23, 2023

I was excited to see the new typesOverrides option in [email protected]. However, I'm not sure it works as advertised?

I added the following to my pgtyped.config.json based on the config in CLI.md:

  "typesOverrides": {
    "date": "string"
  }

Then I removed the following from my post-processing script:

| perl -pe 's/: Date/: string/' \

Now when I run pgtyped, I see the following sorts of diffs for all my queries:

-  created_at: string | null;
+  created_at: Date | null;
-  expires_on: string | null;
+  expires_on: Date | null;
   id: string;

in other words, the type override isn't working. I don't see any tests in #476 so it's hard for me to tell what the expected behavior is.

I'm definitely on the right version because "hungarianNotation": false worked great.

Some guidance on what sorts of types you can override would be helpful, for example I'd like to change Jsonany and JsonArrayany[].

@adelsz
Copy link
Owner

adelsz commented Jan 24, 2023

Thanks for flagging this.
I am a bit busy testing a big structural rewrite #478, which should land this week as v2.0.0.
Will get back to this issue right after that. Meanwhile maybe @nick-keller has some ideas on what is going on.

@petetnt
Copy link

petetnt commented Jan 27, 2023

I ran into the same issue, the typesOverrides seemingly does nothing, or I don't now how to use it properly.

@nick-keller
Copy link
Contributor

Before @adelsz merged my changes I used this in my package.json and it worked.

{
  "devDependencies": {
    "@pgtyped/cli": "https://github.com/nick-keller/pgtyped/raw/patched/packages/cli/pgtyped-cli-1.0.2.tgz",
    "@pgtyped/query": "^1.0.1",
    }
}

I am now using:

{
  "devDependencies": {
    "@pgtyped/cli": "^1.1.0",
    "@pgtyped/query": "^1.1.0",
  }
}

And it still works. I run pgtyped -w -c pgtyped.json and my config file looks like this:

{
  "transforms": [
    {
      "mode": "sql",
      "include": "**/*.sql",
      "emitTemplate": "{{dir}}/{{name}}.queries.ts"
    }
  ],
  "srcDir": "./src/",
  "failOnError": true,
  "db": {
      ...
  },
  "typesOverrides": {
    "date": "string"
  }
}

@adelsz
Copy link
Owner

adelsz commented Jan 27, 2023

I have added e2e tests for type override testing https://github.com/adelsz/pgtyped/pull/481/files.
They are passing on master, and should work on 1.1.0 too.

To have the correct types (ts string for pg DATE) at runtime you should setup pg correctly:

pg.types.setTypeParser(pg.types.builtins.DATE, function(val) {
    return val;
})

To have the correct types (ts string for pg DATE) at compile-time you should add the following to your config:

  "typesOverrides": {
    "date": "string"
  },

@danvk & @petetnt you can try 2.0.0-alpha.1 that has just been released, just make sure you follow the README as there were some package name changes in v2.

@petetnt
Copy link

petetnt commented Jan 27, 2023

Ah, I assumed that the setTypeParser calls would have been automatically called on the used client, but that makes way more sense. Thanks @adelsz!

@adelsz
Copy link
Owner

adelsz commented Jan 27, 2023

Great, it works as expected for you now?

Ah, I assumed that the setTypeParser calls would have been automatically called on the used client, but that makes way more sense. Thanks @adelsz!

Got it, I guess we should highlight this in the docs.

@danvk
Copy link
Contributor Author

danvk commented Jan 27, 2023

I figured out what was going on — I was overriding date instead of timestamptz. I imagine the latter is vastly more common in real applications, so perhaps the docs could be updated to use it as an example?

{
  "typesOverrides": {
    "date": "string",
    "timestamp": "string",
    "timestamptz": "string"
  }
}

I've also been having trouble using typesOverrides to override the jsonb type, but I can open a separate issue for that.

@danvk danvk closed this as completed Jan 27, 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

4 participants