use bun sql instead of postgres.js

This commit is contained in:
2026-03-12 23:40:44 +01:00
parent aaef31254b
commit be19e26d1b
10 changed files with 31 additions and 41 deletions

View File

@@ -20,13 +20,13 @@ bun x sv create --template minimal --types ts --add prettier eslint vitest="usag
## Developing ## 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 ```sh
npm run dev bun run dev
# or start the server and open the app in a new browser tab # or start the server and open the app in a new browser tab
npm run dev -- --open bun run dev -- --open
``` ```
## Building ## Building
@@ -34,9 +34,9 @@ npm run dev -- --open
To create a production version of your app: To create a production version of your app:
```sh ```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. > To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment.

View File

@@ -7,7 +7,6 @@
"dependencies": { "dependencies": {
"bancho.js": "^0.12.3", "bancho.js": "^0.12.3",
"discord.js": "^14.25.1", "discord.js": "^14.25.1",
"postgres": "^3.4.8",
}, },
"devDependencies": { "devDependencies": {
"@eslint/compat": "^2.0.2", "@eslint/compat": "^2.0.2",

View File

@@ -4,10 +4,10 @@
"version": "0.0.1", "version": "0.0.1",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite dev", "dev": "bun --bun vite dev",
"build": "vite build", "build": "bun --bun vite build",
"start": "bun ./build/index.js", "start": "bun ./build/index.js",
"preview": "vite preview", "preview": "bun --bun vite preview",
"prepare": "svelte-kit sync || echo ''", "prepare": "svelte-kit sync || echo ''",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
@@ -20,7 +20,7 @@
"db:generate": "drizzle-kit generate", "db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate", "db:migrate": "drizzle-kit migrate",
"db:studio": "drizzle-kit studio", "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": { "devDependencies": {
"@eslint/compat": "^2.0.2", "@eslint/compat": "^2.0.2",
@@ -57,7 +57,6 @@
}, },
"dependencies": { "dependencies": {
"bancho.js": "^0.12.3", "bancho.js": "^0.12.3",
"discord.js": "^14.25.1", "discord.js": "^14.25.1"
"postgres": "^3.4.8"
} }
} }

View File

