All files / src/decorators decorator-factory.ts

100% Statements 24/24
100% Branches 12/12
100% Functions 14/14
100% Lines 24/24

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                                                      24x 24x                 763x 149x 149x 149x                 331x 107x                     393x 15x                       131x 16x                   3668x 6x 6x 1x   5x             149x 149x 4x   145x 145x 298x           131x  
import type { HookType } from '../hooks/hooks.ts';
import type { AttributeOptions } from '../model/attribute-options.ts';
import { signature } from './decorator-signature.ts';
import type {
  ModelClassDecorator,
  ModelPropertyDecorator,
  ModelStaticMethodDecorator,
} from './decorator-types.ts';
import { metadata, type ModelMetadata } from './model-metadata.ts';
 
type Tc39FieldContext = Readonly<{
  name: string | symbol;
  addInitializer: (initializer: (this: object) => void) => void;
}>;
 
/**
 * Builds decorators that work with both calling conventions from a function that records
 * something in the metadata of the decorated class.
 *
 * @internal
 */
export class DecoratorFactory {
  /**
   * @param record - Records into the metadata of the class.
   * @returns A class decorator.
   */
  forClass(record: (meta: ModelMetadata) => void): ModelClassDecorator {
    return ((...args: unknown[]): void => {
      record(metadata.of(signature.resolve(args)));
    }) as never;
  }
 
  /**
   * @param record - Records into the metadata of the class, given the attribute name.
   * @returns An attribute decorator.
   */
  forProperty(record: (meta: ModelMetadata, key: string) => void): ModelPropertyDecorator {
    return ((...args: unknown[]) => {
      const target = signature.resolve(args);
      record(metadata.of(target), target.key as string);
      this.#unshadow(args[1]);
    }) as never;
  }
 
  /**
   * @param partial - Column options to merge into the attribute.
   * @returns An attribute decorator.
   */
  column(partial: Partial<AttributeOptions>): ModelPropertyDecorator {
    return this.forProperty((meta, key) => {
      meta.columns[key] = { ...meta.columns[key], ...partial };
    });
  }
 
  /**
   * @param build - Creates the decorator from the optional argument of the factory call.
   * @returns A decorator usable bare (`@Unique`) and called (`@Unique('name')`).
   */
  optionalProperty<A, D extends ModelPropertyDecorator>(
    build: (argument?: A) => D,
  ): D & ((argument?: A) => D) {
    return ((...args: unknown[]) =>
      args.length >= 2
        ? (build() as unknown as (...a: unknown[]) => void)(...args)
        : build(args[0] as A | undefined)) as never;
  }
 
  /**
   * @param build - Creates the decorator from the optional argument of the factory call.
   * @returns A class decorator usable bare (`@Table`) and called (`@Table({ ... })`).
   */
  optionalClass<A>(
    build: (argument?: A) => ModelClassDecorator,
  ): ModelClassDecorator & ((argument?: A) => ModelClassDecorator) {
    return ((...args: unknown[]) =>
      typeof args[0] === 'function'
        ? (build() as unknown as (...a: unknown[]) => void)(...args)
        : build(args[0] as A | undefined)) as never;
  }
 
  /**
   * @param type - Hook type to register the decorated static method for.
   * @returns A static method decorator.
   */
  forHook(type: HookType): ModelStaticMethodDecorator {
    return ((...args: unknown[]) => {
      const target = signature.resolve(args);
      if (!target.isStatic || target.key === undefined) {
        throw new Error(`Hook decorators apply to static methods only (${type}).`);
      }
      metadata.of(target).hooks.push({ type, key: target.key });
    }) as never;
  }
 
  // A class field would shadow the attribute accessors Model.init installs on the prototype, so
  // standard decorators drop the own property right after the field is defined.
  #unshadow(context: unknown): void {
    const field = context as Partial<Tc39FieldContext>;
    if (field.addInitializer === undefined || field.name === undefined) {
      return;
    }
    const name = field.name;
    field.addInitializer(function dropOwnField(this: object) {
      Reflect.deleteProperty(this, name);
    });
  }
}
 
/** @internal Shared decorator factory. */
export const decorate = new DecoratorFactory();