From be19e26d1b25105b4c5e11b362238a3e04d27628 Mon Sep 17 00:00:00 2001 From: Stan Runge Date: Thu, 12 Mar 2026 23:40:44 +0100 Subject: [PATCH] use bun sql instead of postgres.js --- README.md | 10 +++++----- bun.lock | 1 - package.json | 11 +++++------ scripts/backfill-personal-team-names.ts | 10 ++++------ scripts/backfill-r2.ts | 7 +++---- scripts/cancel-matches.ts | 6 ++---- scripts/clear-db.ts | 6 ++---- scripts/reseed-ratings.ts | 6 +++--- scripts/seed-db.ts | 6 ++---- src/lib/server/db/index.ts | 9 +++++---- 10 files changed, 31 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 18c4bd3..b2fd5c6 100644 --- a/README.md +++ b/README.md @@ -20,13 +20,13 @@ bun x sv create --template minimal --types ts --add prettier eslint vitest="usag ## Developing -Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server: +This project now requires Bun at runtime because the server uses Bun SQL. Start the development server with: ```sh -npm run dev +bun run dev # or start the server and open the app in a new browser tab -npm run dev -- --open +bun run dev -- --open ``` ## Building @@ -34,9 +34,9 @@ npm run dev -- --open To create a production version of your app: ```sh -npm run build +bun run build ``` -You can preview the production build with `npm run preview`. +You can preview the production build with `bun run preview`. > To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment. diff --git a/bun.lock b/bun.lock index a67ff7d..29b7b60 100644 --- a/bun.lock +++ b/bun.lock @@ -7,7 +7,6 @@ "dependencies": { "bancho.js": "^0.12.3", "discord.js": "^14.25.1", - "postgres": "^3.4.8", }, "devDependencies": { "@eslint/compat": "^2.0.2", diff --git a/package.json b/package.json index 4ed6ff5..3eee9cc 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,10 @@ "version": "0.0.1", "type": "module", "scripts": { - "dev": "vite dev", - "build": "vite build", + "dev": "bun --bun vite dev", + "build": "bun --bun vite build", "start": "bun ./build/index.js", - "preview": "vite preview", + "preview": "bun --bun vite preview", "prepare": "svelte-kit sync || echo ''", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", @@ -20,7 +20,7 @@ "db:generate": "drizzle-kit generate", "db:migrate": "drizzle-kit migrate", "db:studio": "drizzle-kit studio", - "auth:schema": "npx @better-auth/cli generate --config src/lib/server/auth.ts --output src/lib/server/db/auth.schema.ts --yes" + "auth:schema": "bun x @better-auth/cli generate --config src/lib/server/auth.ts --output src/lib/server/db/auth.schema.ts --yes" }, "devDependencies": { "@eslint/compat": "^2.0.2", @@ -57,7 +57,6 @@ }, "dependencies": { "bancho.js": "^0.12.3", - "discord.js": "^14.25.1", - "postgres": "^3.4.8" + "discord.js": "^14.25.1" } } diff --git a/scripts/backfill-personal-team-names.ts b/scripts/backfill-personal-team-names.ts index bcd6243..0bf6b0a 100644 --- a/scripts/backfill-personal-team-names.ts +++ b/scripts/backfill-personal-team-names.ts @@ -5,16 +5,14 @@ * Usage: bun scripts/backfill-personal-team-names.ts */ -import { drizzle } from 'drizzle-orm/postgres-js'; -import postgres from 'postgres'; -import { eq, and } from 'drizzle-orm'; -import { pgTable, text, uuid, boolean, timestamp } from 'drizzle-orm/pg-core'; +import { drizzle } from 'drizzle-orm/bun-sql'; +import { eq } from 'drizzle-orm'; +import { pgTable, text, uuid, boolean } from 'drizzle-orm/pg-core'; const DATABASE_URL = process.env.DATABASE_URL; if (!DATABASE_URL) throw new Error('DATABASE_URL not set'); -const client = postgres(DATABASE_URL); -const db = drizzle(client); +const db = drizzle(DATABASE_URL); const team = pgTable('team', { id: uuid('id').primaryKey(), diff --git a/scripts/backfill-r2.ts b/scripts/backfill-r2.ts index 5a3cce1..5d1c014 100644 --- a/scripts/backfill-r2.ts +++ b/scripts/backfill-r2.ts @@ -11,8 +11,7 @@ * then run this script to populate them. */ -import postgres from 'postgres'; -import { S3Client } from 'bun'; +import { SQL, S3Client } from 'bun'; // ── Config ───────────────────────────────────────────────────────────── @@ -130,7 +129,7 @@ async function getBeatmap(beatmapId: string): Promise { // ── Main ─────────────────────────────────────────────────────────────── -const sql = postgres(DATABASE_URL); +const sql = new SQL(DATABASE_URL); // ════════════════════════════════════════════════════════════════════════ // 1. MAPPOOL SLOTS — backfill title/artist/version/coverUrl/listCoverUrl @@ -263,4 +262,4 @@ console.log(` Mappool slots: ${slotOk} backfilled, ${slotFail} failed`); console.log(` User avatars: ${avatarOk} re-proxied, ${avatarSkip} already R2`); console.log(` Team avatars: ${teamOk} re-proxied, ${teamSkip} already R2`); -await sql.end(); +await sql.close(); diff --git a/scripts/cancel-matches.ts b/scripts/cancel-matches.ts index f5b2ff4..0063d59 100644 --- a/scripts/cancel-matches.ts +++ b/scripts/cancel-matches.ts @@ -4,15 +4,13 @@ * Usage: bun scripts/cancel-matches.ts */ -import { drizzle } from 'drizzle-orm/postgres-js'; -import postgres from 'postgres'; +import { drizzle } from 'drizzle-orm/bun-sql'; import { sql } from 'drizzle-orm'; const DATABASE_URL = process.env.DATABASE_URL; if (!DATABASE_URL) throw new Error('DATABASE_URL not set'); -const client = postgres(DATABASE_URL); -const db = drizzle(client); +const db = drizzle(DATABASE_URL); async function main() { const result = await db.execute(sql` diff --git a/scripts/clear-db.ts b/scripts/clear-db.ts index 6298b72..ec46267 100644 --- a/scripts/clear-db.ts +++ b/scripts/clear-db.ts @@ -7,15 +7,13 @@ * --full Nuclear: clear everything except users and personal teams */ -import { drizzle } from 'drizzle-orm/postgres-js'; -import postgres from 'postgres'; +import { drizzle } from 'drizzle-orm/bun-sql'; import { sql } from 'drizzle-orm'; const DATABASE_URL = process.env.DATABASE_URL; if (!DATABASE_URL) throw new Error('DATABASE_URL not set'); -const client = postgres(DATABASE_URL); -const db = drizzle(client); +const db = drizzle(DATABASE_URL); const flag = process.argv[2]; diff --git a/scripts/reseed-ratings.ts b/scripts/reseed-ratings.ts index 845dff1..c5fe8b8 100644 --- a/scripts/reseed-ratings.ts +++ b/scripts/reseed-ratings.ts @@ -7,7 +7,7 @@ * Run with: bun run scripts/reseed-ratings.ts */ -import postgres from 'postgres'; +import { SQL } from 'bun'; const DATABASE_URL = process.env.DATABASE_URL!; const OSU_CLIENT_ID = process.env.OSU_CLIENT_ID!; @@ -20,7 +20,7 @@ for (const [k, v] of Object.entries({ DATABASE_URL, OSU_CLIENT_ID, OSU_CLIENT_SE } } -const sql = postgres(DATABASE_URL); +const sql = new SQL(DATABASE_URL); // ── osu! API ─────────────────────────────────────────────────────────── @@ -119,7 +119,7 @@ async function main() { } console.log(`\nDone: ${updated} updated, ${skipped} skipped`); - await sql.end(); + await sql.close(); } main().catch((err) => { diff --git a/scripts/seed-db.ts b/scripts/seed-db.ts index a60221d..0b45cde 100644 --- a/scripts/seed-db.ts +++ b/scripts/seed-db.ts @@ -5,16 +5,14 @@ * Usage: bun scripts/seed-db.ts */ -import { drizzle } from 'drizzle-orm/postgres-js'; -import postgres from 'postgres'; +import { drizzle } from 'drizzle-orm/bun-sql'; import { sql } from 'drizzle-orm'; import { pgTable, text, uuid, boolean, timestamp, integer, real, jsonb } from 'drizzle-orm/pg-core'; const DATABASE_URL = process.env.DATABASE_URL; if (!DATABASE_URL) throw new Error('DATABASE_URL not set'); -const client = postgres(DATABASE_URL); -const db = drizzle(client); +const db = drizzle(DATABASE_URL); const mappool = pgTable('mappool', { id: uuid('id').primaryKey().defaultRandom(), diff --git a/src/lib/server/db/index.ts b/src/lib/server/db/index.ts index fb8b355..fcca45d 100644 --- a/src/lib/server/db/index.ts +++ b/src/lib/server/db/index.ts @@ -1,10 +1,11 @@ -import { drizzle } from 'drizzle-orm/postgres-js'; -import postgres from 'postgres'; import * as schema from './schema'; import { env } from '$env/dynamic/private'; if (!env.DATABASE_URL) throw new Error('DATABASE_URL is not set'); +if (!process.versions.bun) { + throw new Error('Bun SQL requires the Bun runtime. If in a dev environment, try running the app with `bun --bun vite dev`.'); +} -const client = postgres(env.DATABASE_URL); +const { drizzle } = await import('drizzle-orm/bun-sql'); -export const db = drizzle(client, { schema }); +export const db = drizzle(env.DATABASE_URL, { schema });