jsm-core
Version:
Core library for JSM project
124 lines (123 loc) • 5.95 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;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDatabaseName = void 0;
const node_1 = require("jsm-utilities/dist/node");
const jsm_logger_1 = __importStar(require("jsm-logger"));
/**
* @description Database Configuration
* @generator Jsm
* @author dr. Salmi <reevosolutions@gmail.com>
* @since 05-03-2024 05:38:21
*/
const logger = (0, jsm_logger_1.default)(jsm_logger_1.LoggerContext.LOADER, 'DB_CONFIG');
const getDatabaseName = (service) => {
// Get the database name prefix from the environment variable
const DB_NAME_PREFIX = (0, node_1.getEnv)('MONGODB_DB_NAME_PREFIX', '');
// Generate the database name based on the service name and prefix
const DB_NAME = `${DB_NAME_PREFIX}${service}`;
return DB_NAME;
};
exports.getDatabaseName = getDatabaseName;
// Function to get the database configuration
const getDatabaseConfig = () => {
// Define the service name based on the environment variable
const SERVICE_NAME = (0, node_1.getEnv)('SERVICE_NAME', '').toLowerCase();
// Define the list of services excluded from Atlas
const SERVICES_EXCLUDED_FROM_ATLAS = [
// 'cm',
];
// Define the list of services excluded from Replica Set
const SERVICES_EXCLUDED_FROM_RS = [];
// Get the database name prefix from the environment variable
const DB_NAME_PREFIX = (0, node_1.getEnv)('MONGODB_DB_NAME_PREFIX', '');
// Generate the database name based on the service name and prefix
const DB_NAME = (0, exports.getDatabaseName)(SERVICE_NAME);
// Initialize the configuration object
const config = {
atlasActivated: (0, node_1.getEnv)('MONGODB_ATLAS_ACTIVATED', false),
atlas: {
version: (0, node_1.getEnv)('MONGODB_ATLAS_VERSION', 2),
uriV2: (0, node_1.getEnv)('MONGODB_ATLAS_URI_2', ''),
uriV4: (0, node_1.getEnv)('MONGODB_ATLAS_URI_4', ''),
uri: '',
username: (0, node_1.getEnv)('MONGODB_ATLAS_USER', ''),
password: (0, node_1.getEnv)('MONGODB_ATLAS_PASS', ''),
replicaSet: (0, node_1.getEnv)('MONGODB_ATLAS_REPLICASET', ''),
},
rs: {
uri: (0, node_1.getEnv)('MONGODB_RS_URI', ''),
username: (0, node_1.getEnv)('MONGODB_RS_USER', ''),
password: (0, node_1.getEnv)('MONGODB_RS_PASS', ''),
replicaSet: (0, node_1.getEnv)('MONGODB_RS_REPLICASET', ''),
},
singleton: {
uri: (0, node_1.getEnv)('MONGODB_SINGLETON_URI', ''),
username: (0, node_1.getEnv)('MONGODB_SINGLETON_USER', ''),
password: (0, node_1.getEnv)('MONGODB_SINGLETON_PASS', ''),
},
dbNamePrefix: DB_NAME_PREFIX,
dbName: DB_NAME,
uri: '', // will be generated
connectTo: 'rs',
};
// Determine the connection type based on the environment
config.connectTo = ((0, node_1.getEnv)('MONGODB_CONNECT_TO', 'rs'));
const forceSingleton = (0, node_1.getEnv)('MONGODB_FORCE_SINGLETON', false);
// Check if the service is excluded from Atlas or Replica Set, and fallback to local connection
if ((config.connectTo === 'atlas' && SERVICES_EXCLUDED_FROM_ATLAS.includes(SERVICE_NAME)) ||
(config.connectTo === 'rs' && SERVICES_EXCLUDED_FROM_RS.includes(SERVICE_NAME))) {
config.connectTo = 'singleton';
}
else if (forceSingleton) {
config.connectTo = 'singleton';
}
// Generate the MongoDB URI based on the connection type
if (config.connectTo === 'rs') {
config.uri = `mongodb://${config.rs.username}:${config.rs.password}@${config.rs.uri}/${config.dbName}?replicaSet=${config.rs.replicaSet}&authSource=admin&retryWrites=true&w=majority`;
}
else if (config.connectTo === 'atlas') {
config.uri = config.atlas.version === 4
? `mongodb+srv://${config.atlas.username}:${config.atlas.password}@${config.atlas.uriV4}/${config.dbName}?retryWrites=true&w=majority`
: `mongodb://${config.atlas.username}:${config.atlas.password}@${config.atlas.uriV2}/${config.dbName}?ssl=true&replicaSet=${config.atlas.replicaSet}&authSource=admin&retryWrites=true&w=majority`;
}
else {
config.uri = `mongodb://${config.singleton.username && config.singleton.password ? `${config.singleton.username}:${config.singleton.password}@` : ''}${config.singleton.uri || 'localhost:27017'}/${config.dbName}`;
}
// Return the configuration object
return config;
};
// Export the getDatabaseConfig function as the default export
exports.default = getDatabaseConfig;