Files
t5de-hub/packages/db/src/index.ts
T
dhkatzandClaude Opus 5.5 76c8e0342e
CI / test (push) Successful in 1m10s
Hosted rooms phase 2: moderators, bans, privacy, invites, images, favorites, ratings, recent rooms, occupancy watch
Moderators (up to 20 IMVU accounts, set by ID or name) and the owner account boot
and ban over realtime (room.boot); bans are listed and lifted over REST. Private
rooms are unlisted and joined by the owner, moderators and holders of an accepted
invite (room.invite / room.invited / room.invite_reply / room.invite_answer).
Room images (PNG/JPEG by magic bytes, up to 512 KB) are stored in the database
and served with cache headers. Per-person favorites, ratings and recent rooms.
room.watch pushes room.occupancy; participants carry their IMVU name, and
GET .../participants lists them. Contract in docs/hosted-rooms.md, "Phase 2".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-30 06:07:41 -07:00

39 lines
1.8 KiB
TypeScript

// The hub's database: Postgres in production (DATABASE_URL), or PGlite (Postgres compiled to WebAssembly, in
// process) for development and tests, so nothing needs installing.
import { drizzle as drizzleBun } from "drizzle-orm/bun-sql";
import { migrate as migrateBun } from "drizzle-orm/bun-sql/migrator";
import { drizzle as drizzlePglite } from "drizzle-orm/pglite";
import { migrate as migratePglite } from "drizzle-orm/pglite/migrator";
import { PGlite } from "@electric-sql/pglite";
import { SQL } from "bun";
import { mkdirSync } from "node:fs";
import { fileURLToPath } from "node:url";
import * as schema from "./schema";
export { schema };
export type { DecorationItem, FileEntry, ParentInfo, RoomModerator, SceneItem } from "./schema";
export * from "drizzle-orm";
const migrationsFolder = fileURLToPath(new URL("../drizzle", import.meta.url));
export type Database = ReturnType<typeof drizzlePglite<typeof schema>>;
/**
* Opens the database and brings its schema up to date.
* @param url a postgres:// URL, "memory" (a fresh in-process database), or a directory for PGlite's files.
*/
export async function openDatabase(url: string): Promise<{ db: Database; close: () => Promise<void> }> {
if (url.startsWith("postgres://") || url.startsWith("postgresql://")) {
const client = new SQL(url);
const db = drizzleBun({ client, schema });
await migrateBun(db, { migrationsFolder });
// Both drivers speak the same query builder; the PGlite type stands for both.
return { db: db as unknown as Database, close: async () => client.close() };
}
if (url !== "memory") mkdirSync(url, { recursive: true });
const client = url === "memory" ? new PGlite() : new PGlite(url);
const db = drizzlePglite({ client, schema });
await migratePglite(db, { migrationsFolder });
return { db, close: async () => client.close() };
}