feat(database): add

This commit is contained in:
Pihkaal
2025-12-03 15:34:36 +01:00
parent ebb157991e
commit 39068b6104
12 changed files with 989 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
import { z } from "zod";
import { parseEnv } from "@lbf-bot/utils";
export const env = parseEnv({
DATABASE_URL: z.url().min(1, "Required"),
});

View File

@@ -0,0 +1,7 @@
import { drizzle } from "drizzle-orm/postgres-js";
import { env } from "~/env";
import * as schema from "~/schema";
export const db = drizzle(env.DATABASE_URL, {
schema,
});

View File

@@ -0,0 +1,2 @@
export * from "./relations";
export * from "./tables";

View File

@@ -0,0 +1,22 @@
import { relations } from "drizzle-orm";
import { trackedPlayers, usernameHistory } from "./tables";
/**
* TRACKING SYSTEM
*/
export const trackedPlayersRelations = relations(
trackedPlayers,
({ many }) => ({
usernameHistory: many(usernameHistory),
}),
);
export const usernameHistoryRelations = relations(
usernameHistory,
({ one }) => ({
trackedPlayer: one(trackedPlayers, {
fields: [usernameHistory.playerId],
references: [trackedPlayers.playerId],
}),
}),
);

View File

@@ -0,0 +1,29 @@
import { integer, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
/**
* ECONOMY SYSTEM
*/
export const accounts = pgTable("accounts", {
playerId: uuid("player_id").primaryKey(),
balance: integer("balance").notNull().default(0),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
});
/**
* TRACKING SYSTEM
*/
export const trackedPlayers = pgTable("tracked_players", {
playerId: uuid("player_id").primaryKey(),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
});
export const usernameHistory = pgTable("username_history", {
id: uuid("id").primaryKey().defaultRandom(),
playerId: uuid("player_id")
.notNull()
.references(() => trackedPlayers.playerId, { onDelete: "cascade" }),
username: text("username").notNull(),
firstSeenAt: timestamp("first_seen_at").notNull().defaultNow(),
});