Skip to content

Commit

Permalink
fix(drizzle): correctly pass joins from queryDrafts
Browse files Browse the repository at this point in the history
  • Loading branch information
r1tsuu committed Oct 15, 2024
1 parent 4192b60 commit d0b5321
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/drizzle/src/find/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export const traverseFields = ({
if (joinQuery === false) {
break
}

const {
limit: limitArg = 10,
sort,
Expand Down
25 changes: 23 additions & 2 deletions packages/drizzle/src/queryDrafts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PayloadRequest, QueryDrafts, SanitizedCollectionConfig } from 'payload'
import type { JoinQuery, PayloadRequest, QueryDrafts, SanitizedCollectionConfig } from 'payload'

import { buildVersionCollectionFields, combineQueries } from 'payload'
import toSnakeCase from 'to-snake-case'
Expand All @@ -9,7 +9,17 @@ import { findMany } from './find/findMany.js'

export const queryDrafts: QueryDrafts = async function queryDrafts(
this: DrizzleAdapter,
{ collection, limit, locale, page = 1, pagination, req = {} as PayloadRequest, sort, where },
{
collection,
joins,
limit,
locale,
page = 1,
pagination,
req = {} as PayloadRequest,
sort,
where,
},
) {
const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collection].config
const tableName = this.tableNameMap.get(
Expand All @@ -19,9 +29,20 @@ export const queryDrafts: QueryDrafts = async function queryDrafts(

const combinedWhere = combineQueries({ latest: { equals: true } }, where)

let queryDraftsJoins: JoinQuery = joins

if (typeof joins === 'object') {
queryDraftsJoins = {}

for (const k in joins) {
queryDraftsJoins[`version.${k}`] = joins[k]
}
}

const result = await findMany({
adapter: this,
fields,
joins: queryDraftsJoins,
limit,
locale,
page,
Expand Down
32 changes: 16 additions & 16 deletions test/_community/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface Config {
'payload-migrations': PayloadMigrationsSelect<false> | PayloadMigrationsSelect<true>;
};
db: {
defaultIDType: number;
defaultIDType: string;
};
globals: {
menu: Menu;
Expand Down Expand Up @@ -67,7 +67,7 @@ export interface UserAuthOperations {
* via the `definition` "posts".
*/
export interface Post {
id: number;
id: string;
text?: string | null;
serverTextField?: string | null;
richText?: {
Expand Down Expand Up @@ -110,7 +110,7 @@ export interface Post {
* via the `definition` "simple".
*/
export interface Simple {
id: number;
id: string;
text?: string | null;
updatedAt: string;
createdAt: string;
Expand All @@ -120,7 +120,7 @@ export interface Simple {
* via the `definition` "media".
*/
export interface Media {
id: number;
id: string;
updatedAt: string;
createdAt: string;
url?: string | null;
Expand Down Expand Up @@ -164,7 +164,7 @@ export interface Media {
* via the `definition` "users".
*/
export interface User {
id: number;
id: string;
updatedAt: string;
createdAt: string;
email: string;
Expand All @@ -181,28 +181,28 @@ export interface User {
* via the `definition` "payload-locked-documents".
*/
export interface PayloadLockedDocument {
id: number;
id: string;
document?:
| ({
relationTo: 'posts';
value: number | Post;
value: string | Post;
} | null)
| ({
relationTo: 'simple';
value: number | Simple;
value: string | Simple;
} | null)
| ({
relationTo: 'media';
value: number | Media;
value: string | Media;
} | null)
| ({
relationTo: 'users';
value: number | User;
value: string | User;
} | null);
globalSlug?: string | null;
user: {
relationTo: 'users';
value: number | User;
value: string | User;
};
updatedAt: string;
createdAt: string;
Expand All @@ -212,10 +212,10 @@ export interface PayloadLockedDocument {
* via the `definition` "payload-preferences".
*/
export interface PayloadPreference {
id: number;
id: string;
user: {
relationTo: 'users';
value: number | User;
value: string | User;
};
key?: string | null;
value?:
Expand All @@ -235,7 +235,7 @@ export interface PayloadPreference {
* via the `definition` "payload-migrations".
*/
export interface PayloadMigration {
id: number;
id: string;
name?: string | null;
batch?: number | null;
updatedAt: string;
Expand Down Expand Up @@ -383,7 +383,7 @@ export interface PayloadMigrationsSelect<T extends boolean = true> {
* via the `definition` "menu".
*/
export interface Menu {
id: number;
id: string;
globalText?: string | null;
updatedAt?: string | null;
createdAt?: string | null;
Expand All @@ -393,7 +393,7 @@ export interface Menu {
* via the `definition` "custom-ts".
*/
export interface CustomT {
id: number;
id: string;
custom?: 'hello' | 'world';
withDefinitionsUsage?: ObjectWithNumber[];
json: {
Expand Down
5 changes: 4 additions & 1 deletion test/joins/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,10 @@ describe('Joins Field', () => {
data: { categoryVersion: category.id },
})

const res = await payload.find({ collection: 'categories-versions', draft: true })
const res = await payload.find({
collection: 'categories-versions',
draft: true,
})

expect(
res.docs[0].relatedVersions.docs.some((x) => (x as TypeWithID).id === version.id),
Expand Down

0 comments on commit d0b5321

Please sign in to comment.