{"id":14111155,"url":"https://github.com/ambroiseRabier/typeorm-nestjs-migration-example","last_synced_at":"2025-08-01T12:31:29.804Z","repository":{"id":38375652,"uuid":"187394846","full_name":"ambroiseRabier/typeorm-nestjs-migration-example","owner":"ambroiseRabier","description":"\"Example of how to use migrations feature of TypeORM with NestJS.","archived":false,"fork":false,"pushed_at":"2019-05-18T19:03:19.000Z","size":82,"stargazers_count":253,"open_issues_count":2,"forks_count":30,"subscribers_count":13,"default_branch":"master","last_synced_at":"2024-12-05T21:34:02.657Z","etag":null,"topics":["docs","migrations","nestjs","nestjs-backend","typeorm"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ambroiseRabier.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-18T18:57:12.000Z","updated_at":"2024-10-10T09:28:36.000Z","dependencies_parsed_at":"2022-08-09T09:15:45.026Z","dependency_job_id":null,"html_url":"https://github.com/ambroiseRabier/typeorm-nestjs-migration-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ambroiseRabier/typeorm-nestjs-migration-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ambroiseRabier%2Ftypeorm-nestjs-migration-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ambroiseRabier%2Ftypeorm-nestjs-migration-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ambroiseRabier%2Ftypeorm-nestjs-migration-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ambroiseRabier%2Ftypeorm-nestjs-migration-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ambroiseRabier","download_url":"https://codeload.github.com/ambroiseRabier/typeorm-nestjs-migration-example/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ambroiseRabier%2Ftypeorm-nestjs-migration-example/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268223049,"owners_count":24215710,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-01T02:00:08.611Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docs","migrations","nestjs","nestjs-backend","typeorm"],"created_at":"2024-08-14T10:03:10.197Z","updated_at":"2025-08-01T12:31:29.424Z","avatar_url":"https://github.com/ambroiseRabier.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"https://docs.nestjs.com/\n\n### TypeORM migration\n\n#### Objectives\nGenerate and use migrations instead of syncing database. In dev and prod.\nThis is the recommended method by TypeORM once you have data on prod, to avoid any loss.\n\n#### Pre-requisites\nTypeORM installed: https://docs.nestjs.com/techniques/database\n\n#### Install\n\n\u003e src/app.module.ts\n```ts\nimport { DynamicModule, Module } from '@nestjs/common';\nimport { AppController } from './app.controller';\nimport { AppService } from './app.service';\nimport { TypeOrmModule } from '@nestjs/typeorm';\nimport * as ormconfig from './ormconfig';\n\nexport function DatabaseOrmModule(): DynamicModule {\n  // we could load the configuration from dotEnv here,\n  // but typeORM cli would not be able to find the configuration file.\n\n  return TypeOrmModule.forRoot(ormconfig);\n}\n\n@Module({\n  imports: [\n    TypeOrmModule.forRoot(ormconfig)\n    // or\n    // DatabaseOrmModule(),\n  ],\n  controllers: [AppController],\n  providers: [AppService],\n})\nexport class AppModule {}\n```\n\n\n\u003e src/ormconfig.ts\n```ts\nimport {ConnectionOptions} from 'typeorm';\n\n// You can load you .env file here synchronously using dotenv package (not installed here),\n// import * as dotenv from 'dotenv';\n// import * as fs from 'fs';\n// const environment = process.env.NODE_ENV || 'development';\n// const data: any = dotenv.parse(fs.readFileSync(`${environment}.env`));\n// You can also make a singleton service that load and expose the .env file content.\n// ...\n\n\n// Check typeORM documentation for more information.\nconst config: ConnectionOptions = {\n  type: 'postgres',\n  host: 'localhost',\n  port: 5432,\n  username: 'postgres',\n  password: 'pwd',\n  database: 'migrationexample',\n  entities: [__dirname + '/**/*.entity{.ts,.js}'],\n\n  // We are using migrations, synchronize should be set to false.\n  synchronize: false,\n\n  // Run migrations automatically,\n  // you can disable this if you prefer running migration manually.\n  migrationsRun: true,\n  logging: true,\n  logger: 'file',\n\n  // Allow both start:prod and start:dev to use migrations\n  // __dirname is either dist or src folder, meaning either\n  // the compiled js in prod or the ts in dev.\n  migrations: [__dirname + '/migrations/**/*{.ts,.js}'],\n  cli: {\n    // Location of migration should be inside src folder\n    // to be compiled into dist/ folder.\n    migrationsDir: 'src/migrations',\n  },\n};\n\nexport = config;\n```\n\n\n\u003e package.json\n```json\n\"scripts\": {\n    \"typeorm\": \"ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/ormconfig.ts\",\n    \"typeorm:migrate\": \"npm run typeorm migration:generate -- -n\",\n    \"typeorm:run\": \"npm run typeorm migration:run\"\n}\n```\n\n#### Usage\n1. `npm run typeorm:migrate \u003cmyEntity-migration\u003e`\n2. Check your migration queries in `src/migrations`\n3. `npm run start:dev` or `npm run start:prod` or `npm run typeorm:run`\n\nIf everything went well, you have up to date entites and a `migrations` table listing applied migrations.\n\n#### Additionnal information\n- If you set `migrationsRun` to false in ormconfig.ts, you will have to use `npm run typeorm:run` to apply the migration, otherwise all migrations are applied automatically at application start.\n- If you do not set `--config` parameter typeorm seek a valid configuration file at the root of the project.\n- You do not want `ormconfig.ts` at the root of the project, otherwise it change /dist structure, you would have to change `start:prod: node dist/main.js` to `start:prod: node dist/src/main.js`.\n\n@SeeAlso https://github.com/typeorm/typeorm/blob/master/docs/migrations.md  \n@SeeAlso https://github.com/typeorm/typeorm/blob/master/docs/using-cli.md#notes-on-entity-files-written-in-typescript  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FambroiseRabier%2Ftypeorm-nestjs-migration-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FambroiseRabier%2Ftypeorm-nestjs-migration-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FambroiseRabier%2Ftypeorm-nestjs-migration-example/lists"}