All files / core decorators.ts

90.9% Statements 130/143
82.27% Branches 65/79
94.73% Functions 36/38
90.9% Lines 130/143

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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561    17x                                               885x                   17x 314x   314x     314x 252x     314x 264x     314x                   17x 251x 251x     251x     251x           251x     251x 251x 251x 251x                     17x 476x 476x     476x     476x 476x 474x 474x                     17x 20x 20x     20x     20x             20x               20x               20x                 20x                 20x                   20x 20x 20x 20x                     17x 34x 34x     34x     34x             34x               34x               34x                 34x                 34x                   34x 34x 34x 34x                     17x 30x 30x     30x     30x             30x               30x               30x 1x               30x 1x               30x 1x                 30x 30x 30x 30x                     17x 49x 49x     49x     49x             49x               49x               49x 1x               49x 1x               49x 1x                 49x 49x 49x 49x             24x 24x 24x 24x           17x 6x 6x                     17x 4x 4x                     17x 1x 1x                     17x 5x 5x                     17x 1x 1x                     17x 1x 1x                     17x 4x 4x                     17x 1x 1x                     17x 1x 1x                     17x 1x 1x     1x     1x 1x 1x 1x               17x                            
// core/decorators.ts
// Base decorator types for database-agnostic schema definition
import "reflect-metadata";
 
import type {
  EntityOptions,
  PropertyOptions,
  RelationshipOptions,
  OneToOneOptions,
  OneToManyOptions,
  ManyToOneOptions,
  ManyToManyOptions,
  RelationshipTypeFunc,
  ValidationRule,
  MinMaxOptions,
  PatternOptions,
  EmailOptions,
  URLOptions,
  CustomValidationOptions,
  LengthOptions,
  IndexOptions,
  TimestampOptions,
} from "./types";
 
// Helper function to get constructor
function getConstructor(target: any): Function {
  return target.constructor || target;
}
 
/**
 * Entity decorator - marks a class as an entity to be persisted
 * 
 * This decorator no longer directly registers with a registry.
 * Instead, it stores metadata on the class that can be later
 * processed by SchemaBuilder.
 */
export function Entity(options: EntityOptions = {}) {
  return function <T extends { new (...args: any[]): any }>(target: T): T {
    // Store entity metadata on the class
    Reflect.defineMetadata("entity:options", options, target);
    
    // Initialize empty arrays for property and relationship tracking if they don't exist
    if (!Reflect.hasMetadata("entity:properties", target)) {
      Reflect.defineMetadata("entity:properties", [], target);
    }
    
    if (!Reflect.hasMetadata("entity:relationships", target)) {
      Reflect.defineMetadata("entity:relationships", [], target);
    }
    
    return target;
  };
}
 
/**
 * Id decorator - marks a property as a primary identifier
 * 
 * This decorator stores metadata on the property that can be later
 * processed by SchemaBuilder.
 */
export function Id(options: PropertyOptions = {}) {
  return function (target: any, propertyKey: string) {
    const constructor = getConstructor(target);
    
    // Mark as ID property
    Reflect.defineMetadata("property:id", true, target, propertyKey);
    
    // Also register as a property with enhanced metadata
    const enhancedOptions = {
      required: true,
      unique: true,
      ...options,
    };
    
    Reflect.defineMetadata("property:options", enhancedOptions, target, propertyKey);
    
    // Add to the list of properties on the class
    const properties: string[] = Reflect.getMetadata("entity:properties", constructor) || [];
    if (!properties.includes(propertyKey)) {
      properties.push(propertyKey);
      Reflect.defineMetadata("entity:properties", properties, constructor);
    }
  };
}
 
/**
 * Property decorator - marks a property to be persisted
 * 
 * This decorator stores metadata on the property that can be later
 * processed by SchemaBuilder.
 */
