Skip to content

Instantly share code, notes, and snippets.

@bay007
Forked from mafalt/app.module.ts
Created January 22, 2024 09:55
Show Gist options
  • Save bay007/73074316ba4d5f3002349e34fff6e6b5 to your computer and use it in GitHub Desktop.
Save bay007/73074316ba4d5f3002349e34fff6e6b5 to your computer and use it in GitHub Desktop.
NestJS + TypeORM configuration
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { getTypeOrmModuleOptions } from './config/orm.config';
import { CoreModule } from './core/core.module';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: `.env.${process.env.NODE_ENV}`,
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
...getTypeOrmModuleOptions(),
}),
}),
CoreModule,
],
controllers: [],
providers: [],
})
export class AppModule {}
import { TypeOrmModuleOptions } from "@nestjs/typeorm";
import { DataSource, DataSourceOptions } from "typeorm";
export const getTypeOrmModuleOptions = (): TypeOrmModuleOptions => {
const dbOptions = {
synchronize: false,
autoLoadEntities: true,
entities: [
__dirname + '/../entities/**/*.entity.{js,ts}',
],
};
switch (process.env.NODE_ENV) {
case 'development':
Object.assign(dbOptions, {
type: 'sqlite',
database: process.env.DB_NAME || 'db.sqlite',
});
break;
case 'test':
Object.assign(dbOptions, {
type: 'sqlite',
database: 'test.sqlite',
});
break;
case 'production':
Object.assign(dbOptions, {
type: 'postgres',
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT) || 5432,
database: process.env.DB_NAME,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
});
break;
default:
throw new Error('unknown environment');
}
return dbOptions;
}
export const getDataSourceOptions = (): DataSourceOptions => {
const options: DataSourceOptions = { ...getTypeOrmModuleOptions() } as DataSourceOptions;
Object.assign(options, {
migrationsTableName: '__migrations',
migrations: ['./src/migrations/*.ts'],
cli: {
"migrationsDir": "src/migrations",
},
} as Partial<DataSourceOptions>);
return options;
};
export default new DataSource(getDataSourceOptions());
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "cross-env NODE_ENV=development nest start",
"start:dev": "cross-env NODE_ENV=development nest start --watch",
"start:debug": "cross-env NODE_ENV=development nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "cross-env NODE_ENV=test jest",
"test:watch": "cross-env NODE_ENV=test jest --watch",
"test:cov": "cross-env NODE_ENV=test jest --coverage",
"test:debug": "cross-env NODE_ENV=test node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "cross-env NODE_ENV=test jest --config ./test/jest-e2e.json",
"typeorm": "cross-env NODE_ENV=development typeorm-ts-node-esm",
"generate-migration": "npm run typeorm migration:generate -- -d ./src/config/orm.config.ts",
"run-migration": "npm run typeorm migration:run -- -d ./src/config/orm.config.ts",
"revert-migration": "npm run typeorm migration:revert -- -d ./src/config/orm.config.ts"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment