how to specify ormconfig.ts for typeorm?
Hey i up this conversation since i can propose you a solution.
You can put the following line in your package.json
file:
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config server/environments/database.ts",
And your ts config must export directly the config by doing that:
export = { /* your config */ };
As you can see, you can also specify the path of your config. No need for your config to be at the root level of your project.
Hope that will help you
Just remove the default
while exporting.Your ormconfig.ts
should be something like:
import env from './src/env';export = { host: env.DB_CONFIG.host, type: 'mysql', port: env.DB_CONFIG.port, username: env.DB_CONFIG.username, password: env.DB_CONFIG.password, database: env.DB_CONFIG.database, entities: [ 'src/**/**.entity{.ts,.js}', ], migrations: [ 'src/database/migrations/*.ts', ], cli: { migrationsDir: 'src/database/migrations', }, synchronize: false,};
In my case I'm using a main env.ts
file, as the database connection needs to be different depending on the environment.Also, don't forget using ts-node
for dealing with typeorm cli
in package.json
:
..."scripts": { ... "migrate:create": "ts-node ./node_modules/typeorm/cli.js migration:create -n", "migrate:up": "ts-node ./node_modules/typeorm/cli.js migration:run", "migrate:down": "ts-node ./node_modules/typeorm/cli.js migration:revert" ... }...
So creating, running or rolling back migrations should be like:
npm run migrate:create FileNamenpm run migrate:upnpm run migrate:down
At the time of writing, TypeORM only look for ormconfig.json
and ignores ormconfig.ts
. There is work in progress to support it though.
Beside having ormconfig.json you need these commands in your package.json.
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/.bin/typeorm","migration:generate": "npm run typeorm -- migration:generate --config src/config/ormconfig.json --connection --name ","migration:run": "npm run typeorm -- migration:run"