export function Property(options: PropertyOptions = {}) {
  return function (target: any, propertyKey: string) {
    const constructor = getConstructor(target);
    
    // Store property metadata
    Reflect.defineMetadata("property:options", options, target, propertyKey);
    
    // Add to the list of properties on the class
    const properties: string[] = Reflect.getMetadata("entity:properties", constructor) || [];
    if (!properties.includes(propertyKey)) {
      properties.push(propertyKey);
      Reflect.defineMetadata("entity:properties", properties, constructor);
    }
  };
}
 
/**
 * OneToOne decorator - defines a one-to-one relationship
 * 
 * This decorator stores metadata on the property that can be later
 * processed by SchemaBuilder.
 */
export function OneToOne<T>(options: OneToOneOptions<T>) {
  return function (target: any, propertyKey: string) {
    const constructor = getConstructor(target);
 
    // Get name from options or use property key
    const name = options.name || propertyKey.toUpperCase();
 
    // Store relationship options metadata
    const relationshipOptions = {
      name,
      target: options.target(),
      inverse: options.inverse,
      required: options.required,
    };
    
    Reflect.defineMetadata(
      "relationship:options", 
      relationshipOptions,
      target,
      propertyKey
    );
 
    // Store the metadata about this being a OneToOne relationship
    Reflect.defineMetadata(
      "relationship:type",
      "one-to-one",
      target,
      propertyKey
    );
 
    // Store database-specific options
    Iif (options.neo4j) {
      Reflect.defineMetadata(
        "neo4j:relationship",
        options.neo4j,
        target,
        propertyKey
      );
    }
 
    Iif (options.mongodb) {
      Reflect.defineMetadata(
        "mongodb:relationship",
        options.mongodb,
        target,
        propertyKey
      );
    }
 
    Iif (options.postgresql) {
      Reflect.defineMetadata(
        "postgresql:relationship",
        options.postgresql,
        target,
        propertyKey
      );
    }
    
    // Add to the list of relationships on the class
    const relationships: string[] = Reflect.getMetadata("entity:relationships", constructor) || [];
    if (!relationships.includes(propertyKey)) {
      relationships.push(propertyKey);
      Reflect.defineMetadata("entity:relationships", relationships, constructor);
    }
  };
}
 
/**
 * OneToMany decorator - defines a one-to-many relationship
 * 
 * This decorator stores metadata on the property that can be later
 * processed by SchemaBuilder.
 */
export function OneToMany<T>(options: OneToManyOptions<T>) {
  return function (target: any, propertyKey: string) {
    const constructor = getConstructor(target);
 
    // Get name from options or use property key
    const name = options.name || propertyKey.toUpperCase();
 
    // Store relationship options metadata
    const relationshipOptions = {
      name,
      target: options.target(),
      inverse: options.inverse,
      required: options.required,
    };
    
    Reflect.defineMetadata(
      "relationship:options", 
      relationshipOptions,
      target,
      propertyKey
    );
 
    // Store the metadata about this being a OneToMany relationship
    Reflect.defineMetadata(
      "relationship:type",
      "one-to-many",
      target,
      propertyKey
    );
 
    // Store database-specific options
    Iif (options.neo4j) {
      Reflect.defineMetadata(
        "neo4j:relationship",
        options.neo4j,
        target,
        propertyKey
      );
    }
 
    Iif (options.mongodb) {
      Reflect.defineMetadata(
        "mongodb:relationship",
        options.mongodb,
        target,
        propertyKey
      );
    }
 
    Iif (options.postgresql) {
      Reflect.defineMetadata(
        "postgresql:relationship",
        options.postgresql,
        target,
        propertyKey
      );
    }
    
    // Add to the list of relationships on the class
    const relationships: string[] = Reflect.getMetadata("entity:relationships", constructor) || [];
    if (!relationships.includes(propertyKey)) {
      relationships.push(propertyKey);
      Reflect.defineMetadata("entity:relationships", relationships, constructor);
    }
  };
}
 
/**
 * ManyToOne decorator - defines a many-to-one relationship
 * 
 * This decorator stores metadata on the property that can be later
 * processed by SchemaBuilder.
 */
