refactor(backend): refactor dependency

This commit is contained in:
syuilo 2022-09-22 05:11:26 +09:00
parent db2d391b3a
commit e6077b03c3
3 changed files with 13 additions and 14 deletions

View file

@ -17,27 +17,30 @@ const $config: Provider = {
const $db: Provider = { const $db: Provider = {
provide: DI.db, provide: DI.db,
useFactory: async () => { useFactory: async (config) => {
const db = createPostgreDataSource(); const db = createPostgreDataSource(config);
return await db.initialize(); return await db.initialize();
}, },
inject: [DI.config],
}; };
const $redis: Provider = { const $redis: Provider = {
provide: DI.redis, provide: DI.redis,
useFactory: () => { useFactory: (config) => {
const redisClient = createRedisConnection(); const redisClient = createRedisConnection(config);
return redisClient; return redisClient;
}, },
inject: [DI.config],
}; };
const $redisSubscriber: Provider = { const $redisSubscriber: Provider = {
provide: DI.redisSubscriber, provide: DI.redisSubscriber,
useFactory: () => { useFactory: (config) => {
const redisSubscriber = createRedisConnection(); const redisSubscriber = createRedisConnection(config);
redisSubscriber.subscribe(config.host); redisSubscriber.subscribe(config.host);
return redisSubscriber; return redisSubscriber;
}, },
inject: [DI.config],
}; };
@Global() @Global()

View file

@ -70,7 +70,7 @@ import { UserSecurityKey } from '@/models/entities/UserSecurityKey.js';
import { Webhook } from '@/models/entities/Webhook.js'; import { Webhook } from '@/models/entities/Webhook.js';
import { Channel } from '@/models/entities/Channel.js'; import { Channel } from '@/models/entities/Channel.js';
import { loadConfig } from '@/config.js'; import { Config } from '@/config.js';
import Logger from '@/logger.js'; import Logger from '@/logger.js';
import { envOption } from './env.js'; import { envOption } from './env.js';
@ -179,9 +179,7 @@ export const entities = [
const log = process.env.NODE_ENV !== 'production'; const log = process.env.NODE_ENV !== 'production';
const config = loadConfig(); export function createPostgreDataSource(config: Config) {
export function createPostgreDataSource() {
return new DataSource({ return new DataSource({
type: 'postgres', type: 'postgres',
host: config.db.host, host: config.db.host,

View file

@ -1,9 +1,7 @@
import Redis from 'ioredis'; import Redis from 'ioredis';
import { loadConfig } from '@/config.js'; import { Config } from '@/config.js';
export function createRedisConnection(): Redis.Redis {
const config = loadConfig();
export function createRedisConnection(config: Config): Redis.Redis {
return new Redis({ return new Redis({
port: config.redis.port, port: config.redis.port,
host: config.redis.host, host: config.redis.host,