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

feat: typescript support for migration files #8

Open
jousby opened this issue Jul 23, 2024 · 3 comments
Open

feat: typescript support for migration files #8

jousby opened this issue Jul 23, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@jousby
Copy link

jousby commented Jul 23, 2024

It would be great to be able to write and test migration files as typescript files.

i.e

migrations/1.0.0.ts
          /1.1.0.ts

My current tsconfig/jest setup complains bitterly when the Migrate class attempts to load up some js files. I can probably bash my way through to a solution but it would be great not to have to.

Secondly having the types in the up / down function definitions makes writing the actual migration logic easier. I know i can point the migrate cli / migrate class at the js files post typescript compilation but this still leads to the issue above when trying to test.

@mobsense
Copy link

The TS script would need to be processed by Esbuild or TSC so they could be loaded.

I'm not sure I understand what you mean that tsconfig complains when loading "js" files. Can you specify allowJS?

Your second point, having types. Do you mean types for the Migrate class or for your code/

A bit more detail on your suggestions will help me understand.

@jousby
Copy link
Author

jousby commented Jul 28, 2024

The TS script would need to be processed by Esbuild or TSC so they could be loaded.

This is where i ended up. I have a step in my build process that does an esbuild/bundling of each each individual migration script in my migrations folder. The output of tsc wasn't enough in my case as my project is using a nx monorepo and was leveraging local shared modules that aren't published anywhere.

Maybe I didn't think this one through enough. The above felt a little heavyweight and I was hoping there might be a simple way to push some of this into the library to make writing migrations as typescript files easier.

I'm not sure I understand what you mean that tsconfig complains when loading "js" files. Can you specify allowJS?

My bad, I found the 'allowJS' setting after writing this. I now have a nice solution for unit testing migrations with a local dynamodb emulator and jest.

Your second point, having types. Do you mean types for the Migrate class or for your code

I mean types in the migration scripts. This has made discovering methods/intellisense on the db object in particular much easier.

  async up(db: Table, migrate: Migrate, params: { dry: boolean }) {}

vs

  async up(db, migrate, params) {}

So at this point I now have a solution for writing my migration scripts in typescript with the up/down functions full typed as above. I'm not blocked. My remaining ask is a 'hand wavy' can we make it easier for users of this library (and the associated onetable-cli) to write migration scripts in Typescript. Maybe the recent PR to add typescript support to Node might make this easier in the future: nodejs/node#53725

@mobsense mobsense added the enhancement New feature or request label Aug 6, 2024
@mobsense
Copy link

mobsense commented Aug 6, 2024

Great that allowJs helped.

Yes, stripping types by node would be REALLY great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants