# Code coverage report for framework/models/decorators/relationships.ts

# All files / framework/models/decorators relationships.ts

0% Statements 0/33
0% Branches 0/24
0% Functions 0/13
0% Lines 0/33

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

     import { BaseModel } from '../BaseModel';
    import { RelationshipConfig } from '../../types/models';

    export function BelongsTo(
      model: typeof BaseModel,
      foreignKey: string,
      options: { localKey?: string } = {},
    ) {
      return function (target: any, propertyKey: string) {
        const config: RelationshipConfig = {
          type: 'belongsTo',
          model,
          foreignKey,
          localKey: options.localKey || 'id',
          lazy: true,
        };

        registerRelationship(target, propertyKey, config);
        createRelationshipProperty(target, propertyKey, config);
      };
    }

    export function HasMany(
      model: typeof BaseModel,
      foreignKey: string,
      options: { localKey?: string; through?: typeof BaseModel } = {},
    ) {
      return function (target: any, propertyKey: string) {
        const config: RelationshipConfig = {
          type: 'hasMany',
          model,
          foreignKey,
          localKey: options.localKey || 'id',
          through: options.through,
          lazy: true,
        };

        registerRelationship(target, propertyKey, config);
        createRelationshipProperty(target, propertyKey, config);
      };
    }

    export function HasOne(
      model: typeof BaseModel,
      foreignKey: string,
      options: { localKey?: string } = {},
    ) {
      return function (target: any, propertyKey: string) {
        const config: RelationshipConfig = {
          type: 'hasOne',
          model,
          foreignKey,
          localKey: options.localKey || 'id',
          lazy: true,
        };

        registerRelationship(target, propertyKey, config);
        createRelationshipProperty(target, propertyKey, config);
      };
    }

    export function ManyToMany(
      model: typeof BaseModel,
      through: typeof BaseModel,
      foreignKey: string,
      options: { localKey?: string; throughForeignKey?: string } = {},
    ) {
      return function (target: any, propertyKey: string) {
        const config: RelationshipConfig = {
          type: 'manyToMany',
          model,
          foreignKey,
          localKey: options.localKey || 'id',
          through,
          lazy: true,
        };

        registerRelationship(target, propertyKey, config);
        createRelationshipProperty(target, propertyKey, config);
      };
    }

    function registerRelationship(target: any, propertyKey: string, config: RelationshipConfig): void {
      // Initialize relationships map if it doesn't exist
      if (!target.constructor.relationships) {
        target.constructor.relationships = new Map();
      }

      // Store relationship configuration
      target.constructor.relationships.set(propertyKey, config);

      console.log(
        `Registered ${config.type} relationship: ${target.constructor.name}.${propertyKey} -> ${config.model.name}`,
      );
    }

    function createRelationshipProperty(
      target: any,
      propertyKey: string,
      config: RelationshipConfig,
    ): void {
      const _relationshipKey = `_relationship_${propertyKey}`; // For future use

      Object.defineProperty(target, propertyKey, {
        get() {
          // Check if relationship is already loaded
          if (this._loadedRelations && this._loadedRelations.has(propertyKey)) {
            return this._loadedRelations.get(propertyKey);
          }

          if (config.lazy) {
            // Return a promise for lazy loading
            return this.loadRelation(propertyKey);
          } else {
            throw new Error(
              `Relationship '${propertyKey}' not loaded. Use .load(['${propertyKey}']) first.`,
            );
          }
        },
        set(value) {
          // Allow manual setting of relationship values
          if (!this._loadedRelations) {
            this._loadedRelations = new Map();
          }
          this._loadedRelations.set(propertyKey, value);
        },
        enumerable: true,
        configurable: true,
      });
    }

    // Utility function to get relationship configuration
    export function getRelationshipConfig(
      target: any,
      propertyKey: string,
    ): RelationshipConfig | undefined {
      if (!target.constructor.relationships) {
        return undefined;
      }
      return target.constructor.relationships.get(propertyKey);
    }

    // Type definitions for decorators
    export type BelongsToDecorator = (
      model: typeof BaseModel,
      foreignKey: string,
      options?: { localKey?: string },
    ) => (target: any, propertyKey: string) => void;

    export type HasManyDecorator = (
      model: typeof BaseModel,
      foreignKey: string,
      options?: { localKey?: string; through?: typeof BaseModel },
    ) => (target: any, propertyKey: string) => void;

    export type HasOneDecorator = (
      model: typeof BaseModel,
      foreignKey: string,
      options?: { localKey?: string },
    ) => (target: any, propertyKey: string) => void;

    export type ManyToManyDecorator = (
      model: typeof BaseModel,
      through: typeof BaseModel,
      foreignKey: string,
      options?: { localKey?: string; throughForeignKey?: string },
    ) => (target: any, propertyKey: string) => void;