Skip to main content

CSV Export

Every CRUDController includes a CSV endpoint at GET /<base-path>/export. It supports the same generated filters, search hook, authorization filter, default sorting, and soft-delete visibility options as collection reads.

GET /todo-items/export?completed=false
Accept: text/csv
"id","title","completed"
1,"Write REST documentation",false
2,"Review examples",false

The response content type is text/csv. String fields are quoted and spreadsheet formulas are escaped.

Configure exports

The default export limit is 1000. Change it or disable export independently:

{
DTOClass: TodoItemDTO,
EntityClass: TodoItemEntity,
basePath: 'todo-items',
export: {
limit: 5000,
many: {
path: 'reports/todo-items',
description: 'Download visible todo items as CSV'
}
}
}

The generated path appends /export to many.path, so the example above is served at /todo-items/reports/todo-items/export.

{
DTOClass: TodoItemDTO,
EntityClass: TodoItemEntity,
basePath: 'todo-items',
export: { disabled: true }
}

Export a different shape

Use ExportDTOClass to select CSV columns without changing the normal response DTO. Decorated fields are serialized in the export, and their property names become the CSV headers.

todo-item-export.dto.ts
import { Field } from '@ptc-org/nestjs-query-rest'

export class TodoItemExportDTO {
@Field()
id!: number

@Field()
title!: string

@Field()
completed!: boolean
}
{
DTOClass: TodoItemDTO,
EntityClass: TodoItemEntity,
basePath: 'todo-items',
export: { ExportDTOClass: TodoItemExportDTO }
}

For very large datasets, keep the export limit bounded or implement a custom streaming controller.