adelsz / pgtyped

pgTyped - Typesafe SQL in TypeScript
https://pgtyped.dev
MIT License
2.97k stars 97 forks source link

Types overrides imports #488

Closed nick-keller closed 1 year ago

nick-keller commented 1 year ago

Fix #480

Please review it carefully, I did not test it end to end.

nick-keller commented 1 year ago

Hey @adelsz, I took your comments into consideration but I could not make the example work.

Running test gives me:

Step 2/6 : FROM $NODE_IMAGE
ERROR: Service 'build' failed to build : invalid reference format

And running check gives me:

error TS6059: File '/Users/nick/projects/pgtyped/packages/example/jest-cjs.config.ts' is not under 'rootDir' '/Users/nick/projects/pgtyped/packages/example/src'. 'rootDir' is expected to contain all source files.
  The file is in the program because:
    Matched by default include pattern '**/*'

error TS6059: File '/Users/nick/projects/pgtyped/packages/example/jest.config.ts' is not under 'rootDir' '/Users/nick/projects/pgtyped/packages/example/src'. 'rootDir' is expected to contain all source files.
  The file is in the program because:
    Matched by default include pattern '**/*'

Found 2 errors.

If you can help me make it work so that I can add examples, or add them yourself if you feel like it that;s be great!

adelsz commented 1 year ago

Thanks @nick-keller. The changes look good. To run the example packages you need to specify the node version as follows NODE_VERSION=16 npm run test. We run it as part of the CI https://github.com/adelsz/pgtyped/blob/master/.github/workflows/main.yml

nick-keller commented 1 year ago

Sorry @adelsz but I cannot get it to work. Running check still gives me the two errors I listed above. And running test kinda works until src/index.test.ts timesout. This is definitly not my cup of tea, and I don't have the skills and the patience to solve those kind of issues.

Furthermore:

I'll have to let you take it from here I'm afraid :/

adelsz commented 1 year ago

No worries, I can handle that on my side. Thanks for working on this major feature, it will be a great addition to pgTyped.

As for the issues you encountered, packages/example is one of the confusing parts of the codebase. I will have to write more detailed instructions in a CONTRIBUTING.md doc.

In case it might be of any future use I will comment on the issues you brought up:

The tests are not really checking that the generated types are correct. For instance expect(typeof dateAsString).toBe('string'); only checks that pg.types.setTypeParser(...) works, it is not checking that pgtyped actually typed the column as string.

The idea of the example package is to serve both as an e2e test (compile-time and runtime) and example usage of pgTyped.

The npm run test command does two things:

  1. Runs pgTyped, generating code for all queries. After running the type generation, a git diff will show if there are any unexpected problems with the generated queries and types (e2e compile-time test).
  2. Runs the generated queries as part of a Jest test suite. This ensures that the queries work at runtime.

In the package.json, dependencies are ^2.0.0, meaning that my work will not be reflected until you actually publish the package anyway.

pgTyped is a monorepo using npm workspaces, so running npm install actually crosslinks all of the pgTyped packages together locally (the exact versions in package jsons don't matter). Running npm run build builds all packages and makes them available to each other as if they were published.

As I said, all of this belongs in CONTRIBUTING.md doc, which has been long overdue.