Files
payload/packages/drizzle/src/findOne.ts
Sasha 1b2b6a1b15 fix: respect draft: true when querying docs for the join field (#11763)
Previously, if you were querying a collection that has a join field with
`draft: true`, and the join field's collection also has
`versions.drafts: true` our db adapter would still query the original
SQL table / mongodb collection instead of the versions one which isn't
quite right since we respect `draft: true` when populating relationships
2025-03-24 09:49:30 -04:00

36 lines
901 B
TypeScript

import type { FindOneArgs, SanitizedCollectionConfig, TypeWithID } from 'payload'
import toSnakeCase from 'to-snake-case'
import type { DrizzleAdapter } from './types.js'
import { findMany } from './find/findMany.js'
export async function findOne<T extends TypeWithID>(
this: DrizzleAdapter,
{ collection, draftsEnabled, joins, locale, req, select, where }: FindOneArgs,
): Promise<T> {
const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collection].config
const tableName = this.tableNameMap.get(toSnakeCase(collectionConfig.slug))
const { docs } = await findMany({
adapter: this,
collectionSlug: collection,
draftsEnabled,
fields: collectionConfig.flattenedFields,
joins,
limit: 1,
locale,
page: 1,
pagination: false,
req,
select,
sort: undefined,
tableName,
where,
})
return docs?.[0] || null
}