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

0% Statements 0/20
0% Branches 0/17
0% Functions 0/3
0% Lines 0/20

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

     import { BaseModel } from '../BaseModel';
    import { ModelConfig } from '../../types/models';
    import { StoreType } from '../../types/framework';
    import { ModelRegistry } from '../../core/ModelRegistry';

    export function Model(config: ModelConfig = {}) {
      return function <T extends typeof BaseModel>(target: T): T {
        // Set model configuration on the class
        target.modelName = config.tableName || target.name;
        target.dbType = config.type || autoDetectType(target);
        target.scope = config.scope || 'global';
        target.sharding = config.sharding;
        target.pinning = config.pinning;

        // Register with framework
        ModelRegistry.register(target.name, target, config);

        // TODO: Set up automatic database creation when DatabaseManager is ready
        // DatabaseManager.scheduleCreation(target);

        return target;
      };
    }

    function autoDetectType(modelClass: typeof BaseModel): StoreType {
      // Analyze model fields to suggest optimal database type
      const fields = modelClass.fields;

      if (!fields || fields.size === 0) {
        return 'docstore'; // Default for complex objects
      }

      let hasComplexFields = false;
      let _hasSimpleFields = false;

      for (const [_fieldName, fieldConfig] of fields) {
        if (fieldConfig.type === 'object' || fieldConfig.type === 'array') {
          hasComplexFields = true;
        } else {
          _hasSimpleFields = true;
        }
      }

      // If we have complex fields, use docstore
      if (hasComplexFields) {
        return 'docstore';
      }

      // If we only have simple fields, we could use keyvalue
      // But docstore is more flexible, so let's default to that
      return 'docstore';
    }

    // Export the decorator type for TypeScript
    export type ModelDecorator = (config?: ModelConfig) => <T extends typeof BaseModel>(target: T) => T;