File size: 2,807 Bytes
bede2fc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | import { relations } from "drizzle-orm";
import { pgTable, text, integer, timestamp, jsonb, uuid, index } from "drizzle-orm/pg-core";
import { user } from "./auth";
export const userCredit = pgTable(
"user_credit",
{
userId: text("user_id")
.primaryKey()
.references(() => user.id, { onDelete: "cascade" }),
tokenBalance: integer("token_balance").default(0).notNull(),
lifetimeTokensUsed: integer("lifetime_tokens_used").default(0).notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at")
.defaultNow()
.$onUpdate(() => new Date())
.notNull(),
},
(table) => [index("userCredit_userId_idx").on(table.userId)],
);
export const creditTransaction = pgTable(
"credit_transaction",
{
id: uuid("id").defaultRandom().primaryKey(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
amount: integer("amount").notNull(),
type: text("type").notNull(),
description: text("description"),
tokensUsed: integer("tokens_used"),
createdAt: timestamp("created_at").defaultNow().notNull(),
},
(table) => [index("creditTransaction_userId_idx").on(table.userId)],
);
export const adminAuditLog = pgTable(
"admin_audit_log",
{
id: uuid("id").defaultRandom().primaryKey(),
adminUserId: text("admin_user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
action: text("action").notNull(),
targetUserId: text("target_user_id").references(() => user.id, {
onDelete: "set null",
}),
details: jsonb("details"),
createdAt: timestamp("created_at").defaultNow().notNull(),
},
(table) => [
index("adminAuditLog_adminUserId_idx").on(table.adminUserId),
index("adminAuditLog_action_idx").on(table.action),
],
);
export const userCreditRelations = relations(userCredit, ({ one }) => ({
user: one(user, {
fields: [userCredit.userId],
references: [user.id],
}),
}));
export const creditTransactionRelations = relations(creditTransaction, ({ one }) => ({
user: one(user, {
fields: [creditTransaction.userId],
references: [user.id],
}),
}));
export const adminAuditLogRelations = relations(adminAuditLog, ({ one }) => ({
admin: one(user, {
fields: [adminAuditLog.adminUserId],
references: [user.id],
relationName: "adminAuditLog_adminUser",
}),
target: one(user, {
fields: [adminAuditLog.targetUserId],
references: [user.id],
relationName: "adminAuditLog_targetUser",
}),
}));
export const platformConfig = pgTable("platform_config", {
key: text("key").primaryKey(),
value: text("value").notNull(),
updatedAt: timestamp("updated_at")
.defaultNow()
.$onUpdate(() => new Date())
.notNull(),
});
|