All files / src/model define-hooks.ts

100% Statements 9/9
75% Branches 6/8
100% Functions 2/2
100% Lines 9/9

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                                                                  1292x 1292x 1290x   2x 1292x 1292x             1292x       2x 2x      
import { HookRegistry } from '../hooks/hooks.ts';
import { inflector } from './inflector.ts';
import type { ModelAttributes } from './model-definition.ts';
import type { InitOptions } from './options.ts';
import { runtimeKey } from './runtime.ts';
 
/** Attributes and options of a model after the `beforeDefine` hooks ran. */
export type DefinedModel = Readonly<{
  name: string;
  attributes: ModelAttributes;
  options: InitOptions;
}>;
 
/**
 * `beforeDefine(attributes, options)` hooks of the Sequelize instance: they may change the model name, its
 * `name` forms (`options.name.plural`), the attributes and the options before the definition is normalized (v6).
 *
 * @internal
 */
export class DefineHooks {
  /**
   * Runs the hooks on copies of the input.
   *
   * @param model - Model class being initialized.
   * @param attributes - Attribute definitions.
   * @param options - Model options, including the `sequelize` instance.
   * @returns The (possibly changed) name, attributes and options.
   */
  before(
    model: Readonly<{ name: string }>,
    attributes: ModelAttributes,
    options: InitOptions,
  ): DefinedModel {
    const hooks = options.sequelize[runtimeKey].hooks();
    if (!hooks.has('beforeDefine')) {
      return { name: options.modelName ?? model.name, attributes, options };
    }
    const name = options.modelName ?? model.name;
    const copy = { attributes: { ...attributes }, options: this.#withNames(options, name) };
    HookRegistry.runAllSync(
      [hooks],
      'beforeDefine',
      options.sequelize,
      copy.attributes,
      copy.options,
    );
    return { name: copy.options.modelName ?? name, ...copy };
  }
 
  #withNames(options: InitOptions, name: string): InitOptions {
    const forms = { plural: inflector.pluralize(name), singular: inflector.singularize(name) };
    return { name: forms, ...options, modelName: name };
  }
}