@flatfile/implementation-utils-dedupe-worker
Version:
Provides functionality for easily implementing record deduplication logic.
64 lines (62 loc) • 2.26 kB
text/typescript
import { Collection } from "collect.js";
import { Flatfile } from "@flatfile/api";
import { SheetJobWorker } from "@flatfile/implementation-utils-job-worker";
//#region src/types.d.ts
/**
* Result of deduplication operation.
*/
interface DedupeResult<T> {
pristineRecords: T[];
conflictingRecords: T[];
duplicateRecords: T[];
mergedRecords: T[];
}
/**
* Interface for records that can be deduplicated, compatible with Item and `@flatfile/safe-api`'s FlatfileRecord.
*/
interface DedupableRecord {
id: any;
hasConflict(other: DedupableRecord): boolean;
merge(other: DedupableRecord): void;
delete(): void;
hash(...keys: string[]): string;
}
/**
* Collection with changes interface.
*/
interface CollectionWithChanges<T> extends Collection<T> {
changes(): Collection<T>;
}
/**
* Input field key for deduplication.
*/
//#endregion
//#region src/dedupe.d.ts
/**
* Deduplicates a collection of records based on specified dedupe keys.
*
* @param records - Collection of records to deduplicate.
* @param dedupeKeys - Array of field keys to use for deduplication.
* @param isMergeable - Optional function to determine if a source record can be merged into a target record.
* @returns Result object containing categorized records.
*/
declare const deduplicateRecords: <T extends DedupableRecord>(records: Collection<T>, dedupeKeys: string[], isMergeable?: (source: T, target: T) => boolean) => DedupeResult<T>;
//#endregion
//#region src/dedupe.worker.d.ts
/**
* This abstract job is responsible for deduplicating records in a sheet that have the exact same values
* based on the dedupeOn field.
*
* It must be extended to provide record fetching and write functionality.
*/
declare abstract class DedupeJobWorker<T extends DedupableRecord> extends SheetJobWorker {
abstract fetchRecords(): Promise<CollectionWithChanges<T>>;
abstract writeRecords(records: CollectionWithChanges<T>): Promise<void>;
execute(): Promise<Flatfile.JobOutcome | undefined>;
}
/**
* Returns the dedupe action input form for a sheet with an option to select which field to dedupe on.
*/
declare const getDedupeActionInputForm: (fields: Flatfile.Property[]) => Flatfile.InputForm;
//#endregion
export { DedupeJobWorker, deduplicateRecords, getDedupeActionInputForm };