/** * Backend Example Domain Service * * Demonstrates extending BaseBackendDomainService which provides: * - Automatic validation → mapper → repository/API → mapper flow * - Event emission throughout CRUD lifecycle * - Dual mode: Repository (DbService/dummy) OR HTTP API * - Configurable error throwing behavior * * This service only needs to: * - Initialize repository in constructor * - Set eventPrefix for event emission * - Implement isAvailable() and dispose() * - Add any custom domain methods * * All standard CRUD operations are inherited with cache support: * - create(data, options?) - Create with optional caching * - patch(id, data, options?) - Update with automatic cache invalidation * - delete(id, deleteOptions?, crudOptions?) - Delete with automatic cache invalidation * - getById(id, options?) - Read with cache-aside pattern * * Runtime: Backend (NestJS, Express, Node.js) * - Uses repository (DbService or dummy data fallback) * - Auto-initialized by Core.initialize() as part of DomainServices * - For HTTP API calls, use fetchers from @plyaz/api directly * * @example * ```tsx * // Create service * const service = await BackendExampleDomainService.create({ * throwOnValidationError: true, // Throw on validation errors (default) * throwOnRepositoryError: true, // Throw on repository errors (default) * }); * * // Use inherited CRUD methods * const entity = await service.create({ name: 'Test', amount: 100 }); * const updated = await service.patch(entity.id, { amount: 200 }); * await service.delete(entity.id, { soft: true }); * ``` */ import { BaseBackendDomainService } from '../base'; import type { ExampleEntity, ExampleResponseDTO, CreateExampleDTO, PatchExampleDTO, QueryExampleDTO, DeleteExampleDTO, ExampleStoreItem, ExampleDomainServiceConfig as _ExampleDomainServiceConfig } from '@plyaz/types/examples'; import type { CoreServiceCreateOptions, CoreInjectedServices } from '@plyaz/types/core'; import { ExampleRepository, type ExampleDatabaseRow } from '../../models/example'; import { ExampleMapperClass } from './mappers/ExampleMapper'; import { ExampleValidatorClass } from './validators/ExampleValidator'; /** * Mapper type for this service */ type ExampleMapper = InstanceType; /** * Validator type for this service */ type ExampleValidator = InstanceType; /** * Example event types (for subscription) */ export type ExampleEventType = 'example:creating' | 'example:created' | 'example:updating' | 'example:updated' | 'example:deleting' | 'example:deleted' | 'example:complete' | 'example:error' | 'example:validation:started' | 'example:validation:success' | 'example:validation:failed' | 'example:sanitization:started' | 'example:sanitization:complete' | 'example:document:generating' | 'example:document:generated' | 'example:document:error' | 'example:upload:uploading' | 'example:upload:uploaded' | 'example:upload:error' | 'example:email:sending' | 'example:email:sent' | 'example:email:error'; /** * Backend Example Domain Service * * Extends BaseBackendDomainService with all generic types: * - TConfig: ExampleDomainServiceConfig * - TEntity: ExampleEntity * - TResponseDTO: ExampleResponseDTO * - TCreateDTO: CreateExampleDTO * - TUpdateDTO: CreateExampleDTO (same as create) * - TPatchDTO: PatchExampleDTO * - TQueryDTO: QueryExampleDTO * - TDeleteOptions: DeleteExampleDTO * - TRepository: ExampleRepository * - TDatabaseRow: ExampleDatabaseRow * - TStoreState: ExampleStoreItem * - TMapper: ExampleMapper * - TValidator: ExampleValidator * * All CRUD methods are inherited from base class - no need to implement! */ export declare class BackendExampleDomainService extends BaseBackendDomainService<_ExampleDomainServiceConfig, ExampleEntity, ExampleResponseDTO, CreateExampleDTO, CreateExampleDTO, // Update same as create PatchExampleDTO, QueryExampleDTO, // Query/filter type for getAll() DeleteExampleDTO, ExampleDatabaseRow, ExampleRepository, ExampleStoreItem, ExampleMapper, ExampleValidator> { /** * Repository instance for data access * Required by BaseBackendDomainService */ protected repository: ExampleRepository; /** * Event prefix for all events emitted by this service * Required by BaseBackendDomainService */ protected eventPrefix: string; /** * Cache prefix for namespacing cache keys * Optional: defaults to serviceName.toLowerCase() if not set */ protected cachePrefix: string; /** * Unique key for this service (used by ServiceRegistry) */ static readonly serviceKey: "example"; /** * Factory method for ServiceRegistry auto-initialization. * Creates and initializes the service instance. * * @param config - Service configuration * @param options - Global options from the registry (includes cache, db, api instances) * @returns Promise resolving to the initialized service instance */ static create(config: _ExampleDomainServiceConfig, options?: CoreServiceCreateOptions): Promise; constructor(config?: _ExampleDomainServiceConfig, injected?: CoreInjectedServices); /** * Check if service is available * Service is available if enabled (repository is always available) */ isAvailable(): boolean; /** * Dispose/cleanup the service */ dispose(): void; /** * Configure the service (mutates config) * Use this to update configuration at runtime */ configure(updates: Partial<_ExampleDomainServiceConfig>): void; /** * Subscribe to service events * Uses CoreEventManager under the hood * * @param event - Event type to listen for * @param handler - Event handler function * @returns Unsubscribe function */ on(event: ExampleEventType, handler: (data: unknown) => void): () => void; /** * Demo: Single validation error * Validates data with ONE field invalid to trigger single validation error in array * * @throws ValidationError[] (array with 1 error) */ demoSingleValidationError(): void; /** * Demo: Multiple validation errors * Validates data with MULTIPLE fields invalid to trigger array of validation errors * * @throws ValidationError[] (array with multiple errors) */ demoMultipleValidationErrors(): void; /** * Demo: Get all examples with optional filters * Demonstrates the getAll() method with typed query parameters * * @param filters - Optional query filters (status, pagination, sorting) * @returns Promise resolving to array of ExampleEntity * * @example * ```typescript * // Get all active examples * const activeExamples = await service.getExamples({ status: 'active' }); * * // Get archived examples with pagination * const archivedExamples = await service.getExamples({ * status: 'archived', * page: 2, * limit: 10 * }); * * // Get all examples sorted by name * const sortedExamples = await service.getExamples({ * sort_by: 'name', * sort_order: 'asc' * }); * ``` */ getExamples(filters?: QueryExampleDTO): Promise; /** * Demo: Get active examples only * Convenience method demonstrating filtering */ getActiveExamples(): Promise; /** * Demo: Get draft examples only * Convenience method demonstrating filtering */ getDraftExamples(): Promise; /** * Demo: Get archived examples only * Convenience method demonstrating filtering */ getArchivedExamples(): Promise; /** * Demo: Check if an example exists * More efficient than fetching the full entity when you only need existence * * @param id - Example ID to check * @returns Promise resolving to boolean * * @example * ```typescript * const exists = await service.checkExampleExists('123'); * if (exists) { * console.log('Example exists!'); * } * ``` */ checkExampleExists(id: string): Promise; /** * Demo: Create multiple examples at once * More efficient than calling create() multiple times * * @param dataArray - Array of example data to create * @returns Promise resolving to array of created entities * * @example * ```typescript * const examples = await service.createMultipleExamples([ * { name: 'Example 1', amount: 100 }, * { name: 'Example 2', amount: 200 }, * { name: 'Example 3', amount: 300 }, * ]); * console.log(`Created ${examples.length} examples`); * ``` */ createMultipleExamples(dataArray: CreateExampleDTO[]): Promise; /** * Demo: Delete multiple examples at once * More efficient than calling delete() multiple times * * @param ids - Array of example IDs to delete * @param soft - Whether to soft delete (default: true) * @returns Promise resolving when all deletions complete * * @example * ```typescript * // Soft delete multiple examples * await service.deleteMultipleExamples(['id1', 'id2', 'id3'], true); * * // Hard delete multiple examples * await service.deleteMultipleExamples(['id1', 'id2', 'id3'], false); * ``` */ deleteMultipleExamples(ids: string[], soft?: boolean): Promise; } /** * Get the singleton instance of BackendExampleDomainService * Instance is only created when first called, not at module load time. * * @example * ```tsx * import { getBackendExampleDomainService } from '@plyaz/core'; * * const service = await getBackendExampleDomainService(); * const entity = await service.create({ name: 'Test', amount: 100 }); * ``` */ export declare function getBackendExampleDomainService(): Promise; /** * Reset singleton instance (for testing) */ export declare function resetExampleDomainService(): void; export {}; //# sourceMappingURL=BackendExampleDomainService.d.ts.map