Add export script

This commit is contained in:
Ondřej 2024-03-01 15:10:02 +01:00
parent ead2b22964
commit a74c48bc3b
3 changed files with 25 additions and 1 deletions

2
emails.csv Normal file
View file

@ -0,0 +1,2 @@
ondrej@nyv.lt,"Ha Ha"
onnyvlt@gmail.com,"Test Test"
1 ondrej@nyv.lt Ha Ha
2 onnyvlt@gmail.com Test Test

View file

@ -10,7 +10,8 @@
"preview": "astro preview",
"astro": "astro",
"test": "vitest",
"migrate": "node --loader ts-node/esm ./scripts/migrate.ts"
"migrate": "node --loader ts-node/esm ./scripts/migrate.ts",
"tn": "node --loader ts-node/esm"
},
"dependencies": {
"@astrojs/check": "^0.4.1",

21
scripts/export-csv.ts Normal file
View file

@ -0,0 +1,21 @@
import "dotenv/config";
import { db } from "../src/db";
import * as fs from "node:fs/promises";
async function exportEmails() {
const file = await fs.open("./emails.csv", "w");
const letters = await db
.selectFrom("letters")
.where("confirmed", "=", 1)
.groupBy("email")
.select(["email", "firstName", "lastName"])
.execute();
for (const letter of letters) {
console.log(letter);
file.write(`${letter.email},"${letter.firstName} ${letter.lastName}"\n`);
}
}
exportEmails();