Skip to content

Configuring Record Type Hooks

andrewbrazzatti edited this page Mar 5, 2024 · 1 revision

In the ReDBox platform, hooks provide a powerful mechanism to execute custom logic at different stages of a record's lifecycle. This document covers the types of hooks available and offers guidance on configuring them for optimal performance and functionality.

Types of Hooks

Hooks in ReDBox are categorized based on when they execute in relation to the record lifecycle events:

  • Pre Hooks: These hooks run synchronously before a record is saved. They're ideal for operations that need to happen prior to record creation or updating, such as data validation or modification.

  • PostSync Hooks: These hooks are unique to the onCreate event and run synchronously after the record is initially saved. They are used when operations must occur immediately following record creation, and require access to the record's identifier.

  • Post Hooks: These hooks run asynchronously after the record has been saved. They're suitable for longer-running operations that should not delay the server's response to the client.

Configuring Hooks

To effectively utilize hooks in your ReDBox configuration, follow these guidelines:

module.exports.recordtype = {
  "rdmp": {
    "packageType": "rdmp",
    "hooks": {
      "onCreate": {
        "pre": [
          // Pre-creation synchronous hooks
        ],
        "postSync": [
          // Post-creation synchronous hooks needing the record ID
        ],
        "post": [
          // Post-creation asynchronous hooks
        ]
      },
      "onUpdate": {
        "pre": [
          // Pre-update synchronous hooks
        ],
        "post": [
          // Post-update asynchronous hooks
        ]
      }
    },
    // Additional record type configurations...
  }
  // Define other record types as needed...
};

Best Practices for Configuring and Using Hooks

To maximize the efficiency and reliability of hooks within ReDBox, adhere to the following best practices:

  • Keep Hooks Focused: Each hook should perform a single, clear function. This simplifies maintenance and debugging.

  • Error Handling: Implement robust error handling within your hooks to manage exceptions and maintain system stability, particularly for synchronous hooks that affect the record lifecycle.

  • Documentation: Thoroughly document each hook, detailing its purpose, required parameters, and expected behavior to facilitate understanding and future modifications.

  • Testing: Conduct extensive tests on your hooks in various scenarios to ensure they perform as intended and do not introduce side effects.

  • Utilize Asynchronous Processing: For time-consuming tasks, leverage asynchronous hooks (post) and the message queue system. This approach prevents long-running processes from delaying user feedback.

    For example, to queue a background task using the message system within a post hook:

    "post": [
      {
        "function": "sails.services.rdmpservice.queueTriggerCall",
        "options": {
          "hookName": "sails.services.backgroundService.processTask",
          "hookOptions": {
            // Task-specific options
          }
        }
      }
    ]
  • Monitoring and Resource Management: Monitor your message queue to track and address performance issues promptly. Be mindful of resource utilization to prevent overloading.

  • Prioritize User Experience: Use the message queue for non-urgent tasks to keep the application responsive. Prioritize user interactions by offloading heavy processing to background tasks.

By understanding and implementing hooks according to these guidelines, you can significantly enhance the functionality and user experience of your ReDBox platform. Hooks allow for a high degree of customization and automation, aligning the platform with your specific data management workflows and institutional needs.