Class DataController<TInterface, TService>Abstract

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.

Type Parameters

Hierarchy (View Summary)

Constructors

Properties

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.

service: TService

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 Promise<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 Promise<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 Promise<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 Promise<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. Uses 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 the necessary parameters and context.

    • dataLoader: Promise<T[]>

      The function used to load data from the data source.

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

    • A promise that resolves to an object containing the cache and status code.

    May throw if there are issues with the data loader function.

    const result = await this.loadDataAndGenerateChecksum(request, this.service.getAll()
    .then(data => data
  • 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 Promise<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.

  • 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 | DateTime<boolean>

    Parameters

    • request: Request

      {Express.Request}

    • key: string

      {string}

    • defaultValue: T

      {T}

    Returns T