import type { AllowIterable } from '@sequelize/utils'; import type { AttributeNames, Attributes, CreateOptions, CreationAttributes, DestroyOptions, Filterable, FindOptions, InstanceUpdateOptions, Model, ModelStatic, Transactionable } from '../model'; import type { Association, AssociationOptions, Key, MultiAssociationAccessors, MultiAssociationOptions } from './base'; import { MultiAssociation } from './base'; import { BelongsToAssociation } from './belongs-to.js'; import type { NormalizeBaseAssociationOptions } from './helpers'; /** * One-to-many association. * See {@link Model.hasMany} * * Like with {@link HasOneAssociation}, 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.hasMany(Project)` the getter will be `user.getProjects()`. * If the association is aliased, use the alias instead, e.g. `User.hasMany(Project, { as: 'jobs' })` will be `user.getJobs()`. * * @template S The model on which {@link Model.hasMany} has been called, on which the association methods will be added. * @template T The model passed to {@link Model.hasMany}. 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 HasManySetAssociationsMixin} & others. */ export declare class HasManyAssociation = AttributeNames, TargetKey extends AttributeNames = AttributeNames, TargetPrimaryKey extends AttributeNames = AttributeNames> extends MultiAssociation> { #private; accessors: MultiAssociationAccessors; /** * @deprecated use {@link foreignKeys} */ get foreignKey(): TargetKey; /** * The column name of the foreign key (on the target model) */ get identifierField(): string; /** * The name of the attribute the foreign key points to. * * This key is on the Source Model. * The {@link Association.foreignKey} is on the Target Model. */ get sourceKey(): SourceKey; /** * @deprecated use {@link sourceKey} */ get sourceKeyAttribute(): SourceKey; get sourceKeyField(): string; readonly inverse: BelongsToAssociation; readonly foreignKeys: Array>; constructor(secret: symbol, source: ModelStatic, target: ModelStatic, options: NormalizedHasManyOptions, parent?: Association, inverse?: BelongsToAssociation); static associate, TargetKey extends AttributeNames>(secret: symbol, source: ModelStatic, target: ModelStatic, options?: HasManyOptions, parent?: Association, inverse?: BelongsToAssociation): HasManyAssociation; /** * Get everything currently associated with this, using an optional where clause. * * @param instances source instances * @param options find options */ get(instances: S, options?: HasManyGetAssociationsMixinOptions): Promise; get(instances: S[], options?: HasManyGetAssociationsMixinOptions): Promise>; /** * Count everything currently associated with this, using an optional where clause. * * @param instance the source instance * @param options find & count options */ count(instance: S, options?: HasManyCountAssociationsMixinOptions): Promise; /** * Check if one or more rows are associated with `this`. * * @param sourceInstance the source instance * @param targets A list of instances or their primary keys * @param options Options passed to getAssociations */ has(sourceInstance: S, targets: AllowIterable>, options?: HasManyHasAssociationsMixinOptions): Promise; /** * Set the associated models by passing an array of persisted instances or their primary keys. Everything that is not in the passed array will be un-associated * * @param sourceInstance source instance to associate new instances with * @param targets An array of persisted instances or primary key of instances to associate with this. Pass `null` to remove all associations. * @param options Options passed to `target.findAll` and `update`. */ set(sourceInstance: S, targets: AllowIterable> | null, options?: HasManySetAssociationsMixinOptions): Promise; /** * Associate one or more target rows with `this`. This method accepts a Model / string / number to associate a single row, * or a mixed array of Model / string / numbers to associate multiple rows. * * @param sourceInstance the source instance * @param [rawTargetInstances] A single instance or primary key, or a mixed array of persisted instances or primary keys * @param [options] Options passed to `target.update`. */ add(sourceInstance: S, rawTargetInstances: AllowIterable>, options?: HasManyAddAssociationsMixinOptions): Promise; /** * Un-associate one or several target rows. * * @param sourceInstance instance to un associate instances with * @param targets Can be an Instance or its primary key, or a mixed array of instances and primary keys * @param options Options passed to `target.update` */ remove(sourceInstance: S, targets: AllowIterable>, options?: HasManyRemoveAssociationsMixinOptions): Promise; /** * Create a new instance of the associated model and associate it with this. * * @param sourceInstance source instance * @param values values for target model instance * @param options Options passed to `target.create` */ create(sourceInstance: S, values?: CreationAttributes, options?: HasManyCreateAssociationMixinOptions | HasManyCreateAssociationMixinOptions['fields']): Promise; } export type NormalizedHasManyOptions = NormalizeBaseAssociationOptions, 'inverse'>> & { inverse?: Exclude['inverse'], string>; }; /** * Options provided when associating models with hasMany relationship */ export interface HasManyOptions extends MultiAssociationOptions { /** * 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 */ 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 getAssociations mixin of the hasMany association. * * Can provide an optional where clause to limit the associated models through {@link HasManyGetAssociationsMixinOptions.where}. * * @see HasManyGetAssociationsMixin */ export interface HasManyGetAssociationsMixinOptions 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 getAssociations mixin applied to models with hasMany. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare getRoles: HasManyGetAssociationsMixin; * } * * User.hasMany(Role); * ``` * * @see Model.hasMany */ export type HasManyGetAssociationsMixin = (options?: HasManyGetAssociationsMixinOptions) => Promise; /** * The options for the setAssociations mixin of the hasMany association. * * @see HasManySetAssociationsMixin */ export interface HasManySetAssociationsMixinOptions extends FindOptions>, 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>, 'where' | 'transaction' | 'logging' | 'benchmark'> | undefined; } /** * The setAssociations mixin applied to models with hasMany. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare setRoles: HasManySetAssociationsMixin; * } * * User.hasMany(Role); * ``` * * @see Model.hasMany */ export type HasManySetAssociationsMixin = (newAssociations?: Iterable | null, options?: HasManySetAssociationsMixinOptions) => Promise; /** * The options for the addAssociations mixin of the hasMany association. * * @see HasManyAddAssociationsMixin */ export interface HasManyAddAssociationsMixinOptions extends InstanceUpdateOptions> { } /** * The addAssociations mixin applied to models with hasMany. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare addRoles: HasManyAddAssociationsMixin; * } * * User.hasMany(Role); * ``` * * @see Model.hasMany */ export type HasManyAddAssociationsMixin = (newAssociations?: Iterable, options?: HasManyAddAssociationsMixinOptions) => Promise; /** * The options for the addAssociation mixin of the hasMany association. * * @see HasManyAddAssociationMixin */ export interface HasManyAddAssociationMixinOptions extends HasManyAddAssociationsMixinOptions { } /** * The addAssociation mixin applied to models with hasMany. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare addRole: HasManyAddAssociationMixin; * } * * User.hasMany(Role); * ``` * * @see Model.hasMany */ export type HasManyAddAssociationMixin = (newAssociation?: T | TModelPrimaryKey, options?: HasManyAddAssociationMixinOptions) => Promise; /** * The options for the createAssociation mixin of the hasMany association. * * @see HasManyCreateAssociationMixin */ export interface HasManyCreateAssociationMixinOptions extends CreateOptions> { } /** * The createAssociation mixin applied to models with hasMany. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare createRole: HasManyCreateAssociationMixin; * } * * User.hasMany(Role); * ``` * * @see Model.hasMany */ export type HasManyCreateAssociationMixin = never> = (values?: Omit, ExcludedAttributes>, options?: HasManyCreateAssociationMixinOptions) => Promise; /** * The options for the removeAssociation mixin of the hasMany association. * * @see HasManyRemoveAssociationMixin */ export interface HasManyRemoveAssociationMixinOptions extends HasManyRemoveAssociationsMixinOptions { } /** * The removeAssociation mixin applied to models with hasMany. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare removeRole: HasManyRemoveAssociationMixin; * } * * User.hasMany(Role); * ``` * * @see Model.hasMany */ export type HasManyRemoveAssociationMixin = (oldAssociated?: T | TModelPrimaryKey, options?: HasManyRemoveAssociationMixinOptions) => Promise; /** * The options for the removeAssociations mixin of the hasMany association. * * @see HasManyRemoveAssociationsMixin */ export interface HasManyRemoveAssociationsMixinOptions extends Omit>, 'where'> { /** * Delete the associated model. Default to false. * * Only applies if the foreign key is nullable. If the foreign key is not nullable, * the associated model is always deleted. */ destroy?: boolean | Omit>, 'where' | 'transaction' | 'logging' | 'benchmark'> | undefined; } /** * The removeAssociations mixin applied to models with hasMany. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare removeRoles: HasManyRemoveAssociationsMixin; * } * * User.hasMany(Role); * ``` * * @see Model.hasMany */ export type HasManyRemoveAssociationsMixin = (oldAssociateds?: Iterable, options?: HasManyRemoveAssociationsMixinOptions) => Promise; /** * The options for the hasAssociation mixin of the hasMany association. * * @see HasManyHasAssociationMixin */ export interface HasManyHasAssociationMixinOptions extends HasManyGetAssociationsMixinOptions { } /** * The hasAssociation mixin applied to models with hasMany. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare hasRole: HasManyHasAssociationMixin; * } * * User.hasMany(Role); * ``` * * @see Model.hasMany */ export type HasManyHasAssociationMixin = (target: TModel | TModelPrimaryKey, options?: HasManyHasAssociationMixinOptions) => Promise; /** * The options for the hasAssociations mixin of the hasMany association. * * @see HasManyHasAssociationsMixin */ export interface HasManyHasAssociationsMixinOptions extends HasManyGetAssociationsMixinOptions { } /** * The removeAssociations mixin applied to models with hasMany. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare hasRoles: HasManyHasAssociationsMixin; * } * * User.hasMany(Role); * ``` * * @see Model.hasMany */ export type HasManyHasAssociationsMixin = (targets: Iterable, options?: HasManyHasAssociationsMixinOptions) => Promise; /** * The options for the countAssociations mixin of the hasMany association. * * @see HasManyCountAssociationsMixin */ export interface HasManyCountAssociationsMixinOptions extends Transactionable, Filterable> { /** * Apply a scope on the related model, or remove its default scope by passing false. */ scope?: string | boolean; } /** * The countAssociations mixin applied to models with hasMany. * An example of usage is as follows: * * ```typescript * class User extends Model, InferCreationAttributes> { * declare countRoles: HasManyCountAssociationsMixin; * } * * User.hasMany(Role); * ``` * * @see Model.hasMany */ export type HasManyCountAssociationsMixin = (options?: HasManyCountAssociationsMixinOptions) => Promise; //# sourceMappingURL=has-many.d.ts.map