feat(database): add
This commit is contained in:
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(),
|
||||
});
|
||||
Reference in New Issue
Block a user