refactor: atualizações e remoção de módulos não utilizados
This commit is contained in:
21
src/core/configs/cache/IRedisClient.ts
vendored
21
src/core/configs/cache/IRedisClient.ts
vendored
@@ -1,10 +1,13 @@
|
||||
export interface IRedisClient {
|
||||
get<T>(key: string): Promise<T | null>;
|
||||
set<T>(key: string, value: T, ttlSeconds?: number): Promise<void>;
|
||||
del(key: string): Promise<void>;
|
||||
del(...keys: string[]): Promise<void>;
|
||||
keys(pattern: string): Promise<string[]>;
|
||||
ttl(key: string): Promise<number>;
|
||||
eval(script: string, numKeys: number, ...keysAndArgs: (string | number)[]): Promise<any>;
|
||||
}
|
||||
|
||||
get<T>(key: string): Promise<T | null>;
|
||||
set<T>(key: string, value: T, ttlSeconds?: number): Promise<void>;
|
||||
del(key: string): Promise<void>;
|
||||
del(...keys: string[]): Promise<void>;
|
||||
keys(pattern: string): Promise<string[]>;
|
||||
ttl(key: string): Promise<number>;
|
||||
eval(
|
||||
script: string,
|
||||
numKeys: number,
|
||||
...keysAndArgs: (string | number)[]
|
||||
): Promise<any>;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
import { RedisClientAdapter } from './redis-client.adapter';
|
||||
export const RedisClientToken = 'RedisClientInterface';
|
||||
|
||||
|
||||
10
src/core/configs/cache/redis-client.adapter.ts
vendored
10
src/core/configs/cache/redis-client.adapter.ts
vendored
@@ -6,13 +6,13 @@ import { IRedisClient } from './IRedisClient';
|
||||
export class RedisClientAdapter implements IRedisClient {
|
||||
constructor(
|
||||
@Inject('REDIS_CLIENT')
|
||||
private readonly redis: Redis
|
||||
private readonly redis: Redis,
|
||||
) {}
|
||||
|
||||
async get<T>(key: string): Promise<T | null> {
|
||||
const data = await this.redis.get(key);
|
||||
if (!data) return null;
|
||||
|
||||
|
||||
try {
|
||||
return JSON.parse(data);
|
||||
} catch (error) {
|
||||
@@ -43,7 +43,11 @@ export class RedisClientAdapter implements IRedisClient {
|
||||
return this.redis.ttl(key);
|
||||
}
|
||||
|
||||
async eval(script: string, numKeys: number, ...keysAndArgs: (string | number)[]): Promise<any> {
|
||||
async eval(
|
||||
script: string,
|
||||
numKeys: number,
|
||||
...keysAndArgs: (string | number)[]
|
||||
): Promise<any> {
|
||||
return this.redis.eval(script, numKeys, ...keysAndArgs);
|
||||
}
|
||||
}
|
||||
|
||||
2
src/core/configs/cache/redis.module.ts
vendored
2
src/core/configs/cache/redis.module.ts
vendored
@@ -9,4 +9,4 @@ import { RedisClientAdapterProvider } from './redis-client.adapter.provider';
|
||||
providers: [RedisProvider, RedisClientAdapterProvider],
|
||||
exports: [RedisProvider, RedisClientAdapterProvider],
|
||||
})
|
||||
export class RedisModule {}
|
||||
export class RedisModule {}
|
||||
|
||||
36
src/core/configs/cache/redis.provider.ts
vendored
36
src/core/configs/cache/redis.provider.ts
vendored
@@ -1,21 +1,21 @@
|
||||
import { Provider } from '@nestjs/common';
|
||||
import Redis from 'ioredis';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Provider } from '@nestjs/common';
|
||||
import Redis from 'ioredis';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
export const RedisProvider: Provider = {
|
||||
provide: 'REDIS_CLIENT',
|
||||
useFactory: (configService: ConfigService) => {
|
||||
const redis = new Redis({
|
||||
host: configService.get<string>('REDIS_HOST', '10.1.1.109'),
|
||||
port: configService.get<number>('REDIS_PORT', 6379),
|
||||
password: configService.get<string>('REDIS_PASSWORD', '1234'),
|
||||
});
|
||||
export const RedisProvider: Provider = {
|
||||
provide: 'REDIS_CLIENT',
|
||||
useFactory: (configService: ConfigService) => {
|
||||
const redis = new Redis({
|
||||
host: configService.get<string>('REDIS_HOST', '10.1.1.109'),
|
||||
port: configService.get<number>('REDIS_PORT', 6379),
|
||||
password: configService.get<string>('REDIS_PASSWORD', '1234'),
|
||||
});
|
||||
|
||||
redis.on('error', (err) => {
|
||||
console.error('Erro ao conectar ao Redis:', err);
|
||||
});
|
||||
redis.on('error', (err) => {
|
||||
console.error('Erro ao conectar ao Redis:', err);
|
||||
});
|
||||
|
||||
return redis;
|
||||
},
|
||||
inject: [ConfigService],
|
||||
};
|
||||
return redis;
|
||||
},
|
||||
inject: [ConfigService],
|
||||
};
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { registerAs } from '@nestjs/config';
|
||||
|
||||
export const databaseConfig = registerAs('database', () => ({
|
||||
oracle: {
|
||||
connectString: `(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ${process.env.ORACLE_HOST})(PORT = ${process.env.ORACLE_PORT})))(CONNECT_DATA = (SERVICE_NAME = ${process.env.ORACLE_SERVICE})))`,
|
||||
username: process.env.ORACLE_USER,
|
||||
password: process.env.ORACLE_PASSWORD,
|
||||
},
|
||||
postgres: {
|
||||
host: process.env.POSTGRES_HOST,
|
||||
port: parseInt(process.env.POSTGRES_PORT || '5432', 10),
|
||||
username: process.env.POSTGRES_USER,
|
||||
password: process.env.POSTGRES_PASSWORD,
|
||||
database: process.env.POSTGRES_DB,
|
||||
},
|
||||
}));
|
||||
import { registerAs } from '@nestjs/config';
|
||||
|
||||
export const databaseConfig = registerAs('database', () => ({
|
||||
oracle: {
|
||||
connectString: `(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ${process.env.ORACLE_HOST})(PORT = ${process.env.ORACLE_PORT})))(CONNECT_DATA = (SERVICE_NAME = ${process.env.ORACLE_SERVICE})))`,
|
||||
username: process.env.ORACLE_USER,
|
||||
password: process.env.ORACLE_PASSWORD,
|
||||
},
|
||||
postgres: {
|
||||
host: process.env.POSTGRES_HOST,
|
||||
port: parseInt(process.env.POSTGRES_PORT || '5432', 10),
|
||||
username: process.env.POSTGRES_USER,
|
||||
password: process.env.POSTGRES_PASSWORD,
|
||||
database: process.env.POSTGRES_DB,
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -2,8 +2,6 @@ import { DataSourceOptions } from 'typeorm';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import * as oracledb from 'oracledb';
|
||||
|
||||
|
||||
|
||||
oracledb.initOracleClient({ libDir: process.env.ORACLE_CLIENT_LIB_DIR });
|
||||
|
||||
// Definir a estratégia de pool padrão para Oracle
|
||||
@@ -12,19 +10,22 @@ oracledb.queueTimeout = 60000; // timeout da fila em milissegundos
|
||||
oracledb.poolIncrement = 1; // incremental de conexões
|
||||
|
||||
export function createOracleConfig(config: ConfigService): DataSourceOptions {
|
||||
|
||||
const poolMin = parseInt(config.get('ORACLE_POOL_MIN', '5'));
|
||||
const poolMax = parseInt(config.get('ORACLE_POOL_MAX', '20'));
|
||||
const poolIncrement = parseInt(config.get('ORACLE_POOL_INCREMENT', '5'));
|
||||
const poolTimeout = parseInt(config.get('ORACLE_POOL_TIMEOUT', '30000'));
|
||||
const idleTimeout = parseInt(config.get('ORACLE_POOL_IDLE_TIMEOUT', '300000'));
|
||||
const idleTimeout = parseInt(
|
||||
config.get('ORACLE_POOL_IDLE_TIMEOUT', '300000'),
|
||||
);
|
||||
|
||||
const validPoolMin = Math.max(1, poolMin);
|
||||
const validPoolMax = Math.max(validPoolMin + 1, poolMax);
|
||||
const validPoolIncrement = Math.max(1, poolIncrement);
|
||||
|
||||
if (validPoolMax <= validPoolMin) {
|
||||
console.warn('Warning: poolMax deve ser maior que poolMin. Ajustando poolMax para poolMin + 1');
|
||||
console.warn(
|
||||
'Warning: poolMax deve ser maior que poolMin. Ajustando poolMax para poolMin + 1',
|
||||
);
|
||||
}
|
||||
|
||||
const options: DataSourceOptions = {
|
||||
|
||||
@@ -5,17 +5,23 @@ export function createPostgresConfig(config: ConfigService): DataSourceOptions {
|
||||
// Obter configurações de ambiente ou usar valores padrão
|
||||
const poolMin = parseInt(config.get('POSTGRES_POOL_MIN', '5'));
|
||||
const poolMax = parseInt(config.get('POSTGRES_POOL_MAX', '20'));
|
||||
const idleTimeout = parseInt(config.get('POSTGRES_POOL_IDLE_TIMEOUT', '30000'));
|
||||
const connectionTimeout = parseInt(config.get('POSTGRES_POOL_CONNECTION_TIMEOUT', '5000'));
|
||||
const acquireTimeout = parseInt(config.get('POSTGRES_POOL_ACQUIRE_TIMEOUT', '60000'));
|
||||
|
||||
const idleTimeout = parseInt(
|
||||
config.get('POSTGRES_POOL_IDLE_TIMEOUT', '30000'),
|
||||
);
|
||||
const connectionTimeout = parseInt(
|
||||
config.get('POSTGRES_POOL_CONNECTION_TIMEOUT', '5000'),
|
||||
);
|
||||
const acquireTimeout = parseInt(
|
||||
config.get('POSTGRES_POOL_ACQUIRE_TIMEOUT', '60000'),
|
||||
);
|
||||
|
||||
// Validação de valores mínimos
|
||||
const validPoolMin = Math.max(1, poolMin);
|
||||
const validPoolMax = Math.max(validPoolMin + 1, poolMax);
|
||||
const validIdleTimeout = Math.max(1000, idleTimeout);
|
||||
const validConnectionTimeout = Math.max(1000, connectionTimeout);
|
||||
const validAcquireTimeout = Math.max(1000, acquireTimeout);
|
||||
|
||||
|
||||
const options: DataSourceOptions = {
|
||||
type: 'postgres',
|
||||
host: config.get('POSTGRES_HOST'),
|
||||
@@ -25,7 +31,10 @@ export function createPostgresConfig(config: ConfigService): DataSourceOptions {
|
||||
database: config.get('POSTGRES_DB'),
|
||||
synchronize: config.get('NODE_ENV') === 'development',
|
||||
entities: [__dirname + '/../**/*.entity.{ts,js}'],
|
||||
ssl: config.get('NODE_ENV') === 'production' ? { rejectUnauthorized: false } : false,
|
||||
ssl:
|
||||
config.get('NODE_ENV') === 'production'
|
||||
? { rejectUnauthorized: false }
|
||||
: false,
|
||||
logging: config.get('NODE_ENV') === 'development',
|
||||
poolSize: validPoolMax, // máximo de conexões no pool
|
||||
extra: {
|
||||
|
||||
@@ -1 +1 @@
|
||||
export const DATA_SOURCE = 'DATA_SOURCE';
|
||||
export const DATA_SOURCE = 'DATA_SOURCE';
|
||||
|
||||
@@ -20,4 +20,4 @@ import { createOracleConfig } from '../configs/typeorm.oracle.config';
|
||||
],
|
||||
exports: [DATA_SOURCE],
|
||||
})
|
||||
export class DatabaseModule {}
|
||||
export class DatabaseModule {}
|
||||
|
||||
Reference in New Issue
Block a user