@flatfile/implementation-utils-dedupe-worker
v0.0.7
Published
Provides functionality for easily implementing record deduplication logic.
Readme
@flatfile/implementation-utils-dedupe-worker
Provides a job worker for deduping records in a sheet as well as utility functions for deduping records.
Features
deduplicateRecords: A function for deduping records in a sheet based on specified dedupe keys.getDedupeActionInputForm: A function for getting the input form for the dedupe action.DedupeJobWorker: An abstract job worker for deduping records in a sheet, compatible with both@flatfile/safe-api's FlatfileRecord and the legacyItemclass from the embedded "safe" client.
Usage
To use the DedupeJobWorker, you need to create a concrete class that extends it and implements the fetchRecords and writeRecords methods. This allows you to define how records are retrieved from and written back to your sheet.
Example Implementation
Here's a complete example of how to set up a deduplication action on a sheet.
Create your Dedupe Worker
Create a file, for example
my-dedupe.worker.ts, and define your worker by extendingDedupeJobWorker. This implementation uses@flatfile/safe-apifor easier record handling.import type { Collection } from "collect.js"; import { DedupeJobWorker } from "@flatfile/dedupe-worker"; import api from "@flatfile/safe-api"; export class MyDedupeWorker extends DedupeJobWorker { /** * Fetches all records from the sheet using a stream. */ async fetchRecords() { return await api.records.stream({ sheetId: this.sheetId }); } /** * Writes the updated, deleted, and new records back to the sheet. */ async writeRecords(records: Collection) { return await api.records.write({ sheetId: this.sheetId }, records); } }Register the Action on a Sheet
When defining your sheet, add an action with the input form from
getDedupeActionInputForm.import { getDedupeActionInputForm } from "@flatfile/dedupe-worker"; const mySheetFields = [ { key: "firstName", type: "string", label: "First Name" }, { key: "lastName", type: "string", label: "Last Name" }, { key: "email", type: "string", label: "Email" }, ]; export const MY_SHEET = { name: "My Sheet", slug: "my-sheet", fields: mySheetFields, actions: { dedupe: { operation: ACTION_DEDUPE, label: "Dedupe", description: "Dedupe records in this sheet based on the selected field.", mode: "foreground", primary: false, inputForm: getDedupeActionInputForm(mySheetFields), }, }, };Register the Listener
In your main listener file (e.g.,
index.ts), import your worker and use theworker()utility from@flatfile/implementation-utils-job-workerto simplify listener setup.import { FlatfileListener } from "@flatfile/listener"; import { worker } from "@flatfile/implementation-utils-job-worker"; import { getDedupeActionInputForm } from "@flatfile/dedupe-worker"; import { MyDedupeWorker } from "./my-dedupe.worker"; export default function (listener: FlatfileListener) { listener.use(worker(MyDedupeWorker)); }
