naga-audit-service
Version:
A comprehensive audit service library for NestJS applications with MongoDB support
81 lines • 2.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NoukhaAuditLog = void 0;
const lib_module_1 = require("./lib.module");
const mongoose_1 = require("mongoose");
class NoukhaAuditLog {
static configure(config) {
this.config = config;
return this.initialize();
}
static async initialize() {
if (this.isInitialized) {
console.log('NoukhaAuditLog is already initialized');
return;
}
if (!this.config) {
throw new Error('NoukhaAuditLog must be configured before initialization.');
}
try {
console.log('🔄 Initializing NoukhaAuditLog...');
console.log(`📊 Connecting to MongoDB: ${this.config.dbUrl}`);
await mongoose_1.default.connect(this.config.dbUrl);
console.log('✅ MongoDB connected successfully');
console.log('✅ NoukhaAuditLog service started automatically');
this.isInitialized = true;
process.on('SIGINT', this.gracefulShutdown.bind(this));
process.on('SIGTERM', this.gracefulShutdown.bind(this));
}
catch (error) {
console.error('❌ Failed to initialize NoukhaAuditLog:', error);
throw error;
}
}
static async gracefulShutdown() {
console.log('🔄 Shutting down NoukhaAuditLog...');
try {
await mongoose_1.default.connection.close();
console.log('✅ NoukhaAuditLog shutdown complete');
process.exit(0);
}
catch (error) {
console.error('❌ Error during shutdown:', error);
process.exit(1);
}
}
static getModule() {
if (!this.config) {
throw new Error('NoukhaAuditLog must be configured before use. Call NoukhaAuditLog.configure() first.');
}
return lib_module_1.NagaAuditServiceModule.forRoot(this.config);
}
static getConfig() {
return this.config;
}
static isServiceReady() {
return this.isInitialized && mongoose_1.default.connection.readyState === 1;
}
static async waitForReady() {
if (this.isServiceReady()) {
return;
}
return new Promise((resolve, reject) => {
const checkReady = () => {
if (this.isServiceReady()) {
resolve();
}
else if (mongoose_1.default.connection.readyState === 0) {
reject(new Error('MongoDB connection failed'));
}
else {
setTimeout(checkReady, 100);
}
};
checkReady();
});
}
}
exports.NoukhaAuditLog = NoukhaAuditLog;
NoukhaAuditLog.config = null;
NoukhaAuditLog.isInitialized = false;
//# sourceMappingURL=config.js.map