export function ManyToOne<T>(options: ManyToOneOptions<T>) {
  return function (target: any, propertyKey: string) {
    const constructor = getConstructor(target);
 
    // Get name from options or use property key
    const name = options.name || propertyKey.toUpperCase();
 
    // Store relationship options metadata
    const relationshipOptions = {
      name,
      target: options.target(),
      inverse: options.inverse,
      required: options.required,
    };
    
    Reflect.defineMetadata(
      "relationship:options", 
      relationshipOptions,
      target,
      propertyKey
    );
 
    // Store the metadata about this being a ManyToOne relationship
    Reflect.defineMetadata(
      "relationship:type",
      "many-to-one",
      target,
      propertyKey
    );
 
    // Store database-specific options
    if (options.neo4j) {
      Reflect.defineMetadata(
        "neo4j:relationship",
        options.neo4j,
        target,
        propertyKey
      );
    }
 
    if (options.mongodb) {
      Reflect.defineMetadata(
        "mongodb:relationship",
        options.mongodb,
        target,
        propertyKey
      );
    }
 
    if (options.postgresql) {
      Reflect.defineMetadata(
        "postgresql:relationship",
        options.postgresql,
        target,
        propertyKey
      );
    }
    
    // Add to the list of relationships on the class
    const relationships: string[] = Reflect.getMetadata("entity:relationships", constructor) || [];
    if (!relationships.includes(propertyKey)) {
      relationships.push(propertyKey);
      Reflect.defineMetadata("entity:relationships", relationships, constructor);
    }
  };
}
 
/**
 * ManyToMany decorator - defines a many-to-many relationship
 * 
 * This decorator stores metadata on the property that can be later
 * processed by SchemaBuilder.
 */
export function ManyToMany<T>(options: ManyToManyOptions<T>) {
  return function (target: any, propertyKey: string) {
    const constructor = getConstructor(target);
 
    // Get name from options or use property key
    const name = options.name || propertyKey.toUpperCase();
 
    // Store relationship options metadata
    const relationshipOptions = {
      name,
      target: options.target(),
      inverse: options.inverse,
      required: options.required,
    };
    
    Reflect.defineMetadata(
      "relationship:options", 
      relationshipOptions,
      target,
      propertyKey
    );
 
    // Store the metadata about this being a ManyToMany relationship
    Reflect.defineMetadata(
      "relationship:type",
      "many-to-many",
      target,
      propertyKey
    );
 
    // Store database-specific options
    if (options.neo4j) {
      Reflect.defineMetadata(
        "neo4j:relationship",
        options.neo4j,
        target,
        propertyKey
      );
    }
 
    if (options.mongodb) {
      Reflect.defineMetadata(
        "mongodb:relationship",
        options.mongodb,
        target,
        propertyKey
      );
    }
 
    if (options.postgresql) {
      Reflect.defineMetadata(
        "postgresql:relationship",
        options.postgresql,
        target,
        propertyKey
      );
    }
    
    // Add to the list of relationships on the class
    const relationships: string[] = Reflect.getMetadata("entity:relationships", constructor) || [];
    if (!relationships.includes(propertyKey)) {
      relationships.push(propertyKey);
      Reflect.defineMetadata("entity:relationships", relationships, constructor);
    }
  };
}
 
// Helper function to add validation rules
function addValidationRule(target: any, propertyKey: string, rule: ValidationRule) {
  const constructor = getConstructor(target);
  const existingRules: ValidationRule[] = Reflect.getMetadata("validation:rules", target, propertyKey) || [];
  existingRules.push(rule);
  Reflect.defineMetadata("validation:rules", existingRules, target, propertyKey);
}
 
/**
 * Min decorator - validates that a numeric value is greater than or equal to specified minimum
 */
export function Min(options: MinMaxOptions) {
  return function (target: any, propertyKey: string) {
    addValidationRule(target, propertyKey, {
      type: "min",
      options,
      message: options.message || `Value must be greater than or equal to ${options.value}`,
    });
  };
}
 
