Skip to content

Commit 8daac4e

Browse files
authored
fix: properly store timestamps in versions (#8646)
This PR makes a more clear gap between `version_createdAt` / `version_updatedAt` and `createdAt` / `updatedAt` columns / fields in mongodb. - `createdAt` - This should be a new value in a new version. Before this change it was the same all the time. Should remain the same on autosave. - The same for `updatedAt`, but it should be updated on every change (including autosave) - `version_createdAt` - Should remain equal to `createdAt` from the parent collection / table - `version_updatedAt` - On a latest version it makes sense this be the same as `updatedAt` from the parent collection / table, as all the `version_*` fields should be just synced with it
1 parent 067d353 commit 8daac4e

File tree

15 files changed

+128
-48
lines changed

15 files changed

+128
-48
lines changed

packages/db-mongodb/src/queryDrafts.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ export const queryDrafts: QueryDrafts = async function queryDrafts(
9999
_id: doc.parent,
100100
id: doc.parent,
101101
...doc.version,
102-
createdAt: doc.createdAt,
103-
updatedAt: doc.updatedAt,
104102
}
105103

106104
return sanitizeInternalFields(doc)

packages/drizzle/src/createGlobalVersion.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ export async function createGlobalVersion<T extends TypeWithID>(
1212
this: DrizzleAdapter,
1313
{
1414
autosave,
15+
createdAt,
1516
globalSlug,
1617
publishedLocale,
1718
req = {} as PayloadRequest,
1819
snapshot,
20+
updatedAt,
1921
versionData,
2022
}: CreateGlobalVersionArgs,
2123
) {
@@ -28,9 +30,11 @@ export async function createGlobalVersion<T extends TypeWithID>(
2830
adapter: this,
2931
data: {
3032
autosave,
33+
createdAt,
3134
latest: true,
3235
publishedLocale,
3336
snapshot,
37+
updatedAt,
3438
version: versionData,
3539
},
3640
db,

packages/drizzle/src/createVersion.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ export async function createVersion<T extends TypeWithID>(
1313
{
1414
autosave,
1515
collectionSlug,
16+
createdAt,
1617
parent,
1718
publishedLocale,
1819
req = {} as PayloadRequest,
1920
snapshot,
21+
updatedAt,
2022
versionData,
2123
}: CreateVersionArgs<T>,
2224
) {
@@ -33,17 +35,15 @@ export async function createVersion<T extends TypeWithID>(
3335

3436
const data: Record<string, unknown> = {
3537
autosave,
38+
createdAt,
3639
latest: true,
3740
parent,
3841
publishedLocale,
3942
snapshot,
43+
updatedAt,
4044
version,
4145
}
4246

43-
if ('createdAt' in version) {
44-
data.createdAt = version.createdAt
45-
}
46-
4747
const result = await upsertRow<TypeWithVersion<T>>({
4848
adapter: this,
4949
data,

packages/drizzle/src/queryDrafts.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ export const queryDrafts: QueryDrafts = async function queryDrafts(
3838
doc = {
3939
id: doc.parent,
4040
...doc.version,
41-
createdAt: doc.createdAt,
42-
updatedAt: doc.updatedAt,
4341
}
4442

4543
return doc

packages/payload/src/collections/operations/duplicate.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,7 @@ export const duplicateOperation = async <TSlug extends CollectionSlug>(
267267
result = await saveVersion({
268268
id: versionDoc.id,
269269
collection: collectionConfig,
270-
docWithLocales: {
271-
...versionDoc,
272-
createdAt: result.createdAt,
273-
},
270+
docWithLocales: versionDoc,
274271
draft: shouldSaveDraft,
275272
payload,
276273
req,

packages/payload/src/collections/operations/find.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export const findOperation = async <TSlug extends CollectionSlug>(
131131
page: sanitizedPage,
132132
pagination: usePagination,
133133
req,
134-
sort: getQueryDraftsSort(sort),
134+
sort: getQueryDraftsSort({ collectionConfig, sort }),
135135
where: fullWhere,
136136
})
137137
} else {

packages/payload/src/collections/operations/update.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,7 @@ export const updateOperation = async <TSlug extends CollectionSlug>(
329329
result = await saveVersion({
330330
id,
331331
collection: collectionConfig,
332-
docWithLocales: {
333-
...result,
334-
createdAt: doc.createdAt,
335-
},
332+
docWithLocales: result,
336333
payload,
337334
req,
338335
})

packages/payload/src/collections/operations/updateByID.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,7 @@ export const updateByIDOperation = async <TSlug extends CollectionSlug>(
356356
id,
357357
autosave,
358358
collection: collectionConfig,
359-
docWithLocales: {
360-
...result,
361-
createdAt: docWithLocales.createdAt,
362-
},
359+
docWithLocales: result,
363360
draft: shouldSaveDraft,
364361
payload,
365362
publishSpecificLocale,

packages/payload/src/globals/operations/update.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,7 @@ export const updateOperation = async <TSlug extends GlobalSlug>(
245245
const { globalType } = result
246246
result = await saveVersion({
247247
autosave,
248-
docWithLocales: {
249-
...result,
250-
createdAt: result.createdAt,
251-
updatedAt: result.updatedAt,
252-
},
248+
docWithLocales: result,
253249
draft: shouldSaveDraft,
254250
global: globalConfig,
255251
payload,

packages/payload/src/versions/drafts/getQueryDraftsSort.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
import type { SanitizedCollectionConfig } from '../../collections/config/types.js'
2+
13
/**
24
* Takes the incoming sort argument and prefixes it with `versions.` and preserves any `-` prefixes for descending order
35
* @param sort
46
*/
5-
export const getQueryDraftsSort = (sort: string): string => {
7+
export const getQueryDraftsSort = ({
8+
collectionConfig,
9+
sort,
10+
}: {
11+
collectionConfig: SanitizedCollectionConfig
12+
sort: string
13+
}): string => {
614
if (!sort) {
7-
return sort
15+
if (collectionConfig.defaultSort) {
16+
sort = collectionConfig.defaultSort
17+
} else {
18+
sort = '-createdAt'
19+
}
820
}
921

1022
let direction = ''

0 commit comments

Comments
 (0)