33import React , { useState , useEffect } from "react" ;
44import { FaCloudUploadAlt , FaDatabase } from "react-icons/fa" ;
55import initSqlJs from "sql.js" ;
6+ import JSZip from "jszip" ;
67
78import Navbar from "@/components/Navbar" ;
89import Questions from "@/components/Questions" ;
@@ -55,9 +56,10 @@ export default function Home() {
5556
5657 const tableNames = tablesResult [ 0 ] . values . flat ( ) as string [ ] ;
5758
58- // Fetch data for each table
59- const dbData : { [ key : string ] : any [ ] } = { } ;
60- let jsonContent : { [ key : string ] : any [ ] } = { } ;
59+ // Store files for later download
60+ const blobs : { name : string ; blob : Blob } [ ] = [ ] ;
61+ const zip = new JSZip ( ) ;
62+ const jsonContent : { [ key : string ] : any [ ] } = { } ;
6163
6264 for ( const table of tableNames ) {
6365 const result = database . exec ( `SELECT * FROM ${ table } ` ) ;
@@ -70,55 +72,67 @@ export default function Home() {
7072 ) ,
7173 ) ;
7274
73- dbData [ table ] = rows ;
75+ if ( selectedKeys . has ( "JSON" ) ) {
76+ jsonContent [ table ] = rows ;
77+ }
7478
7579 if ( selectedKeys . has ( "CSV" ) ) {
76- // Convert table data to CSV
7780 const csvContent = [
78- columns . join ( "," ) ,
81+ columns . join ( "," ) , // Header
7982 ...rows . map ( ( row ) =>
8083 columns . map ( ( col ) => row [ col ] ) . join ( "," ) ,
81- ) ,
84+ ) , // Rows
8285 ] . join ( "\n" ) ;
8386
84- // Create and download CSV file
85- const blob = new Blob ( [ csvContent ] , {
87+ const csvBlob = new Blob ( [ csvContent ] , {
8688 type : "text/csv;charset=utf-8;" ,
8789 } ) ;
8890
89- const link = document . createElement ( "a" ) ;
90-
91- link . href = URL . createObjectURL ( blob ) ;
92- link . download = `${ table } .csv` ;
93- document . body . appendChild ( link ) ;
94- link . click ( ) ;
95- document . body . removeChild ( link ) ;
96- }
97-
98- if ( selectedKeys . has ( "JSON" ) ) {
99- jsonContent [ table ] = rows ;
91+ blobs . push ( { name : `${ table } .csv` , blob : csvBlob } ) ;
10092 }
101- } else {
102- dbData [ table ] = [ ] ;
10393 }
10494 }
10595
106- // Export combined JSON file if selected
10796 if ( selectedKeys . has ( "JSON" ) ) {
108- const blob = new Blob ( [ JSON . stringify ( jsonContent , null , 2 ) ] , {
109- type : "application/json;charset=utf-8;" ,
110- } ) ;
111-
112- const link = document . createElement ( "a" ) ;
97+ const jsonBlob = new Blob (
98+ [ JSON . stringify ( jsonContent , null , 2 ) ] ,
99+ {
100+ type : "application/json;charset=utf-8;" ,
101+ } ,
102+ ) ;
103+
104+ blobs . push ( { name : "database.json" , blob : jsonBlob } ) ;
105+ }
113106
114- link . href = URL . createObjectURL ( blob ) ;
115- link . download = `database.json` ;
116- document . body . appendChild ( link ) ;
117- link . click ( ) ;
118- document . body . removeChild ( link ) ;
107+ if ( blobs . length === 1 ) {
108+ // Download single file directly
109+ const { name, blob } = blobs [ 0 ] ;
110+ const url = URL . createObjectURL ( blob ) ;
111+ const a = document . createElement ( "a" ) ;
112+
113+ a . href = url ;
114+ a . download = name ;
115+ document . body . appendChild ( a ) ;
116+ a . click ( ) ;
117+ document . body . removeChild ( a ) ;
118+ URL . revokeObjectURL ( url ) ;
119+ } else if ( blobs . length > 1 ) {
120+ // Create ZIP archive for multiple files
121+ blobs . forEach ( ( { name, blob } ) => zip . file ( name , blob ) ) ;
122+ zip . generateAsync ( { type : "blob" } ) . then ( ( zipBlob ) => {
123+ const zipUrl = URL . createObjectURL ( zipBlob ) ;
124+ const a = document . createElement ( "a" ) ;
125+
126+ a . href = zipUrl ;
127+ a . download = "database_export.zip" ;
128+ document . body . appendChild ( a ) ;
129+ a . click ( ) ;
130+ document . body . removeChild ( a ) ;
131+ URL . revokeObjectURL ( zipUrl ) ;
132+ } ) ;
119133 }
120134
121- console . debug ( dbData ) ;
135+ console . debug ( "Exported blobs:" , blobs ) ;
122136 } ;
123137
124138 reader . readAsArrayBuffer ( file ) ;
0 commit comments