/**
 * Max decorator - validates that a numeric value is less than or equal to specified maximum
 */
export function Max(options: MinMaxOptions) {
  return function (target: any, propertyKey: string) {
    addValidationRule(target, propertyKey, {
      type: "max",
      options,
      message: options.message || `Value must be less than or equal to ${options.value}`,
    });
  };
}
 
/**
 * Pattern decorator - validates that a string matches a regular expression pattern
 */
export function Pattern(options: PatternOptions) {
  return function (target: any, propertyKey: string) {
    addValidationRule(target, propertyKey, {
      type: "pattern",
      options,
      message: options.message || `Value must match pattern ${options.pattern.toString()}`,
    });
  };
}
 
/**
 * Email decorator - validates that a string is a valid email address
 */
export function Email(options: EmailOptions = {}) {
  return function (target: any, propertyKey: string) {
    addValidationRule(target, propertyKey, {
      type: "email",
      options,
      message: options.message || "Value must be a valid email address",
    });
  };
}
 
/**
 * URL decorator - validates that a string is a valid URL
 */
export function URL(options: URLOptions = {}) {
  return function (target: any, propertyKey: string) {
    addValidationRule(target, propertyKey, {
      type: "url",
      options,
      message: options.message || "Value must be a valid URL",
    });
  };
}
 
/**
 * Custom decorator - validates using a custom validation function
 */
export function Custom(options: CustomValidationOptions) {
  return function (target: any, propertyKey: string) {
    addValidationRule(target, propertyKey, {
      type: "custom",
      options,
      message: options.message || "Value failed custom validation",
    });
  };
}
 
/**
 * MinLength decorator - validates that a string or array has minimum length
 */
export function MinLength(length: number, message?: string) {
  return function (target: any, propertyKey: string) {
    addValidationRule(target, propertyKey, {
      type: "minLength",
      options: { value: length },
      message: message || `Value must have at least ${length} characters`,
    });
  };
}
 
/**
 * MaxLength decorator - validates that a string or array has maximum length
 */
export function MaxLength(length: number, message?: string) {
  return function (target: any, propertyKey: string) {
    addValidationRule(target, propertyKey, {
      type: "maxLength",
      options: { value: length },
      message: message || `Value must have at most ${length} characters`,
    });
  };
}
 
/**
 * Length decorator - validates that a string or array has length within specified range
 */
export function Length(options: LengthOptions) {
  return function (target: any, propertyKey: string) {
    addValidationRule(target, propertyKey, {
      type: "length",
      options,
      message: options.message || `Value length must be between ${options.min || 0} and ${options.max || 'unlimited'}`,
    });
  };
}
 
/**
 * Index decorator - marks a property to be indexed in the database
 */
export function Index(options: IndexOptions = {}) {
  return function (target: any, propertyKey: string) {
    const constructor = getConstructor(target);
    
    // Store index metadata
    Reflect.defineMetadata("index:options", options, target, propertyKey);
    
    // Add to the list of indexed properties on the class
    const indexes: string[] = Reflect.getMetadata("entity:indexes", constructor) || [];
    if (!indexes.includes(propertyKey)) {
      indexes.push(propertyKey);
      Reflect.defineMetadata("entity:indexes", indexes, constructor);
    }
  };
}
 
/**
 * Timestamp decorator - automatically manages timestamp fields
 */
export function Timestamp(options: TimestampOptions = {}) {
  return function (target: any, propertyKey: string) {
    const constructor = getConstructor(target);
    
    // Store timestamp metadata
    Reflect.defineMetadata("timestamp:options", options, target, propertyKey);
    
    // Add to the list of timestamp properties on the class
    const timestamps: string[] = Reflect.getMetadata("entity:timestamps", constructor) || [];
    Iif (!timestamps.includes(propertyKey)) {
      timestamps.push(propertyKey);
      Reflect.defineMetadata("entity:timestamps", timestamps, constructor);
    }
  };
}