refactor: Update .gitignore and package.json, and add database functionality

This commit is contained in:
mauro 🤙
2024-07-04 10:06:30 +00:00
parent 5396e42be8
commit a1149838e6
5 changed files with 246 additions and 1 deletions

31
src/lib/db.ts Normal file
View File

@@ -0,0 +1,31 @@
import { sql } from '@vercel/postgres';
import { config } from 'dotenv';
var hasLoadedEnv = false;
function loadEnv() {
if (hasLoadedEnv) {
return;
}
config();
}
async function createDownloadsTable() {
loadEnv();
await sql`
CREATE TABLE IF NOT EXISTS downloads (
id SERIAL PRIMARY KEY,
platform TEXT NOT NULL,
count INT NOT NULL
);
`;
}
export async function addDownload(platform: string) {
await createDownloadsTable();
await sql`
INSERT INTO downloads (platform, count)
VALUES (${platform}, 1)
ON CONFLICT (platform)
DO UPDATE SET count = downloads.count + 1;
`;
}