0
votes

I'm working on a node project and I'm using typeORM for the first time so I know this is probably user error. My ideal setup would be:

  • Use the typeorm cli via ts-node to generate *.ts migrations based on the *.ts entities.
  • Compile the typescript to javascript.
  • Use the typeorm cli to apply and revert the migrations based on the javascript output.

Trying to set this up has been pretty messy, I've ended up in some situation where generating migrations via the cli using ts-node creates my *.ts migrations where I want them - but it's determining the schema difference based on the javascript migration directory, not the typescript one. It would still be my preference, even if it requires extra scripts and another config.

Since the above hasn't been working, I'm trying to get all of the typeorm cli stuff to work via ts-node (generate, migrate, revert, etc). I have the following ormconfig for this:

{
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": process.env.DATABASE_USER,
  "password": process.env.DATABASE_PASSWORD,
  "database": process.env.DATABASE_NAME,
  "logging": false,
  "entities": [
     "src/database/models/**/*.ts"
  ],
  "migrations": [
     "src/database/migrations/**/*.ts"
  ],
  "subscribers": [
     "src/database/subscribers/**/*.ts"
  ],
  "cli": {
      "entitiesDir": "src/database/models/",
      "migrationsDir": "src/database/migrations",
      "subscribersDir": "src/database/subscribers"
   }
}

And I have the following scripts:

"scripts": {
    "typeorm": "ts-node --require ts-node/register ./node_modules/typeorm/cli.js  --config ormconfig.js",
    "typeorm:revert": "npm run typeorm migration:revert",
    "typeorm:generate": "npm run typeorm migration:generate -- -n",
    "typeorm:create": "npm run typeorm migration:create",
    "typeorm:migrate": "npm run typeorm migration:run"
}

Whenever I run npm run typeorm:generate I get the following errors:

src/database/models/MyModel.ts:4:55 - error TS2339: Property 'decorate' does not exist on type 'typeof Reflect'.

4     if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
                                                        ~~~~~~~~
src/database/models/MyModel.ts:4:92 - error TS2339: Property 'decorate' does not exist on type 'typeof Reflect'.

4     if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
                                                                                             ~~~~~~~~
src/database/models/MyModel.ts:9:55 - error TS2339: Property 'metadata' does not exist on type 'typeof Reflect'.

9     if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
                                                        ~~~~~~~~
src/database/models/MyModel.ts:9:95 - error TS2339: Property 'metadata' does not exist on type 'typeof Reflect'.

9     if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);

reflect-metadata is installed and it is imported in the entry point of my app and it compiles fine when running tsc. I'm really struggling with figuring out the best way to configure all this based on the TypeORM documentation, why isn't it working and what do I need to do to fix it? Would really appreciate it if someone could point me in the right direction and clarify my confusion. It just seems like this is something that should be really simple and straight forward, work out of the box with minimal trouble, but I've gone over the documentation and can't figure it out.

My tsconfig is as follows:

{
  "compilerOptions": {
    "module": "CommonJS",
    "target": "ES2018",
    "outDir": "dist",
    "lib": [
      "ES6",
      "ESNext",
      "ESNext.Array",
      "ESNext.AsyncIterable",
      "ESNext.Intl",
      "ESNext.Symbol",
      "DOM"
    ],
    "sourceMap": false,
    "inlineSourceMap": true,
    "inlineSources": true,
    "incremental": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "paths": {
      "my-module": ["./src/typings"]
    }
  },
  "include": [
    "./src/**/*"
  ],
  "exclude": [
    "node_modules"
  ]
}
1
Seems like TypeORM isn't using your tsconfig.json - Aluan Haddad
It definitely was - it's passed in via the command line in the script and if the config was invalid it would spit an error saying so, this was a very tiring mistake. I figured out my issue and I'll post it as an answer. - Psibean

1 Answers

1
votes

I started up a fresh project following the typeorm installation / setup guide and got everything working with their default generated project. I then slowly started copying the entities and other database related stuff over and added the dependencies as they came in and kept checking to ensure the project compiled each time. I then ensured I could still run all of the typeorm migration commands without having the built version present.

As I did this and got it working - it worked.

I then compared the differences between the projects and the issue is right there in my original post. The typeorm script should have been:

"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js --config ormconfig.json",

Not what I originally had - however, when I change this back now to what is in the original post, I am unable to re-produce the errors. So I'm genuinely not sure what the causes was. There was nothing else I changed throughout this process.