UNPKG

@energica-city/shared-amplify-utils

Version:

Shared utilities for AWS Amplify projects

164 lines 6.32 kB
import type { QueryFactoryResult, AmplifyModelType, CacheConfig, AmplifyOutputs } from './types'; /** * Singleton manager for AWS Amplify client instances and query factories. * * Handles complete lifecycle management including: * - Amplify client initialization * - Query factory creation and storage * - Global system configuration * - Multi-client isolation via unique keys */ export declare class ClientManager { private readonly clientKey; private static instances; private client; private initPromise; private queryFactories; private schema; private constructor(); /** * Gets or creates a ClientManager instance for the specified key with optional schema. * * @template TSchema - Schema type containing model definitions * @param clientKey - Unique identifier for client isolation (default: "default") * @param schema - Optional schema to use for typing the client * @returns ClientManager instance for the specified key */ static getInstance<TSchema extends { models: Record<string, unknown>; } = { models: Record<string, unknown>; }>(clientKey?: string, schema?: TSchema): ClientManager; /** * Main initialization method for the entire system. * * Configures Amplify globally and initializes query factories for specified entities. * This is the primary entry point for system setup. * * @template TSchema - Schema type containing model definitions * @template TTypes - Record of all available Amplify model types * @template TSelected - Selected entity names to initialize * @param config - Configuration object for initialization * @returns Promise resolving to query factories for all specified entities */ static initializeQueries<TSchema extends { models: Record<string, unknown>; }, TTypes extends Record<string, AmplifyModelType>, TSelected extends keyof TTypes & string = keyof TTypes & string>(config: { amplifyOutputs: AmplifyOutputs; schema: TSchema; entities?: readonly TSelected[] | undefined; clientKey?: string | undefined; cache?: CacheConfig | undefined; }): Promise<{ [K in TSelected]: QueryFactoryResult<K, TTypes>; }>; /** * Gets global identifier fields for an entity from the schema. * * @param entityName - Name of the entity to get identifier fields for * @returns Array of identifier field names */ static getIdentifierFields(entityName: string): string[]; /** * Gets the current global Amplify configuration. * * @returns Global Amplify outputs or null if not configured */ static getGlobalAmplifyOutputs(): AmplifyOutputs | null; /** * Resets all client instances and global state. * Primarily used for testing scenarios. */ static resetAll(): void; /** * Sets the schema for this client manager instance. * * @template TSchema - Schema type containing model definitions * @param schema - Schema to use for typing */ setSchema<TSchema extends { models: Record<string, unknown>; }>(schema: TSchema): void; /** * Gets the stored schema for this instance. * * @returns The stored schema or null if not set */ getSchema(): { models: Record<string, unknown>; } | null; /** * Gets the initialized Amplify client with proper typing based on the schema. * * @template TTypes - Record of all available Amplify model types (inferred from schema) * @returns Promise resolving to the initialized client */ getClient<TTypes extends Record<string, unknown> = Record<string, unknown>>(): Promise<TTypes>; /** * Checks if the client has been initialized. * * @returns True if client is initialized, false otherwise */ isInitialized(): boolean; /** * Resets this client instance, clearing all state. */ reset(): void; /** * Stores a query factory for reuse. * * @template T - Entity name as string literal * @template TTypes - Record of all available Amplify model types * @param entityName - Name of the entity * @param factory - Query factory instance to store */ private setQueryFactory; /** * Retrieves a stored query factory. * * @template T - Entity name as string literal * @template TTypes - Record of all available Amplify model types * @param entityName - Name of the entity * @returns Query factory instance or null if not found */ private getQueryFactory; /** * Gets query factories with automatic initialization of missing entities. * * This is the main method for retrieving query factories. If entities haven't * been initialized yet, they will be created automatically. * * @template TTypes - Record of all available Amplify model types * @template TSelected - Selected entity names to retrieve * @param config - Configuration for query factory retrieval * @returns Promise resolving to query factories for all specified entities */ getQueryFactories<TTypes extends Record<string, AmplifyModelType>, TSelected extends keyof TTypes & string>(config: { entities: readonly TSelected[]; cache?: CacheConfig; }): Promise<{ [K in TSelected]: QueryFactoryResult<K, TTypes>; }>; /** * Ensures query factories exist, creating them if needed. * * This is a centralized method that eliminates code duplication between * different initialization functions. * * @template TTypes - Record of all available Amplify model types * @template TSelected - Selected entity names to ensure * @param config - Configuration for factory creation * @returns Promise resolving to factory creation results */ private ensureQueryFactories; /** * Initializes the Amplify client instance. * * @private * @template T - Type of the client (should extend Record<string, unknown>) * @returns Promise that resolves when client is initialized * @throws Error if client generation fails */ private initializeClient; } //# sourceMappingURL=ClientManager.d.ts.map