1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { Statement } from "https://esm.town/v/postpostscript/sqliteBuilder";
import { Parser } from "npm:@json2csv/plainjs";
import type { ResultSet } from "npm:@libsql/client";
export const sqlToJSON = (result: ResultSet) => {
const { columns, rows } = result;
return rows.map(row =>
columns.reduce((obj, col, index) => {
obj[col] = row[index];
return obj;
}, {})
);
};
export const sqlToCSV = (result: ResultSet) => {
try {
const { columns, rows } = result;
const records = sqlToJSON(result);
return new Parser({ fields: columns }).parse(records);
} catch (error) {
return error.message;
}
};
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.
Comments
pomdtr avatar

as discussed in discord, the sqlToCSV val will not work if a column contains a comma.

I use this lib to handle edge cases in the VS Code Extension: https://www.npmjs.com/package/@json2csv/plainjs