sqlpage / SQLPage

Fast SQL-only data application builder. Automatically build a UI on top of SQL queries.
https://sql.datapage.app
MIT License
1.57k stars 89 forks source link

feat: add csv_attachment component #608

Open mtt-artis opened 2 weeks ago

mtt-artis commented 2 weeks ago

Hi 👋,

This commit follows the discussion in #260 (better late than never!). It implements the csv_attachment component to return data as a CSV file.

SELECT 
    'csv_attachment' AS component, 
    ';' AS separator, 
    'todo.csv' AS filename;
SELECT * FROM todos;

It works, but it feels a bit hacky due to the use of is_embedded and Handlebars. Please feel free to make any changes, rename the component, or point me in the right direction.

If you like this implementation, I can add the component to the documentation.

lovasoa commented 2 weeks ago

Hi ! Thank you for the PR. Let's do it, the ability to efficiently download a CSV is a great addition.

Indeed, I think we should be able to do that without handlebars (using the csv library that we already include), and reuse the existing csv component name and parameters. This way the component downloads the csv directly when used in header position, and creates a download button when used in the middle of the page.

We will need to implement a mechanism to handle row-level parameters cleanly in header components (in HeaderContext), but it can then be reused for the json component

mtt-artis commented 2 weeks ago

Hi (again),

I would love to reuse the csv component, but I'm concerned it might confuse users since the same component could either be used as a button or as a HTTP response. This new option would require two separate files — one for the download button and another for the endpoint. Is the button a csv or button component now ?

For the handlerbars, that what I thought. Can you provide the mechanism ? From what i understand, I may see a way to do it with the HeaderContext, but It not very clean as the body/stream is handle in this header section... Moreover it might take some time cause i do not master rust as you do 😉