Skip to content

Commit

Permalink
fix: duplicate list preferences stored (#9185)
Browse files Browse the repository at this point in the history
The collection list columns are stored as user preferences to the
payload-preferences collection. Normally one user should never have
duplicate documents with the same key. This is controlled by using an
upsert normally. The collection list does not have a good way to call
upsert and was creating preferences documents every time. This change
makes it so that existing preferences are updated rather than created
with each column change.
  • Loading branch information
DanRibbens authored Nov 15, 2024
1 parent ba06ce6 commit a5cae07
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions packages/ui/src/utilities/buildTableState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export const buildTableState = async (
collection: 'payload-preferences',
depth: 0,
limit: 1,
pagination: false,
where: {
and: [
{
Expand All @@ -186,30 +187,39 @@ export const buildTableState = async (
],
},
})
.then((res) => res.docs[0]?.value as ListPreferences)
.then((res) => res.docs[0] ?? { id: null, value: {} })

let newPrefs = preferencesResult
let newPrefs = preferencesResult.value

if (!preferencesResult || !dequal(columns, preferencesResult?.columns)) {
if (!preferencesResult.id || !dequal(columns, preferencesResult?.columns)) {
const mergedPrefs = {
...(preferencesResult || {}),
columns,
}

newPrefs = await payload
.create({
collection: 'payload-preferences',
data: {
key: preferencesKey,
user: {
collection: user.collection,
value: user.id,
},
value: mergedPrefs,
const preferencesArgs = {
collection: 'payload-preferences',
data: {
key: preferencesKey,
user: {
collection: user.collection,
value: user.id,
},
req,
})
?.then((res) => res.value as ListPreferences)
value: mergedPrefs,
},
depth: 0,
req,
}

if (preferencesResult.id) {
newPrefs = await payload
.update({
...preferencesArgs,
id: preferencesResult.id,
})
?.then((res) => res.value as ListPreferences)
} else {
newPrefs = await payload.create(preferencesArgs)?.then((res) => res.value as ListPreferences)
}
}

const fields = collectionConfig.fields
Expand Down

0 comments on commit a5cae07

Please sign in to comment.