DennisSnijder / payload-workflow

A workflow plugin for Payload CMS

Home Page:https://github.com/DennisSnijder/payload-workflow

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lexorank Collision

zakinadhif opened this issue · comments

Hello!

I've been trying to replicate some of the mechanisms which are used here for my project. I've found which appears to be a strange behavior on the generateOrderRank hook, and I'm not sure if this is the expected output.

Basically, it appears that the hook gets stuck and generates the same lexorank multiple times because it received an input from a wrongly sorted query?

image

  const collectionData = await payload.find({
    collection: 'menu-items',
    sort: 'orderRank',
    limit: 1,
  });

  const lastOrderRank = collectionData.docs[0]?.orderRank ?? null;

  console.log("Last order rank:", lastOrderRank)

  const lastRank =
    lastOrderRank && typeof lastOrderRank === 'string'
      ? LexoRank.parse(lastOrderRank)
      : LexoRank.min()

  console.log("Last rank:", lastRank.toString());

  const nextRank = lastRank.genNext().genNext();
  data.orderRank = nextRank.toString();

  console.log("Next rank:", nextRank.toString());

image

I suspect it might be happening here too, since my code is nearly identical since I copied from here. The problem disappears when I flip the order of the sorting as such:

  sort: '-orderRank'

Thanks for making the plugin! I'll be using this as a reference on making my custom tree collection.

Interesting find! I'll take a look later this week! Thanks for the report!

@zakinadhif Thank you for looking into this! This bug actually resolved into the user being unable to sort their documents in some cases. This resolved that! 🥳

The fix is available in 0.3.2