All files / src/model model-registry.ts

100% Statements 66/66
96.66% Branches 29/30
100% Functions 25/25
100% Lines 61/61

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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293                      131x   131x 131x 131x 131x 131x 131x                                           1292x 1292x 6x   1292x 1292x 1292x 1292x 1292x 1292x                     567482x 567482x 567482x 2x   567480x                 3x 3x 3x 3x                   341425x                   1188x 1188x 1x   1187x                   16x                                 139x                                 3x                         2x 1x       1x                     38x 38x                         5x 5x 5x 5x   5x 5x 1x                     178x 178x 178x                     9138x 9138x 9138x                         1811x                   627x                     522x 522x 522x 522x                   1x                 1x       569146x               1x      
import { HookRegistry, type HookFunction, type HookType } from '../hooks/hooks.ts';
import { DefineHooks } from './define-hooks.ts';
import type { ModelAttributes, ModelDefinition } from './model-definition.ts';
import { ModelAccessors } from './model-accessors.ts';
import { ModelStateFactory, type ModelState } from './model-state.ts';
import type { InitOptions } from './options.ts';
import type { RuntimeOwner } from './runtime.ts';
import type { ScopeDefinition, ScopeOptions, ScopeSelector } from './scope-registry.ts';
 
export type { ModelState } from './model-state.ts';
 
const NOT_INITIALIZED = 'Model is not initialized. Call Model.init() or sequelize.define() first.';
 
const states = new WeakMap<object, ModelState>();
const selections = new WeakMap<object, readonly ScopeSelector[]>();
const owners = new WeakMap<object, RuntimeOwner>();
const accessors = new ModelAccessors();
const factory = new ModelStateFactory();
const defineHooks = new DefineHooks();
 
/**
 * Per-class state of models: definition, runtime, hooks, scopes, validators and associations of
 * each initialized model class (shared by its scoped subclasses), the scopes selected by
 * `Model.scope()`, and the attribute / association accessors installed on model prototypes.
 * Instances share one store, so they can be created wherever needed.
 */
export class ModelRegistry {
  /**
   * Initializes a model class (`Model.init`): normalizes the definition, stores the state,
   * installs attribute accessors and registers the model in `sequelize.models`.
   *
   * @param model - Model class.
   * @param attributes - Attribute definitions.
   * @param options - Model options, including the `sequelize` instance.
   */
  init(
    model: Readonly<{ name: string; prototype: object }>,
    attributes: ModelAttributes,
    options: InitOptions,
  ): void {
    const defined = defineHooks.before(model, attributes, options);
    if (defined.name !== model.name) {
      Object.defineProperty(model, 'name', { value: defined.name, configurable: true });
    }
    const state = factory.create(defined.name, defined.attributes, defined.options);
    states.set(model, state);
    owners.set(model, options.sequelize);
    this.defineAttributeAccessors(model.prototype, state.definition);
    state.runtime.register(state.definition.name, model);
    HookRegistry.runAllSync([state.runtime.hooks()], 'afterDefine', options.sequelize, model);
  }
 
  /**
   * State of a model class or of a scoped subclass of it.
   *
   * @param model - Model class.
   * @returns The state.
   * @throws Error when the model is not initialized.
   */
  state(model: object): ModelState {
    const base = this.#owner(model);
    const state = base === undefined ? undefined : states.get(base);
    if (state === undefined) {
      throw new Error(NOT_INITIALIZED);
    }
    return state.definition.name === (base as { name: string }).name
      ? state
      : this.#renamed(base as object, state);
  }
 
