Seed remote files

default discord avatar
francocdev
9 months 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
    Payload Team
    9 months 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
    francocdev
    9 months 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
    martin.rahbek
    9 months 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
    francocdev
    9 months ago

    Thanks!

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.