[email protected] unclear release documentation about migrations dir

4.7k Views Asked by At

I recently moved from [email protected] to version 0.3 and there is this sentence on the docs of the release: https://github.com/typeorm/typeorm/releases/tag/0.3.0 that doesn't make much sense to me (under the section of DEPRECATED):

entities, migrations, subscribers options inside DataSourceOptions accepting string directories support is deprecated. You'll be only able to pass entity references in the future versions.

From this I get that we now must specify the entities and can not use (or better said, won't be able to use in the future) wildcard paths i.e. entities: ['dist/**/*.entity.{ts,js}'] instead we must use: entities: [User, AnoherEntity...]

but does this apply for migrations too? I find it confusing because migrations are generated by the cli of typeorm, meaning we must generate the migration i.e. 1652169197705-SomeMigration and then add that file name with its full path into the DataSource's migrations? migrations: ['1652169197705-SomeMigration'...]

Thanks!

2

There are 2 best solutions below

0
James Claridge On

Yes it does :)

I agree the documentation is in need of some serious love and old examples have been updated but their readmes have not - this is causing some significant confusion.

1
Akshay Joy On

I hope this can help you. I figured out this migration and DB access as two different configuration.

for Migration I use ormconfig.ts

this will be my ormconfig.ts

import * as dotenv from 'dotenv';

import { SnakeNamingStrategy } from './src/shared/typeorm/snake-naming.strategy';

dotenv.config({
  path: `.env`,
});

for (const envName of Object.keys(process.env)) {
  process.env[envName] = process.env[envName].replace(/\\n/g, '\n');
}
 // use this if you want to use with Connection String Parts
 module.exports = {
   name: 'default',
   type: 'postgres',
   host: process.env.DB_HOST,
   port: +process.env.DB_PORT,
   username: process.env.DB_USER,
   password: process.env.DB_PASSWORD,
   database: process.env.DB_NAME,
   entities: [__dirname + '/src/**/*.entity{.ts,.js}'],
   migrationsTableName: 'migrations',
   migrations: ['dist/src/migrations/*.js'],
   cli: {
     migrationsDir: 'src/migrations',
   },
   synchronize: false,
   logging: ['error', 'query'],
   timezone: 'Z'
 };
// use this if you want to use with Connection DB url
module.exports = {
  name: 'default',
  type: 'postgres',
  url: process.env.DB_URL,
  entities: [__dirname + '/src/**/*.entity{.ts,.js}'],
  migrationsTableName: 'migrations',
  migrations: ['dist/src/migrations/*.js'],
  cli: {
    migrationsDir: 'src/migrations',
  },
  synchronize: false,
  logging: ['error', 'query'],
  timezone: 'Z'
};

if you have isolated example of your codebase I can help with you that

migrationsDir: 'src/migrations' -- this is where your migration ts scripts is going to be created

migrations: ['dist/src/migrations/*.js'], -- this is where when you have build the migration as output from dist

package.json should be like this

"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config ./ormconfig.ts",
    "migration:generate": "npm run build & npm run typeorm migration:generate -- -n",
    "migration:revert": "ts-node node_modules/typeorm/cli.js migration:revert -f ormconfig",
    "migration:create": "ts-node node_modules/typeorm/cli.js migration:create -f ormconfig -n",
    "migration:run": "npm run build & npm run typeorm migration:run",