relay-repository.ts raw

   1  import { IdentityId, RelayId } from '../value-objects';
   2  
   3  /**
   4   * Snapshot of a relay for persistence.
   5   */
   6  export interface RelaySnapshot {
   7    id: string;
   8    identityId: string;
   9    url: string;
  10    read: boolean;
  11    write: boolean;
  12  }
  13  
  14  /**
  15   * Query criteria for finding relays.
  16   */
  17  export interface RelayQuery {
  18    identityId?: IdentityId;
  19    url?: string;
  20    read?: boolean;
  21    write?: boolean;
  22  }
  23  
  24  /**
  25   * Repository interface for Relay aggregate.
  26   */
  27  export interface RelayRepository {
  28    /**
  29     * Find a relay by its ID.
  30     */
  31    findById(id: RelayId): Promise<RelaySnapshot | undefined>;
  32  
  33    /**
  34     * Find relays matching the query criteria.
  35     */
  36    find(query: RelayQuery): Promise<RelaySnapshot[]>;
  37  
  38    /**
  39     * Find a relay by URL for a specific identity.
  40     * Used for duplicate detection.
  41     */
  42    findByUrl(identityId: IdentityId, url: string): Promise<RelaySnapshot | undefined>;
  43  
  44    /**
  45     * Get all relays for an identity.
  46     */
  47    findByIdentity(identityId: IdentityId): Promise<RelaySnapshot[]>;
  48  
  49    /**
  50     * Get all relays.
  51     */
  52    findAll(): Promise<RelaySnapshot[]>;
  53  
  54    /**
  55     * Save a new or updated relay.
  56     */
  57    save(relay: RelaySnapshot): Promise<void>;
  58  
  59    /**
  60     * Delete a relay by its ID.
  61     */
  62    delete(id: RelayId): Promise<boolean>;
  63  
  64    /**
  65     * Delete all relays for an identity.
  66     * Used when deleting an identity (cascade delete).
  67     */
  68    deleteByIdentity(identityId: IdentityId): Promise<number>;
  69  
  70    /**
  71     * Count relays matching the query.
  72     */
  73    count(query?: RelayQuery): Promise<number>;
  74  }
  75  
  76  /**
  77   * Error thrown when a relay operation fails.
  78   */
  79  export class RelayRepositoryError extends Error {
  80    constructor(
  81      message: string,
  82      public readonly code: RelayErrorCode
  83    ) {
  84      super(message);
  85      this.name = 'RelayRepositoryError';
  86    }
  87  }
  88  
  89  export enum RelayErrorCode {
  90    DUPLICATE_URL = 'DUPLICATE_URL',
  91    NOT_FOUND = 'NOT_FOUND',
  92    ENCRYPTION_FAILED = 'ENCRYPTION_FAILED',
  93    STORAGE_FAILED = 'STORAGE_FAILED',
  94  }
  95