UNPKG

appwrite-utils-cli

Version:

Appwrite Utility Functions to help with database management, data conversion, data import, migrations, and much more. Meant to be used as a CLI tool, I do not recommend installing this in frontend environments.

121 lines (120 loc) 4.2 kB
import type { AppwriteConfig, CollectionCreate } from "appwrite-utils"; import type { UserMappingService } from "./UserMappingService.js"; export interface CollectionImportData { collection?: CollectionCreate; data: Array<{ rawData: any; finalData: any; context: any; importDef?: any; }>; } /** * Service responsible for resolving relationships and ID mappings during import. * Preserves all existing relationship resolution logic from DataLoader. * Extracted to provide focused, testable relationship management. */ export declare class RelationshipResolver { private config; private userMappingService; private oldIdToNewIdPerCollectionMap; constructor(config: AppwriteConfig, userMappingService: UserMappingService); /** * Helper method to generate a consistent key for collections. * Preserves existing logic from DataLoader. */ getCollectionKey(name: string): string; /** * Stores ID mapping for a collection. * * @param collectionName - The collection name * @param oldId - The old ID * @param newId - The new ID */ setIdMapping(collectionName: string, oldId: string, newId: string): void; /** * Gets the new ID for an old ID in a specific collection. * * @param collectionName - The collection name * @param oldId - The old ID to look up * @returns The new ID if found, otherwise undefined */ getNewIdForOldId(collectionName: string, oldId: string): string | undefined; /** * Checks if an ID mapping exists for a collection. * * @param collectionName - The collection name * @param oldId - The old ID to check * @returns True if mapping exists */ hasIdMapping(collectionName: string, oldId: string): boolean; /** * Gets the value to match for a given key in the final data or context. * Preserves existing logic from DataLoader. * * @param finalData - The final data object * @param context - The context object * @param key - The key to get the value for * @returns The value to match for from finalData or Context */ private getValueFromData; /** * Updates old references with new IDs based on ID mappings. * Preserves existing reference update logic from DataLoader. * * @param importMap - Map of collection data by collection key * @param collections - Array of collection configurations */ updateOldReferencesForNew(importMap: Map<string, CollectionImportData>, collections: CollectionCreate[]): void; /** * Resolves relationship references using the merged user map. * Handles cases where users have been merged during deduplication. * * @param oldId - The old user ID to resolve * @param targetCollection - The target collection name * @returns The resolved ID (new user ID if merged, otherwise mapped ID) */ private getMergedId; /** * Validates relationship integrity before import. * Checks that all referenced collections and fields exist. * * @param collections - Array of collections to validate * @returns Array of validation errors */ validateRelationships(collections: CollectionCreate[]): string[]; /** * Gets relationship statistics for reporting. * * @returns Statistics about relationships and ID mappings */ getStatistics(): { totalCollections: number; totalIdMappings: number; collectionsWithMappings: string[]; }; /** * Clears all ID mappings (useful for testing or resetting state). */ clearAllMappings(): void; /** * Exports ID mappings for debugging or external use. * * @returns Serializable object containing all ID mappings */ exportMappings(): { [collectionKey: string]: { [oldId: string]: string; }; }; /** * Imports ID mappings from an external source. * * @param mappings - The mappings to import */ importMappings(mappings: { [collectionKey: string]: { [oldId: string]: string; }; }): void; }