Skip to content

Commit

Permalink
fix(configuration): DRY, pushed database configuration code into a si…
Browse files Browse the repository at this point in the history
…ngle configurationManager script (#74)
  • Loading branch information
deweyjose authored and goldcaddy77 committed Mar 25, 2019
1 parent 52bd5c4 commit 6086fe5
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 31 deletions.
35 changes: 24 additions & 11 deletions src/core/code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,26 +119,39 @@ export class CodeGenerator {
private async writeOrmConfig() {
const contents = `
import { SnakeNamingStrategy } from '${this.options.warthogImportPath}';
import {
getDatabaseName,
getDatabaseEntityPaths,
getDatabaseHost,
getDatabaseLoggingLevel,
getDatabaseMigrationPaths,
getDatabaseSubscriberPaths,
getDatabasePassword,
getDatabasePort,
shouldSchronizeDatabaseSchema,
getDatabaseType,
getDatabaseUsername
} from '../utils/configurationManager'
module.exports = {
cli: {
entitiesDir: 'src/models',
migrationsDir: 'db/migrations',
subscribersDir: 'src/subscribers'
},
database: process.env.TYPEORM_DATABASE,
entities: process.env.TYPEORM_ENTITIES ? process.env.TYPEORM_ENTITIES.split(',') : ['src/**/*.model.ts'],
host: process.env.TYPEORM_HOST || 'localhost',
database: getDatabaseName(),
entities: getDatabaseEntityPaths(),
host: getDatabaseHost(),
logger: 'advanced-console',
logging: process.env.TYPEORM_LOGGING || 'all',
migrations: process.env.TYPEORM_MIGRATIONS ? process.env.TYPEORM_MIGRATIONS.split(',') : ['src/migration/**/*.ts'],
logging: getDatabaseLoggingLevel(),
migrations: getDatabaseMigrationPaths(),
namingStrategy: new SnakeNamingStrategy(),
password: process.env.TYPEORM_PASSWORD,
port: parseInt(process.env.TYPEORM_PORT || '', 10) || 5432,
subscribers: process.env.TYPEORM_SUBSCRIBERS ? process.env.TYPEORM_SUBSCRIBERS.split(',') : ['src/**/*.model.ts'],
synchronize: process.env.TYPEORM_SYNCHRONIZE === 'true',
type: 'postgres',
username: process.env.TYPEORM_USERNAME
password: getDatabasePassword(),
port: getDatabasePort(),
subscribers: getDatabaseSubscriberPaths(),
synchronize: shouldSchronizeDatabaseSchema(),
type: getDatabaseType(),
username: getDatabaseUsername()
};`;

return this.writeToGeneratedFolder('ormconfig.ts', contents);
Expand Down
48 changes: 28 additions & 20 deletions src/torm/createConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ import { ConnectionOptions, createConnection } from 'typeorm';

import { SnakeNamingStrategy } from './SnakeNamingStrategy';

import {
getDatabaseEntityPaths,
getDatabaseHost,
getDatabaseLoggingLevel,
getDatabaseMigrationPaths,
getDatabaseName,
getDatabasePassword,
getDatabasePort,
getDatabaseSubscriberPaths,
getDatabaseType,
getDatabaseUsername,
shouldSchronizeDatabaseSchema
} from '../utils/configurationManager';

export const createDBConnection = (dbOptions: Partial<ConnectionOptions> = {}) => {
const config = {
...getBaseConfig(),
Expand All @@ -24,9 +38,9 @@ export const createDBConnection = (dbOptions: Partial<ConnectionOptions> = {}) =
export const mockDBConnection = (dbOptions: Partial<ConnectionOptions> = {}) => {
return createDBConnection({
...dbOptions,
database: 'warthog.sqlite.tmp',
synchronize: false,
type: 'sqlite'
database: getDatabaseHost(),
synchronize: shouldSchronizeDatabaseSchema(),
type: getDatabaseType()
} as any);
};

Expand All @@ -37,24 +51,18 @@ function getBaseConfig() {
migrationsDir: 'src/migration',
subscribersDir: 'src/subscriber'
},
database: process.env.TYPEORM_DATABASE,
entities: process.env.TYPEORM_ENTITIES
? process.env.TYPEORM_ENTITIES.split(',')
: ['src/**/*.model.ts'],
host: process.env.TYPEORM_HOST || 'localhost',
database: getDatabaseName(),
entities: getDatabaseEntityPaths(),
host: getDatabaseHost(),
logger: 'advanced-console',
logging: process.env.TYPEORM_LOGGING || 'all',
migrations: process.env.TYPEORM_MIGRATIONS
? process.env.TYPEORM_MIGRATIONS.split(',')
: ['src/migration/**/*.ts'],
logging: getDatabaseLoggingLevel(),
migrations: getDatabaseMigrationPaths(),
namingStrategy: new SnakeNamingStrategy(),
password: process.env.TYPEORM_PASSWORD,
port: parseInt(process.env.TYPEORM_PORT || '', 10) || 5432,
subscribers: process.env.TYPEORM_SUBSCRIBERS
? process.env.TYPEORM_SUBSCRIBERS.split(',')
: ['src/**/*.model.ts'],
synchronize: process.env.TYPEORM_SYNCHRONIZE === 'true',
type: 'postgres',
username: process.env.TYPEORM_USERNAME
password: getDatabasePassword(),
port: getDatabasePort(),
subscribers: getDatabaseSubscriberPaths(),
synchronize: shouldSchronizeDatabaseSchema(),
type: getDatabaseType(),
username: getDatabaseUsername()
};
}
49 changes: 49 additions & 0 deletions src/utils/configurationManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export function getDatabaseName(): string {
return process.env.TYPEORM_DATABASE ? process.env.TYPEORM_DATABASE : '';
}

export function getDatabaseType(): string {
return process.env.TYPEORM_DATABASE_TYPE ? process.env.TYPEORM_DATABASE_TYPE : 'postgres';
}

export function getDatabaseHost(): string {
return process.env.TYPEORM_HOST || 'localhost';
}

export function shouldSchronizeDatabaseSchema(): boolean {
return process.env.TYPEORM_SYNCHRONIZE === 'true';
}

export function getDatabaseLoggingLevel() {
return process.env.TYPEORM_LOGGING || 'all';
}

export function getDatabaseEntityPaths(): string[] {
return process.env.TYPEORM_ENTITIES
? process.env.TYPEORM_ENTITIES.split(',')
: ['src/**/*.model.ts'];
}

export function getDatabaseMigrationPaths(): string[] {
return process.env.TYPEORM_MIGRATIONS
? process.env.TYPEORM_MIGRATIONS.split(',')
: ['src/migration/**/*.ts'];
}

export function getDatabaseSubscriberPaths(): string[] {
return process.env.TYPEORM_SUBSCRIBERS
? process.env.TYPEORM_SUBSCRIBERS.split(',')
: ['src/**/*.model.ts'];
}

export function getDatabaseUsername(): string | undefined {
return process.env.TYPEORM_USERNAME;
}

export function getDatabasePassword(): string | undefined {
return process.env.TYPEORM_PASSWORD;
}

export function getDatabasePort(): number {
return parseInt(process.env.TYPEORM_PORT || '', 10) || 5432;
}

0 comments on commit 6086fe5

Please sign in to comment.