Files
payload/packages/drizzle/src/queryDrafts.ts
Dan Ribbens d03658de01 feat: join field with polymorphic relationships (#9990)
### What?
The join field had a limitation imposed that prevents it from targeting
polymorphic relationship fields. With this change we can support any
relationship fields.

### Why?
Improves the functionality of join field.

### How?
Extended the database adapters and removed the config sanitization that
would throw an error when polymorphic relationships were used.

Fixes #
2024-12-19 22:34:52 +00:00

51 lines
1.2 KiB
TypeScript

import type { QueryDrafts, SanitizedCollectionConfig } from 'payload'
import { buildVersionCollectionFields, combineQueries } from 'payload'
import toSnakeCase from 'to-snake-case'
import type { DrizzleAdapter } from './types.js'
import { findMany } from './find/findMany.js'
export const queryDrafts: QueryDrafts = async function queryDrafts(
this: DrizzleAdapter,
{ collection, joins, limit, locale, page = 1, pagination, req, select, sort, where },
) {
const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collection].config
const tableName = this.tableNameMap.get(
`_${toSnakeCase(collectionConfig.slug)}${this.versionsSuffix}`,
)
const fields = buildVersionCollectionFields(this.payload.config, collectionConfig, true)
const combinedWhere = combineQueries({ latest: { equals: true } }, where)
const result = await findMany({
adapter: this,
collectionSlug: collection,
fields,
joins,
limit,
locale,
page,
pagination,
req,
select,
sort,
tableName,
versions: true,
where: combinedWhere,
})
return {
...result,
docs: result.docs.map((doc) => {
doc = {
id: doc.parent,
...doc.version,
}
return doc
}),
}
}