diff --git a/packages/drizzle/src/find/traverseFields.ts b/packages/drizzle/src/find/traverseFields.ts index b109307382..712d39da0f 100644 --- a/packages/drizzle/src/find/traverseFields.ts +++ b/packages/drizzle/src/find/traverseFields.ts @@ -526,7 +526,7 @@ export const traverseFields = ({ if ( !withTabledFields.rels && - field.type === 'relationship' && + (field.type === 'relationship' || field.type === 'upload') && (field.hasMany || Array.isArray(field.relationTo)) ) { withTabledFields.rels = true diff --git a/test/select/collections/Posts/index.ts b/test/select/collections/Posts/index.ts index 3c36d9c2fd..9aeb0cb6eb 100644 --- a/test/select/collections/Posts/index.ts +++ b/test/select/collections/Posts/index.ts @@ -116,5 +116,33 @@ export const PostsCollection: CollectionConfig = { }, ], }, + { + type: 'relationship', + name: 'hasOne', + relationTo: 'rels', + }, + { + type: 'relationship', + name: 'hasMany', + hasMany: true, + relationTo: 'rels', + }, + { + type: 'upload', + name: 'hasManyUpload', + hasMany: true, + relationTo: 'upload', + }, + { + type: 'relationship', + name: 'hasOnePoly', + relationTo: ['rels'], + }, + { + type: 'relationship', + name: 'hasManyPoly', + hasMany: true, + relationTo: ['rels'], + }, ], } diff --git a/test/select/config.ts b/test/select/config.ts index 7f0cb3a728..19a6a38f42 100644 --- a/test/select/config.ts +++ b/test/select/config.ts @@ -23,6 +23,17 @@ export default buildConfigWithDefaults({ DeepPostsCollection, Pages, Points, + { + slug: 'upload', + fields: [], + upload: { + staticDir: path.resolve(dirname, 'media'), + }, + }, + { + slug: 'rels', + fields: [], + }, ], globals: [ { diff --git a/test/select/image.jpg b/test/select/image.jpg new file mode 100644 index 0000000000..4d2dfc3457 Binary files /dev/null and b/test/select/image.jpg differ diff --git a/test/select/int.spec.ts b/test/select/int.spec.ts index ffa515817d..081a3f633c 100644 --- a/test/select/int.spec.ts +++ b/test/select/int.spec.ts @@ -64,6 +64,7 @@ describe('Select', () => { collection: 'posts', id: postId, select: {}, + depth: 0, }) expect(res).toStrictEqual({ @@ -78,6 +79,7 @@ describe('Select', () => { select: { number: true, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -93,6 +95,7 @@ describe('Select', () => { select: { select: true, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -108,6 +111,7 @@ describe('Select', () => { select: { selectMany: true, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -124,6 +128,7 @@ describe('Select', () => { number: true, text: true, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -133,6 +138,50 @@ describe('Select', () => { }) }) + it('should select relationships', async () => { + const res_1 = await payload.findByID({ + collection: 'posts', + id: postId, + select: { + hasManyUpload: true, + }, + depth: 0, + }) + + expect(res_1).toStrictEqual({ + id: postId, + hasManyUpload: post.hasManyUpload, + }) + + const res_2 = await payload.findByID({ + collection: 'posts', + id: postId, + select: { + hasOne: true, + }, + depth: 0, + }) + + expect(res_2).toStrictEqual({ + id: postId, + hasOne: post.hasOne, + }) + + const res_3 = await payload.findByID({ + collection: 'posts', + id: postId, + select: { + hasManyPoly: true, + }, + depth: 0, + }) + + expect(res_3).toStrictEqual({ + id: postId, + hasManyPoly: post.hasManyPoly, + }) + }) + it('should select all the fields inside of group', async () => { const res = await payload.findByID({ collection: 'posts', @@ -140,6 +189,7 @@ describe('Select', () => { select: { group: true, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -157,6 +207,7 @@ describe('Select', () => { text: true, }, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -174,6 +225,7 @@ describe('Select', () => { select: { tab: true, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -191,6 +243,7 @@ describe('Select', () => { text: true, }, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -208,6 +261,7 @@ describe('Select', () => { select: { unnamedTabText: true, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -223,6 +277,7 @@ describe('Select', () => { select: { array: {}, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -238,6 +293,7 @@ describe('Select', () => { select: { array: true, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -255,6 +311,7 @@ describe('Select', () => { text: true, }, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -273,6 +330,7 @@ describe('Select', () => { select: { blocks: {}, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -288,6 +346,7 @@ describe('Select', () => { select: { blocks: true, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -305,6 +364,7 @@ describe('Select', () => { cta: true, }, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -330,6 +390,7 @@ describe('Select', () => { cta: { ctaText: true }, }, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -372,6 +433,7 @@ describe('Select', () => { select: { text: false, }, + depth: 0, }) const expected = { ...post } @@ -388,6 +450,7 @@ describe('Select', () => { select: { number: false, }, + depth: 0, }) const expected = { ...post } @@ -404,6 +467,7 @@ describe('Select', () => { select: { select: false, }, + depth: 0, }) const expected = { ...post } @@ -420,6 +484,7 @@ describe('Select', () => { select: { selectMany: false, }, + depth: 0, }) const expected = { ...post } @@ -437,6 +502,7 @@ describe('Select', () => { number: false, text: false, }, + depth: 0, }) const expected = { ...post } @@ -447,6 +513,29 @@ describe('Select', () => { expect(res).toStrictEqual(expected) }) + it('should exclude relationships', async () => { + const res = await payload.findByID({ + collection: 'posts', + id: postId, + select: { + hasOne: false, + hasMany: false, + hasManyPoly: false, + hasOnePoly: false, + }, + depth: 0, + }) + + const expected = { ...post } + + delete expected['hasOne'] + delete expected['hasMany'] + delete expected['hasManyPoly'] + delete expected['hasOnePoly'] + + expect(res).toStrictEqual(expected) + }) + it('should exclude group', async () => { const res = await payload.findByID({ collection: 'posts', @@ -454,6 +543,7 @@ describe('Select', () => { select: { group: false, }, + depth: 0, }) const expected = { ...post } @@ -472,6 +562,7 @@ describe('Select', () => { text: false, }, }, + depth: 0, }) const expected = deepCopyObject(post) @@ -488,6 +579,7 @@ describe('Select', () => { select: { array: false, }, + depth: 0, }) const expected = { ...post } @@ -506,6 +598,7 @@ describe('Select', () => { text: false, }, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -524,6 +617,7 @@ describe('Select', () => { select: { blocks: false, }, + depth: 0, }) const expected = { ...post } @@ -542,6 +636,7 @@ describe('Select', () => { cta: false, }, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -562,6 +657,7 @@ describe('Select', () => { cta: { ctaText: false }, }, }, + depth: 0, }) expect(res).toStrictEqual({ @@ -1630,6 +1726,7 @@ describe('Select', () => { const res = await restClient .GET(`/posts/${postId}`, { query: { + depth: 0, select: { text: true, } satisfies Config['collectionsSelect']['posts'], @@ -1647,6 +1744,7 @@ describe('Select', () => { const res = await restClient .GET(`/posts/${postId}`, { query: { + depth: 0, select: { number: true, text: true, @@ -1666,6 +1764,7 @@ describe('Select', () => { const res = await restClient .GET(`/posts/${postId}`, { query: { + depth: 0, select: { group: true, } satisfies Config['collectionsSelect']['posts'], @@ -1683,6 +1782,7 @@ describe('Select', () => { const res = await restClient .GET(`/posts/${postId}`, { query: { + depth: 0, select: { group: { text: true }, } satisfies Config['collectionsSelect']['posts'], @@ -1704,6 +1804,7 @@ describe('Select', () => { const res = await restClient .GET(`/posts/${postId}`, { query: { + depth: 0, select: { text: false, } satisfies Config['collectionsSelect']['posts'], @@ -1722,6 +1823,7 @@ describe('Select', () => { const res = await restClient .GET(`/posts/${postId}`, { query: { + depth: 0, select: { number: false, } satisfies Config['collectionsSelect']['posts'], @@ -1740,6 +1842,7 @@ describe('Select', () => { const res = await restClient .GET(`/posts/${postId}`, { query: { + depth: 0, select: { number: false, text: false, @@ -1760,6 +1863,7 @@ describe('Select', () => { const res = await restClient .GET(`/posts/${postId}`, { query: { + depth: 0, select: { group: { text: false, @@ -2129,7 +2233,19 @@ describe('Select', () => { }) }) -function createPost() { +async function createPost() { + const upload = await payload.create({ + collection: 'upload', + data: {}, + filePath: path.resolve(dirname, 'image.jpg'), + }) + + const relation = await payload.create({ + depth: 0, + collection: 'rels', + data: {}, + }) + return payload.create({ collection: 'posts', depth: 0, @@ -2142,6 +2258,11 @@ function createPost() { number: 1, text: 'text', }, + hasMany: [relation], + hasManyUpload: [upload], + hasOne: relation, + hasManyPoly: [{ relationTo: 'rels', value: relation }], + hasOnePoly: { relationTo: 'rels', value: relation }, blocks: [ { blockType: 'cta', diff --git a/test/select/payload-types.ts b/test/select/payload-types.ts index 9d6c7ca08c..7b5004d5d1 100644 --- a/test/select/payload-types.ts +++ b/test/select/payload-types.ts @@ -17,6 +17,8 @@ export interface Config { 'deep-posts': DeepPost; pages: Page; points: Point; + upload: Upload; + rels: Rel; users: User; 'payload-locked-documents': PayloadLockedDocument; 'payload-preferences': PayloadPreference; @@ -30,6 +32,8 @@ export interface Config { 'deep-posts': DeepPostsSelect | DeepPostsSelect; pages: PagesSelect | PagesSelect; points: PointsSelect | PointsSelect; + upload: UploadSelect | UploadSelect; + rels: RelsSelect | RelsSelect; users: UsersSelect | UsersSelect; 'payload-locked-documents': PayloadLockedDocumentsSelect | PayloadLockedDocumentsSelect; 'payload-preferences': PayloadPreferencesSelect | PayloadPreferencesSelect; @@ -116,6 +120,28 @@ export interface Post { }; unnamedTabText?: string | null; unnamedTabNumber?: number | null; + hasOne?: (string | null) | Rel; + hasMany?: (string | Rel)[] | null; + hasManyUpload?: (string | Rel)[] | null; + hasOnePoly?: { + relationTo: 'rels'; + value: string | Rel; + } | null; + hasManyPoly?: + | { + relationTo: 'rels'; + value: string | Rel; + }[] + | null; + updatedAt: string; + createdAt: string; +} +/** + * This interface was referenced by `Config`'s JSON-Schema + * via the `definition` "rels". + */ +export interface Rel { + id: string; updatedAt: string; createdAt: string; } @@ -343,6 +369,24 @@ export interface Point { updatedAt: string; createdAt: string; } +/** + * This interface was referenced by `Config`'s JSON-Schema + * via the `definition` "upload". + */ +export interface Upload { + id: string; + updatedAt: string; + createdAt: string; + url?: string | null; + thumbnailURL?: string | null; + filename?: string | null; + mimeType?: string | null; + filesize?: number | null; + width?: number | null; + height?: number | null; + focalX?: number | null; + focalY?: number | null; +} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "users". @@ -391,6 +435,14 @@ export interface PayloadLockedDocument { relationTo: 'points'; value: string | Point; } | null) + | ({ + relationTo: 'upload'; + value: string | Upload; + } | null) + | ({ + relationTo: 'rels'; + value: string | Rel; + } | null) | ({ relationTo: 'users'; value: string | User; @@ -487,6 +539,11 @@ export interface PostsSelect { }; unnamedTabText?: T; unnamedTabNumber?: T; + hasOne?: T; + hasMany?: T; + hasManyUpload?: T; + hasOnePoly?: T; + hasManyPoly?: T; updatedAt?: T; createdAt?: T; } @@ -705,6 +762,31 @@ export interface PointsSelect { updatedAt?: T; createdAt?: T; } +/** + * This interface was referenced by `Config`'s JSON-Schema + * via the `definition` "upload_select". + */ +export interface UploadSelect { + updatedAt?: T; + createdAt?: T; + url?: T; + thumbnailURL?: T; + filename?: T; + mimeType?: T; + filesize?: T; + width?: T; + height?: T; + focalX?: T; + focalY?: T; +} +/** + * This interface was referenced by `Config`'s JSON-Schema + * via the `definition` "rels_select". + */ +export interface RelsSelect { + updatedAt?: T; + createdAt?: T; +} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "users_select".