Resource Helpers

The resource helpers are a set of functions that are used to abstract away the details of working with the database. They are used internally by the defineEventHandler function to implement the index, show, create, update, and destroy methods.

Index

The ResourceHelper.index method is used to implement the index method. It returns a function that accepts an event object and returns a promise that resolves to an array of resources.

server/api/users.get.ts
import { ResourceHelper } from "#nuxt-server-utils";
import { User } from "~~/server/models/User.model";

export default defineEventHandler(
  ResourceHelper.index({
    model: User,
  })
);

The ResourceHelper.index method accepts an options object with the following properties:

  • model - The Mongoose model to use for querying the database.
  • related - An array of related models to populate.
  • beforeFnHook - A function that is called before the query is executed.
  • afterFnHook - A function that is called after the query is executed.

Show

The ResourceHelper.show method is used to implement the show method. It returns a function that accepts an event object and returns a promise that resolves to a single resource.

server/api/users/[id
import { ResourceHelper } from "#nuxt-server-utils";
import { User } from "~~/server/models/User.model";

export default defineEventHandler(
  ResourceHelper.show({
    model: User,
  })
);

The ResourceHelper.show method accepts an options object with the following properties:

  • model - The Mongoose model to use for querying the database.
  • related - An array of related models to populate.
  • beforeFnHook - A function that is called before the query is executed.
  • afterFnHook - A function that is called after the query is executed.

Destroy

The ResourceHelper.destroy method is used to implement the delete method. It returns a function that accepts an event object and returns a promise that resolves to a single resource.

server/api/users/[id
import { ResourceHelper } from "#nuxt-server-utils";
import { User } from "~~/server/models/User.model";

export default defineEventHandler(
  ResourceHelper.destroy({
    model: User,
  })
);

The ResourceHelper.destroy method accepts an options object with the following properties:

  • model - The Mongoose model to use for querying the database.
  • intigrity - An array of related models to which the resource must not be related.
  • checkSelf - A boolean indicating whether the resource must not be related to itself.
  • afterFnHook - A function that is called after the query is executed.
Table of Contents