Created
July 9, 2022 18:42
-
-
Save mafalt/431dc31d59bb22d1397b62e668240f4a to your computer and use it in GitHub Desktop.
NestJS + TypeORM configuration
This file contains hidden or 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 { 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 {} |
This file contains hidden or 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 { 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()); |
This file contains hidden or 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
"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
Can someone help with latest configurations guide please - using Data source?
Throwing errors