Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cache messages #331

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stupid-boats-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@flatfile/plugin-record-hook': patch
---

This update handles caching of record messages.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 16 additions & 27 deletions plugins/record-hook/src/RecordHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,39 +72,41 @@ export const BulkRecordHook = async (
const fetchData = async () => {
try {
const data = await event.data
return data.records && data.records.length
? prepareXRecords(data.records)
: undefined
return data.records && data.records.length ? data.records : undefined
} catch (e) {
console.log(`Error fetching records: ${e}`)
}
}

try {
const batch = await event.cache.init<FlatfileRecords<any>>(
'records',
const originalRecords = await event.cache.init<Flatfile.Record_[]>(
'originalRecords',
fetchData
)

if (!batch || batch.records.length === 0) {
if (!originalRecords || originalRecords.length === 0) {
if (options.debug) {
console.log('No records to process')
}
await completeCommit()
return
}

await event.cache.init<FlatfileRecords<any>>('originalRecords', fetchData)
const batch = await event.cache.init<FlatfileRecords<any>>(
'records',
async () => await prepareXRecords(originalRecords)
)

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

event.afterAll(async () => {
const batch = event.cache.get<FlatfileRecords<any>>('records')
const { records } = event.cache.get<FlatfileRecords<any>>('records')
const batch = new RecordTranslator<FlatfileRecord>(records).toXRecords()
const originalRecords =
event.cache.get<FlatfileRecords<any>>('originalRecords')
const modifiedRecords = batch.records.filter((record) =>
hasRecordChanges(record, originalRecords.records)
event.cache.get<Flatfile.Record_[]>('originalRecords')
const modifiedRecords = batch.filter((record) =>
hasRecordChanges(record, originalRecords)
)
if (!modifiedRecords || modifiedRecords.length === 0) {
if (options.debug) {
Expand All @@ -114,12 +116,8 @@ export const BulkRecordHook = async (
return
}

const recordsUpdates = new RecordTranslator<FlatfileRecord>(
modifiedRecords
).toXRecords()

try {
return await event.update(recordsUpdates)
return await event.update(modifiedRecords)
} catch (e) {
console.log(`Error updating records: ${e}`)
}
Expand All @@ -138,16 +136,7 @@ const hasRecordChanges = (record, originalRecords) => {
return JSON.stringify(record) !== JSON.stringify(originalRecord)
}

const prepareXRecords = (records: any): FlatfileRecords<any> => {
const clearedMessages: Flatfile.Record_[] = records.map(
(record: { values: { [x: string]: { messages: never[] } } }) => {
// clear existing cell validation messages
Object.keys(record.values).forEach((k) => {
record.values[k].messages = []
})
return record
}
)
const fromX = new RecordTranslator<Flatfile.Record_>(clearedMessages)
const prepareXRecords = async (records: any): Promise<FlatfileRecords<any>> => {
const fromX = new RecordTranslator<Flatfile.Record_>(records)
return fromX.toFlatFileRecords()
}
Loading