Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/payload/src/collections/operations/findByID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { afterRead, type AfterReadArgs } from '../../fields/hooks/afterRead/inde
import { validateQueryPaths } from '../../index.js'
import { lockedDocumentsCollectionSlug } from '../../locked-documents/config.js'
import { appendNonTrashedFilter } from '../../utilities/appendNonTrashedFilter.js'
import { getSelectMode } from '../../utilities/getSelectMode.js'
import { killTransaction } from '../../utilities/killTransaction.js'
import { sanitizeSelect } from '../../utilities/sanitizeSelect.js'
import { replaceWithDraftIfAvailable } from '../../versions/drafts/replaceWithDraftIfAvailable.js'
Expand Down Expand Up @@ -140,6 +141,17 @@ export const findByIDOperation = async <
req,
})

let dbSelect = select

if (
collectionConfig.versions?.drafts &&
draftEnabled &&
select &&
getSelectMode(select) === 'include'
) {
dbSelect = { ...select, createdAt: true, updatedAt: true }
}

const findOneArgs: FindOneArgs = {
collection: collectionConfig.slug,
draftsEnabled: draftEnabled,
Expand All @@ -148,7 +160,7 @@ export const findByIDOperation = async <
req: {
transactionID: req.transactionID,
} as PayloadRequest,
select,
select: dbSelect,
where: fullWhere,
}

