All files / src/decorators foreign-key-decorator.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 2/2
100% Lines 3/3

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                                              131x 14x 14x    
import type { ForeignKey as ForeignKeyType } from '../model/types.ts';
import { decorate } from './decorator-factory.ts';
import type { ModelPropertyDecorator } from './decorator-types.ts';
import type { ModelGetter } from './model-metadata.ts';
 
/** Marks an attribute type as a foreign key, so it stays out of the creation attributes (Sequelize v6 `ForeignKey`). */
export type ForeignKey<T> = ForeignKeyType<T>;
 
/**
 * Marks the attribute as a foreign key to a model (also the v6 type `ForeignKey<T>` of the same name). Association decorators use it to find the
 * foreign key; it does not change the column definition.
 *
 * @param target - Function returning the referenced model.
 * @returns An attribute decorator.
 * @example
 * ```ts
 * class Post extends Model {
 *   @ForeignKey(() => User)
 *   @Column(DataTypes.INTEGER)
 *   userId!: number;
 * }
 * ```
 */
export const ForeignKey = (target: ModelGetter): ModelPropertyDecorator =>
  decorate.forProperty((meta, key) => {
    meta.foreignKeys[key] = target;
  });