All files / src/model model-state.ts

100% Statements 11/11
100% Branches 4/4
100% Functions 5/5
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 137 138 139 140 141 142 143 144                                                                                                                1292x 1292x 1292x                                 527x 527x                                         1x 1x 1x                                           5x         5x                 1292x                
import { HookRegistry } from '../hooks/hooks.ts';
import type { StandardSchemaV1 } from '../validation/standard-schema.ts';
import type { ModelValidatorFunction } from '../validation/types.ts';
import {
  ModelDefinition,
  type DefinitionOptions,
  type ModelAttributes,
} from './model-definition.ts';
import type { InitOptions } from './options.ts';
import { runtimeKey, type ModelRuntime } from './runtime.ts';
import { ScopeRegistry } from './scope-registry.ts';
 
/** Internal state of an initialized model class. */
export type ModelState = Readonly<{
  definition: ModelDefinition;
  runtime: ModelRuntime;
  hooks: HookRegistry;
  scopes: ScopeRegistry;
  validators: Readonly<Record<string, ModelValidatorFunction>>;
  schema: StandardSchemaV1 | undefined;
  /** `init` input, kept to rebuild the definition when associations add attributes. */
  input: Readonly<{
    className: string;
    attributes: ModelAttributes;
    options: DefinitionOptions;
    extra: ModelAttributes;
  }>;
  /** Associations by alias. */
  associations: ReadonlyMap<string, unknown>;
}>;
 
/**
 * Builds the state of a model class from `Model.init` arguments, and rebuilds it when
 * associations add attributes.
 *
 * @internal
 */
export class ModelStateFactory {
  /**
   * State of a model class just initialized by `Model.init`.
   *
   * @param className - Class name (default model name).
   * @param attributes - Attribute definitions.
   * @param options - Model options, including the `sequelize` instance.
   * @returns The state.
   */
  create(className: string, attributes: ModelAttributes, options: InitOptions): ModelState {
    const {
      sequelize,
      hooks,
      defaultScope,
      scopes,
      whereMergeStrategy,
      validate,
      validationSchema,
      ...definitionOptions
    } = options;
    const runtime = sequelize[runtimeKey];
    return {
      ...this.#definitionState(className, attributes, definitionOptions, runtime),
      hooks: HookRegistry.fromOption(hooks, HookRegistry.fromOption(runtime.defaults.hooks)),
      scopes: new ScopeRegistry(defaultScope, scopes, whereMergeStrategy),
      validators: validate ?? {},
      schema: validationSchema,
    };
  }
 
  /**
   * State with the definition rebuilt from the `init` input plus attributes of associations.
   *
   * @param state - Current state.
   * @param attributes - Added attributes (foreign keys).
   * @returns The new state.
   */
  withAssociationAttributes(state: ModelState, attributes: ModelAttributes): ModelState {
    const extra = { ...state.input.extra, ...attributes };
    return {
      ...state,
      input: { ...state.input, extra },
      definition: ModelDefinition.normalize(
        state.input.className,
        state.input.attributes,
        state.input.options,
        state.runtime.defaults,
        extra,
      ),
    };
  }
 
  /**
   * State with an attribute removed from the `init` input and the added attributes.
   *
   * @param state - Current state.
   * @param attribute - Attribute name.
   * @returns The new state.
   */
  withoutAttribute(state: ModelState, attribute: string): ModelState {
    const { [attribute]: _removed, ...attributes } = state.input.attributes;
    const { [attribute]: _added, ...extra } = state.input.extra;
    return {
      ...state,
      input: { ...state.input, attributes, extra },
      definition: ModelDefinition.normalize(
        state.input.className,
        attributes,
        state.input.options,
        state.runtime.defaults,
        extra,
      ),
    };
  }
 
  /**
   * State of a schema-bound copy of a model: the definition is rebuilt with the schema.
   *
   * @param state - State of the model the copy extends.
   * @param schema - Schema name.
   * @param delimiter - Separator reported by `getTableName`, `.` when not given.
   * @returns The new state.
   */
  withSchema(state: ModelState, schema: string, delimiter: string | undefined): ModelState {
    const options = {
      ...state.input.options,
      schema,
      ...(delimiter === undefined ? {} : { schemaDelimiter: delimiter }),
    };
    return this.withAssociationAttributes({ ...state, input: { ...state.input, options } }, {});
  }
 
  #definitionState(
    className: string,
    attributes: ModelAttributes,
    options: DefinitionOptions,
    runtime: ModelRuntime,
  ): Pick<ModelState, 'definition' | 'runtime' | 'input' | 'associations'> {
    return {
      definition: ModelDefinition.normalize(className, attributes, options, runtime.defaults),
      runtime,
      input: { className, attributes, options, extra: {} },
      associations: new Map(),
    };
  }
}