Expand Down
12 changes: 11 additions & 1 deletion packages/payload/src/globals/operations/findOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,21 @@ export const findOneOperation = async <T extends Record<string, unknown>>(
// Perform database operation
// /////////////////////////////////////

let dbSelect = select

if (
globalConfig.versions?.drafts &&
draftEnabled &&
select &&
getSelectMode(select) === 'include'
) {
dbSelect = { ...select, createdAt: true, updatedAt: true }
}
const docFromDB = await req.payload.db.findGlobal({
slug,
locale: locale!,
req,
select,
select: dbSelect,
where: overrideAccess ? undefined : (accessResult as Where),
})

Expand Down
28 changes: 14 additions & 14 deletions test/_community/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export interface Config {
'payload-migrations': PayloadMigrationsSelect<false> | PayloadMigrationsSelect<true>;
};
db: {
defaultIDType: string;
defaultIDType: number;
};
globals: {
menu: Menu;
Expand Down Expand Up @@ -126,7 +126,7 @@ export interface UserAuthOperations {
* via the `definition` "posts".
*/
export interface Post {
id: string;
id: number;
title?: string | null;
content?: {
root: {
Expand All @@ -151,7 +151,7 @@ export interface Post {
* via the `definition` "media".
*/
export interface Media {
id: string;
id: number;
updatedAt: string;
createdAt: string;
url?: string | null;
Expand Down Expand Up @@ -195,7 +195,7 @@ export interface Media {
* via the `definition` "payload-kv".
*/
export interface PayloadKv {
id: string;
id: number;
key: string;
data:
| {
Expand All @@ -212,7 +212,7 @@ export interface PayloadKv {
* via the `definition` "users".
*/
export interface User {
id: string;
id: number;
updatedAt: string;
createdAt: string;
email: string;
Expand All @@ -236,24 +236,24 @@ export interface User {
* via the `definition` "payload-locked-documents".
*/
export interface PayloadLockedDocument {
id: string;
id: number;
document?:
| ({
relationTo: 'posts';
value: string | Post;
value: number | Post;
} | null)
| ({
relationTo: 'media';
value: string | Media;
value: number | Media;
} | null)
| ({
relationTo: 'users';
value: string | User;
value: number | User;
} | null);
globalSlug?: string | null;
user: {
relationTo: 'users';
value: string | User;
value: number | User;
};
updatedAt: string;
createdAt: string;
Expand All @@ -263,10 +263,10 @@ export interface PayloadLockedDocument {
* via the `definition` "payload-preferences".
*/
export interface PayloadPreference {
id: string;
id: number;
user: {
relationTo: 'users';
value: string | User;
value: number | User;
};
key?: string | null;
value?:
Expand All @@ -286,7 +286,7 @@ export interface PayloadPreference {
* via the `definition` "payload-migrations".
*/
export interface PayloadMigration {
id: string;
id: number;
name?: string | null;
batch?: number | null;
updatedAt: string;
Expand Down Expand Up @@ -420,7 +420,7 @@ export interface PayloadMigrationsSelect<T extends boolean = true> {
* via the `definition` "menu".
*/
export interface Menu {
id: string;
id: number;
globalText?: string | null;
updatedAt?: string | null;
createdAt?: string | null;
Expand Down
33 changes: 33 additions & 0 deletions test/select/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,39 @@ describe('Select', () => {
expect(doc.version.text).toBe(post.text)
})

it('should return a latest version with findByID and draft: true', async () => {
const doc = await payload.create({
collection: 'versioned-posts',
data: { text: 'draft-post', _status: 'draft' },
draft: true,
})

const res = await payload.findByID({
collection: 'versioned-posts',
id: doc.id,
draft: true,
select: { text: true },
})
expect(res.text).toBe('draft-post')
await payload.update({
collection: 'versioned-posts',
id: doc.id,
data: { text: 'published', _status: 'published' },
})

const res_2 = await payload.findByID({
collection: 'versioned-posts',
id: doc.id,
draft: true,
select: { text: true },
})

expect(res_2).toStrictEqual({
text: 'published',
id: res_2.id,
})
})

it('should create versions with complete data when updating with select', async () => {
// First, update the post with select to only return the id field
const updatedPost = await payload.update({
Expand Down
30 changes: 30 additions & 0 deletions test/select/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export interface Config {
'relationships-blocks': RelationshipsBlock;
'custom-ids': CustomId;
users: User;
'payload-kv': PayloadKv;
'payload-locked-documents': PayloadLockedDocument;
'payload-preferences': PayloadPreference;
'payload-migrations': PayloadMigration;
Expand All @@ -97,13 +98,15 @@ export interface Config {
'relationships-blocks': RelationshipsBlocksSelect<false> | RelationshipsBlocksSelect<true>;
'custom-ids': CustomIdsSelect<false> | CustomIdsSelect<true>;
users: UsersSelect<false> | UsersSelect<true>;
'payload-kv': PayloadKvSelect<false> | PayloadKvSelect<true>;
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
'payload-migrations': PayloadMigrationsSelect<false> | PayloadMigrationsSelect<true>;
};
db: {
defaultIDType: string;
};
fallbackLocale: ('false' | 'none' | 'null') | false | null | ('en' | 'de') | ('en' | 'de')[];
globals: {
'global-post': GlobalPost;
'force-select-global': ForceSelectGlobal;
Expand Down Expand Up @@ -524,6 +527,23 @@ export interface User {
| null;
password?: string | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-kv".
*/
export interface PayloadKv {
id: string;
key: string;
data:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-locked-documents".
Expand Down Expand Up @@ -991,6 +1011,14 @@ export interface UsersSelect<T extends boolean = true> {
expiresAt?: T;
};
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-kv_select".
*/
export interface PayloadKvSelect<T extends boolean = true> {
key?: T;
data?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-locked-documents_select".
Expand Down Expand Up @@ -1031,6 +1059,7 @@ export interface GlobalPost {
id: string;
text?: string | null;
number?: number | null;
_status?: ('draft' | 'published') | null;
updatedAt?: string | null;
createdAt?: string | null;
}
Expand Down Expand Up @@ -1058,6 +1087,7 @@ export interface ForceSelectGlobal {
export interface GlobalPostSelect<T extends boolean = true> {
text?: T;
number?: T;
_status?: T;
updatedAt?: T;
createdAt?: T;
globalType?: T;
Expand Down
Loading