UNPKG

@avonjs/avonjs

Version:

A fluent Node.js API generator.

138 lines (137 loc) 5.01 kB
import Joi from 'joi'; import { type AnyRecord, type AnyValue, type Attachable, type FilledCallback, type Model, type OpenApiSchema, type Optional, type PivotFieldCallback, type PrimaryKey, type RelatableQueryCallback, type Rules, type SanitizeCallback } from '../Contracts'; import type AvonRequest from '../Http/Requests/AvonRequest'; import type Resource from '../Resource'; import Relation from './Relation'; export default class BelongsToMany extends Relation { /** * The pivot resource instance */ pivotResource: Resource; /** * The foreign key of the related model. * The attribute name that holds the parent model key. */ protected resourceForeignKey?: string; /** * The associated key on the related model. * Defaults to primary key of related model. */ protected resourceOwnerKey?: string; /** * Indicates fields uses to update pivot table. */ protected pivotFields: PivotFieldCallback; /** * The callback that should be run to pivots table. */ pivotQueryCallback: RelatableQueryCallback; /** * The callback that should be run to sanitize related resources. */ sanitizeCallback: SanitizeCallback; constructor(resource: string, pivot: string, attribute?: string); /** * Determine the pivot resource query. */ pivotQueryUsing(pivotQueryCallback: RelatableQueryCallback): this; /** * Get the pivot resource. */ protected getPivotResource(resourceName: string): Resource<Model>; /** * Determine pivot fields. */ pivots(callback: PivotFieldCallback): this; /** * Get the validation rules for this field. */ getRules(request: AvonRequest): Rules; /** * Get Joi rule to validate resource existence. */ protected existenceRule(request: AvonRequest): Joi.ExternalValidationFunction; /** * Set related model foreign key. */ setResourceForeignKey(resourceForeignKey: string): this; /** * Get attribute that hold the related model key. */ resourceForeignKeyName(request: AvonRequest): string; /** * Set the related model owner key. */ setResourceOwnerKey(resourceOwnerKey: string): this; /** * Get attribute that hold the related model key. */ resourceOwnerKeyName(request: AvonRequest): string; /** * Hydrate the given attribute on the model based on the incoming request. */ fillForAction<TModel extends Model>(request: AvonRequest, model: TModel): AnyValue; /** * Hydrate the given attribute on the model based on the incoming request. */ protected fillAttributeFromRequest(request: AvonRequest, requestAttribute: string): Optional<FilledCallback>; /** * Detach all related models. */ protected clearAttachments(request: AvonRequest, resource: Model): Promise<AnyValue>; protected allowedDetachments(request: AvonRequest, model: Model): Promise<Model[]>; prepareAttachments(request: AvonRequest, resource: Model, requestAttribute: string): Promise<Model[]>; protected getAttachments(request: AvonRequest, requestAttribute: string): Attachable[]; /** * Filter attachments by policy. */ protected filterAllowedAttachments(request: AvonRequest, model: Model, attachments: Attachable[]): Promise<Attachable[]>; protected getRelatedResources(request: AvonRequest, resourceIds: Array<PrimaryKey>): Promise<Model[]>; sanitizeUsing(sanitizeCallback: SanitizeCallback): this; /** * Fill pivot models. */ fillPivotFromRequest(request: AvonRequest, requestAttribute: string, attachments: Attachable[]): Model[]; /** * Resolve related value for given resources. */ resolveRelatables(request: AvonRequest, resources: Model[]): Promise<AnyValue>; /** * Get related models for given resources. */ searchRelatables(request: AvonRequest, resources: Model[]): Promise<Model[]>; /** * Get pivot records for given resources. */ protected getPivotModels(request: AvonRequest, resources: Model[]): Promise<Model[]>; /** * Get pivot records for given resources. */ protected getRelatedModels(request: AvonRequest, pivots: Model[]): Promise<Model[]>; /** * Format the given related resource. */ formatRelatedResource(request: AvonRequest, resource: Model & { pivot?: Model; }): AnyRecord; /** * Get the swagger-ui schema. */ protected responseSchema(request: AvonRequest): OpenApiSchema; /** * Get the swagger-ui schema. */ protected payloadSchema(request: AvonRequest): OpenApiSchema; /** * Get the pivot fields swagger-ui schema. */ protected pivotSchema(request: AvonRequest): Record<string, OpenApiSchema>; /** * Get the value considered as null. */ nullValue(): AnyValue; /** * Determine field is filterable or not. */ isFilterable(): boolean; }