Versions. If all documents are draft, why are latest changes only in versions collection?

default discord avatar
kaspartr
6 months ago
4

When all document versions are _status: "Draft", why aren't the latest changes reflected in the main collection but only in the versions collection?



This causes some issues down the line

.


For example, when I am creating a document with a reference field to another collection, and the reference is to a document where all versions are drafts, then inside the hooks, the referenced document doesn't have the latest state, but rather the origin state (state at creation) because that is the sate stored in the main collection.



I'm trying to understand this design principle and decide if I'm using versions wrongly and should disable drafts.

  • default discord avatar
    martin.rahbek
    6 months ago

    Sorry if I don't understand your question correctly.



    You need to set the state yourself, so you can have a collection that is not published and only is a draft.



    {


          name: 'status',


          type: 'select',


          options: [


            {


              value: 'draft',


              label: 'Draft',


            },


            {


              value: 'published',


              label: 'Published',


            },


          ],


          defaultValue: 'draft',


          admin: {


            position: 'sidebar',


          }


    }

  • default discord avatar
    kaspartr
    6 months ago

    As-is: When all your document versions are in draft, only the origin state (at creation) is stored in DB at the main collection document, all the later stages (save as draft) are stored in _versions document.



    consequences: when one collection references another via the reference field, then accessing the data of the referenced collection, you get the origin state, not the latest draft state.



    solution (ugly): create a function to return the latest version of the referenced collection before using the referenced collection data.

  • default discord avatar
    martin.rahbek
    6 months ago

    Arghh 😄 think I miss understood versions. According to the docs, there should also be a changed status if the draft is newer than the published one; I can't get that working.



    Do you want the relationship to be the newest draft, not the published one?

  • default discord avatar
    kaspartr
    6 months ago

    In this context, there are no published versions. All are draft.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.