This repository was archived by the owner on Aug 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Copy link
Copy link
Closed
Description
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
executeDbOperationhelper 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 planon this issue. - Generate a pull request for this issue: Comment
@sourcery-ai developto
generate a PR that addresses this issue.
Getting Help
- Contact our support team for questions or feedback.
- Visit our documentation for detailed guides and information.
- Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request