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:
Parameter | Type | Description |
---|---|---|
options | ShowResourceHelperOptions<T> | An object that provides configuration options for showing. |
options.model | Model<T> | The model used for showing. |
options.related | PopulateOptions[] | ((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
Property | Type | Description |
---|---|---|
path | string | The field path to populate. |
select | string | string[] | object | Field(s) to select from the populated document(s). |
model | string | Model<any> | The model to use for population. |
match | object | Conditions to match against the populated documents. |
options | object | Additional query options for population. |
populate | string | PopulateOptions | Array<PopulateOptions> | The options to use for populating nested paths. |
perDocumentLimit | number | Limit the number of populated documents per document. This option is only applicable when populating an array field. |
justOne | boolean | If true , mongoose will populate only the first document from the array of referenced documents. This option is only applicable when populating an array field. |
lean | boolean | If 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.