Last active
November 4, 2020 23:04
-
-
Save freaz/930906795734240804644b2450ff94af to your computer and use it in GitHub Desktop.
Nest.js + TypeORM and shared config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Module } from '@nestjs/common'; | |
import { TypeOrmModule } from '@nestjs/typeorm'; | |
import { ConfigModule, ConfigService } from '@nestjs/config'; | |
import * as Joi from '@hapi/joi'; | |
@Module({ | |
imports: [ | |
ConfigModule.forRoot({ | |
isGlobal: true, | |
validationSchema: Joi.object({ | |
POSTGRES_HOST: Joi.string().required(), | |
POSTGRES_PORT: Joi.number().required(), | |
POSTGRES_USER: Joi.string().required(), | |
POSTGRES_PASSWORD: Joi.string().required(), | |
POSTGRES_DB: Joi.string().required(), | |
PORT: Joi.number(), | |
}) | |
}), | |
TypeOrmModule.forRootAsync({ | |
imports: [ConfigModule], | |
inject: [ConfigService], | |
useFactory: (configService: ConfigService) => ({ | |
type: 'postgres', | |
host: configService.get('POSTGRES_HOST'), | |
port: configService.get('POSTGRES_PORT'), | |
username: configService.get('POSTGRES_USER'), | |
password: configService.get('POSTGRES_PASSWORD'), | |
database: configService.get('POSTGRES_DB'), | |
synchronize: true, | |
logging: false, | |
entities: [__dirname + '/**/*.entity{.ts,.js}'], | |
subscribers: [__dirname + '/**/*.subscriber{.ts,.js}'], | |
migrations: [__dirname + '/migrations/*{.ts,.js}'], | |
cli: { | |
migrationsDir: __dirname + '/migrations', | |
}, | |
}) | |
}), | |
], | |
}) | |
export class AppModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NestFactory } from '@nestjs/core'; | |
import { TYPEORM_MODULE_OPTIONS } from '@nestjs/typeorm/dist/typeorm.constants'; | |
import { AppModule } from './src/ap.module'; | |
const setup = async () => { | |
const app = await NestFactory.create(AppModule); | |
const typeOrmModuleOptions = app.get(TYPEORM_MODULE_OPTIONS); | |
await app.close(); | |
return typeOrmModuleOptions; | |
}; | |
module.exports = setup(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "nestjs-typeorm", | |
"version": "0.0.1", | |
"description": "", | |
"author": "", | |
"scripts": { | |
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js" | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment