permission-id.ts raw

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