All files / src/decorators association-decorators.ts

100% Statements 11/11
100% Branches 3/3
100% Functions 6/6
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136                                                                        131x           18x 18x                                                 131x 2x                               131x     4x                                         131x     5x                                   131x       7x  
import type { BelongsTo as BelongsToAssociation } from '../associations/belongs-to.ts';
import type { BelongsToMany as BelongsToManyAssociation } from '../associations/belongs-to-many.ts';
import type { HasMany as HasManyAssociation } from '../associations/has-many.ts';
import type { HasOne as HasOneAssociation } from '../associations/has-one.ts';
import type { AssociationOptions, BelongsToManyOptions } from '../associations/types.ts';
import type { AnyModel } from '../model/options.ts';
import { decorate } from './decorator-factory.ts';
import type { ModelPropertyDecorator } from './decorator-types.ts';
import type { AssociationSpec, ModelGetter } from './model-metadata.ts';
 
type Kind = AssociationSpec['kind'];
 
// The v6 association classes share their names with these decorators, so the root exports the class
// types under the same names: `HasMany<S, T>` in a type position is the association, `@HasMany(...)` the decorator.
 
/** The association created by `Model.hasOne` (Sequelize v6 `HasOne` class type); the `@HasOne` decorator has the same name as a value. */
export type HasOne<
  S extends AnyModel = AnyModel,
  T extends AnyModel = AnyModel,
> = HasOneAssociation<S, T>;
/** The association created by `Model.hasMany` (Sequelize v6 `HasMany` class type); the `@HasMany` decorator has the same name as a value. */
export type HasMany<
  S extends AnyModel = AnyModel,
  T extends AnyModel = AnyModel,
> = HasManyAssociation<S, T>;
/** The association created by `Model.belongsTo` (Sequelize v6 `BelongsTo` class type); the `@BelongsTo` decorator has the same name as a value. */
export type BelongsTo<
  S extends AnyModel = AnyModel,
  T extends AnyModel = AnyModel,
> = BelongsToAssociation<S, T>;
/** The association created by `Model.belongsToMany` (Sequelize v6 `BelongsToMany` class type); the `@BelongsToMany` decorator has the same name as a value. */
export type BelongsToMany<
  S extends AnyModel = AnyModel,
  T extends AnyModel = AnyModel,
> = BelongsToManyAssociation<S, T>;
 
const association = (
  kind: Kind,
  target: ModelGetter,
  options: AssociationOptions = {},
  through?: AssociationSpec['through'],
): ModelPropertyDecorator =>
  decorate.forProperty((meta, key) => {
    meta.associations.push({
      kind,
      key,
      target,
      options,
      ...(through === undefined ? {} : { through }),
    });
  });
 
/**
 * Adds a `hasOne` association; the foreign key is on the target. The property name is the alias (`as`).
 * The foreign key is the attribute of the target marked with `@ForeignKey` for this model, unless
 * `options.foreignKey` is given.
 *
 * @param target - Function returning the associated model.
 * @param options - Association options (`foreignKey`, `onDelete`, …).
 * @returns An attribute decorator.
 * @example
 * ```ts
 * class User extends Model {
 *   @HasOne(() => Profile)
 *   profile!: Profile;
 * }
 * ```
 */
export const HasOne = (target: ModelGetter, options?: AssociationOptions): ModelPropertyDecorator =>
  association('hasOne', target, options);
 
/**
 * Adds a `hasMany` association; the foreign key is on the target. The property name is the alias (`as`).
 *
 * @param target - Function returning the associated model.
 * @param options - Association options (`foreignKey`, `onDelete`, …).
 * @returns An attribute decorator.
 * @example
 * ```ts
 * class User extends Model {
 *   @HasMany(() => Post)
 *   posts!: Post[];
 * }
 * ```
 */
export const HasMany = (
  target: ModelGetter,
  options?: AssociationOptions,
): ModelPropertyDecorator => association('hasMany', target, options);
 
/**
 * Adds a `belongsTo` association; the foreign key is on this model, marked with `@ForeignKey`
 * for the target unless `options.foreignKey` is given. The property name is the alias (`as`).
 *
 * @param target - Function returning the associated model.
 * @param options - Association options (`foreignKey`, `targetKey`, `onDelete`, …).
 * @returns An attribute decorator.
 * @example
 * ```ts
 * class Post extends Model {
 *   @ForeignKey(() => User)
 *   @Column(DataTypes.INTEGER)
 *   userId!: number;
 *
 *   @BelongsTo(() => User)
 *   user!: User;
 * }
 * ```
 */
export const BelongsTo = (
  target: ModelGetter,
  options?: AssociationOptions,
): ModelPropertyDecorator => association('belongsTo', target, options);
 
/**
 * Adds a `belongsToMany` association through a junction model. The junction model's `@ForeignKey`
 * attributes give `foreignKey` (to this model) and `otherKey` (to the target) unless `options` sets them.
 *
 * @param target - Function returning the associated model.
 * @param through - Junction table name, function returning the junction model, or `{ model, scope }`.
 * @param options - Association options (`foreignKey`, `otherKey`, …).
 * @returns An attribute decorator.
 * @example
 * ```ts
 * class Post extends Model {
 *   @BelongsToMany(() => Tag, 'post_tags')
 *   tags!: Tag[];
 * }
 * ```
 */
export const BelongsToMany = (
  target: ModelGetter,
  through: NonNullable<AssociationSpec['through']>,
  options?: Omit<BelongsToManyOptions, 'through'>,
): ModelPropertyDecorator => association('belongsToMany', target, options, through);