feat: Introduce new modules for class, inventory, leveling, and quests with expanded schema, refactor user service, and add verification scripts.
This commit is contained in:
183
src/db/schema.ts
183
src/db/schema.ts
@@ -1,19 +1,172 @@
|
||||
import { pgTable, integer, text, timestamp, serial } from "drizzle-orm/pg-core";
|
||||
import {
|
||||
pgTable,
|
||||
bigint,
|
||||
varchar,
|
||||
boolean,
|
||||
jsonb,
|
||||
timestamp,
|
||||
serial,
|
||||
text,
|
||||
integer,
|
||||
primaryKey,
|
||||
bigserial,
|
||||
check
|
||||
} from 'drizzle-orm/pg-core';
|
||||
import { relations, sql } from 'drizzle-orm';
|
||||
|
||||
export const users = pgTable("users", {
|
||||
userId: text("user_id").primaryKey().notNull(),
|
||||
balance: integer("balance").notNull().default(0),
|
||||
lastDaily: timestamp("last_daily"),
|
||||
dailyStreak: integer("daily_streak").notNull().default(0),
|
||||
createdAt: timestamp("created_at").defaultNow(),
|
||||
// --- TABLES ---
|
||||
|
||||
// 1. Classes
|
||||
export const classes = pgTable('classes', {
|
||||
id: bigint('id', { mode: 'bigint' }).primaryKey(),
|
||||
name: varchar('name', { length: 255 }).unique().notNull(),
|
||||
balance: bigint('balance', { mode: 'bigint' }).default(0n),
|
||||
});
|
||||
|
||||
export const transactions = pgTable("transactions", {
|
||||
transactionId: serial("transaction_id").primaryKey().notNull(),
|
||||
fromUserId: text("from_user_id").references(() => users.userId),
|
||||
toUserId: text("to_user_id").references(() => users.userId),
|
||||
amount: integer("amount").notNull(),
|
||||
occuredAt: timestamp("occured_at").defaultNow(),
|
||||
type: text("type").notNull(),
|
||||
description: text("description"),
|
||||
// 2. Users
|
||||
export const users = pgTable('users', {
|
||||
id: bigint('id', { mode: 'bigint' }).primaryKey(),
|
||||
classId: bigint('class_id', { mode: 'bigint' }).references(() => classes.id),
|
||||
username: varchar('username', { length: 255 }).unique().notNull(),
|
||||
isActive: boolean('is_active').default(true),
|
||||
|
||||
// Economy
|
||||
balance: bigint('balance', { mode: 'bigint' }).default(0n),
|
||||
xp: bigint('xp', { mode: 'bigint' }).default(0n),
|
||||
level: integer('level').default(1),
|
||||
dailyStreak: integer('daily_streak').default(0),
|
||||
|
||||
// Metadata
|
||||
settings: jsonb('settings').default({}),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow(),
|
||||
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow(),
|
||||
});
|
||||
|
||||
// 3. Items
|
||||
export const items = pgTable('items', {
|
||||
id: serial('id').primaryKey(),
|
||||
name: varchar('name', { length: 255 }).unique().notNull(),
|
||||
description: text('description'),
|
||||
rarity: varchar('rarity', { length: 20 }).default('Common'),
|
||||
|
||||
// Economy & Visuals
|
||||
price: bigint('price', { mode: 'bigint' }),
|
||||
iconUrl: text('icon_url').notNull(),
|
||||
imageUrl: text('image_url').notNull(),
|
||||
});
|
||||
|
||||
// 4. Inventory (Join Table)
|
||||
export const inventory = pgTable('inventory', {
|
||||
userId: bigint('user_id', { mode: 'bigint' })
|
||||
.references(() => users.id, { onDelete: 'cascade' }).notNull(),
|
||||
itemId: integer('item_id')
|
||||
.references(() => items.id, { onDelete: 'cascade' }).notNull(),
|
||||
quantity: bigint('quantity', { mode: 'bigint' }).default(1n),
|
||||
}, (table) => [
|
||||
primaryKey({ columns: [table.userId, table.itemId] }),
|
||||
check('quantity_check', sql`${table.quantity} > 0`)
|
||||
]);
|
||||
|
||||
// 5. Transactions
|
||||
export const transactions = pgTable('transactions', {
|
||||
id: bigserial('id', { mode: 'bigint' }).primaryKey(),
|
||||
userId: bigint('user_id', { mode: 'bigint' })
|
||||
.references(() => users.id, { onDelete: 'cascade' }),
|
||||
amount: bigint('amount', { mode: 'bigint' }).notNull(),
|
||||
type: varchar('type', { length: 50 }).notNull(),
|
||||
description: text('description'),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow(),
|
||||
});
|
||||
|
||||
// 6. Quests
|
||||
export const quests = pgTable('quests', {
|
||||
id: serial('id').primaryKey(),
|
||||
name: varchar('name', { length: 255 }).notNull(),
|
||||
description: text('description'),
|
||||
triggerEvent: varchar('trigger_event', { length: 50 }).notNull(),
|
||||
requirements: jsonb('requirements').notNull().default({}),
|
||||
rewards: jsonb('rewards').notNull().default({}),
|
||||
});
|
||||
|
||||
// 7. User Quests (Join Table)
|
||||
export const userQuests = pgTable('user_quests', {
|
||||
userId: bigint('user_id', { mode: 'bigint' })
|
||||
.references(() => users.id, { onDelete: 'cascade' }).notNull(),
|
||||
questId: integer('quest_id')
|
||||
.references(() => quests.id, { onDelete: 'cascade' }).notNull(),
|
||||
progress: integer('progress').default(0),
|
||||
completedAt: timestamp('completed_at', { withTimezone: true }),
|
||||
}, (table) => [
|
||||
primaryKey({ columns: [table.userId, table.questId] })
|
||||
]);
|
||||
|
||||
// 8. Cooldowns
|
||||
export const cooldowns = pgTable('cooldowns', {
|
||||
userId: bigint('user_id', { mode: 'bigint' })
|
||||
.references(() => users.id, { onDelete: 'cascade' }).notNull(),
|
||||
actionKey: varchar('action_key', { length: 50 }).notNull(),
|
||||
readyAt: timestamp('ready_at', { withTimezone: true }).notNull(),
|
||||
}, (table) => [
|
||||
primaryKey({ columns: [table.userId, table.actionKey] })
|
||||
]);
|
||||
|
||||
// --- RELATIONS ---
|
||||
|
||||
export const classesRelations = relations(classes, ({ many }) => ({
|
||||
users: many(users),
|
||||
}));
|
||||
|
||||
export const usersRelations = relations(users, ({ one, many }) => ({
|
||||
class: one(classes, {
|
||||
fields: [users.classId],
|
||||
references: [classes.id],
|
||||
}),
|
||||
inventory: many(inventory),
|
||||
transactions: many(transactions),
|
||||
quests: many(userQuests),
|
||||
cooldowns: many(cooldowns),
|
||||
}));
|
||||
|
||||
export const itemsRelations = relations(items, ({ many }) => ({
|
||||
inventoryEntries: many(inventory),
|
||||
}));
|
||||
|
||||
export const inventoryRelations = relations(inventory, ({ one }) => ({
|
||||
user: one(users, {
|
||||
fields: [inventory.userId],
|
||||
references: [users.id],
|
||||
}),
|
||||
item: one(items, {
|
||||
fields: [inventory.itemId],
|
||||
references: [items.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
export const transactionsRelations = relations(transactions, ({ one }) => ({
|
||||
user: one(users, {
|
||||
fields: [transactions.userId],
|
||||
references: [users.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
export const questsRelations = relations(quests, ({ many }) => ({
|
||||
userEntries: many(userQuests),
|
||||
}));
|
||||
|
||||
export const userQuestsRelations = relations(userQuests, ({ one }) => ({
|
||||
user: one(users, {
|
||||
fields: [userQuests.userId],
|
||||
references: [users.id],
|
||||
}),
|
||||
quest: one(quests, {
|
||||
fields: [userQuests.questId],
|
||||
references: [quests.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
export const cooldownsRelations = relations(cooldowns, ({ one }) => ({
|
||||
user: one(users, {
|
||||
fields: [cooldowns.userId],
|
||||
references: [users.id],
|
||||
}),
|
||||
}));
|
||||
Reference in New Issue
Block a user