Relationship Object String to ObjectID

default discord avatar
rarciflast year
22

Hi,



I have noticed that the object ID is stored as a string in MongoDB. Is there a way to store it as ObjectID? It would be easier if the type is ObjectID for easier lookups.



I am referring to the tutorial here:



https://payloadcms.com/docs/fields/relationship


Anyone?

  • discord user avatar
    jmikrut
    last year

    Hey @rarcif — we store object IDs as strings intentionally, and there is no real easy way to store them as full ObjectIDs. This is for a few reasons, but we could certainly evaluate a way to be able to "opt in" to ObjectIDs instead of string representations in a new feature



    there are a few places in the code that would need to be adjusted, but technically, this could be doable with a PR



    right now, it's not possible though

  • default discord avatar
    gokulscklast year

    @jmikrut Is there a way to handle this with custom code? Example, by using hooks?

  • discord user avatar
    jmikrut
    last year

    probably yes



    maybe a

    beforeChange

    collection hook and a

    beforeRead

    collection hook



    the

    beforeChange

    to transform any relationship field string IDs to ObjectIDs before storage, and the

    beforeRead

    to convert them back to strings



    Hey @gokulsck — I just looked into this and built a plugin to do exactly what you thought - - just add a field hook to each relationship / upload field to convert to ObjectIDs before storage:



    https://www.npmjs.com/package/@payloadcms/plugin-relationship-object-ids


    nice and simple 👍 good question! this is a nice thing to have!

  • default discord avatar
    gokulscklast year

    Thanks will check it...



    @jmikrut This solution is working. Thanks for the quick fix. Still just wanted to know if this is under testing or not? Because I did not see the details updated in the payload documentation of the relationship. Could you confirm this is an official release or not?

  • discord user avatar
    jmikrut
    last year

    This is an official release, but it is a plugin, so it won't be listed in the Payload docs directly as it's not a part of "core" specifically. But it is officially supported and will continue to be



    in the future, we will be listing all of our official plugins in the Payload docs, and this one will be listed as well

  • default discord avatar
    gokulscklast year

    Hey @jmikrut I have noticed an issue, the filter is not working as expected for relationship field. Could you confirm that, the plugin you implemented works or not for relationship fields filtering?

  • discord user avatar
    jmikrut
    last year

    let me check on this immediately

  • discord user avatar
    denolfe
    last year

    @gokulsck It appears relationship filtering is not working as expected. Looking into a fix now.



    Issue logged here. Expect this to be taken care of in a day or two.


    https://github.com/payloadcms/plugin-relationship-object-ids/issues/1
  • discord user avatar
    jmikrut
    last year

    hey @gokulsck - just a heads-up, we fixed this!



    you shouldn't need to update your plugin, instead, you should just update your Payload version to the newest



    i also added a test suite to the plugin to make sure that it's nice and covered

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

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