How to extend global config?

default discord avatar
jacko495 months ago
7

Hey!


I am trying to set the access permissions across all of my globals, does anyone know how to do this without having to paste the code into each one? Would additionally like to add a field at this stage.



Have tried a couple methods but feel like I'm missing something obvious.



Thanks!

  • default discord avatar
    tinouti5 months ago

    Just put it into a variable. 😊



    eg:


    const myCustomAccess = ({ req: { user } }) => {
      return Boolean(user)
    }
    export default buildConfig({
      globals: [
        {
          slug: 'global1',
          access: myCustomAccess,
          fields: []
        },
        {
          slug: 'global2',
          access: myCustomAccess,
          fields: []
        },
      ]
    })
  • default discord avatar
    nvskx5 months ago

    how would you override it?

  • default discord avatar
    tinouti5 months ago

    Like if for some globals or collections you want to have almost the same logic, but slightly different?

  • default discord avatar
    nvskx5 months ago

    yes, for example I want some specific collections to have a different access

  • default discord avatar
    jacko495 months ago

    Hey thanks for this, I think I was more wondering if I can just set it once and forget, with the proposed method I would still have to fill out 20 or more objects


    appreciate the response :payloadlove:

  • default discord avatar
    tinouti5 months ago

    @Nvskx The beauty of Payload it's that it's all just Typescript, so however you would abstract/override things in Javascript, that's how you're gonna be doing it in Payload. So for example, if only

    some

    of your access logic is reusable, abstract that piece into it's own function and call it where needed.



    @Jacko depends on your needs really. For example, if you have 5 globals that all use pretty much the same config, you could make a constructor function. eg:


    export const baseGlobal = (slug: string): GlobalConfig => {
      return {
        slug,
        access: () => {}
        fields: []
      }
    }

    and then use it like this, for example:


    const globals: string[] = ['global1', 'global2', 'global3', 'global4', 'global5'];
    export default buildConfig({
      globals: globals.map((slug) => baseGlobal(slug))
    })
  • default discord avatar
    jacko495 months ago

    Leaving this for anyone else, in my case I was adding a tenant field and permissions to all my globals:


    export const baseGlobal = (existingConfig: GlobalConfig): GlobalConfig => {
        return {
            slug: existingConfig.slug,
            access: {
                read: isSuperAdminOrHasSiteAccess(),
                update: isSuperAdminOrHasSiteAccess()
            },
            fields: [
                {
                    name: "tenant",
                    type: "relationship",
                    relationTo: "tenants",
                    admin: {
                        hidden: true
                    }
                },
                ...existingConfig.fields
            ]
        }   
    }


    Thanks again @Tinouti !

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.