All Projects → ambroiseRabier → Typeorm Nestjs Migration Example

ambroiseRabier / Typeorm Nestjs Migration Example

"Example of how to use migrations feature of TypeORM with NestJS.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Typeorm Nestjs Migration Example

Domain Driven Hexagon
Guide on Domain-Driven Design, software architecture, design patterns, best practices etc.
Stars: ✭ 4,417 (+2409.66%)
Mutual labels:  nestjs
Config
Configuration module for Nest framework (node.js) 🍓
Stars: ✭ 161 (-8.52%)
Mutual labels:  nestjs
Ng Aquila
Angular UI Component library for the Open Insurance Platform
Stars: ✭ 170 (-3.41%)
Mutual labels:  docs
Nestjs Flub
Pretty Error 😫 Stack Viewer for NestJS Framework 🛠️
Stars: ✭ 158 (-10.23%)
Mutual labels:  nestjs
Riot Cheatsheet
🚩 Riot at a glance
Stars: ✭ 159 (-9.66%)
Mutual labels:  docs
Wipi
nextjs + nestjs + TypeScript +MySQL 开发的前后端分离,服务端渲染的博客系统
Stars: ✭ 163 (-7.39%)
Mutual labels:  nestjs
Symfony Docs
The Symfony documentation
Stars: ✭ 1,924 (+993.18%)
Mutual labels:  docs
Edit Text
Collaborative rich text editor for the web. Written in Rust + WebAssembly.
Stars: ✭ 171 (-2.84%)
Mutual labels:  docs
Sentry Docs
The new place for the sentry documentation (and tools to build it)
Stars: ✭ 160 (-9.09%)
Mutual labels:  docs
Serve Static
Serve static websites (SPA's) using Nest framework (node.js) 🥦
Stars: ✭ 167 (-5.11%)
Mutual labels:  nestjs
Pizza Sync
Pizza-Sync is a web app built on the frontend with angular, ngrx and on the backend with Nest. It let you and your friends/colleagues choose a pizza before placing a group order. Built using https://github.com/maxime1992/angular-ngrx-starter
Stars: ✭ 158 (-10.23%)
Mutual labels:  nestjs
Docs
Various Yii 3.0 related documentation
Stars: ✭ 159 (-9.66%)
Mutual labels:  docs
Nestjs Mongoose Crud
Nest.js crud module for mongoose models without `nestjsx/crud`
Stars: ✭ 164 (-6.82%)
Mutual labels:  nestjs
Jsdoc
An API documentation generator for JavaScript.
Stars: ✭ 12,555 (+7033.52%)
Mutual labels:  docs
Typescript Definitive Guide
TypeScript: Definitive Guide (book and docs in one)
Stars: ✭ 169 (-3.98%)
Mutual labels:  docs
Express Typescript Boilerplate
A delightful way to building a RESTful API with NodeJs & TypeScript by @w3tecch
Stars: ✭ 2,293 (+1202.84%)
Mutual labels:  migrations
Postgres Migrations
🐦 A Stack Overflow-inspired PostgreSQL migration library with strict ordering and immutable migrations
Stars: ✭ 161 (-8.52%)
Mutual labels:  migrations
Nestjs Microservice Architecture
A reference architecture for building microservices with Nest
Stars: ✭ 174 (-1.14%)
Mutual labels:  nestjs
Django Swappable Models
Swapper - The unofficial Django swappable models API.
Stars: ✭ 169 (-3.98%)
Mutual labels:  migrations
Webpack.js.org
Repository for webpack documentation and more!
Stars: ✭ 2,049 (+1064.2%)
Mutual labels:  docs

https://docs.nestjs.com/

TypeORM migration

Objectives

Generate and use migrations instead of syncing database. In dev and prod. This is the recommended method by TypeORM once you have data on prod, to avoid any loss.

Pre-requisites

TypeORM installed: https://docs.nestjs.com/techniques/database

Install

src/app.module.ts

import { DynamicModule, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import * as ormconfig from './ormconfig';

export function DatabaseOrmModule(): DynamicModule {
  // we could load the configuration from dotEnv here,
  // but typeORM cli would not be able to find the configuration file.

  return TypeOrmModule.forRoot(ormconfig);
}

@Module({
  imports: [
    TypeOrmModule.forRoot(ormconfig)
    // or
    // DatabaseOrmModule(),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

src/ormconfig.ts

import {ConnectionOptions} from 'typeorm';

// You can load you .env file here synchronously using dotenv package (not installed here),
// import * as dotenv from 'dotenv';
// import * as fs from 'fs';
// const environment = process.env.NODE_ENV || 'development';
// const data: any = dotenv.parse(fs.readFileSync(`${environment}.env`));
// You can also make a singleton service that load and expose the .env file content.
// ...


// Check typeORM documentation for more information.
const config: ConnectionOptions = {
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'postgres',
  password: 'pwd',
  database: 'migrationexample',
  entities: [__dirname + '/**/*.entity{.ts,.js}'],

  // We are using migrations, synchronize should be set to false.
  synchronize: false,

  // Run migrations automatically,
  // you can disable this if you prefer running migration manually.
  migrationsRun: true,
  logging: true,
  logger: 'file',

  // Allow both start:prod and start:dev to use migrations
  // __dirname is either dist or src folder, meaning either
  // the compiled js in prod or the ts in dev.
  migrations: [__dirname + '/migrations/**/*{.ts,.js}'],
  cli: {
    // Location of migration should be inside src folder
    // to be compiled into dist/ folder.
    migrationsDir: 'src/migrations',
  },
};

export = config;

package.json

"scripts": {
    "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/ormconfig.ts",
    "typeorm:migrate": "npm run typeorm migration:generate -- -n",
    "typeorm:run": "npm run typeorm migration:run"
}

Usage

  1. npm run typeorm:migrate <myEntity-migration>
  2. Check your migration queries in src/migrations
  3. npm run start:dev or npm run start:prod or npm run typeorm:run

If everything went well, you have up to date entites and a migrations table listing applied migrations.

Additionnal information

  • 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.
  • If you do not set --config parameter typeorm seek a valid configuration file at the root of the project.
  • 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.

@SeeAlso https://github.com/typeorm/typeorm/blob/master/docs/migrations.md
@SeeAlso https://github.com/typeorm/typeorm/blob/master/docs/using-cli.md#notes-on-entity-files-written-in-typescript

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].