Skip to content

Commit

Permalink
Update mutations should not patch fields
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Dec 13, 2024
1 parent 3c8cd31 commit c7450ad
Showing 1 changed file with 22 additions and 32 deletions.
54 changes: 22 additions & 32 deletions src/core/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
} from './util/EntryFilenames.js'
import {createEntryRow, entryParentPaths} from './util/EntryRows.js'
import {generateKeyBetween} from './util/FractionalIndexing.js'
import {entries, fromEntries} from './util/Objects.js'
import {basename, extname, join, normalize} from './util/Paths.js'
import {slugify} from './util/Slugs.js'

Expand Down Expand Up @@ -189,63 +188,54 @@ export class UpdateOperation<Definition> extends Operation {
else if (changeStatus === EntryStatus.Archived) status = 'preferPublished'
else if (changeStatus === EntryStatus.Published) status = 'preferDraft'
else status = 'preferPublished'
const entry = await cms.get({
const current = await cms.get({
select: Entry,
id: entryId,
locale: query.locale,
status
})
const parent = entry.parentId
const parent = current.parentId
? await cms.get({
select: Entry,
id: entry.parentId,
id: current.parentId,
locale: query.locale,
status: 'preferPublished'
})
: undefined
const parentPaths = entryParentPaths(cms.config, entry)
const parentPaths = entryParentPaths(cms.config, current)

const file = entryFileName(
cms.config,
{...entry, status: entry.status},
{...current, status: current.status},
parentPaths
)
const type = cms.config.schema[entry.type]
const type = cms.config.schema[current.type]
const mutations: Array<Mutation> = []
const createDraft = changeStatus === EntryStatus.Draft
if (createDraft)
const entry = await createEntry(
cms.config,
this.typeName(cms.config, type),
{
...current,
status: EntryStatus.Draft,
data: {...current.data, ...set}
},
parent
)
if (createDraft || set)
mutations.push({
type: MutationType.Edit,
entryId: entryId,
locale: entry.locale,
file,
entry: await createEntry(
cms.config,
this.typeName(cms.config, type),
{
...entry,
status: EntryStatus.Draft,
data: {...entry.data, ...set}
},
parent
)
})
else if (set)
mutations.push({
type: MutationType.Patch,
entryId: entryId,
locale: entry.locale,
locale: current.locale,
file,
patch: fromEntries(
entries(set).map(([key, value]) => [key, value ?? null])
)
entry
})
switch (changeStatus) {
case EntryStatus.Published:
mutations.push({
type: MutationType.Publish,
locale: entry.locale,
status: entry.status,
locale: current.locale,
status: current.status,
entryId: entryId,
file
})
Expand All @@ -254,7 +244,7 @@ export class UpdateOperation<Definition> extends Operation {
mutations.push({
type: MutationType.Archive,
entryId: entryId,
locale: entry.locale,
locale: current.locale,
file
})
break
Expand Down

0 comments on commit c7450ad

Please sign in to comment.