import type { MapView } from '@sequelize/utils'; import type { AttributeNames, Attributes, CreateOptions, CreationAttributes, FindOptions, Model, ModelStatic, NormalizedAttributeOptions, SaveOptions } from '../model'; import type { AssociationOptions, Key, SingleAssociationAccessors } from './base'; import { Association } from './base'; import type { NormalizeBaseAssociationOptions } from './helpers'; /** * One-to-one association * See {@link Model.belongsTo} * * This is almost the same as {@link HasOneAssociation}, but the foreign key will be defined on the source model. * * In the API reference below, add the name of the association to the method, e.g. for `User.belongsTo(Project)` the getter will be `user.getProject()`. * * @template S The model on which {@link Model.belongsTo} has been called, on which the association methods, as well as the foreign key attribute, will be added. * @template T The model passed to {@link Model.belongsTo}. * @template SourceKey The name of the Foreign Key attribute on the Source model. * @template TargetKey The name of the attribute that the foreign key in the source model will reference, typically the Primary Key. */ export declare class BelongsToAssociation = AttributeNames, TargetKey extends AttributeNames = AttributeNames> extends Association> { #private; readonly accessors: SingleAssociationAccessors; /** * The attribute name of the identifier * * @deprecated use {@link foreignKey} instead */ get identifier(): string; foreignKey: SourceKey; foreignKeys: Array>; /** * The column name of the foreign key */ identifierField: string; /** * The name of the attribute the foreign key points to. * In belongsTo, this key is on the Target Model, instead of the Source Model (unlike {@link HasOneAssociation.sourceKey}). * The {@link Association.foreignKey} is on the Source Model. */ targetKey: TargetKey; /** * The names of the attributes the foreign key points to. * In belongsTo, these keys is on the Target Model, instead of the Source Model * The {@link Association.foreignKeys} is on the Source Model. */ targetKeys: TargetKey[]; targetKeyIsPrimary(targetKey: TargetKey): boolean; /** * The column name of the target key * * @param targetKey */ targetKeyField(targetKey: TargetKey): string; readonly isCompositeKey: boolean; /** * @deprecated use {@link BelongsToAssociation.targetKey} */ get targetIdentifier(): string; inverse: Association | undefined; constructor(secret: symbol, source: ModelStatic, target: ModelStatic, options: NormalizedBelongsToOptions, parent?: Association); /** * Edge case for hashing binary composite key for backwards compatibility (?) * unclear if this only happens with 2 but tests are written for 2, but it was probably written * that way to hack / 'support' composite primary keys for some scenarios like * packages/core/test/unit/dialects/abstract/query.test.js:147 * * @param targetAttributes * @protected */ protected shouldHashPrimaryKey(targetAttributes: MapView>>): Boolean; static associate, TargetKey extends AttributeNames>(secret: symbol, source: ModelStatic, target: ModelStatic, options?: BelongsToOptions, parent?: Association): BelongsToAssociation; protected inferForeignKey(): string; /** * Get the associated instance. * * See {@link BelongsToGetAssociationMixinOptions} for a full explanation of options. * This method is mixed-in the source model prototype. See {@link BelongsToGetAssociationMixin}. * * @param instances source instances * @param options find options */ get(instances: S, options?: BelongsToGetAssociationMixinOptions): Promise; get(instances: S[], options?: BelongsToGetAssociationMixinOptions): Promise>; /** * Set the associated model. * * @param sourceInstance the source instance * @param associatedInstance An persisted instance or the primary key of an instance to associate with this. Pass `null` to remove the association. * @param options options passed to `this.save` */ set(sourceInstance: S, associatedInstance: T | T[TargetKey] | null, options?: BelongsToSetAssociationMixinOptions): Promise; /** * Create a new instance of the associated model and associate it with this. * * @param sourceInstance the source instance * @param values values to create associated model instance with * @param options Options passed to `target.create` and setAssociation. * * @returns The created target model */ create(sourceInstance: S, values?: CreationAttributes, options?: BelongsToCreateAssociationMixinOptions): Promise; } export type NormalizedBelongsToOptions = NormalizeBaseAssociationOptions>; /** * Options provided when associating models with belongsTo relationship * * @see Association class belongsTo method */ export interface BelongsToOptions extends AssociationOptions { /** * The name of the field to use as the key for the association in the target table. Defaults to the primary * key of the target table */ targetKey?: TargetKey; inverse?: { type: 'hasMany' | 'hasOne'; as?: string; scope?: AssociationOptions['scope']; }; } /** * The options for the getAssociation mixin of the belongsTo association. * * @see BelongsToGetAssociationMixin */ export interface BelongsToGetAssociationMixinOptions extends FindOptions> { /** * Apply a scope on the related model, or remove its default scope by passing false. */ scope?: string | string[] | boolean; /** * Apply a schema on the related model */ schema?: string; schemaDelimiter?: string; } /** * The getAssociation mixin applied to models with belongsTo. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare getRole: BelongsToGetAssociationMixin; * } * * User.belongsTo(Role); * ``` * * @see Model.belongsTo */ export type BelongsToGetAssociationMixin = (options?: BelongsToGetAssociationMixinOptions) => Promise; /** * The options for the setAssociation mixin of the belongsTo association. * * @see BelongsToSetAssociationMixin */ export interface BelongsToSetAssociationMixinOptions extends SaveOptions> { /** * Skip saving this after setting the foreign key if false. */ save?: boolean; } /** * The setAssociation mixin applied to models with belongsTo. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare setRole: BelongsToSetAssociationMixin; * } * * User.belongsTo(Role); * ``` * * @see Model.belongsTo * * @template TargetKeyType The type of the attribute that the foreign key references. */ export type BelongsToSetAssociationMixin = (newAssociation?: T | TargetKeyType, options?: BelongsToSetAssociationMixinOptions) => Promise; /** * The options for the createAssociation mixin of the belongsTo association. * * @see BelongsToCreateAssociationMixin */ export interface BelongsToCreateAssociationMixinOptions extends CreateOptions>, BelongsToSetAssociationMixinOptions { } /** * The createAssociation mixin applied to models with belongsTo. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare createRole: BelongsToCreateAssociationMixin; * } * * User.belongsTo(Role); * ``` * * @see Model.belongsTo */ export type BelongsToCreateAssociationMixin = (values?: CreationAttributes, options?: BelongsToCreateAssociationMixinOptions) => Promise; //# sourceMappingURL=belongs-to.d.ts.map