@@ -5,16 +5,14 @@
* Usage: bun scripts/backfill-personal-team-names.ts * Usage: bun scripts/backfill-personal-team-names.ts
*/ */
import { drizzle } from 'drizzle-orm/postgres-js'; import { drizzle } from 'drizzle-orm/bun-sql';
import postgres from 'postgres'; import { eq } from 'drizzle-orm';
import { eq, and } from 'drizzle-orm'; import { pgTable, text, uuid, boolean } from 'drizzle-orm/pg-core';
import { pgTable, text, uuid, boolean, timestamp } from 'drizzle-orm/pg-core';
const DATABASE_URL = process.env.DATABASE_URL; const DATABASE_URL = process.env.DATABASE_URL;
if (!DATABASE_URL) throw new Error('DATABASE_URL not set'); if (!DATABASE_URL) throw new Error('DATABASE_URL not set');
const client = postgres(DATABASE_URL); const db = drizzle(DATABASE_URL);
const db = drizzle(client);
const team = pgTable('team', { const team = pgTable('team', {
id: uuid('id').primaryKey(), id: uuid('id').primaryKey(),

View File

@@ -11,8 +11,7 @@
* then run this script to populate them. * then run this script to populate them.
*/ */
import postgres from 'postgres'; import { SQL, S3Client } from 'bun';
import { S3Client } from 'bun';
// ── Config ───────────────────────────────────────────────────────────── // ── Config ─────────────────────────────────────────────────────────────
@@ -130,7 +129,7 @@ async function getBeatmap(beatmapId: string): Promise<BeatmapData | null> {
// ── Main ─────────────────────────────────────────────────────────────── // ── Main ───────────────────────────────────────────────────────────────
const sql = postgres(DATABASE_URL); const sql = new SQL(DATABASE_URL);
// ════════════════════════════════════════════════════════════════════════ // ════════════════════════════════════════════════════════════════════════
// 1. MAPPOOL SLOTS — backfill title/artist/version/coverUrl/listCoverUrl // 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(` User avatars: ${avatarOk} re-proxied, ${avatarSkip} already R2`);
console.log(` Team avatars: ${teamOk} re-proxied, ${teamSkip} already R2`); console.log(` Team avatars: ${teamOk} re-proxied, ${teamSkip} already R2`);
await sql.end(); await sql.close();

View File

@@ -4,15 +4,13 @@
* Usage: bun scripts/cancel-matches.ts * Usage: bun scripts/cancel-matches.ts
*/ */
import { drizzle } from 'drizzle-orm/postgres-js'; import { drizzle } from 'drizzle-orm/bun-sql';
import postgres from 'postgres';
import { sql } from 'drizzle-orm'; import { sql } from 'drizzle-orm';
const DATABASE_URL = process.env.DATABASE_URL; const DATABASE_URL = process.env.DATABASE_URL;
if (!DATABASE_URL) throw new Error('DATABASE_URL not set'); if (!DATABASE_URL) throw new Error('DATABASE_URL not set');
const client = postgres(DATABASE_URL); const db = drizzle(DATABASE_URL);
const db = drizzle(client);
async function main() { async function main() {
const result = await db.execute(sql` const result = await db.execute(sql`

View File

@@ -7,15 +7,13 @@
* --full Nuclear: clear everything except users and personal teams * --full Nuclear: clear everything except users and personal teams
*/ */
import { drizzle } from 'drizzle-orm/postgres-js'; import { drizzle } from 'drizzle-orm/bun-sql';
import postgres from 'postgres';
import { sql } from 'drizzle-orm'; import { sql } from 'drizzle-orm';
const DATABASE_URL = process.env.DATABASE_URL; const DATABASE_URL = process.env.DATABASE_URL;
if (!DATABASE_URL) throw new Error('DATABASE_URL not set'); if (!DATABASE_URL) throw new Error('DATABASE_URL not set');
const client = postgres(DATABASE_URL); const db = drizzle(DATABASE_URL);
const db = drizzle(client);
const flag = process.argv[2]; const flag = process.argv[2];

View File

@@ -7,7 +7,7 @@
* Run with: bun run scripts/reseed-ratings.ts * Run with: bun run scripts/reseed-ratings.ts
*/ */
import postgres from 'postgres'; import { SQL } from 'bun';
const DATABASE_URL = process.env.DATABASE_URL!; const DATABASE_URL = process.env.DATABASE_URL!;
const OSU_CLIENT_ID = process.env.OSU_CLIENT_ID!; 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 ─────────────────────────────────────────────────────────── // ── osu! API ───────────────────────────────────────────────────────────
@@ -119,7 +119,7 @@ async function main() {
} }
console.log(`\nDone: ${updated} updated, ${skipped} skipped`); console.log(`\nDone: ${updated} updated, ${skipped} skipped`);
await sql.end(); await sql.close();
} }
main().catch((err) => { main().catch((err) => {

View File

@@ -5,16 +5,14 @@
* Usage: bun scripts/seed-db.ts * Usage: bun scripts/seed-db.ts
*/ */
import { drizzle } from 'drizzle-orm/postgres-js'; import { drizzle } from 'drizzle-orm/bun-sql';
import postgres from 'postgres';
import { sql } from 'drizzle-orm'; import { sql } from 'drizzle-orm';
import { pgTable, text, uuid, boolean, timestamp, integer, real, jsonb } from 'drizzle-orm/pg-core'; import { pgTable, text, uuid, boolean, timestamp, integer, real, jsonb } from 'drizzle-orm/pg-core';
const DATABASE_URL = process.env.DATABASE_URL; const DATABASE_URL = process.env.DATABASE_URL;
if (!DATABASE_URL) throw new Error('DATABASE_URL not set'); if (!DATABASE_URL) throw new Error('DATABASE_URL not set');
const client = postgres(DATABASE_URL); const db = drizzle(DATABASE_URL);
const db = drizzle(client);
const mappool = pgTable('mappool', { const mappool = pgTable('mappool', {
id: uuid('id').primaryKey().defaultRandom(), id: uuid('id').primaryKey().defaultRandom(),

View File

@@ -1,10 +1,11 @@
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import * as schema from './schema'; import * as schema from './schema';
import { env } from '$env/dynamic/private'; import { env } from '$env/dynamic/private';
if (!env.DATABASE_URL) throw new Error('DATABASE_URL is not set'); 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 });