Compare commits

...

1 Commits

Author SHA1 Message Date
Jarrod Flesch
8145743d72 chore: parallelizes versions queries for docs 2024-11-23 01:00:55 -05:00

View File

@@ -24,9 +24,6 @@ type Result = Promise<{
unpublishedVersionCount: number
versionCount: number
}>
// TODO: in the future, we can parallelize some of these queries
// this will speed up the API by ~30-100ms or so
export const getVersions = async ({
id: idArg,
collectionConfig,
@@ -37,7 +34,6 @@ export const getVersions = async ({
user,
}: Args): Result => {
const id = sanitizeID(idArg)
let publishedQuery
let hasPublishedDoc = false
let mostRecentVersionIsAutosaved = false
let unpublishedVersionCount = 0
@@ -69,43 +65,40 @@ export const getVersions = async ({
}
}
if (versionsConfig?.drafts) {
publishedQuery = await payload.find({
collection: collectionConfig.slug,
depth: 0,
locale: locale || undefined,
user,
where: {
and: [
{
or: [
{
_status: {
equals: 'published',
const [publishedQuery, mostRecentVersion, versionsQuery] = await Promise.all([
versionsConfig.drafts &&
payload.find({
collection: collectionConfig.slug,
depth: 0,
locale: locale || undefined,
user,
where: {
and: [
{
or: [
{
_status: {
equals: 'published',
},
},
},
{
_status: {
exists: false,
{
_status: {
exists: false,
},
},
},
],
},
{
id: {
equals: id,
],
},
},
],
},
})
if (publishedQuery.docs?.[0]) {
hasPublishedDoc = true
}
if (versionsConfig.drafts?.autosave) {
const mostRecentVersion = await payload.findVersions({
{
id: {
equals: id,
},
},
],
},
}),
versionsConfig.drafts &&
versionsConfig.drafts?.autosave &&
(await payload.findVersions({
collection: collectionConfig.slug,
depth: 0,
limit: 1,
@@ -119,20 +112,30 @@ export const getVersions = async ({
},
],
},
})
})),
await payload.countVersions({
collection: collectionConfig.slug,
depth: 0,
user,
where: {
and: [
{
parent: {
equals: id,
},
},
],
},
}),
])
if (
mostRecentVersion.docs[0] &&
'autosave' in mostRecentVersion.docs[0] &&
mostRecentVersion.docs[0].autosave
) {
mostRecentVersionIsAutosaved = true
}
}
if (publishedQuery?.docs?.[0]) {
hasPublishedDoc = true
if (publishedQuery.docs?.[0]?.updatedAt) {
if (publishedQuery.docs[0].updatedAt) {
;({ totalDocs: unpublishedVersionCount } = await payload.countVersions({
collection: collectionConfig.slug,
depth: 0,
user,
where: {
and: [
@@ -157,81 +160,86 @@ export const getVersions = async ({
}
}
;({ totalDocs: versionCount } = await payload.countVersions({
collection: collectionConfig.slug,
user,
where: {
and: [
{
parent: {
equals: id,
},
},
],
},
}))
if (
mostRecentVersion &&
mostRecentVersion.docs[0] &&
'autosave' in mostRecentVersion.docs[0] &&
mostRecentVersion.docs[0].autosave
) {
mostRecentVersionIsAutosaved = true
}
if (versionsQuery && versionsQuery?.totalDocs) {
versionCount = versionsQuery.totalDocs
}
}
if (globalConfig) {
if (versionsConfig?.drafts) {
publishedQuery = await payload.findGlobal({
slug: globalConfig.slug,
depth: 0,
locale,
user,
})
if (publishedQuery?._status === 'published') {
hasPublishedDoc = true
}
if (versionsConfig.drafts?.autosave) {
const mostRecentVersion = await payload.findGlobalVersions({
const [publishedQuery, mostRecentVersion, versionsQuery] = await Promise.all([
versionsConfig.drafts &&
(await payload.findGlobal({
slug: globalConfig.slug,
depth: 0,
locale,
user,
})),
versionsConfig.drafts &&
versionsConfig.drafts?.autosave &&
(await payload.findGlobalVersions({
slug: globalConfig.slug,
limit: 1,
select: {
autosave: true,
},
user,
})
})),
await payload.countGlobalVersions({
depth: 0,
global: globalConfig.slug,
user,
}),
])
if (
mostRecentVersion.docs[0] &&
'autosave' in mostRecentVersion.docs[0] &&
mostRecentVersion.docs[0].autosave
) {
mostRecentVersionIsAutosaved = true
}
}
if (publishedQuery?._status === 'published') {
hasPublishedDoc = true
}
if (publishedQuery?.updatedAt) {
;({ totalDocs: unpublishedVersionCount } = await payload.countGlobalVersions({
depth: 0,
global: globalConfig.slug,
user,
where: {
and: [
{
'version._status': {
equals: 'draft',
},
},
{
updatedAt: {
greater_than: publishedQuery.updatedAt,
},
},
],
},
}))
if (versionsConfig.drafts && versionsConfig.drafts?.autosave) {
if (
mostRecentVersion &&
mostRecentVersion.docs[0] &&
'autosave' in mostRecentVersion.docs[0] &&
mostRecentVersion.docs[0].autosave
) {
mostRecentVersionIsAutosaved = true
}
}
;({ totalDocs: versionCount } = await payload.countGlobalVersions({
depth: 0,
global: globalConfig.slug,
user,
}))
if (publishedQuery?.updatedAt) {
;({ totalDocs: unpublishedVersionCount } = await payload.countGlobalVersions({
depth: 0,
global: globalConfig.slug,
user,
where: {
and: [
{
'version._status': {
equals: 'draft',
},
},
{
updatedAt: {
greater_than: publishedQuery.updatedAt,
},
},
],
},
}))
}
if (versionsQuery && versionsQuery?.totalDocs) {
versionCount = versionsQuery.totalDocs
}
}
return {