Skip to content

Commit

Permalink
fix(db-postgres): select query on upload fields with hasMany: true
Browse files Browse the repository at this point in the history
  • Loading branch information
r1tsuu committed Dec 17, 2024
1 parent b0b2fc6 commit 5fdc0bc
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/drizzle/src/find/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions test/select/collections/Posts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,33 @@ export const PostsCollection: CollectionConfig = {
},
],
},
{
type: 'relationship',
name: 'hasOne',
relationTo: 'posts',
},
{
type: 'relationship',
name: 'hasMany',
hasMany: true,
relationTo: 'posts',
},
{
type: 'upload',
name: 'hasManyUpload',
hasMany: true,
relationTo: 'posts',
},
{
type: 'relationship',
name: 'hasOnePoly',
relationTo: ['posts'],
},
{
type: 'relationship',
name: 'hasManyPoly',
hasMany: true,
relationTo: ['posts'],
},
],
}
7 changes: 7 additions & 0 deletions test/select/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export default buildConfigWithDefaults({
DeepPostsCollection,
Pages,
Points,
{
slug: 'upload',
fields: [],
upload: {
staticDir: path.resolve(dirname, 'media'),
},
},
],
globals: [
{
Expand Down
Binary file added test/select/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 81 additions & 1 deletion test/select/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,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',
Expand Down Expand Up @@ -447,6 +491,29 @@ describe('Select', () => {
expect(res).toStrictEqual(expected)

Check failure on line 491 in test/select/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-mongodb

Select › Local API - Base › Exclude mode › should exclude number and text

expect(received).toStrictEqual(expected) // deep equality - Expected - 4 + Received + 40 @@ -24,25 +24,61 @@ "group": Object { "number": 1, "text": "text", }, "hasMany": Array [ - "6761cc0e46fb8bdb4bb76697", + Object { + "array": Array [], + "blocks": Array [], + "createdAt": "2024-12-17T19:07:58.610Z", + "group": Object {}, + "id": "6761cc0e46fb8bdb4bb76697", + "selectMany": Array [], + "tab": Object {}, + "updatedAt": "2024-12-17T19:07:58.610Z", + }, ], "hasManyPoly": Array [ Object { "relationTo": "posts", - "value": "6761cc0e46fb8bdb4bb76697", + "value": Object { + "array": Array [], + "blocks": Array [], + "createdAt": "2024-12-17T19:07:58.610Z", + "group": Object {}, + "id": "6761cc0e46fb8bdb4bb76697", + "selectMany": Array [], + "tab": Object {}, + "updatedAt": "2024-12-17T19:07:58.610Z", + }, }, ], "hasManyUpload": Array [ "6761cc0e46fb8bdb4bb76695", ], - "hasOne": "6761cc0e46fb8bdb4bb76697", + "hasOne": Object { + "array": Array [], + "blocks": Array [], + "createdAt": "2024-12-17T19:07:58.610Z", + "group": Object {}, + "id": "6761cc0e46fb8bdb4bb76697", + "selectMany": Array [], + "tab": Object {}, + "updatedAt": "2024-12-17T19:07:58.610Z", + }, "hasOnePoly": Object { "relationTo": "posts", - "value": "6761cc0e46fb8bdb4bb76697", + "value": Object { + "array": Array [], + "blocks": Array [], + "createdAt": "2024-12-17T19:07:58.610Z", + "group": Object {}, + "id": "6761cc0e46fb8bdb4bb76697", + "selectMany": Array [], + "tab": Object {}, + "updatedAt": "2024-12-17T19:07:58.610Z", + }, }, "id": "6761cc0e46fb8bdb4bb76699", "select": "a", "selectMany": Array [ "a", at Object.toStrictEqual (select/int.spec.ts:491:21)

Check failure on line 491 in test/select/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-sqlite

Select › Local API - Base › Exclude mode › should exclude number and text

expect(received).toStrictEqual(expected) // deep equality - Expected - 5 + Received + 125 @@ -26,25 +26,145 @@ "group": Object { "number": 1, "text": "text", }, "hasMany": Array [ - 24, + Object { + "array": Array [], + "blocks": Array [], + "createdAt": "2024-12-17T19:06:48.899Z", + "group": Object { + "number": null, + "text": null, + }, + "hasMany": Array [], + "hasManyPoly": Array [], + "hasManyUpload": Array [], + "hasOne": null, + "id": 24, + "number": null, + "select": null, + "selectMany": Array [], + "tab": Object { + "number": null, + "text": null, + }, + "text": null, + "unnamedTabNumber": null, + "unnamedTabText": null, + "updatedAt": "2024-12-17T19:06:48.899Z", + }, ], "hasManyPoly": Array [ Object { "relationTo": "posts", - "value": 24, + "value": Object { + "array": Array [], + "blocks": Array [], + "createdAt": "2024-12-17T19:06:48.899Z", + "group": Object { + "number": null, + "text": null, + }, + "hasMany": Array [], + "hasManyPoly": Array [], + "hasManyUpload": Array [], + "hasOne": null, + "id": 24, + "number": null, + "select": null, + "selectMany": Array [], + "tab": Object { + "number": null, + "text": null, + }, + "text": null, + "unnamedTabNumber": null, + "unnamedTabText": null, + "updatedAt": "2024-12-17T19:06:48.899Z", }, + }, ], "hasManyUpload": Array [ - 24, + Object { + "array": Array [], + "blocks": Array [], + "createdAt": "2024-12-17T19:06:48.899Z", + "group": Object { + "number": null, + "text": null, + }, + "hasMany": Array [], + "hasManyPoly": Array [], + "hasManyUpload": Array [], + "hasOne": null, + "id": 24, + "number": null, + "select": null, + "selectMany": Array [], + "tab": Object { + "number": null, + "text": null, + }, + "text": null, + "unnamedTabNumber": null, + "unnamedTabText": null, + "updatedAt": "2024-12-17T19:06:48.899Z", + }, ], - "hasOne": 24, + "hasOne": Object { + "array": Array [], + "blocks": Array [], + "createdAt": "2024-12-17T19:06:48.899Z", + "group": Object { + "number": null, + "text": null, + }, + "hasMany": Array [], + "hasManyPoly": Array [], + "hasManyUpload": Array [], + "hasOne": null, + "id": 24, + "number": null, + "select": null, + "selectMany": Array [], + "tab": Object { + "number": null, + "text": null, + }, + "text": null, + "unnamedTabNumber": null, + "unnamedTabText": null, + "updatedAt": "2024-12-17T19:06:48.899Z", + }, "hasOnePoly": Object { "relationTo": "posts", - "value": 24, + "value": Object { + "array": Array [], + "blocks": Array [], + "createdAt": "2024-12-17T19:06:48.899Z", + "group": Object { + "number": null, + "text": null, + }, + "hasMany": Array [], + "hasManyPoly": Array [], + "hasManyUpload": Array [], + "hasOne": null, + "id": 24, + "number": null, + "select": null, + "selectMany": Array [], + "tab": Object { + "number": null, + "text": null, + }, + "text": null, + "unnamedTabNumber": null, + "unnamedTabText": null, + "updatedAt": "2024-12-17T19:06:48.899Z", + }, }, "id": 25, "select": "a", "selectMany": Array [ "a", at Object.toStrictEqual (select/int.spec.ts:491:21)
})

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',
Expand Down Expand Up @@ -2129,7 +2196,15 @@ 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: 'posts', data: {} })

return payload.create({
collection: 'posts',
depth: 0,
Expand All @@ -2142,6 +2217,11 @@ function createPost() {
number: 1,
text: 'text',
},
hasMany: [relation],
hasManyUpload: [upload],
hasOne: relation,
hasManyPoly: [{ relationTo: 'posts', value: relation }],
hasOnePoly: { relationTo: 'posts', value: relation },
blocks: [
{
blockType: 'cta',
Expand Down
59 changes: 59 additions & 0 deletions test/select/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Config {
'deep-posts': DeepPost;
pages: Page;
points: Point;
upload: Upload;
users: User;
'payload-locked-documents': PayloadLockedDocument;
'payload-preferences': PayloadPreference;
Expand All @@ -30,6 +31,7 @@ export interface Config {
'deep-posts': DeepPostsSelect<false> | DeepPostsSelect<true>;
pages: PagesSelect<false> | PagesSelect<true>;
points: PointsSelect<false> | PointsSelect<true>;
upload: UploadSelect<false> | UploadSelect<true>;
users: UsersSelect<false> | UsersSelect<true>;
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
Expand Down Expand Up @@ -116,8 +118,39 @@ export interface Post {
};
unnamedTabText?: string | null;
unnamedTabNumber?: number | null;
hasOne?: (string | null) | Post;
hasMany?: (string | Post)[] | null;
hasManyUpload?: (string | Upload)[] | null;
hasOnePoly?: {
relationTo: 'posts';
value: string | Post;
} | null;
hasManyPoly?:
| {
relationTo: 'posts';
value: string | Post;
}[]
| null;
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
Expand Down Expand Up @@ -391,6 +424,10 @@ export interface PayloadLockedDocument {
relationTo: 'points';
value: string | Point;
} | null)
| ({
relationTo: 'upload';
value: string | Upload;
} | null)
| ({
relationTo: 'users';
value: string | User;
Expand Down Expand Up @@ -487,6 +524,11 @@ export interface PostsSelect<T extends boolean = true> {
};
unnamedTabText?: T;
unnamedTabNumber?: T;
hasOne?: T;
hasMany?: T;
hasManyUpload?: T;
hasOnePoly?: T;
hasManyPoly?: T;
updatedAt?: T;
createdAt?: T;
}
Expand Down Expand Up @@ -705,6 +747,23 @@ export interface PointsSelect<T extends boolean = true> {
updatedAt?: T;
createdAt?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "upload_select".
*/
export interface UploadSelect<T extends boolean = true> {
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` "users_select".
Expand Down

0 comments on commit 5fdc0bc

Please sign in to comment.