cqrs-eda
Version:
Lightweight CQRS and Event-Driven Architecture library using TypeScript decorators, handlers and typings. Perfect for scalable event-driven apps.
107 lines (106 loc) • 4.25 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadDecoratedClasses = loadDecoratedClasses;
const glob_1 = __importDefault(require("glob"));
const path_1 = __importDefault(require("path"));
const url_1 = require("url");
/**
* Dynamically loads all decorated classes (Commands, Queries, Observers)
* so that the internal registries are automatically populated.
*
* This must be called before using CommandHandler, QueryHandler, or ObserverHandler,
* otherwise the handlers won't know about the decorated classes.
*
* Example without any dependency injection container:
* ```ts
* import { Utilities, Handlers } from "cqrs-eda";
*
* async function bootstrap() {
* // Load all decorated commands, queries, and observers
* await Utilities.loadDecoratedClasses("src/application");
*
* // Initialize handlers
* const commandHandler = new Handlers.CommandHandler();
* const queryHandler = new Handlers.QueryHandler();
* const observerHandler = new Handlers.ObserverHandler();
*
* // Use the handlers
* const segment = await queryHandler.fire("GET_SEGMENT", { phrase: "Hello", accent: "american" });
* console.log(segment);
*
* await commandHandler.fire("SAVE_SEGMENT", { phrase: "Hello", accent: "american", videoUrl: "...", startTime: 0, endTime: 2 });
* await observerHandler.publish("SEGMENT.SAVED", { phrase: "Hello", accent: "american", ... });
* }
*
* bootstrap();
* ```
*
* @param basePath The base path where decorated files are located (e.g. "src/application")
*/
async function loadDecoratedClasses(basePath) {
// Ajusta o basePath para dev ou dist
const fullPath = path_1.default.isAbsolute(basePath)
? basePath
: path_1.default.join(process.cwd(), basePath);
const files = glob_1.default.sync(path_1.default.join(fullPath, "**/*.js")); // só JS no build
if (files.length === 0) {
console.warn(`[CQRS-EDA] No decorated classes found in ${fullPath}`);
return;
}
for (const file of files) {
try {
await Promise.resolve(`${file.startsWith("file://") ? file : (0, url_1.pathToFileURL)(file).href}`).then(s => __importStar(require(s)));
}
catch (err) {
console.error(`[CQRS-EDA] Error loading file ${file}:`, err);
}
}
}
async function test() {
const basePath = path_1.default.join(process.cwd(), "dist", "application"); // caminho para os arquivos compilados
console.log("Carregando classes decoradas de:", basePath);
try {
await loadDecoratedClasses(basePath);
console.log("✅ Classes carregadas com sucesso!");
}
catch (err) {
console.error("❌ Erro ao carregar classes:", err);
}
}
test();