@overture-stack/lyric
Version:
Data Submission system
40 lines (39 loc) • 1.28 kB
JavaScript
const toMessage = (record, action) => ({
value: JSON.stringify({
action,
data: record.data,
entityName: record.entityName,
isValid: record.isValid,
organization: record.organization,
systemId: record.systemId,
}),
});
/**
* Returns an `onFinishCommit` handler that batches all records from a commit into a single
* `producer.send` call. Each message includes an `action` field (`insert`, `update`, or `delete`)
* describing what happened to the record in the submission, and `isValid` reflecting the record's
* current stored state.
*/
export const createKafkaPublisher = ({ onError, producer, topic }) => async (result) => {
if (!result.data)
return;
const { deletes, inserts, updates } = result.data;
const messages = [
...inserts.map((r) => toMessage(r, 'insert')),
...updates.map((r) => toMessage(r, 'update')),
...deletes.map((r) => toMessage(r, 'delete')),
];
if (messages.length === 0)
return;
try {
await producer.send({ topic, messages });
}
catch (err) {
if (onError) {
onError(err);
}
else {
console.error('[kafkaPublisher] Failed to publish commit result:', err);
}
}
};