From e6077b03c34e618eb46e3167f15e609f52c16736 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 22 Sep 2022 05:11:26 +0900 Subject: [PATCH] refactor(backend): refactor dependency --- packages/backend/src/GlobalModule.ts | 15 +++++++++------ packages/backend/src/postgre.ts | 6 ++---- packages/backend/src/redis.ts | 6 ++---- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/packages/backend/src/GlobalModule.ts b/packages/backend/src/GlobalModule.ts index 525dbf14fc..8a3a821829 100644 --- a/packages/backend/src/GlobalModule.ts +++ b/packages/backend/src/GlobalModule.ts @@ -17,27 +17,30 @@ const $config: Provider = { const $db: Provider = { provide: DI.db, - useFactory: async () => { - const db = createPostgreDataSource(); + useFactory: async (config) => { + const db = createPostgreDataSource(config); return await db.initialize(); }, + inject: [DI.config], }; const $redis: Provider = { provide: DI.redis, - useFactory: () => { - const redisClient = createRedisConnection(); + useFactory: (config) => { + const redisClient = createRedisConnection(config); return redisClient; }, + inject: [DI.config], }; const $redisSubscriber: Provider = { provide: DI.redisSubscriber, - useFactory: () => { - const redisSubscriber = createRedisConnection(); + useFactory: (config) => { + const redisSubscriber = createRedisConnection(config); redisSubscriber.subscribe(config.host); return redisSubscriber; }, + inject: [DI.config], }; @Global() diff --git a/packages/backend/src/postgre.ts b/packages/backend/src/postgre.ts index f7044b374e..4da55d7c39 100644 --- a/packages/backend/src/postgre.ts +++ b/packages/backend/src/postgre.ts @@ -70,7 +70,7 @@ import { UserSecurityKey } from '@/models/entities/UserSecurityKey.js'; import { Webhook } from '@/models/entities/Webhook.js'; import { Channel } from '@/models/entities/Channel.js'; -import { loadConfig } from '@/config.js'; +import { Config } from '@/config.js'; import Logger from '@/logger.js'; import { envOption } from './env.js'; @@ -179,9 +179,7 @@ export const entities = [ const log = process.env.NODE_ENV !== 'production'; -const config = loadConfig(); - -export function createPostgreDataSource() { +export function createPostgreDataSource(config: Config) { return new DataSource({ type: 'postgres', host: config.db.host, diff --git a/packages/backend/src/redis.ts b/packages/backend/src/redis.ts index d1678ae65f..690f4715dd 100644 --- a/packages/backend/src/redis.ts +++ b/packages/backend/src/redis.ts @@ -1,9 +1,7 @@ import Redis from 'ioredis'; -import { loadConfig } from '@/config.js'; - -export function createRedisConnection(): Redis.Redis { - const config = loadConfig(); +import { Config } from '@/config.js'; +export function createRedisConnection(config: Config): Redis.Redis { return new Redis({ port: config.redis.port, host: config.redis.host,