fix(db-postgres): sorting on versions (#3611)
* fix(db-postgres): WIP sorting on versions * fix: sorting collections with drafts * chore: getQueryDraftsSort readability
This commit is contained in:
@@ -150,7 +150,10 @@ export const findMany = async function find({
|
||||
const countResult = await chainMethods({
|
||||
methods: selectCountMethods,
|
||||
query: db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.select({
|
||||
count: sql<number>`count
|
||||
(*)`,
|
||||
})
|
||||
.from(table)
|
||||
.where(where),
|
||||
})
|
||||
|
||||
@@ -12,6 +12,7 @@ import { initTransaction } from '../../utilities/initTransaction'
|
||||
import { killTransaction } from '../../utilities/killTransaction'
|
||||
import { buildVersionCollectionFields } from '../../versions/buildCollectionFields'
|
||||
import { appendVersionToQueryKey } from '../../versions/drafts/appendVersionToQueryKey'
|
||||
import { getQueryDraftsSort } from '../../versions/drafts/getQueryDraftsSort'
|
||||
import { buildAfterOperation } from './utils'
|
||||
|
||||
export type Arguments = {
|
||||
@@ -127,7 +128,7 @@ async function find<T extends TypeWithID & Record<string, unknown>>(
|
||||
page: sanitizedPage,
|
||||
pagination: usePagination,
|
||||
req,
|
||||
sort,
|
||||
sort: getQueryDraftsSort(sort),
|
||||
where: fullWhere,
|
||||
})
|
||||
} else {
|
||||
|
||||
17
packages/payload/src/versions/drafts/getQueryDraftsSort.ts
Normal file
17
packages/payload/src/versions/drafts/getQueryDraftsSort.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Takes the incoming sort argument and prefixes it with `versions.` and preserves any `-` prefixes for descending order
|
||||
* @param sort
|
||||
*/
|
||||
export const getQueryDraftsSort = (sort: string): string => {
|
||||
if (!sort) return sort
|
||||
|
||||
let direction = ''
|
||||
let orderBy = sort
|
||||
|
||||
if (sort[0] === '-') {
|
||||
direction = '-'
|
||||
orderBy = sort.substring(1)
|
||||
}
|
||||
|
||||
return `${direction}version.${orderBy}`
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import { initPayloadTest } from '../helpers/configHelpers'
|
||||
import AutosavePosts from './collections/Autosave'
|
||||
import configPromise from './config'
|
||||
import AutosaveGlobal from './globals/Autosave'
|
||||
import { autosaveSlug } from './shared'
|
||||
import { autosaveSlug, draftSlug } from './shared'
|
||||
|
||||
let collectionLocalPostID: string
|
||||
let collectionLocalVersionID
|
||||
@@ -200,6 +200,44 @@ describe('Versions', () => {
|
||||
expect(versions.docs[0].version.title.en).toStrictEqual(newEnglishTitle)
|
||||
expect(versions.docs[0].version.title.es).toStrictEqual(spanishTitle)
|
||||
})
|
||||
|
||||
it('should query drafts with sort', async () => {
|
||||
const draftsAscending = await payload.find({
|
||||
collection: draftSlug,
|
||||
draft: true,
|
||||
sort: 'title',
|
||||
})
|
||||
const draftsDescending = await payload.find({
|
||||
collection: draftSlug,
|
||||
draft: true,
|
||||
sort: '-title',
|
||||
})
|
||||
|
||||
expect(draftsAscending).toBeDefined()
|
||||
expect(draftsDescending).toBeDefined()
|
||||
expect(draftsAscending.docs[0]).toMatchObject(
|
||||
draftsDescending.docs[draftsDescending.docs.length - 1],
|
||||
)
|
||||
})
|
||||
|
||||
it('should findVersions with sort', async () => {
|
||||
const draftsAscending = await payload.findVersions({
|
||||
collection: draftSlug,
|
||||
draft: true,
|
||||
sort: 'createdAt',
|
||||
})
|
||||
const draftsDescending = await payload.findVersions({
|
||||
collection: draftSlug,
|
||||
draft: true,
|
||||
sort: '-updatedAt',
|
||||
})
|
||||
|
||||
expect(draftsAscending).toBeDefined()
|
||||
expect(draftsDescending).toBeDefined()
|
||||
expect(draftsAscending.docs[0]).toMatchObject(
|
||||
draftsDescending.docs[draftsDescending.docs.length - 1],
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Restore', () => {
|
||||
|
||||
Reference in New Issue
Block a user