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.

Seed remote files

default discord avatar
francocdev2 years ago
4

Hi, it's possible to seed data from a remote url?


I'm migrating a website with 50k+ posts to payload, but can't find a way to upload the images of each post with the previous url.


And also, I would like to know how to transform text build in plain html (Only <p> tags) to the format that require payload to seed the data.


Thanks!

  • discord user avatar
    jmikrut
    2 years ago

    Yes, you can do this for sure



    you can write a simple node script to seed data using payload's local API



    and you can even upload files with the local api as well



    const payload = require('payload');
    
    require('dotenv').config();
    
    const {
      PAYLOAD_SECRET_KEY,
      MONGO_URL,
    } = process.env;
    
    payload.init({
      secret: PAYLOAD_SECRET_KEY,
      mongoURL: MONGO_URL,
      local: true,
    })
    
    const seed = async () => {
      // fetch your documents
      const docs = await fetch('https://my-api.com').then(res => res.json())
    
      const promises = docs.map(async (doc) => {
        await payload.create({
          collection: 'docs',
          data: doc
        });
      })
    
      await Promise.all(promises)
    
      process.exit(0);
    }
    
    seed();


    something like that



    and then you'd call the script like

    node seed.js

    or whatever your file is called

  • default discord avatar
    francocdev2 years ago

    I understand the use of the local API, but I got problems to upload my old images to /media.


    Also I realize that the tree schema of rich content is really different from my old <p> website structure



    Sorry if I didn't communicate well on the first message. I'm not native, so it's probably unclear.

  • default discord avatar
    Deleted User2 years ago

    const result = await payload.create({


        collection: "media",


        data: {


          alt: "your alt description",


        },


        filePath: path.resolve(__dirname, "./assets/logo.png"),


      });



    You can transform your data at

    https://my-api.com/your-transform

    to fit payload cms.

  • default discord avatar
    francocdev2 years ago

    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.