Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How configure TypeORM ormconfig.json file to parse Entities from js dist folder or ts src folder?

I set my TypeORM config entities path like:

"entities": ["src/entities/**/*.ts"]

This works good when I use ts-node. ts-node src/main.ts

After compile typescripts using tsc, I got a dist folder with the compiled application:

However, typeORM still tries to get entities from the src folder instead of dist. throwing a lot of unexpectec syntax errors for parsing a TS file instead of a JS. So I change the fallowing string to the entities condiguration:

"entities": ["dist/entities/**/*.js"]

It works with node node dist/main.js but it does not works with ts-node src/main.ts

How can I configure ormconfig.json to be able to work with both (node at dist folder and ts-node at src folder)?

like image 228
Daniel Santos Avatar asked Sep 02 '19 20:09

Daniel Santos


1 Answers

I'd suggest using an ormconfig.js instead of the JSON version and use an environment variable or similar to switch between the two configs. eg; something like the following stripped down example.

const srcConfig = {
  "entities": [
    "src/entities/**/*.ts"
  ],
}

const distConfig = {
  "entities": [
    "dist/entities/**/*.js"
  ],
}

module.exports = process.env.TS_NODE ? srcConfig : distConfig;

Note you'll need to set the TS_NODE env var somewhere; I did notice there's a PR not yet merged that will do it.

like image 119
lock Avatar answered Oct 23 '22 22:10

lock