Repository: Mintplex-Labs/anything-llm
File: server/models/systemSettings.js (JavaScript)

Code before:
      action,
      database_id,
      originalDatabaseId,
      connectionString,
      engine,
    } = update;

    // Skip invalid connections
    if ((action === "update" || action === "add") && !connectionString)
      continue;

    switch (action) {
      case "remove":
        connectionsMap.delete(database_id);
        break;

      case "update": {
        const newId = slugify(database_id);

        // Verify original connection exists
        if (!connectionsMap.has(originalDatabaseId)) {
          console.warn(
            `[mergeConnections] Update skipped: Original connection "${originalDatabaseId}" not found`
          );
          break;
        }

        // Check for name conflict (excluding the one being updated)
        if (newId !== originalDatabaseId && connectionsMap.has(newId)) {
          console.warn(
            `[mergeConnections] Update skipped: New name "${newId}" conflicts with existing connection`
          );
          break;
        }

        // Remove old and add updated connection
        connectionsMap.delete(originalDatabaseId);
        connectionsMap.set(newId, {
          engine,
          database_id: newId,
          connectionString,
        });
        break;
      }

      case "add": {
        const slugifiedId = slugify(database_id);

        // Skip if already exists
        if (connectionsMap.has(slugifiedId)) {
          console.warn(

Review comment (refactor):
I refactored this `mergeConnections` util to now use a map to help simplify the implementation. From my testing it works exactly the same as the previous implementation but be sure to test this again on your end.

Code after:
     */
    noLoginRedirect: () => {
      if (!("SIMPLE_SSO_ENABLED" in process.env)) return null; // if simple SSO is not enabled, return null
      if (!("SIMPLE_SSO_NO_LOGIN" in process.env)) return null; // if the no login config is not set, return null
      if (!("SIMPLE_SSO_NO_LOGIN_REDIRECT" in process.env)) return null; // if the no login redirect is not set, return null

      try {
        let url = new URL(process.env.SIMPLE_SSO_NO_LOGIN_REDIRECT);
        return url.toString();
      } catch {}

      // if the no login redirect is not a valid URL or is not set, return null
      return null;
    },
  },
};

/**
 * Merges SQL connection updates from the frontend with existing backend connections.
 * Processes three types of actions: "remove", "update", and "add".
 *
 * @param {Array<Object>} existingConnections - Current connections stored in the database
 * @param {Array<Object>} updates - Connection updates from frontend, each with an action property
 * @returns {Array<Object>} - The merged connections array
 */
function mergeConnections(existingConnections = [], updates = []) {
  const connectionsMap = new Map(
    existingConnections.map((conn) => [conn.database_id, conn])
  );

  for (const update of updates) {
    const {
      action,
      database_id,
      originalDatabaseId,
      connectionString,
      engine,
    } = update;

    switch (action) {
      case "remove": {
        connectionsMap.delete(database_id);
        break;
      }
      case "update": {
        if (!connectionString) continue;
        const newId = slugify(database_id);

        // Verify original connection exists
        if (!connectionsMap.has(originalDatabaseId)) {
          console.warn(