Integration with existing Nest.js project

default discord avatar
sr.amorzinholast year
11

I have a working Nest.js project, and i want to have payload integrated to it. Is there a way to do this ?

  • default discord avatar
    paulpopuslast year

    Technically yes, I'm not a Nest expert but both run on Node so you'll be fine.


    When you say you want payload integrated into it, in what form?



    You'll have your work cutout for you to make Nest handle

    everything

    but I would start at looking at an expressjs integration as Payload essentially runs on that



    So you could configure an expressjs server and run Payload inside that, copy the default server.ts and only use the parts where you're bootstrapping payload



    I would leave the build part up to payload and keep it in its own directory though



    This is kinda how I'd go about it, but I'm not a nestjs dev so I cant say if there's a better way



    The only other thing you may want to do is figure out how to inject the payload instance as a dependency, I'd do that part with a standalone service that imports payload and then exports it so you can re-use it and just make sure that expressjs runs first ahead of all your modules.



    But again I want to stress why you want to integrate Payload into Nest?

  • default discord avatar
    sr.amorzinholast year

    Its just to try it out. The codebase is relative relative new, but its a migration in progress, so to start again would be troblesome at first. But with what said taken in perspective, its easier to just start again in express



    Thanks

  • default discord avatar
    paulpopuslast year

    If it's not too big then yeah you can start a new, Payload has full support for anything custom in the backend or with expressjs you might need



    Otherwise you can do the steps I outlined and then slowly migrate stuff to let payload handle it until it handles everything then you can just move your codebase out

  • default discord avatar
    sentumlast year

    We actually did exactly this - integrated Payload into Nest as a module, with /cms/* requests going to Payload, /api/* going to Nest routes, and having Payload as an injectable dependency for Nest. If you want, I can push a reference implementation to Github and share it with you



    Here we go:

    https://github.com/CloudeyIT/payload-nestjs
  • default discord avatar
    paulpopuslast year

    nice, i added it to madewithpayload.com/templates

  • default discord avatar
    sentumlast year

    The motivation for me was to be able to use the DI, config, etc. functions of Nest in API routes while also being able to use Payload for the admin and ORM(-ish) functionality. Seemed like a good combo. But ultimately we went with pure Payload for the project at hand, since it turned out that not many custom API routes were actually needed. Might still use it for a future project, though, since the concept works quite well.



    Awesome! Hopefully it comes in handy to someone

  • default discord avatar
    javierlinked.ethlast year

    Thanks for that @sentum. I'm having a hard time to make admin front end to be built on a nestjs-payload codebase. I mean

    payload build

    . I guess there is some clash on tsconfig files. Have you tried?

  • default discord avatar
    sentumlast year

    I had to re-export some stuff from Payload to make it work without compiler errors, see here

    https://github.com/CloudeyIT/payload-nestjs/blob/master/src/cms/hooks/payload.hooks.ts

    You might have to do that for some other things too. I think the problem is ESM vs CJS modules, Nest uses one while Payload uses the other

  • default discord avatar
    javierlinked.ethlast year

    I appreciate your help! I noticed

    /graphql

    and

    /graphql-playground

    are not working in your implementation. Do you thing there's a clash in routers?

  • default discord avatar
    sentumlast year

    In

    payload.config.ts

    they are reconfigured to

    /cms/graphql

    and

    /cms/playground

    instead, to keep everything under the same namespace

  • default discord avatar
    thgh.11 months ago

    @sentum thank you very much, that repo helped a lot!

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.