feat(db): Drizzle ORM schema and migrations
Schema domains: users/auth, recipes, social, meal-planning, tiers/usage, webhooks. Includes Better Auth tables, audit_logs, site_settings, push_subscriptions, user_model_prefs, user_nutrition_goals. Migrations 0000–0008 applied.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import {
|
||||
pgTable,
|
||||
text,
|
||||
timestamp,
|
||||
integer,
|
||||
boolean,
|
||||
index,
|
||||
unique,
|
||||
} from "drizzle-orm/pg-core";
|
||||
import { relations } from "drizzle-orm";
|
||||
import { users, tierEnum } from "./users";
|
||||
|
||||
export const tierDefinitions = pgTable("tier_definitions", {
|
||||
tier: tierEnum("tier").primaryKey(),
|
||||
maxRecipes: integer("max_recipes").notNull(),
|
||||
aiCallsPerMonth: integer("ai_calls_per_month").notNull(),
|
||||
storageMb: integer("storage_mb").notNull(),
|
||||
maxPublicRecipes: integer("max_public_recipes").notNull(),
|
||||
});
|
||||
|
||||
export const userUsage = pgTable("user_usage", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
month: text("month").notNull(),
|
||||
aiCallsUsed: integer("ai_calls_used").notNull().default(0),
|
||||
recipeCount: integer("recipe_count").notNull().default(0),
|
||||
storageUsedMb: integer("storage_used_mb").notNull().default(0),
|
||||
}, (t) => [
|
||||
index("user_usage_user_month_idx").on(t.userId, t.month),
|
||||
unique("user_usage_user_month_uniq").on(t.userId, t.month),
|
||||
]);
|
||||
|
||||
export const auditLogs = pgTable("audit_logs", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id").references(() => users.id, { onDelete: "set null" }),
|
||||
action: text("action").notNull(),
|
||||
targetType: text("target_type"),
|
||||
targetId: text("target_id"),
|
||||
metadata: text("metadata"),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
}, (t) => [
|
||||
index("audit_logs_created_idx").on(t.createdAt),
|
||||
]);
|
||||
|
||||
export const siteSettings = pgTable("site_settings", {
|
||||
key: text("key").primaryKey(),
|
||||
value: text("value"),
|
||||
isSecret: boolean("is_secret").notNull().default(false),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
updatedById: text("updated_by_id").references(() => users.id, { onDelete: "set null" }),
|
||||
});
|
||||
|
||||
export const userUsageRelations = relations(userUsage, ({ one }) => ({
|
||||
user: one(users, { fields: [userUsage.userId], references: [users.id] }),
|
||||
}));
|
||||
Reference in New Issue
Block a user