Seed remote files

default discord avatar
francocdevlast year
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
    last year

    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
    francocdevlast year

    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.rahbeklast year

    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
    francocdevlast year

    Thanks!

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.