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,2 @@
# Postgres database url
DATABASE_URL=""

2
packages/database/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
dist
.env

View File

@@ -0,0 +1,11 @@
import { defineConfig } from "drizzle-kit";
import { env } from "~/env";
export default defineConfig({
dialect: "postgresql",
out: "./drizzle",
schema: "./src/schema/index.ts",
dbCredentials: {
url: env.DATABASE_URL,
},
});

View File

@@ -0,0 +1,33 @@
{
"name": "@lbf-bot/database",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"default": "./dist/index.js"
}
},
"files": [
"dist"
],
"scripts": {
"build": "rm -rf dist && tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
"db:push": "drizzle-kit push",
"db:studio": "drizzle-kit studio"
},
"dependencies": {
"@lbf-bot/utils": "workspace:*",
"drizzle-orm": "0.44.7",
"pg": "8.16.3",
"zod": "4.1.11"
},
"devDependencies": {
"drizzle-kit": "0.31.7",
"tsc-alias": "1.8.16",
"typescript": "5.9.3"
}
}

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(),
});

View File

@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"removeComments": false
},
"tsc-alias": {
"resolveFullPaths": true
},
"include": ["src"]
}

View File

@@ -0,0 +1,28 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"moduleResolution": "bundler",
"allowImportingTsExtensions": false,
"baseUrl": ".",
"paths": {
"~/*": ["./src/*"],
"~": ["./src/index"]
},
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
},
"include": ["src", "drizzle.config.ts"],
"exclude": ["node_modules", "dist"]
}

833
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff