showResourceHelper

The function showResourceHelper is a REST API helper function used to retrieve a single document from a collection. It takes in an options object and returns an async function that handles the retrieval process.

Function:

showResourceHelper<T>(options: ShowResourceHelperOptions<T>) => (event: H3Event) => Promise<T | void>

The showResourceHelper function accepts an options object with the following properties:

ParameterTypeDescription
optionsShowResourceHelperOptions<T>An object that provides configuration options for showing.
options.modelModel<T>The model used for showing.
options.relatedPopulateOptions[] | ((event: H3Event) => PopulateOptions[])Options for populating related data. It can be an array of PopulateOptions or a function that returns options based on an event.

PopulateOptions Object

PropertyTypeDescription
pathstringThe field path to populate.
selectstring | string[] | objectField(s) to select from the populated document(s).
modelstring | Model<any>The model to use for population.
matchobjectConditions to match against the populated documents.
optionsobjectAdditional query options for population.
populatestring | PopulateOptions | Array<PopulateOptions>The options to use for populating nested paths.
perDocumentLimitnumberLimit the number of populated documents per document. This option is only applicable when populating an array field.
justOnebooleanIf true, mongoose will populate only the first document from the array of referenced documents. This option is only applicable when populating an array field.
leanbooleanIf true, documents returned from queries with this populate will be lean.

The function returns a Promise that resolves to the retrieved document of type T or void.

The function may throw the following errors:

  • Error: If no document is found.
  • Error: If the ID is not a valid ObjectId.
  • Error: If the ID is not found.

The function also includes an example usage in the JSDoc comment block.