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.

Is there a way to automatically seed on first startup (v3)

default discord avatar
lzyac8 months ago
4

I want to check if some collections exist on the database, and if not, seed those collections with some pre-existing static files stored locally.



How can I achieve this in Payload v3? Do I have to build into a custom server?

  • default discord avatar
    lioloc8 months ago

    payload.config.ts has a property onInit you can use, for instance:



      async onInit(payload) {
        const existingUsers = await payload.find({
          collection: adminCollection.slug,
          limit: 1,
        })
    
        if (existingUsers.docs.length === 0) {
          // run seed code
        )
      )
  • default discord avatar
    gfwgfw8 months ago

    But onInit only execute when admin page is visited or restful api is called

  • default discord avatar
    lioloc8 months ago

    It also runs when you visit your frontend



    I mean I'm using payload 3.0 beta, should precise that, so its coupled with nextjs

  • default discord avatar
    lzyac8 months ago

    I see



    this is good to know

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.