Skip to content

Instantly share code, notes, and snippets.

@ricky-bruner
Last active November 20, 2023 06:12
Show Gist options
  • Save ricky-bruner/411a1b677ee6d10f91fc7fc67efb71bb to your computer and use it in GitHub Desktop.
Save ricky-bruner/411a1b677ee6d10f91fc7fc67efb71bb to your computer and use it in GitHub Desktop.
CreateManagedConfigListTemplate Documentation
//Documentation in CreateManagedConfigListTemplateDocumentation.md
var collectionName = "configlists"
var configurationListName = "ConfigList Name"; //input name of configlist (configlist.name)
var genericConfigDescriptor = "Generic Descriptor" //input generic value for configuration descriptor, maps to configlist.configurations[].descriptor.
var genericDetailDescriptor = "Generic Detail Descriptor" //input generic value for configuration detail descriptor, maps to configlist.configurations[].details[].descriptor.
var logs = [];
var bulkOps = [];
//key is codelist.code value, and value is array that translates to codelist.code.details.
var configListMappings = {
"Config Name": [
"Detail Identifier 1",
"Detail Identifier 2"
]
};
function getConfigurationList(name) {
return {
"_id": ObjectId(),
"applicableDataSegmentIds": [
],
"readOnly": true,
"name": name,
"configurations": [
],
"__v": NumberInt(0)
}
};
function getConfiguration(name, descriptor) {
return {
"_id": ObjectId(),
"name": name,
"descriptor": descriptor,
"details": [
]
}
};
function getConfigurationDetail(identifier, descriptor) {
return {
"identifier": identifier,
"descriptor": descriptor
}
};
function handleUpdateConfigurationList(configList) {
var configKeys = Object.keys(configListMappings);
var existingConfigNames = configList.configurations.map(obj => obj.name);
//handle additions
configKeys.forEach(key => {
if (!existingConfigNames.includes(key)) {
var newConfig = getConfiguration(key, genericConfigDescriptor);
configListMappings[key].forEach(identifier => {
newConfig.details.push(getConfigurationDetail(identifier, genericDetailDescriptor));
});
bulkOps.push({ updateOne: { "filter": { "_id": configList._id }, "update": { $push: { 'configurations': newConfig } } } });
logs.push(`Configuration added to configList.configurations: ${key}, with details ${configListMappings[key]}.`);
} else {
logs.push(`Configuration already present in configList.configurations: ${key}.`);
configList.configurations.forEach(c => {
if (c.name === key) {
var existingDetails = c.details.map(d => d.identifier);
configListMappings[key].forEach(newDetail => {
if (!existingDetails.includes(newDetail)) {
var addedDetail = getConfigurationDetail(newDetail, genericDetailDescriptor);
bulkOps.push({ updateOne: { "filter": { "_id": configList._id, "configurations.name": key }, "update": { $push: { 'configurations.$.details': addedDetail } } } });
logs.push(`Detail added to configList.configurations: ${key}, detail: ${newDetail}.`);
}
});
}
});
}
});
//handle removals
existingConfigNames.forEach(existingKey => {
if (!configKeys.includes(existingKey)) {
bulkOps.push({ updateOne: { "filter": { "_id": configList._id }, "update": { $pull: { 'configurations': { 'name': existingKey } } } } });
logs.push(`Configuration removed from configList.configurations: ${existingKey}.`);
} else {
configList.configurations.forEach(c => {
if (c.name === existingKey) {
var newDetails = configListMappings[existingKey];
c.details.map(d => d.identifier).forEach(existingDetail => {
if (!newDetails.includes(existingDetail)) {
bulkOps.push({
updateOne: {
"filter": {
"_id": configList._id,
"configurations.name": existingKey,
"configurations.details.identifier": existingDetail
},
"update": {
$pull: { "configurations.$.details": { "identifier": existingDetail } }
}
}
});
logs.push(`Detail removed to configlist.configurations: ${existingKey}, detail: ${existingDetail}.`);
}
});
}
});
}
});
};
function handleAddNewConfigurationList() {
var newConfigList = getConfigurationList(configurationListName);
logs.push(`Configuration List added: ${configurationListName}.`);
Object.keys(configListMappings).forEach(key => {
if (!newConfigList.configurations.map(obj => obj.name).includes(key)) {
var newConfig = getConfiguration(key, genericConfigDescriptor);
logs.push(`Configuration added to configlist.configurations: ${key}.`);
configListMappings[key].forEach(identifier => {
newConfig.details.push(getConfigurationDetail(identifier, genericDetailDescriptor));
logs.push(`Configuration Detail added to code ${key}: ${identifier}.`);
})
newConfigList.configurations.push(newConfig)
} else {
logs.push(`Configuration already present in configlist.configurations: ${key}.`);
}
});
bulkOps.push({
insertOne: {
"document": newConfigList
}
});
}
function handleConfigurationList() {
var exists = false;
db.getCollection(collectionName).find({ 'name': configurationListName }).forEach(configList => {
exists = true;
handleUpdateConfigurationList(configList);
})
if (!exists) {
handleAddNewConfigurationList();
}
}
function runOpsAndPrintLogs() {
if (bulkOps.length > 0) {
db.getCollection(collectionName).bulkWrite(bulkOps);
}
logs.forEach(l => { print(l); });
}
(function () {
handleConfigurationList();
runOpsAndPrintLogs();
})();

CreateManagedConfigListTemplate.js Documentation

Purpose

This script is designed to manage configuration lists within a MongoDB collection. It performs updates to an existing configuration list with new configuration items and details or creates an entire new configuration list if one with the specified name does not exist. The configurations represent a set of key-value pairs where the key is the configuration name and the value is an array of detail identifiers.

Usage

Execute the script to update an existing configuration list or to create a new one based on the provided mappings. The list contains configurations, each with its own set of details.

Variables

  • collectionName: The name of the MongoDB collection to be manipulated.
  • configurationListName: The input name of the configuration list (configlist.name).
  • genericConfigDescriptor: Input generic value for configuration descriptor, maps to configlist.configurations[].descriptor.
  • genericDetailDescriptor: Input generic value for configuration detail descriptor, maps to configlist.configurations[].details[].descriptor.
  • logs: An array to store messages about the script's execution progress.
  • bulkOps: An array to store MongoDB bulk operation instructions.
  • configListMappings: An object holding the mapping of configuration names to their associated detail identifiers.

Functions

  • getConfigurationList(name): Creates a new configuration list object.
  • getConfiguration(name, descriptor): Creates a new configuration object.
  • getConfigurationDetail(identifier, descriptor): Creates a new configuration detail object.
  • handleUpdateConfigurationList(configList): Updates an existing list with configurations and details.
  • handleAddNewConfigurationList(): Creates a new configuration list.
  • handleConfigurationList(): Checks the existence of a configuration list and acts accordingly.
  • runOpsAndPrintLogs(): Executes MongoDB bulk operations and prints the logs.

Execution

The script checks for the existence of the specified configuration list. If found, it updates the list with new configurations and details. If not, it proceeds to create a new list. Operations are prepared and executed, followed by log output.

Note

This script serves as a template for managing configuration lists and requires actual data to be set for the variables at the top before execution. It is important to ensure the provided data matches the MongoDB collection's structure and the application's requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment