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 = {
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()

View file

@ -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,

View file

@ -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,