List item
I am creating my first NestJs app using Typeorm
I cannot generate a migration to automatically create a migration with up()
and down()
methods auto generated
Here is my package.json
scripts:{
...
"typeorm": "typeorm-ts-node-commonjs",
"schema:sync": "npx typeorm-ts-node-commonjs schema:sync",
"typeorm:cache": "npx typeorm-ts-node-commonjs cache:clear",
"schema:drop": "npx typeorm-ts-node-commonjs -d ./src/data/data-source.ts",
"migration:create": "typeorm migration:create ./src/data/migrations/schema-update",
"migration:generate": "npx typeorm-ts-node-commonjs migration:generate ./src/data/migrations/schema-update -d ./src/database/data-source.ts",
"migration:show": "npx typeorm-ts-node-commonjs migration:show -d ./src/data/data-source.ts",
"migration:run": "npx typeorm-ts-node-commonjs migration:run -d ./src/data/data-source.ts",
"migration:revert": "npx typeorm-ts-node-commonjs migration:revert -d ./src/data/data-source.ts",
...},
...
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.3",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/typeorm": "^10.0.2",
"mysql2": "^3.11.3",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"typeorm": "^0.3.20"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0",
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"prettier": "^3.0.0",
"source-map-support": "^0.5.21",
"supertest": "^7.0.0",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
...
my data-source.ts
located at src/data/data-source.ts
import { DataSource } from 'typeorm';
import * as dotenv from 'dotenv';
dotenv.config();
const AppDataSource = new DataSource({
type: 'mysql',
host: 'localhost',
port: Number(process.env.DB_PORT),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
entities: ['src/data/entities/*.entity{.ts}'],
migrations: ['src/data/migrations/*{.ts,.js}'],
synchronize: false,
dropSchema: false,
logging: true,
logger: 'file',
subscribers: ['src/data/subscribers/**/*.ts'],
migrationsTableName: 'migration_table',
});
export default AppDataSource;
my app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import AppDataSource from './data/data-source';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
TypeOrmModule.forRoot(AppDataSource.options),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
type: 'mysql',
host: configService.get('DB_HOST'),
port: configService.get('DB_PORT'),
username: configService.get('DB_USERNAME'),
password: configService.get('DB_PASSWORD'),
database: configService.get('DB_DATABASE'),
entities: ['dist/**/*.entity{.ts,.js}'],
synchronize: false,
autoLoadEntities: true,
migrations: [__dirname + '/data/migrations/**/*{.ts,.js}'],
seeds: [__dirname + '/data/seeds/**/*{.ts,.js}'],
factories: [__dirname + '/factories/**/*{.ts,.js}'],
cli: {
migrationsDir: __dirname + '/data/migrations/',
},
}),
inject: [ConfigService],
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
I have import "reflect-metadata"
in my main.js
I run in my terminal npm run migration:generate
I get the below error
> backend@0.0.1 migration:generate
> npx typeorm-ts-node-commonjs migration:generate ./src/data/migrations/schema-update -d ./src/databasedata/data-source.ts
Error during migration generation:
Error: Unable to open file: "D:\project\backend\src\database\data-source.ts". Cannot find module 'D:\project\backend\src\database\data-source.ts'
Require stack:
- D:\project\node_modules\typeorm\util\ImportUtils.js
- D:\project\node_modules\typeorm\commands\CommandUtils.js
- D:\project\node_modules\typeorm\commands\SchemaSyncCommand.js
- D:\project\node_modules\typeorm\cli.js
- D:\project\node_modules\typeorm\cli-ts-node-commonjs.js
at Function.loadDataSource (D:\project\node_modules\src\commands\CommandUtils.ts:22:19)
at async Object.handler (D:\project\node_modules\src\commands\MigrationGenerateCommand.ts:78:26)
npm error Lifecycle script `migration:generate` failed with error:
npm error code 1
s /c npx typeorm-ts-node-commonjs migration:generate ./src/data/migrations/schema-update -d ./src/database/data-source.ts
N.B. The project workspace has the structure
project:
- frontend
- backend
Thanks to @mahig, there was incorrect path in package.json
"migration:generate": "npx typeorm-ts-node-commonjs migration:generate ./src/data/migrations/schema-update -d ./src/database/data-source.ts",
had to replace database with data (well, I copied it as is from the tutorial)
but got another error saying
No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migration:create" command
npm error Lifecycle script `migration:generate` failed with error:
and the solution was using dist
in the entities of the data-source.ts
. (However, I do not know why in development I shall read the migration of dist