Skip to content
This repository was archived by the owner on Aug 2, 2025. It is now read-only.
This repository was archived by the owner on Aug 2, 2025. It is now read-only.

Refactor Database Operations to Reduce Complexity and Duplication #39

@sourcery-ai

Description

@sourcery-ai

Issue: Complexity in Database Operations

The current implementation of database operations involves repeated logic for timing, logging, and validation across multiple functions. This leads to code duplication and increased complexity.

Suggested Solution:

To address this issue, it is recommended to create a helper function that encapsulates the common logic for timing, logging, and validation. This will centralize these cross-cutting concerns, reduce duplication, and maintain all existing functionality.

Proposed Helper Function:

function executeDbOperation<T>(
  label: string,
  operation: () => T,
  validate?: () => void
): T {
  const startTime = Date.now();
  logger.debug(`__task__ __db__ ${label} �3`);
  if (validate) {
    validate();
  }
  const result = operation();
  const duration = Date.now() - startTime;
  logger.debug(`__task__ __db__ ${label} �4f  (${duration}ms)`);
  return result;
}

Example Refactoring:

For instance, the addDockerHost function can be refactored as follows:

addDockerHost(hostId: string, url: string, secure: boolean) {
  return executeDbOperation(
    "Adding Docker Host",
    () => {
      const stmt = db.prepare(`
        INSERT INTO docker_hosts (name, url, secure)
        VALUES (?, ?, ?)
      `);
      return stmt.run(hostId, url, secure);
    },
    () => {
      if (
        typeof hostId !== "string" ||
        typeof url !== "string" ||
        typeof secure !== "boolean"
      ) {
        logger.crit("Invalid parameter types for addDockerHost");
        throw new TypeError("Invalid parameter types for addDockerHost");
      }
    }
  );
}

Action Items:

  • Implement the executeDbOperation helper function.
  • Refactor existing database operation functions to utilize this helper function.
  • Ensure all existing functionality is maintained and thoroughly tested after refactoring.

I created this issue for @Its4Nik from #33 (comment).

Tips and commands

Interacting with Sourcery

  • Generate a plan of action: Comment @sourcery-ai plan on this issue.
  • Generate a pull request for this issue: Comment @sourcery-ai develop to
    generate a PR that addresses this issue.

Getting Help

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions