( databaseDriver: any, schemaName: string, tableName: string, format: ExportFormat, exportTarget: ExportTarget, options?: ExportOptions )
| 248 | } |
| 249 | |
| 250 | export async function exportTableData( |
| 251 | databaseDriver: any, |
| 252 | schemaName: string, |
| 253 | tableName: string, |
| 254 | format: ExportFormat, |
| 255 | exportTarget: ExportTarget, |
| 256 | options?: ExportOptions |
| 257 | ): Promise<string | Blob> { |
| 258 | console.log("Exporting", schemaName, tableName, format, exportTarget, options); |
| 259 | const result = await databaseDriver.query( |
| 260 | `SELECT * FROM ${databaseDriver.escapeId(schemaName)}.${databaseDriver.escapeId(tableName)}` |
| 261 | ); |
| 262 | console.log("QueryResults", result); |
| 263 | if (!result.rows || result.rows.length === 0) { |
| 264 | return ""; |
| 265 | } |
| 266 | |
| 267 | const headers = Object.keys(result.rows[0]); |
| 268 | const records = result.rows.map((row: { [x: string]: string; }) => headers.map(header => row[header])); |
| 269 | |
| 270 | const formatHandlers = { |
| 271 | csv: () => exportDataAsDelimitedText(headers, records, ",", "\n", '"', exportTarget), |
| 272 | json: () => exportRowsToJson(headers, records, exportTarget), |
| 273 | sql: () => exportRowsToSqlInsert(tableName, headers, records, exportTarget), |
| 274 | xlsx: () => exportToExcel(records, headers, tableName, exportTarget), |
| 275 | delimited: () => |
| 276 | exportDataAsDelimitedText( |
| 277 | headers, |
| 278 | records, |
| 279 | options?.fieldSeparator || ",", |
| 280 | options?.lineTerminator || "\n", |
| 281 | options?.encloser || '"', |
| 282 | exportTarget |
| 283 | ), |
| 284 | }; |
| 285 | |
| 286 | const handler = formatHandlers[format]; |
| 287 | if (handler) { |
| 288 | return handler(); |
| 289 | } else { |
| 290 | throw new Error(`Unsupported export format: ${format}`); |
| 291 | } |
| 292 | } |
| 293 | // TODO: maybe we should move export related types here |
| 294 | export type { ExportFormat }; |
no test coverage detected