<!-- source-hash: 77a4199750d9e00b871e516902027707 -->
Utility functions for deriving column order from query results and exporting tabular data to CSV files in the browser.

## Key Components

| Export | Description |
|--------|-------------|
| `deriveColumns(data, columnOrder?)` | Returns an ordered column list from query rows, respecting an optional explicit order while appending any unlisted keys |
| `exportToCSV(data, columns, filename)` | Serializes rows to a CSV blob and triggers a browser download |
| `escapeCSVCell` *(internal)* | Wraps cell values in double-quotes and escapes embedded quotes when the value contains commas, quotes, or newlines |

## Usage Example

```typescript
import { deriveColumns, exportToCSV } from './utils'

const rows = [
  { id: 1, name: 'Alice', role: 'admin' },
  { id: 2, name: 'Bob',   role: 'viewer' },
]

// Derive columns with a preferred order
const columns = deriveColumns(rows, ['name', 'role'])
// → ['name', 'role', 'id']

// Trigger a browser CSV download
exportToCSV(rows, columns, 'users-export')
// Downloads: users-export.csv
```

## Notes

- `deriveColumns` gracefully handles unknown keys in `columnOrder` — only keys present in the data are included.
- `exportToCSV` is browser-only; it relies on `document`, `Blob`, and `URL.createObjectURL`.
- The DOM `<a>` element used for the download is removed and the object URL revoked immediately after the click to prevent memory leaks.