0.0.5 • Published 9 months ago
@oniryk/dreamer-csv v0.0.5
@oniryk/dreamer-csv
@oniryk/dreamer-csv
is a wrapper of papaparse
that provides a more convenient way to deliver AdonisJS v6 responses in csv format.
This package is part of @oniryk/dreamer
. It is not intended to be a general purpose package, but you can use it if you want.
Installation
npm install @oniryk/dreamer-csv
Usage
If you are using @oniryk/dreamer
, you can use it like this:
import Post from '#models/post'
import { index } from '@oniryk/dreamer/extensions/crud'
import csv from '@oniryk/dreamer-csv'
export default class PostsController {
public index = index(Post, {
formats: [ csv() ],
})
// ..
}
You also can use it directly:
import { HttpContext } from '@adonisjs/core/http'
import xls from '@oniryk/dreamer-csv'
export default class PostsController {
public async index(context: HttpContext) {
const data = await Post.all()
await csv()(context, data)
}
}
Settings
You can pass an object to the csv
function to customize the output:
csv({
// Define the filename when downloading
filename: 'posts.csv', // default is 'export.csv'
// Define the columns to export
columns: ['title', 'content', 'author', 'created_at'],
// Define the delimiter
delimiter: ';', // default is ','
// Translate the column names
i18n: {
title: 'Title',
content: 'Content',
}
})