I created a small NestJS project with typeorm and mysql
Store the ormconfig in separate file and create a datasource using the orm config
src/db/ormconfig.ts
import { DataSourceOptions, DataSource } from 'typeorm';
export const dataSourceOptions: DataSourceOptions = {
type: (process.env.TYPE as any) ?? 'mysql',
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT, 10) ?? 3306,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
synchronize: false,
bigNumberStrings: true,
multipleStatements: true,
logging: true,
entities: ['**/*.entity{ .ts,.js}'],
migrations: ['dist/db/migrations/*{.ts,.js}'],
migrationsRun: true,
};
const dataSource = new DataSource(dataSourceOptions);
export default dataSource;
Import the created data source in your app
src/app.module.ts
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";
import { dataSourceOptions } from "./db/ormconfig";
@Module({
imports: [TypeOrmModule.forRoot(dataSourceOptions)], // data source used here
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Add scripts in package.json to generate migrations
package.json
{
// deps and dev deps
"scripts": {
// other package json scripts
"typeorm": "npm run build && npx typeorm -d dist/db/ormconfig.js",
"migration:generate": "npm run typeorm -- migration:generate",
"migration:run": "npm run typeorm -- migration:run",
"migration:revert": "npm run typeorm -- migration:revert"
}
}
Now, all you have to do is consume your scripts to generate migrations. Since we are creating a build to create and run the migrations, your app may not have the context of .env variables. I usually follow these steps for creating a migration
Step 1:
- Create a shell file
app-env.shand add all the.envvariables with value
app-env.sh
export NODE_ENV=development
export HOST=0.0.0.0
export PORT=3000
# your other environment variables here
- Populate the environment variables to your shell
source app-env.sh
Step 2: Generate migrations by using the following command
npm run migration:generate -- ./src/db/migrations/YourMigrationNameHere
Step 3: Run the migrations with the following command
npm run migration:run
Step 4 (Optional): If you wanted to revert the applied migration
npm run migration:revert
Please note that unless you close your terminal and start a new terminal, any change in .env will not reflect in the app (since we sourced app-env.sh)
node.js - How to configure TypeOrm and migrations in NestJs? - Stack Overflow
How I do migrations with TypeORM using DatabaseModule with customs providers?
Can someone share an example for database Migrations?
I used Typeorm in one of our projects and I have nothing but regrets
I'm an old SQL guy and I've tried ORMs but never found a good use case. Either the project is simple enough that an ORM isn't really needed, or it's complex enough that I'm worried about call performance and any ORM I used had terrible performance for anything beyond the simplest requests.
I guess if you're on a project in a sweet spot of "complex but performance isn't a concern", and you don't know SQL and would rather learn a short-lived node API rather than SQL, then it would make sense.
More on reddit.comHello everyone, I hope you are doing well, I'm new in nestjs and I was following this section of the nestjs documentation (link docu) that is to create a DatabaseModule using custom providers, but I have no idea how to create migrations having this implementation of the DatabaseModule. Any advice?