  // v6 reads `Model.name` whenever it builds a query (table alias, include aliases), so a class that
  // was renamed after `init` (a bundler, a test redefining `name`) keeps the SQL and `Model.name`
  // in step: raw fragments built from `Model.name` refer to the alias the query emits.
  #renamed(base: object, state: ModelState): ModelState {
    const name = (base as { name: string }).name;
    const renamed = { ...state, definition: { ...state.definition, name } };
    states.set(base, renamed);
    return renamed;
  }
 
  /**
   * Normalized definition of a model class.
   *
   * @param model - Model class.
   * @returns The definition.
   */
  definition(model: object): ModelDefinition {
    return this.state(model).definition;
  }
 
  /**
   * Replaces the state of the model that owns `model` (scoped subclasses share it).
   *
   * @param model - Model class.
   * @param change - Computes the new state from the current one.
   */
  update(model: object, change: (state: ModelState) => ModelState): void {
    const base = this.#owner(model);
    if (base === undefined) {
      throw new Error(NOT_INITIALIZED);
    }
    states.set(base, change(this.state(base)));
  }
 
  /**
   * `Sequelize` instance given to `Model.init` for exactly this class.
   *
   * @param model - Model class.
   * @returns The instance, or `undefined`.
   */
  sequelizeOf(model: object): RuntimeOwner | undefined {
    return owners.get(model);
  }
 
  /**
   * Adds a hook to a model.
   *
   * @param model - Model class.
   * @param type - Hook type.
   * @param nameOrFn - Hook name or function.
   * @param fn - Hook function when a name is given.
   */
  addHook(
    model: object,
    type: HookType,
    nameOrFn: string | HookFunction,
    fn: HookFunction | undefined,
  ): void {
    this.update(model, (state) => ({
      ...state,
      hooks:
        typeof nameOrFn === 'string'
          ? state.hooks.add(type, fn ?? this.#missingHook(), nameOrFn)
          : state.hooks.add(type, nameOrFn),
    }));
  }
 
  /**
   * Removes hooks of a model by name or function.
   *
   * @param model - Model class.
   * @param type - Hook type.
   * @param nameOrFn - Hook name or function; all hooks of the type when omitted.
   */
  removeHook(model: object, type: HookType, nameOrFn: string | HookFunction | undefined): void {
    this.update(model, (state) => ({ ...state, hooks: state.hooks.remove(type, nameOrFn) }));
  }
 
  /**
   * Adds a named scope (or replaces the default scope with name `'defaultScope'`).
   *
   * @param model - Model class.
   * @param name - Scope name.
   * @param scope - Options or a function returning options.
   * @param override - Replace an existing scope.
   * @throws Error when the scope exists and `override` is not set.
   */
  addScope(model: object, name: string, scope: ScopeDefinition, override: boolean): void {
    if (this.state(model).scopes.has(name) && !override) {
      throw new Error(
        `The scope ${name} already exists. Pass { override: true } as options to silence this error`,
      );
    }
    this.update(model, (state) => ({ ...state, scopes: state.scopes.with(name, scope) }));
  }
 
  /**
   * Marks `scoped` (a subclass of `model`) as using the given scopes, checking that they resolve.
   *
   * @param model - Model class whose scopes are used.
   * @param scoped - Scoped subclass.
   * @param selectors - Selected scopes.
   */
  selectScopes(model: object, scoped: object, selectors: readonly ScopeSelector[]): void {
    this.state(model).scopes.resolve(selectors);
    selections.set(scoped, selectors);
  }
 
  /**
   * Gives `bound` (a subclass of `model`) its own state bound to a schema, and the scope
   * selection of `model`.
   *
   * @param model - Model class the copy extends.
   * @param bound - Schema-bound subclass.
   * @param schema - Schema name.
   * @param delimiter - Separator reported by `getTableName`.
   */
  bindSchema(model: object, bound: object, schema: string, delimiter: string | undefined): void {
    states.set(bound, factory.withSchema(this.state(model), schema, delimiter));
    const owner = owners.get(this.#owner(model) as object);
    Eif (owner !== undefined) {
      owners.set(bound, owner);
    }
    const selected = selections.get(model);
    if (selected !== undefined) {
      selections.set(bound, selected);
    }
  }
 
  /**
   * Scope options in effect for `model`: its selected scopes, or the default scope.
   *
   * @param model - Model class.
   * @returns Scope options.
   */
  activeScope(model: object): ScopeOptions {
    const state = this.state(model);
    const selected = selections.get(model);
    return selected === undefined ? state.scopes.defaultScope : state.scopes.resolve(selected);
  }
 
  /**
   * Query options with the active scope of `model` applied (query options win).
   *
   * @param model - Model class.
   * @param options - Query options.
   * @returns Merged options.
   */
  scoped<T extends ScopeOptions>(model: object, options: T): T {
    const { scopes } = this.state(model);
    const selected = selections.get(model);
    return scopes.inject(
      selected === undefined ? scopes.defaultScope : scopes.resolve(selected),
      options,
    );
  }
 
  /**
   * Defines `instance.<attribute>` getters / setters on the model prototype (once per attribute).
   *
   * @param prototype - Model prototype.
   * @param definition - Model definition.
   */
  defineAttributeAccessors(prototype: object, definition: ModelDefinition): void {
    accessors.defineAttributes(prototype, definition);
  }
 
  /**
   * Defines `instance.<alias>` for an association, holding included / nested instances (Sequelize v6).
   *
   * @param prototype - Model prototype.
   * @param alias - Association alias.
   */
  defineAssociationAccessor(prototype: object, alias: string): void {
    accessors.defineAssociation(prototype, alias);
  }
 
  /**
   * Rebuilds the definition of `model` with attributes contributed by associations.
   *
   * @param model - Model class.
   * @param attributes - Added attributes (foreign keys).
   * @returns The new definition.
   */
  addAssociationAttributes(model: object, attributes: ModelAttributes): ModelDefinition {
    this.update(model, (state) => factory.withAssociationAttributes(state, attributes));
    const { definition } = this.state(model);
    this.defineAttributeAccessors((model as { prototype: object }).prototype, definition);
    return definition;
  }
 
  /**
   * Removes an attribute and rebuilds the definition (`Model.removeAttribute`).
   *
   * @param model - Model class.
   * @param attribute - Attribute name.
   */
  removeAttribute(model: object, attribute: string): void {
    this.update(model, (state) => factory.withoutAttribute(state, attribute));
  }
 
  /**
   * Rebuilds the definition from the `init` input (`Model.refreshAttributes`).
   *
   * @param model - Model class.
   */
  refreshAttributes(model: object): void {
    this.addAssociationAttributes(model, {});
  }
 
  #owner(model: object | null): object | undefined {
    return model === null
      ? undefined
      : states.has(model)
        ? model
        : this.#owner(Object.getPrototypeOf(model) as object | null);
  }
 
  #missingHook(): never {
    throw new Error('addHook: hook function is required');
  }
}