#nest.js & typeorm = migration in directory "migrations" how?

10 messages · Page 1 of 1 (latest)

visual basalt
#

hi everyone, I'm a novice nest developer, and I can't create migrations in the migrations folder. Is it possible to do this without specifying it in the directory attributes?
my command migrations:
"typeorm": "ts-node ./node_modules/typeorm/cli", "migration:run": "npm run typeorm migration:run -- -d ./src/config/typeorm.ts", "migration:generate": "npm run typeorm -- -d ./src/config/typeorm.ts migration:generate", "migration:create": "npm run typeorm -- migration:create", "migration:revert": "npm run typeorm -- -d ./src/config/typeorm.ts migration:revert",

my ./src/config/typeorm.ts
`import { registerAs } from "@nestjs/config";
import { config as dotenvConfig } from 'dotenv';
import { DataSource, DataSourceOptions, Migration } from "typeorm";

dotenvConfig({ path: '.env' });

const config = {
cli: {
migrationsDir: 'src/migrations',
},
type: 'mysql',
host: ${process.env.DATABASE_HOST},
port: ${process.env.DATABASE_PORT},
username: ${process.env.DATABASE_USERNAME},
password: ${process.env.DATABASE_PASSWORD},
database: ${process.env.DATABASE_NAME},
entities: ["dist/**/.entity{.ts,.js}"],
migrations: ["dist/migrations/
{.ts,.js}"],
migrationsTableName: 'migrations',
autoLoadEntities: true,
synchronize: false,
migrationsDir: 'src/migrations',
}

export default registerAs('typeorm', () => config)
export const connectionSource = new DataSource(config as DataSourceOptions);`

command for migration npm run migration:generate test222 - generated migration file on root directory, but command npm run migration:generate ./src/migrations/test - generated in the directory migrations. How make npm run migration:generate test222 to migrations directory? Thanks

lost whale
#

To generate migrations directly in the src/migrations directory without specifying the path each time, adjust your migration:generate command like this:

"migration:generate": "npm run typeorm -- -d ./src/config/typeorm.ts migration:generate -p src/migrations",

This ensures the generated migration test222 will be placed in the src/migrations folder.

knotty heart
lost whale
#

so far i found these 2 solution, both 2 solutions works for me , double tested

solution 1:

"scripts": {
  "migration:generate": "read -p 'Enter migration name: ' name && npm run typeorm -- -d src/config/typeorm.ts migration:generate ./src/migrations/$name"
}

then run

npm run migration:generate

solution 2 -
Create a generate-migration.sh script in your project root

#!/bin/bash
name=$1
if [ -z "$name" ]; then
  echo "No migration name provided. Usage: npm run migration:generate -- <name>"
  exit 1
fi
npm run typeorm -- -d src/config/typeorm.ts migration:generate "./src/migrations/$name"

next run this

chmod +x generate-migration.sh
npm run migration:generate -- your_migration_name

let me know if this works for you. thank you

visual basalt
#

I on windows 11 system, not .sh script use

lost whale
#

There is another way you can using small Node.js script that accepts command-line arguments and then executes the desired command with those arguments

#

If you need i can create script for you . Thank you

visual basalt
#

@knotty heart do you know how to make a migration specifying only the migration name without a directory? npm run migration:generate test222