Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Understanding REST API Routing in Payload CMS

default discord avatar
zed0547last year
13

Hey

@311085554852823040



You want to see the source for where the endpoints are handled?



Basically all operations in payload are routed to one of these functions:


- Collections:

https://github.com/payloadcms/payload/tree/main/packages/payload/src/collections/operations

- Globals:

https://github.com/payloadcms/payload/tree/main/packages/payload/src/globals/operations

- Auth:

https://github.com/payloadcms/payload/tree/main/packages/payload/src/auth/operations

The "basis" of the endpoints is handled by the next package:

https://github.com/payloadcms/payload/blob/main/packages/next/src/routes/index.ts

The actual routing is handled by a payload core utility:

https://github.com/payloadcms/payload/blob/main/packages/payload/src/utilities/handleEndpoints.ts#L62

Let me know if you want more details on this, or what in particular you want more info on.



This was kind-of recently changed, as before most of this aside from the operations was handled by the

next

package.

  • default discord avatar
    diorculalast year

    Oeh thanks man!!

  • default discord avatar
    zed0547last year

    My pleasure

  • default discord avatar
    diorculalast year

    would this be a correct component diagram of payload and payload core?

  • default discord avatar
    zed0547last year

    Kind of yeah, the local api and the rest api use the same operations under the hood, it's just the routing that is slightly different

  • default discord avatar
    diorculalast year
    @654031862146007055

    cool cool, but what i can't wrap my head around is

    how

    actually data is send to the database for example, i have it now as a content manager, but basically that would be Nextjs/Payload core utility combined with the schema definition of collections to check?

  • default discord avatar
    zed0547last year

    All operations eventually get to a

    db

    call. Take the

    create

    operation for example, it does some processing, calls some hooks, does some more processing, then it gets to this line:

    https://github.com/payloadcms/payload/blob/main/packages/payload/src/collections/operations/create.ts#L269

    This is a call to the underlying db adapter directly. DB adapters, as the name suggests, use the adapter pattern to map these calls to actual db operations - in the case of

    create

    , it calls the adapters own

    create

    method. Let's say we are using

    db-postgres

    , then at this point the data that we passed up to now gets transformed for writing (to better represent the actual columns we are writing), and then a special function called

    upsertRow

    is called that updates or inserts this row in the actual database:

    https://github.com/payloadcms/payload/blob/main/packages/drizzle/src/create.ts#L19

    How do we even get the

    db

    object though? The

    db

    object gets assigned to

    payload

    (The

    BasePayload

    instance) via initialization by using the config you passed with your chosen adapter, simple as that. Whenever you get a

    payload

    instance, for example from a

    req

    , we are always referring to the same cached

    BasePayload

    object.

  • default discord avatar
    diorculalast year

    ah ah i see, let me work that out real quick!



    @654031862146007055

    would this be a better component representation?

  • default discord avatar
    zed0547last year

    It's better, in your case the Database Adapter will be mongoose (db-mongodb) but if you want something more representative of the actual flow then it would look a little more linear I think:



    Admin ui -----
    |            |
    |            |
    v            |
    API Handler  | 
    |            |
    |            |
    v            v
    Core Operations / Local API
    |
    |
    v
    DB Adapter
    ...
  • default discord avatar
    diorculalast year

    ah so the admin ui skips the api handler (like the local api goes straight to the database)



    thanks btw for the time man, when i am finished i will be happy to share my project 🙂

  • default discord avatar
    zed0547last year

    Yes, at the end of the day the REST API basically just calls the same operations as the Local API

  • default discord avatar
    diorculalast year
    @654031862146007055

    i know it is quite late (where i live) but i am confused still a bit. There is a Payload Core utility that handles requests received through the Nextjs endpoints. sort of.



    But then there is also Node.js? How does this fit in everything?

  • default discord avatar
    zed0547last year

    Nodejs is a Javascript runtime environment, Next.js needs a runtime environment outside of the browser for certain processes, for example running an endpoint from the context of a server happens in Nodejs

  • default discord avatar
    diorculalast year

    copy that captain, my thanks

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.