mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 10:33:01 +00:00
basic db setup with drizzle
This commit is contained in:
22
src/db/client.ts
Normal file
22
src/db/client.ts
Normal 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
7
src/db/schema.ts
Normal 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
22
src/index.ts
Normal 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();
|
||||
Reference in New Issue
Block a user