Skip to content

Commit 4a76fd8

Browse files
committed
✨ [DEV-29] DB 비활성화 옵션 추가
1 parent a769efc commit 4a76fd8

File tree

5 files changed

+53
-28
lines changed

5 files changed

+53
-28
lines changed

.env.example

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ AWS_REGION=ap-northeast-2
2424
#
2525
# PostgreSQL DATABASE
2626
#
27+
#TYPEORM_ENABLED=true
2728
#TYPEORM_CONNECTION=postgres
2829
#TYPEORM_HOST=localhost
2930
#TYPEORM_PORT=5432
@@ -37,6 +38,7 @@ AWS_REGION=ap-northeast-2
3738
#
3839
# MySQL DATABASE
3940
#
41+
TYPEORM_ENABLED=true
4042
TYPEORM_CONNECTION=mysql
4143
TYPEORM_HOST=localhost
4244
TYPEORM_PORT=3306

.env.test

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ AWS_REGION=ap-northeast-2
2424
#
2525
# DATABASE
2626
#
27+
TYPEORM_ENABLED=true
2728
TYPEORM_CONNECTION=sqlite
2829
TYPEORM_DATABASE=./mydb.sql
2930
TYPEORM_LOGGING=error

src/env.ts

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const env = {
5151
output: getOsEnv('LOG_OUTPUT'),
5252
},
5353
db: {
54+
enabled: toBool(getOsEnv('TYPEORM_ENABLED')),
5455
type: getOsEnv('TYPEORM_CONNECTION'),
5556
host: getOsEnvOptional('TYPEORM_HOST'),
5657
port: toNumber(getOsEnvOptional('TYPEORM_PORT')),

src/loaders/expressLoader.ts

+30-10
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@ import { createExpressServer } from 'routing-controllers';
55
import { authorizationChecker } from '../auth/authorizationChecker';
66
import { currentUserChecker } from '../auth/currentUserChecker';
77
import { env } from '../env';
8+
import { RoutingControllersOptions } from 'routing-controllers/RoutingControllersOptions';
89

910
export const expressLoader: MicroframeworkLoader = (settings: MicroframeworkSettings | undefined) => {
1011
if (settings) {
11-
const connection = settings.getData('connection');
12-
13-
/**
14-
* We create a new express server instance.
15-
* We could have also use useExpressServer here to attach controllers to an existing express instance.
16-
*/
17-
const expressApp: Application = createExpressServer({
12+
const expressOptions: RoutingControllersOptions = {
1813
cors: true,
1914
classTransformer: true,
2015
routePrefix: env.app.routePrefix,
@@ -27,12 +22,37 @@ export const expressLoader: MicroframeworkLoader = (settings: MicroframeworkSett
2722
middlewares: env.app.dirs.middlewares,
2823
interceptors: env.app.dirs.interceptors,
2924

25+
/**
26+
* class-validator를 활용한 Request Payload 타입 검증
27+
*/
28+
validation: true,
29+
30+
// TODO: DB를 사용 안하게 하더라도 Authorization 기능은 쓸 수 있게 개선
31+
// /**
32+
// * Authorization features
33+
// */
34+
// authorizationChecker: authorizationChecker(),
35+
// currentUserChecker: currentUserChecker(),
36+
};
37+
38+
// DB 사용할 경우
39+
if (env.db.enabled) {
40+
/**
41+
* TypeOrm connection
42+
*/
43+
const connection = settings.getData('connection');
3044
/**
3145
* Authorization features
3246
*/
33-
authorizationChecker: authorizationChecker(connection),
34-
currentUserChecker: currentUserChecker(connection),
35-
});
47+
expressOptions.authorizationChecker = authorizationChecker(connection);
48+
expressOptions.currentUserChecker = currentUserChecker(connection);
49+
}
50+
51+
/**
52+
* We create a new express server instance.
53+
* We could have also use useExpressServer here to attach controllers to an existing express instance.
54+
*/
55+
const expressApp: Application = createExpressServer(expressOptions);
3656

3757
// Run application to listen on given port
3858
if (!env.isTest) {

src/loaders/typeormLoader.ts

+19-18
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@ import { createConnection, getConnectionOptions } from 'typeorm';
44
import { env } from '../env';
55

66
export const typeormLoader: MicroframeworkLoader = async (settings: MicroframeworkSettings | undefined) => {
7+
if (env.db.enabled) {
8+
const loadedConnectionOptions = await getConnectionOptions();
79

8-
const loadedConnectionOptions = await getConnectionOptions();
10+
const connectionOptions = Object.assign(loadedConnectionOptions, {
11+
type: env.db.type as any, // See createConnection options for valid types
12+
host: env.db.host,
13+
port: env.db.port,
14+
username: env.db.username,
15+
password: env.db.password,
16+
database: env.db.database,
17+
synchronize: env.db.synchronize,
18+
logging: env.db.logging,
19+
entities: env.app.dirs.entities,
20+
migrations: env.app.dirs.migrations,
21+
});
922

10-
const connectionOptions = Object.assign(loadedConnectionOptions, {
11-
type: env.db.type as any, // See createConnection options for valid types
12-
host: env.db.host,
13-
port: env.db.port,
14-
username: env.db.username,
15-
password: env.db.password,
16-
database: env.db.database,
17-
synchronize: env.db.synchronize,
18-
logging: env.db.logging,
19-
entities: env.app.dirs.entities,
20-
migrations: env.app.dirs.migrations,
21-
});
23+
const connection = await createConnection(connectionOptions);
2224

23-
const connection = await createConnection(connectionOptions);
24-
25-
if (settings) {
26-
settings.setData('connection', connection);
27-
settings.onShutdown(() => connection.close());
25+
if (settings) {
26+
settings.setData('connection', connection);
27+
settings.onShutdown(() => connection.close());
28+
}
2829
}
2930
};

0 commit comments

Comments
 (0)