Skip to content

Instantly share code, notes, and snippets.

@freaz
Last active November 4, 2020 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freaz/930906795734240804644b2450ff94af to your computer and use it in GitHub Desktop.
Save freaz/930906795734240804644b2450ff94af to your computer and use it in GitHub Desktop.
Nest.js + TypeORM and shared config
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 { }
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();
{
"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