@suissa/universal-queues
Version:
Factory universal para mensageria (RabbitMQ, Kafka, SQS) para sistemas distribuídos.
52 lines • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HistoryFactory = exports.MessagingFactory = void 0;
const rabbitmq_1 = require("./rabbitmq");
const history_redis_1 = require("./clients/history.redis");
const instances = new Map();
class MessagingFactory {
static async create(driver, uriOrOptions) {
if (driver !== 'rabbitmq') {
throw new Error(`Driver não suportado: ${driver}`);
}
// Normaliza opções
const opts = typeof uriOrOptions === 'string'
? { uri: uriOrOptions }
: (uriOrOptions || {});
const name = opts.name || 'default';
const cacheKey = `rabbitmq::${name}`;
// Reaproveita instância já conectada
const cached = instances.get(cacheKey);
if (cached)
return cached;
const client = new rabbitmq_1.RabbitMQClient({
prefetch: opts.prefetch ?? 20, // valor padrão razoável
});
const shouldConnect = opts.connect !== false;
if (shouldConnect) {
const uri = opts.uri ||
process.env.RABBITMQ_URI ||
'amqp://localhost:5672';
await client.connect(uri);
}
instances.set(cacheKey, client);
return client;
}
}
exports.MessagingFactory = MessagingFactory;
// export class MessagingFactory {
// static create(type: 'rabbitmq' = 'rabbitmq'): IMessaging {
// if (type === 'rabbitmq') return new RabbitMQClient();
// // Futuro: if (type === 'kafka') return new KafkaClient();
// throw new Error(`Mensageria não suportada: ${type}`);
// }
// }
class HistoryFactory {
static create(type = 'redis') {
if (type === 'redis')
return new history_redis_1.RedisHistory();
throw new Error(`Cache não suportada: ${type}`);
}
}
exports.HistoryFactory = HistoryFactory;
//# sourceMappingURL=index.js.map