유튜브 강의를 보고 따라 만든 프로젝트입니다.
# nestjs cli 설치
npm i -g @nestjs/cli
npx nest new task-management-api
만들어진 폴더로 이동해서 typeorm
세팅
# typeorm 설치
cd task-masnagement-api
npm install --save @nestjs/typeorm typeorm pg
참고 ) nestjs doc
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: '123456',
database: 'task-management',
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: false,
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
nest g module database
src/database/database.module.ts
파일
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import * as ormconifg from '../ormconfig'
// ormconfig를 넣어준다.
@Module({imports: [TypeOrmModule.forRoot(ormconifg)]})
export class DatabaseModule {}
npm run typeorm migration:generate -- -n <migrationNameHere>
npm run typeorm migration:run