import ModelCollection from '../ModelCollection'; import type Model from '../Model'; import CallsApi from './CallsApi'; import type { Attributes } from './HasAttributes'; import Collection from '../../Support/Collection'; import type { MaybeArray } from '../../Support/type'; type Relation = 'belongsTo' | 'belongsToMany' | 'hasMany' | 'hasOne' | 'morphMany' | 'morphOne' | 'morphTo'; type MorphToCallback = (self: MT, relatedData: Attributes) => typeof Model; export default class HasRelations extends CallsApi { /** * The loaded relations for the model. * The keys do not include the relation prefixes. * * @protected */ protected relations: Record)>; /** * The string all the relation methods expected to prefixed by. * * @protected * * @return {string} */ protected get relationMethodPrefix(): string; /** * The key name of the parent of this model which was * instantiated from a hasOne or hasMany relation. * This is used to remove the where query when saving * a new entity like `parent.$child().save({});` * * @protected */ protected hasOneOrManyParentKeyName: string | undefined; /** * Load relationship(s) from remote. * * @param {string|string[]} relations * @param {boolean} forceReload - Whether the already loaded relations should also be reloaded. * * @return {Promise} */ load(relations: MaybeArray, forceReload?: boolean): Promise; /** * Determine if the given relation is loaded. * * @param {string} name */ relationLoaded(name: string): boolean; /** * Get an array of loaded relation names. * * @return {string[]} */ loadedRelationKeys(): string[]; /** * Get the specified relationship. * * @param {string} name * * @return {Model|ModelCollection} */ getRelation(name: string): ModelCollection | T; /** * Assert whether the relation has been defined on this instance. * * @param {string} name * @protected * * @return {boolean} */ protected relationDefined(name: string): boolean; /** * Get the name of the relation type for the given relation. * * @param {string} name * * @protected * * @return {Relation} */ protected getRelationType(name: string): Relation; /** * Parse the given data into a related model class * and add the relation to this instance. * * @param {string} name * @param {Model|ModelCollection|object|object[]} value * * @return this */ addRelation(name: string, value: Collection | Collection | MaybeArray | MaybeArray | ModelCollection): this; /** * Remove the relation and its magic access if set. * * @param {string} name * * @return {this} */ removeRelation(name: string): this; /** * Get all the relations. * * @return {object} */ getRelations(): Record)>; /** * Guess the foreign key name that would be used to reference this model. * * @return {string} */ guessForeignKeyName(): string; /** * Remove the prefix from the given string if set. * * @param {string} name * * @private * * @return {string} */ private removeRelationPrefix; /** * Add the relation type onto the model. * * @param {Model} model * @param {'belongsTo'|'belongsToMany'|'hasOne'|'hasMany'|'morphTo'|'morphMany'|'morphOne'} relationType * * @private * * @return {void} */ private static configureRelationType; /** * Set the endpoint to a nested url structure. * * @param {Model|Model[]} models * * @return this */ for(models: MaybeArray Model)>): this; /** * Set the endpoint on the correct model for querying. * * @param {Model} related * @param {string=} foreignKey * * @return {Model} */ belongsTo(related: new () => T, foreignKey?: string): T; /** * Set the endpoint on the correct model for querying. * * @param {Model} related * @param {string=} relationName - The name of the relation on the backend. * * @return {Model} */ belongsToMany(related: new () => T, relationName?: string): T; /** * Set the endpoint on the correct model for querying. * * @param {Model} related * @param {string=} foreignKey * * @return {Model} */ hasOne(related: new () => T, foreignKey?: string): T; /** * Set the endpoint on the correct model for querying. * * @param {Model} related * @param {string=} foreignKey * * @return {Model} */ hasMany(related: new () => T, foreignKey?: string): T; /** * Set the endpoint on the correct model for querying. * * @param {Model} related * @param {string=} morphName * * @return {Model} */ morphMany(related: new () => T, morphName?: string): T; /** * Set the endpoint on the correct model for querying. * * @param {Model} related * @param {string=} morphName * * @return {Model} */ morphOne(related: new () => T, morphName?: string): T; /** * Add a constraint for the next query to return all relation. * * @param cb - Callback that returns a model that this morphs to. * @param relationName - The name of the relation to be called. E.g.: `'commentable'` * * @example * public $contractable(): this { * return this.morphTo((self, _data) => { * return self.contractableType === 'team' ? Team : User; * }); * } * * @return {Model} */ morphTo(cb: MorphToCallback, relationName?: string): this; /** * Get the polymorphic column names. * * @param {string=} name */ protected getMorphs(name?: string): Record<'id' | 'type', string>; /** * Throw an error if the model does not exist before calling the specified method. * * @param {string} methodName * * @protected * * @internal */ protected throwIfModelDoesntExistsWhenCalling(methodName: string): void; } export {}; //# sourceMappingURL=HasRelations.d.ts.map