kevlened / fireway

A schema migration tool for firestore
MIT License
279 stars 41 forks source link

Suport typescript for migration scripts #21

Closed pinkasey closed 3 years ago

pinkasey commented 3 years ago

I'm not a very good with javascript/typescript, so maybe I'm talking nonsense.

It would be lovely to be able to write the migration scripts in typescript. I've tried it, and got a syntax error when trying to import.

Is it possible to support typescript?

kevlened commented 3 years ago

To support typescript today, you should be able to compile your ts files, then use the —path fireway flag to point to the compiled directory. While writing these scripts, you won’t have intellisense for the preconfigured Firestore instance. To support that, typescript needs a way to define types for the exports of all files in a directory.

I don’t use typescript in my day-to-day, so I’m open to PRs for code changes/docs if anyone is interested in exploring.

pinkasey commented 3 years ago

Right! That makes perfect sense! I'll try that. I think I know how to add type information too.

I'll open a PR when I'm done (documentation only, probably), but that might take a while. Thanks!

kevlened commented 3 years ago

Luke Edwards's ley library has an interesting solution to this. It would require ts-node and look something like:

$ fireway --require ts-node/register migrate

Type annotations could be defined manually in the file w/ ts or jsdoc.

kevlened commented 3 years ago

This is resolved in 0.5.1. See the docs for usage. It works directly with TypeScript or JSDoc, if you prefer.

fringley commented 3 years ago

Hi @kevlened thanks for a great project. I'm aware you don't use typescript on the regular, but I'm having difficulty with using this with ts-node.

I'm using your simple example to get started, but get the following error when running $ fireway --require ts-node/register migrate:

export function migrate(_a) {
^^^^^^

SyntaxError: Unexpected token 'export'

I have tracked this down to a setting in my tsconfig.json - compilerOptions.module: "esnext" doesn't work (it does with commonjs, but this is required by react-app-rewired that I'm using for my firebase project

kevlened commented 3 years ago

@fringley Thanks for the support. I modeled the TS integration after the ley project. They included an extra step in their TS setup that I omitted, because it wasn’t necessary for my tests. Here are their steps: https://github.com/lukeed/ley#typescript. The step I omitted is below. Add this to your tsconfig.json and let me know if it helps:

{
  "ts-node": {
    "transpileOnly": true,
    "compilerOptions": {
      "module": "commonjs"
    }
  }
}
kevlened commented 3 years ago

@fringley I'll assume that fixed your issue. I've documented the extra step in the README. If you still have trouble, let me know.