Skip to content

Commit 605c993

Browse files
fix(drizzle): skip column if undefined in findMany (#12902)
<!-- Thank you for the PR! Please go through the checklist below and make sure you've completed all the steps. Please review the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository if you haven't already. The following items will ensure that your PR is handled as smoothly as possible: - PR Title must follow conventional commits format. For example, `feat: my new feature`, `fix(plugin-seo): my fix`. - Minimal description explained as if explained to someone not immediately familiar with the code. - Provide before/after screenshots or code diffs if applicable. - Link any related issues/discussions from GitHub or Discord. - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Fixes # --> ### What? This PR fixes an issue where sorting on a traditional virtual field with `virtual: true` while using a drizzle-based db adapter would cause a runtime error. ### Why? To skip attempting to sort virtual fields which are not linked to a relationship/upload and prevent a runtime error from surfacing. ### How? Skipping the deletion of the property from the `selectFields` object if the column is false-y. Fixes#12886 Before: [sort-virtualfield-drizzle-error.mp4](https://private-user-images.githubusercontent.com/78685728/457602747-b8661e47-a1a8-4453-b2ec-b7e7199b9846.mp4?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NTA2OTU0NzksIm5iZiI6MTc1MDY5NTE3OSwicGF0aCI6Ii83ODY4NTcyOC80NTc2MDI3NDctYjg2NjFlNDctYTFhOC00NDUzLWIyZWMtYjdlNzE5OWI5ODQ2Lm1wND9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTA2MjMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwNjIzVDE2MTI1OVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTE3NmMzOWI5YjNiNzEwYzk3ZWUyNDllYTBjMzZkNzkzMjhjNzc5YzJhNDlkOTBiNDk5MDFhMTdmNDA4NjJhZWQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.N1GJsiI_gZ8M54VHCAmiPEhcJGqRw3Ucy-VeM5R7UFE) After: [virtualfields-sort-Posts---Payload.webm](https://github.com/user-attachments/assets/f5a15d98-4a40-4817-bc6a-415f3ec27484) <details> <summary>Collection config used above</summary> ```ts export const PostsCollection: CollectionConfig = { slug: postsSlug, admin: { useAsTitle: 'title', defaultColumns: ['title', 'exampleField'], }, fields: [ { name: 'title', type: 'text', }, { name: 'exampleField', type: 'text', virtual: true, admin: { readOnly: true, }, hooks: { afterRead: [({ data }) => data?.title], }, }, { type: 'relationship', name: 'category', relationTo: 'categories', }, { name: 'categoryTitle', type: 'text', virtual: 'category.title', }, ], } ``` </details> --------- Co-authored-by: Sasha <64744993+r1tsuu@users.noreply.github.com>
1 parent a7ad573 commit 605c993

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎packages/drizzle/src/find/findMany.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const findMany = async function find({
8080
if(orderBy){
8181
for(constkeyinselectFields){
8282
constcolumn=selectFields[key]
83-
if(column.primary){
83+
if(!column||column.primary){
8484
continue
8585
}
8686

‎test/database/int.spec.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { initPayloadInt } from '../helpers/initPayloadInt.js'
3232
import{isMongoose}from'../helpers/isMongoose.js'
3333
importremoveFilesfrom'../helpers/removeFiles.js'
3434
import{seed}from'./seed.js'
35-
import{errorOnUnnamedFieldsSlug,postsSlug}from'./shared.js'
35+
import{errorOnUnnamedFieldsSlug,fieldsPersistanceSlug,postsSlug}from'./shared.js'
3636

3737
constfilename=fileURLToPath(import.meta.url)
3838
constdirname=path.dirname(filename)
@@ -2325,6 +2325,19 @@ describe('database', () => {
23252325
expect(localAsc[1].id).toBe(doc_2.id)
23262326
expect(localAsc[0].id).toBe(doc_1.id)
23272327
})
2328+
2329+
it('should allow to sort by a virtual field without error',async()=>{
2330+
awaitpayload.delete({collection: fieldsPersistanceSlug,where: {}})
2331+
awaitpayload.create({
2332+
collection: fieldsPersistanceSlug,
2333+
data: {},
2334+
})
2335+
const{ docs }=awaitpayload.find({
2336+
collection: fieldsPersistanceSlug,
2337+
sort: '-textHooked',
2338+
})
2339+
expect(docs).toHaveLength(1)
2340+
})
23282341
})
23292342

23302343
it('should convert numbers to text',async()=>{

0 commit comments

Comments
 (0)