Abstract class that provides a base implementation for managing data operations such as loading, caching, retrieving, updating, creating, and deleting resources. The DataController is designed to interact with a specified interface for managing data and provides methods for handling common resource-related operations.

This class is designed to be extended by specific implementations for various data types.

Hierarchy (View Summary)

Constructors

Properties

actions: BaseActions<IProductRecord>

The base actions used by the class.

caches: Cache<any[]>[] = []

An array of cache objects used to store data temporarily. Each cache object is expected to hold an array of elements, with the specific type of the elements being flexible (any[]). This variable can be utilized as a mechanism to manage and retrieve stored data efficiently.

defaultCacheLifetime: TimeSpan = ...

The default duration for cache lifetime.

Methods

  • Handles the creation of a new resource by processing the request, sanitizing input and output data, and responding with the created resource.

    Parameters

    • request: Request

      The HTTP request containing the data to create the resource.

    • response: Response

      The HTTP response used to send the result back to the client.

    Returns void

    This method does not return a value, it sends a response to the client.

  • Deletes a resource identified by the provided ID.

    Parameters

    • request: Request

      The HTTP request object, containing the resource ID to delete.

    • response: Response

      The HTTP response object, used to send back the deletion result.

    Returns void

    No value is returned, the response object is used to communicate the result.

  • Retrieves all data based on the provided request, processes it, and sends the appropriate response.

    Parameters

    • request: Request

      The incoming request object containing the necessary parameters and context.

    • response: Response

      The response object used to send back the processed data or error information.

    Returns void

    This method does not return anything directly, but responds to the client with the processed data or an error.

  • Retrieves a record by its identifier from the database, sanitizes the data, and sends the response back to the client. Throws an exception if the record is not found or an error occurs during the process.

    Parameters

    • request: Request

      The HTTP request object, used to extract the record ID.

    • response: Response

      The HTTP response object, used to send back the result or error message.

    Returns void

    Does not return a value, as it sends the response directly to the client.

  • Loads data using the provided loader function and generates a cache with a checksum. Utilizes request-specific parameters to identify caching needs, validate cache validity, and update the cache if necessary.

    Type Parameters

    • T

    Parameters

    • request: Request

      The incoming request object containing path, parameters, body, and headers.

    • dataLoaderFn: () => T[]

      A function to load data that will populate the cache.

    Returns { cache: Cache<T[]>; status: IStatusCode }

    • An object containing the updated or existing cache, and the operation status code.
  • Optional method to perform teardown or clean-up actions specific to the instance. It is invoked when the instance is being destroyed. This method does not take any parameters and does not return any value.

    Returns void

  • Sanitizes the provided value for safe usage and storage in a database.

    Parameters

    • value: IProductRecord

      The input object or data structure that needs to be sanitized.

    Returns IProductRecord

    The sanitized version of the input object or data structure.

  • Updates the resource with the provided data and responds with the updated result.

    Parameters

    • request: Request

      The request object containing the data to update the resource.

    • response: Response

      The response object used to send the result or error to the client.

    Returns void

    This method does not return a value; it sends a response back to the client.

  • Retrieves and parses the "id" parameter from the given request object.

    Parameters

    • request: Request

      The request object containing parameters.

    • OptionalshouldThrow: boolean = true

      Determines whether an exception should be thrown if the "id" parameter is missing.

    Returns number

    The parsed integer value of the "id" parameter if present, or null if not present and shouldThrow is false.

    Throws an exception if the "id" parameter is missing and shouldThrow is true.

  • Type Parameters

    • T extends string | number | boolean | Moment

    Parameters

    • request: Request

      {Express.Request}

    • key: string

      {string}

    • defaultValue: T

      {T}

    Returns T

    Tries to get parameter specified by key from requests route parameters. If the parameter is not found in the request, the default value will be returned.

  • Type Parameters

    • T extends string | number | boolean | Moment

    Parameters

    • request: Request

      {Express.Request}

    • paramDefinition: ParamDefinition<T>

      {ParamDefinition}

    Returns ParamDefinition<T>

    Tries to get parameters specified in paramDefinition from requests route parameters. If a parameter is not found in the request, the parameter is set to the default value specified in paramDefinition.