UNPKG

@adonisjs/route-model-binding

Version:
47 lines (46 loc) 1.63 kB
import type { HttpContext } from '@adonisjs/core/http'; import { HttpRouterService } from '@adonisjs/core/types'; /** * Resource loader job is to query the Lucid models for the given * request using the route params. * * One can provide an array of models matching the position of params * inside a route to conventionally query the database using Lucid * models. For example: * * ``` * Route - /posts/:post_id/comments/:id * Models: [Post, Comment] * ``` * * The resource loader will return the following response. * * ```ts * { * post_id: Post.findOrFail(post_id), * id: Comment.findOrFail(id) * } * ``` * * The first model will be matched against the first param. We use the position, * since we plan to grab models using decorators and in TypeScript decorators * cannot get the name of the parameter on a method. * * One can customize the lookup logic in one of the few ways. * * - /posts/:post(slug) - The slug field will be used for the lookup * - /posts/:post/comments/:>comment - The comment will be loaded as the post relationship * - Post.routeLookupKey - Define the key to be used to lookup the route * - Post.findForRequest() - Define a custom static method to lookup Post for a given request * - Post.findRelatedForRequest() - Define a custom method to lookup Comment for a given post */ export declare class ResourceLoader { #private; ctx: HttpContext; resources: Record<string, any>; constructor(ctx: HttpContext, router: HttpRouterService); /** * Load models based upon the current request route params */ load(models: any[]): Promise<void>; }