z-deduper
Version:
This library will allow you to build a Custom Zapier deduper for your advanced polling triggers use cases in your Zapier app.
119 lines • 3.52 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDeduper = exports.Deduper = void 0;
const lodash_1 = __importDefault(require("lodash"));
const object_hash_1 = __importDefault(require("object-hash"));
const storage_1 = require("./storage");
class Deduper {
constructor(storage) {
this.cache = {};
this.storage = storage;
}
/**
* Initialize the deduper
*
* Note: Should only be called once when the zap is activated
*/
async initialize(currentRecords) {
const records = this.getRecords(currentRecords);
await this.storage.save(records);
return true;
}
async load() {
this.cache = await this.storage.load();
return true;
}
/**
* Find changes
*
* Compares the current records with the cached record hashes to find
* which records are new and which are updated.
*/
findChanges(currentRecords) {
const changes = {
created: [],
updated: [],
all: [],
};
const timestamp = this.getTimestamp();
for (const record of currentRecords) {
const recordHash = this.hash(record);
const cachedRecordHash = this.cache[record.id];
if (cachedRecordHash) {
if (recordHash !== cachedRecordHash) {
// This is an updated record
changes.updated.push({
...record,
id: `${record.id}.${timestamp}`,
_id: record.id,
_hash: recordHash,
});
}
}
else {
// This is a new record
changes.created.push({
...record,
id: `${record.id}.${timestamp}`,
_id: record.id,
_hash: recordHash,
});
}
}
changes.all = [...changes.created, ...changes.updated];
// cache the changes
this.changes = changes;
return this.changes;
}
getTimestamp() {
return new Date().getTime();
}
/**
* Save to the cache
*
* Note: Should be called after each poll to update the deduper cache.
*
*/
async persistChanges(currentRecords) {
const records = this.getRecords(currentRecords);
const data = lodash_1.default.merge({}, this.cache, records);
await this.storage.save(data);
return true;
}
/**
* Hash a record
*/
hash(record) {
return object_hash_1.default(record, {
algorithm: "md5",
encoding: "base64",
});
}
/**
* Convert records into cache records
*
*/
getRecords(currentRecords) {
const records = lodash_1.default.reduce(currentRecords, (records, record) => {
records[record.id] = this.hash(record);
return records;
}, {});
return records;
}
}
exports.Deduper = Deduper;
/**
* Get a deduper instance
*
* @param zapId Zap ID
*/
function getDeduper(zapId) {
const storage = new storage_1.Storage(zapId);
const deduper = new Deduper(storage);
return deduper;
}
exports.getDeduper = getDeduper;
//# sourceMappingURL=deduper.js.map