feat(database): add
This commit is contained in:
2
packages/database/.env.example
Normal file
2
packages/database/.env.example
Normal file
@@ -0,0 +1,2 @@
|
||||
# Postgres database url
|
||||
DATABASE_URL=""
|
||||
2
packages/database/.gitignore
vendored
Normal file
2
packages/database/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
dist
|
||||
.env
|
||||
11
packages/database/drizzle.config.ts
Normal file
11
packages/database/drizzle.config.ts
Normal 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,
|
||||
},
|
||||
});
|
||||
33
packages/database/package.json
Normal file
33
packages/database/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
6
packages/database/src/env.ts
Normal file
6
packages/database/src/env.ts
Normal 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"),
|
||||
});
|
||||
7
packages/database/src/index.ts
Normal file
7
packages/database/src/index.ts
Normal 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,
|
||||
});
|
||||
2
packages/database/src/schema/index.ts
Normal file
2
packages/database/src/schema/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./relations";
|
||||
export * from "./tables";
|
||||
22
packages/database/src/schema/relations.ts
Normal file
22
packages/database/src/schema/relations.ts
Normal 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],
|
||||
}),
|
||||
}),
|
||||
);
|
||||
29
packages/database/src/schema/tables.ts
Normal file
29
packages/database/src/schema/tables.ts
Normal 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(),
|
||||
});
|
||||
14
packages/database/tsconfig.build.json
Normal file
14
packages/database/tsconfig.build.json
Normal 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"]
|
||||
}
|
||||
28
packages/database/tsconfig.json
Normal file
28
packages/database/tsconfig.json
Normal 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
833
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user