Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #24 from grigori-gru/master
Browse files Browse the repository at this point in the history
Add config module (Resolves #16)
  • Loading branch information
jougene committed Sep 18, 2019
2 parents c8e2f78 + a8fbbdd commit dcba98e
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 52 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DATABASE_URL=postgres://test:test@localhost:5433/test
DB_TYPE=sqlite
DB_NAME=interview
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ npm-debug.log*
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

development.*

## sqlite table
interview

production.*
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ test:
npm run test

setup:
cp -n .env.example .env || true
cp -n .env.example development.env || true

install:
npm install
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"start": "ts-node -r tsconfig-paths/register src/main.ts",
"start:dev": "tsc-watch -p tsconfig.build.json --onSuccess \"node dist/main.js\"",
"start:debug": "tsc-watch -p tsconfig.build.json --onSuccess \"node --inspect-brk dist/main.js\"",
"start:prod": "node dist/main.js",
"start:prod": "NODE_ENV=production node dist/main.js",
"lint": "tslint -p tsconfig.json -c tslint.json",
"test": "jest",
"test:watch": "jest --watch",
Expand Down Expand Up @@ -81,4 +81,4 @@
"type": "git",
"url": "git://github.com/Hexlet/hexlet-interview.git"
}
}
}
13 changes: 10 additions & 3 deletions src/modules/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from '../auth/auth.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import * as ormconfig from '../../ormconfig';
import { RequestModule } from '../request/request.module';
import { AuthModule } from '../auth/auth.module';
import { ConfigModule } from '../config/config.module';
import { ConfigService } from '../config/config.service';

@Module({
imports: [
TypeOrmModule.forRoot(ormconfig),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) =>
configService.dbParams,
}),
RequestModule,
AuthModule,
ConfigModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
15 changes: 15 additions & 0 deletions src/modules/config/config.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Module } from '@nestjs/common';
import { ConfigService } from './config.service';

@Module({
providers: [
{
provide: ConfigService,
useValue: new ConfigService(
`${process.env.NODE_ENV || 'development'}.env`,
),
},
],
exports: [ConfigService],
})
export class ConfigModule {}
35 changes: 35 additions & 0 deletions src/modules/config/config.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as fs from 'fs';
import * as dotenv from 'dotenv';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';

export class ConfigService {
private readonly envConfig: { [key: string]: string | number };

constructor(filePath: string) {
this.envConfig = dotenv.parse(fs.readFileSync(filePath));
}

get dbParams(): TypeOrmModuleOptions {
const SOURCE_PATH = process.env.NODE_ENV === 'production' ? 'dist' : 'src';

return {
type: this.envConfig.DB_TYPE,
host: this.envConfig.DB_HOST,
port: this.envConfig.DB_PORT,
username: this.envConfig.DB_USER,
password: this.envConfig.DB_PASSWORD,
database: this.envConfig.DB_NAME,
synchronize: false,
entities: [`${SOURCE_PATH}/**/**.entity{.ts,.js}`],
// dropSchema: process.env.NODE_ENV === 'test',
migrationsRun: true,
logging: true,
url: this.envConfig.DATABASE_URL,
migrations: [`${SOURCE_PATH}/db/migrations/*.ts`],
cli: {
migrationsDir: 'src/db/migrations',
},
keepConnectionAlive: true,
} as TypeOrmModuleOptions;
}
}
46 changes: 3 additions & 43 deletions src/ormconfig.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,5 @@
import { ConnectionOptions } from 'typeorm';
import { ConfigService } from './modules/config/config.service';

const env = process.env.NODE_ENV || 'development';
const config = new ConfigService(`${process.env.NODE_ENV}.env`);

const commonOptions = {
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: false,
migrationsRun: true,
migrations: [__dirname + '/db/migrations/**/*{.ts,.js}'],
cli: {
migrationsDir: 'src/db/migrations',
},
keepConnectionAlive: true,
};

const test: ConnectionOptions = {
type: 'sqlite',
database: ':memory:',
logging: false,
...commonOptions,
};

const development: ConnectionOptions = {
type: 'sqlite',
database: __dirname + '/db/development.sqlite',
logging: true,
...commonOptions,
};

const production: ConnectionOptions = {
type: 'postgres',
url: process.env.DATABASE_URL,
logging: true,
...commonOptions,
};

const configs: { [key: string]: ConnectionOptions } = {
development,
test,
production,
};

const config = configs[env];

export = config;
export = config.dbParams;
2 changes: 2 additions & 0 deletions test.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DB_TYPE=sqlite
DB_NAME=interview
10 changes: 8 additions & 2 deletions test/controllers/request.controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ describe('#request', () => {
});

it('create new request for interview', async () => {
const { body: { id } } = await request(app.http)
const {
body: { id },
} = await request(app.http)
.post('/request')
.send({ username: 'Vasya', profession: 'Backend PHP Developer', position: 'Junior' })
.send({
username: 'Vasya',
profession: 'Backend PHP Developer',
position: 'Junior',
})
.expect(HttpStatus.FOUND);

const newrequest = await app.repos.request.findOne(id);
Expand Down

0 comments on commit dcba98e

Please sign in to comment.