basic db setup with drizzle

This commit is contained in:
Oliver Bryan
2025-12-07 22:55:02 +00:00
parent 7c43971087
commit 87e03b2a62
14 changed files with 475 additions and 15 deletions

22
src/db/client.ts Normal file
View File

@@ -0,0 +1,22 @@
import "dotenv/config";
import { drizzle } from "drizzle-orm/node-postgres";
if (!process.env.DATABASE_URL) {
throw new Error("DATABASE_URL is not set in environment variables");
}
export const db = drizzle({
connection: {
connectionString: process.env.DATABASE_URL,
},
});
export const testDB = async () => {
try {
await db.execute("SELECT 1;");
console.log("db connected");
} catch (err) {
console.log("db down");
process.exit();
}
};

7
src/db/schema.ts Normal file
View File

@@ -0,0 +1,7 @@
import { integer, pgTable, varchar } from "drizzle-orm/pg-core";
export const User = pgTable("User", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
name: varchar({ length: 256 }).notNull(),
username: varchar({ length: 32 }).notNull().unique(),
});

22
src/index.ts Normal file
View File

@@ -0,0 +1,22 @@
import { db, testDB } from "./db/client";
import { User } from "./db/schema";
const DEV = process.argv.find((arg) => ["--dev", "--developer", "-d"].includes(arg.toLowerCase())) != null;
const PORT = process.argv.find((arg) => arg.toLowerCase().startsWith("--port="))?.split("=")[1] || "3500";
const main = async () => {
const server = Bun.serve({
port: Number(PORT),
routes: {
"/": () => new Response(`title: eussi\ndev-mode: ${DEV}\nport: ${PORT}`),
},
});
console.log(`eussi (issue server) listening on ${server.url}`);
await testDB();
const users = await db.select().from(User);
console.log(`serving ${users.length} user${users.length === 1 ? "" : "s"}`);
};
main();