Skip to content

Commit dff71ee

Browse files
authored
fix(db-postgres): handle select query on select fields (#9607)
Fixes #9606 With Postgres / SQLite, select fields (non `hasMany: true`) weren't properly handled in the `traverseFields.ts` function for `select` query.
1 parent 4a324a9 commit dff71ee

File tree

5 files changed

+177
-8
lines changed

5 files changed

+177
-8
lines changed

packages/drizzle/src/find/traverseFields.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -540,16 +540,16 @@ export const traverseFields = ({
540540
}
541541

542542
case 'select': {
543-
if (field.hasMany) {
544-
if (select) {
545-
if (
546-
(selectMode === 'include' && !select[field.name]) ||
547-
(selectMode === 'exclude' && select[field.name] === false)
548-
) {
549-
break
550-
}
543+
if (select && !selectAllOnCurrentLevel) {
544+
if (
545+
(selectMode === 'include' && !select[field.name]) ||
546+
(selectMode === 'exclude' && select[field.name] === false)
547+
) {
548+
break
551549
}
550+
}
552551

552+
if (field.hasMany) {
553553
const withSelect: Result = {
554554
columns: {
555555
id: false,
@@ -560,6 +560,17 @@ export const traverseFields = ({
560560
}
561561

562562
currentArgs.with[`${path}${field.name}`] = withSelect
563+
break
564+
}
565+
566+
if (select || selectAllOnCurrentLevel) {
567+
const fieldPath = `${path}${field.name}`
568+
569+
if ((field.localized || withinLocalizedField) && _locales) {
570+
_locales.columns[fieldPath] = true
571+
} else if (adapter.tables[currentTableName]?.[fieldPath]) {
572+
currentArgs.columns[fieldPath] = true
573+
}
563574
}
564575

565576
break

test/select/collections/LocalizedPosts/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ export const LocalizedPostsCollection: CollectionConfig = {
1616
localized: true,
1717
type: 'number',
1818
},
19+
{
20+
name: 'select',
21+
type: 'select',
22+
options: ['a', 'b'],
23+
},
24+
{
25+
name: 'selectMany',
26+
type: 'select',
27+
options: ['a', 'b'],
28+
hasMany: true,
29+
},
1930
{
2031
name: 'group',
2132
localized: true,

test/select/collections/Posts/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ export const PostsCollection: CollectionConfig = {
1414
name: 'number',
1515
type: 'number',
1616
},
17+
{
18+
name: 'select',
19+
type: 'select',
20+
options: ['a', 'b'],
21+
},
22+
{
23+
name: 'selectMany',
24+
type: 'select',
25+
options: ['a', 'b'],
26+
hasMany: true,
27+
},
1728
{
1829
name: 'group',
1930
type: 'group',

test/select/int.spec.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,36 @@ describe('Select', () => {
8686
})
8787
})
8888

89+
it('should select only select', async () => {
90+
const res = await payload.findByID({
91+
collection: 'posts',
92+
id: postId,
93+
select: {
94+
select: true,
95+
},
96+
})
97+
98+
expect(res).toStrictEqual({
99+
id: postId,
100+
select: post.select,
101+
})
102+
})
103+
104+
it('should select only hasMany select', async () => {
105+
const res = await payload.findByID({
106+
collection: 'posts',
107+
id: postId,
108+
select: {
109+
selectMany: true,
110+
},
111+
})
112+
113+
expect(res).toStrictEqual({
114+
id: postId,
115+
selectMany: post.selectMany,
116+
})
117+
})
118+
89119
it('should select number and text', async () => {
90120
const res = await payload.findByID({
91121
collection: 'posts',
@@ -367,6 +397,38 @@ describe('Select', () => {
367397
expect(res).toStrictEqual(expected)
368398
})
369399

400+
it('should exclude select', async () => {
401+
const res = await payload.findByID({
402+
collection: 'posts',
403+
id: postId,
404+
select: {
405+
select: false,
406+
},
407+
})
408+
409+
const expected = { ...post }
410+
411+
delete expected['select']
412+
413+
expect(res).toStrictEqual(expected)
414+
})
415+
416+
it('should exclude hasMany select', async () => {
417+
const res = await payload.findByID({
418+
collection: 'posts',
419+
id: postId,
420+
select: {
421+
selectMany: false,
422+
},
423+
})
424+
425+
const expected = { ...post }
426+
427+
delete expected['selectMany']
428+
429+
expect(res).toStrictEqual(expected)
430+
})
431+
370432
it('should exclude number and text', async () => {
371433
const res = await payload.findByID({
372434
collection: 'posts',
@@ -573,6 +635,36 @@ describe('Select', () => {
573635
})
574636
})
575637

638+
it('should select only select', async () => {
639+
const res = await payload.findByID({
640+
collection: 'localized-posts',
641+
id: postId,
642+
select: {
643+
select: true,
644+
},
645+
})
646+
647+
expect(res).toStrictEqual({
648+
id: postId,
649+
select: post.select,
650+
})
651+
})
652+
653+
it('should select only hasMany select', async () => {
654+
const res = await payload.findByID({
655+
collection: 'localized-posts',
656+
id: postId,
657+
select: {
658+
selectMany: true,
659+
},
660+
})
661+
662+
expect(res).toStrictEqual({
663+
id: postId,
664+
selectMany: post.selectMany,
665+
})
666+
})
667+
576668
it('should select number and text', async () => {
577669
const res = await payload.findByID({
578670
collection: 'localized-posts',
@@ -877,6 +969,38 @@ describe('Select', () => {
877969
expect(res).toStrictEqual(expected)
878970
})
879971

972+
it('should exclude select', async () => {
973+
const res = await payload.findByID({
974+
collection: 'localized-posts',
975+
id: postId,
976+
select: {
977+
select: false,
978+
},
979+
})
980+
981+
const expected = { ...post }
982+
983+
delete expected['select']
984+
985+
expect(res).toStrictEqual(expected)
986+
})
987+
988+
it('should exclude hasMany select', async () => {
989+
const res = await payload.findByID({
990+
collection: 'localized-posts',
991+
id: postId,
992+
select: {
993+
selectMany: false,
994+
},
995+
})
996+
997+
const expected = { ...post }
998+
999+
delete expected['selectMany']
1000+
1001+
expect(res).toStrictEqual(expected)
1002+
})
1003+
8801004
it('should exclude number and text', async () => {
8811005
const res = await payload.findByID({
8821006
collection: 'localized-posts',
@@ -1963,6 +2087,8 @@ function createPost() {
19632087
data: {
19642088
number: 1,
19652089
text: 'text',
2090+
select: 'a',
2091+
selectMany: ['a'],
19662092
group: {
19672093
number: 1,
19682094
text: 'text',
@@ -2002,6 +2128,8 @@ function createLocalizedPost() {
20022128
data: {
20032129
number: 1,
20042130
text: 'text',
2131+
select: 'a',
2132+
selectMany: ['a'],
20052133
group: {
20062134
number: 1,
20072135
text: 'text',

test/select/payload-types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ export interface Post {
7979
id: string;
8080
text?: string | null;
8181
number?: number | null;
82+
select?: ('a' | 'b') | null;
83+
selectMany?: ('a' | 'b')[] | null;
8284
group?: {
8385
text?: string | null;
8486
number?: number | null;
@@ -125,6 +127,8 @@ export interface LocalizedPost {
125127
id: string;
126128
text?: string | null;
127129
number?: number | null;
130+
select?: ('a' | 'b') | null;
131+
selectMany?: ('a' | 'b')[] | null;
128132
group?: {
129133
text?: string | null;
130134
number?: number | null;
@@ -424,6 +428,8 @@ export interface PayloadMigration {
424428
export interface PostsSelect<T extends boolean = true> {
425429
text?: T;
426430
number?: T;
431+
select?: T;
432+
selectMany?: T;
427433
group?:
428434
| T
429435
| {
@@ -475,6 +481,8 @@ export interface PostsSelect<T extends boolean = true> {
475481
export interface LocalizedPostsSelect<T extends boolean = true> {
476482
text?: T;
477483
number?: T;
484+
select?: T;
485+
selectMany?: T;
478486
group?:
479487
| T
480488
| {

0 commit comments

Comments
 (0)