Skip to content

Commit e5cc915

Browse files
authored
fix(db-postgres): allow to clear select fields with hasMany: true (#9464)
### What? Previously, using Postgres, select fields with `hasMany: true` weren't clearable. Meaning, you couldn't pass an empty array: ```ts const updatedDoc = await payload.update({ id, collection: 'select-fields', data: { selectHasMany: [], }, }) ``` ### Why? To achieve the same behavior with MongoDB. ### How? Modifies logic in `packages/drizzle/src/upsertRow/index.ts` to include empty arrays.
1 parent b96475b commit e5cc915

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

packages/drizzle/src/upsertRow/index.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
114114
// store by table name and rows
115115
if (Object.keys(rowToInsert.selects).length > 0) {
116116
Object.entries(rowToInsert.selects).forEach(([selectTableName, selectRows]) => {
117+
selectsToInsert[selectTableName] = []
118+
117119
selectRows.forEach((row) => {
118120
if (typeof row.parent === 'undefined') {
119121
row.parent = insertedRow.id
120122
}
121-
if (!selectsToInsert[selectTableName]) {
122-
selectsToInsert[selectTableName] = []
123-
}
123+
124124
selectsToInsert[selectTableName].push(row)
125125
})
126126
})
@@ -343,11 +343,14 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
343343
where: eq(selectTable.parent, insertedRow.id),
344344
})
345345
}
346-
await adapter.insert({
347-
db,
348-
tableName: selectTableName,
349-
values: tableRows,
350-
})
346+
347+
if (tableRows.length) {
348+
await adapter.insert({
349+
db,
350+
tableName: selectTableName,
351+
values: tableRows,
352+
})
353+
}
351354
}
352355

353356
// //////////////////////////////////

test/fields/int.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,25 @@ describe('Fields', () => {
521521
expect(updatedDoc.selectHasMany).toEqual(['one', 'two'])
522522
})
523523

524+
it('should clear select hasMany field', async () => {
525+
const { id } = await payload.create({
526+
collection: 'select-fields',
527+
data: {
528+
selectHasMany: ['one', 'two'],
529+
},
530+
})
531+
532+
const updatedDoc = await payload.update({
533+
id,
534+
collection: 'select-fields',
535+
data: {
536+
selectHasMany: [],
537+
},
538+
})
539+
540+
expect(updatedDoc.selectHasMany).toHaveLength(0)
541+
})
542+
524543
it('should query hasMany in', async () => {
525544
const hit = await payload.create({
526545
collection: 'select-fields',

0 commit comments

Comments
 (0)