Skip to content

Commit

Permalink
fix: bulkRecordHook overwriting messages from recordHook (#235)
Browse files Browse the repository at this point in the history
* fix: bulkRecordHook overwriting messages from recordHook

* Update RecordHook.ts

* fix: types

---------

Co-authored-by: David Boskovic <[email protected]>
  • Loading branch information
carlbrugger and dboskovic authored Sep 19, 2023
1 parent 6225dba commit 5ce6306
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions plugins/record-hook/src/RecordHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,28 @@ export const BulkRecordHook = async (
options: { chunkSize?: number; parallel?: number; debug?: boolean } = {}
) => {
try {
const records = await event.cache.init<Records>(
const batch = await event.cache.init<FlatfileRecords<any>>(
'records',
async () => (await event.data).records
async () => {
const data = await event.data
return prepareXRecords(data.records)
}
)
if (!records) return

const batch = await prepareXRecords(records)
if (!batch || batch.records.length === 0) return

// run client defined data hooks
await asyncBatch(batch.records, handler, options, event)

const recordsUpdates = new RecordTranslater<FlatfileRecord>(
batch.records
).toXRecords()
event.afterAll(async () => {
const batch = event.cache.get<FlatfileRecords<any>>('records')

await event.cache.set('records', async () => recordsUpdates)
const recordsUpdates = new RecordTranslater<FlatfileRecord>(
batch.records
).toXRecords()

event.afterAll(async () => {
const records = event.cache.get<Records>('records')
try {
return await event.update(records)
return await event.update(recordsUpdates)
} catch (e) {
console.log(`Error updating records: ${e}`)
}
Expand Down

0 comments on commit 5ce6306

Please sign in to comment.