import type { AttributeNames, Attributes, CreateOptions, CreationAttributes, FindOptions, InstanceDestroyOptions, InstanceUpdateOptions, ModelStatic } from '../model'; import { Model } from '../model'; import type { AssociationOptions, SingleAssociationAccessors } from './base'; import { Association } from './base'; import { BelongsToAssociation } from './belongs-to.js'; import type { NormalizeBaseAssociationOptions } from './helpers'; /** * One-to-one association. * See {@link Model.hasOne} * * This is almost the same as {@link BelongsToAssociation}, but the foreign key will be defined on the target model. * * In the API reference below, add the name of the association to the method, e.g. for `User.hasOne(Project)` the getter will be `user.getProject()`. * * @template S The model on which {@link Model.hasOne} has been called, on which the association methods will be added. * @template T The model passed to {@link Model.hasOne}. This model will receive the Foreign Key attribute. * @template SourceKey The name of the attribute that the foreign key in the target model will reference. * @template TargetKey The name of the Foreign Key attribute on the Target model. * @template TargetPrimaryKey The name of the Primary Key attribute of the Target model. Used by {@link HasOneSetAssociationMixin}. */ export declare class HasOneAssociation = AttributeNames, TargetKey extends AttributeNames = AttributeNames, TargetPrimaryKey extends AttributeNames = AttributeNames> extends Association> { #private; get foreignKey(): TargetKey; get foreignKeys(): import("./base").Key[]; /** * The column name of the foreign key (on the target model) */ get identifierField(): string; /** * The name of the attribute the foreign key points to. * In HasOne, it is on the Source Model, instead of the Target Model (unlike {@link BelongsToAssociation.targetKey}). * The {@link Association.foreignKey} is on the Target Model. */ get sourceKey(): SourceKey; /** * The Column Name of the source key. */ get sourceKeyField(): string; /** * @deprecated use {@link sourceKey} */ get sourceKeyAttribute(): SourceKey; readonly inverse: BelongsToAssociation; readonly accessors: SingleAssociationAccessors; constructor(secret: symbol, source: ModelStatic, target: ModelStatic, options: NormalizedHasOneOptions, parent?: Association, inverse?: BelongsToAssociation); static associate, TargetKey extends AttributeNames>(secret: symbol, source: ModelStatic, target: ModelStatic, options?: HasOneOptions, parent?: Association, inverse?: BelongsToAssociation): HasOneAssociation; /** * Get the associated instance. * * See {@link HasOneGetAssociationMixinOptions} for a full explanation of options. * This method is mixed-in the source model prototype. See {@link HasOneGetAssociationMixin}. * * @param instances source instances * @param options find options */ get(instances: S, options?: HasOneGetAssociationMixinOptions): Promise; get(instances: S[], options?: HasOneGetAssociationMixinOptions): Promise>; /** * Set the associated model. * * @param sourceInstance the source instance * @param associatedInstanceOrPk 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 getAssociation and `target.save` * * @returns The associated instance, or null if disassociated. */ set(sourceInstance: S, associatedInstanceOrPk: T | T[TargetPrimaryKey], options?: HasOneSetAssociationMixinOptions): Promise; set(sourceInstance: S, associatedInstanceOrPk: null, options?: HasOneSetAssociationMixinOptions): Promise; /** * Create a new instance of the associated model and associate it with this. * * See {@link Model.create} for a full explanation of options. * * @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?: HasOneCreateAssociationMixinOptions): Promise; } export type NormalizedHasOneOptions = NormalizeBaseAssociationOptions, 'inverse'>> & { inverse?: Exclude['inverse'], string>; }; /** * Options provided when associating models with hasOne relationship */ export interface HasOneOptions extends AssociationOptions { /** * The name of the field to use as the key for the association in the source table. * Defaults to the primary key of the source table. * * This is the attribute the foreign key will target. Not to be confused with {@link AssociationOptions.foreignKey}. */ sourceKey?: SourceKey; /** * The name of the inverse association, or an object for further association setup. */ inverse?: string | undefined | { as?: AssociationOptions['as']; scope?: AssociationOptions['scope']; }; } /** * The options for the getAssociation mixin of the hasOne association. * * @see HasOneGetAssociationMixin */ export interface HasOneGetAssociationMixinOptions 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 hasOne. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare getRole: HasOneGetAssociationMixin; * } * * User.hasOne(Role); * ``` * * @returns The associated model, or null if no model is associated. HasOne associations are always nullable because the foreign key is on the target model. * * @see Model.hasOne */ export type HasOneGetAssociationMixin = (options?: HasOneGetAssociationMixinOptions) => Promise; /** * The options for the setAssociation mixin of the hasOne association. * * @see HasOneSetAssociationMixin */ export interface HasOneSetAssociationMixinOptions extends HasOneGetAssociationMixinOptions, InstanceUpdateOptions> { /** * Delete the previous associated model. Default to false. * * Only applies if the foreign key is nullable. If the foreign key is not nullable, * the previous associated model is always deleted. */ destroyPrevious?: boolean | Omit; } /** * The setAssociation mixin applied to models with hasOne. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare setRole: HasOneSetAssociationMixin; * } * * User.hasOne(Role); * ``` * * @see Model.hasOne */ export type HasOneSetAssociationMixin = { (newAssociation: null, options?: HasOneSetAssociationMixinOptions): Promise; (newAssociation: T | TModelPrimaryKey, options?: HasOneSetAssociationMixinOptions): Promise; }; /** * The options for the createAssociation mixin of the hasOne association. * * @see HasOneCreateAssociationMixin */ export interface HasOneCreateAssociationMixinOptions extends Omit, 'fields'>, CreateOptions> { } /** * The createAssociation mixin applied to models with hasOne. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare createRole: HasOneCreateAssociationMixin; * } * * User.hasOne(Role); * ``` * * @see Model.hasOne */ export type HasOneCreateAssociationMixin = never> = (values?: Omit, ExcludedAttributes>, options?: HasOneCreateAssociationMixinOptions) => Promise; //# sourceMappingURL=has-one.d.ts.map