identity-id.ts raw

   1  import { v4 as uuidv4 } from 'uuid';
   2  import { EntityId } from './entity-id';
   3  
   4  /**
   5   * Strongly-typed identifier for Identity entities.
   6   * Prevents accidental mixing with other ID types.
   7   */
   8  export class IdentityId extends EntityId<'IdentityId'> {
   9    private readonly _brand = 'IdentityId' as const;
  10  
  11    private constructor(value: string) {
  12      super(value);
  13    }
  14  
  15    /**
  16     * Generate a new unique IdentityId.
  17     */
  18    static generate(): IdentityId {
  19      return new IdentityId(uuidv4());
  20    }
  21  
  22    /**
  23     * Create an IdentityId from an existing string value.
  24     * Use this when reconstituting from storage.
  25     */
  26    static from(value: string): IdentityId {
  27      return new IdentityId(value);
  28    }
  29  
  30    /**
  31     * Type guard to check if two IDs are equal.
  32     */
  33    override equals(other: IdentityId): boolean {
  34      return other instanceof IdentityId && this._value === other._value;
  35    }
  36  }
  37