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.

export list to csv, xlsx

default discord avatar
kosmokoks2 years ago
5

Hello, Is there any option to export collection list to external file?

  • default discord avatar
    thgh.2 years ago

    Built this yesterday:


    import { stringify } from 'csv-stringify/sync'
    import archiver from 'archiver'
    
    export const downloadEndpoint: Endpoint = {
      path: '/download-zip',
      method: 'get',
      async handler(req, res, next) {
        // Only admins allowed
        if (!isAdmin(req.user)) return next()
    
        const result = await req.payload.find({
          collection: 'widgets',
          where, // derived from query params
          limit: 1000, // sanity check
        })
    
        const archive = archiver('zip', { zlib: { level: 9 } })
        archive.pipe(res)
        res.setHeader('Content-Type', 'application/zip')
        res.setHeader('Content-Disposition', 'attachment; filename=widgets.zip')
    
        const csv = stringify(result.docs, { header: true })
        archive.append(csv, { name: 'test.csv' })
        archive.finalize()
      },
    }
  • default discord avatar
    kosmokoks2 years ago

    Thanks for answer! 💚 ,


    Do you have some extra configuration for archiver? When I'm trying use it, I have a lot of errors

  • default discord avatar
    thgh.2 years ago

    If it's a webpack error, you should add archiver to payloadconfig.admin.webpack.resolve.alias so ot doesn't try to bundle it. Otherwise not other config



    You could consider to skip the zipping and return the csv straight away

  • default discord avatar
    kosmokoks2 years ago

    thank you so much! Im going to try it

  • default discord avatar
    techinverted2 years ago

    How do i implement this

    @803037652894482442

    @476523795251855360

    I've been trying to figure it out

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.