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"
]
}