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,6 @@
|
||||
export * from "./users";
|
||||
export * from "./recipes";
|
||||
export * from "./social";
|
||||
export * from "./meal-planning";
|
||||
export * from "./tiers";
|
||||
export * from "./webhooks";
|
||||
@@ -0,0 +1,83 @@
|
||||
import {
|
||||
pgTable,
|
||||
text,
|
||||
timestamp,
|
||||
integer,
|
||||
boolean,
|
||||
date,
|
||||
decimal,
|
||||
pgEnum,
|
||||
} from "drizzle-orm/pg-core";
|
||||
import { relations } from "drizzle-orm";
|
||||
import { users } from "./users";
|
||||
import { recipes } from "./recipes";
|
||||
import { ingredients } from "./recipes";
|
||||
|
||||
export const mealTypeEnum = pgEnum("meal_type", ["breakfast", "lunch", "dinner", "snack"]);
|
||||
export const weekdayEnum = pgEnum("weekday", ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]);
|
||||
|
||||
export const mealPlans = pgTable("meal_plans", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
weekStart: date("week_start").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const mealPlanEntries = pgTable("meal_plan_entries", {
|
||||
id: text("id").primaryKey(),
|
||||
mealPlanId: text("meal_plan_id").notNull().references(() => mealPlans.id, { onDelete: "cascade" }),
|
||||
day: weekdayEnum("day").notNull(),
|
||||
mealType: mealTypeEnum("meal_type").notNull(),
|
||||
recipeId: text("recipe_id").references(() => recipes.id, { onDelete: "set null" }),
|
||||
servings: integer("servings").notNull().default(2),
|
||||
note: text("note"),
|
||||
});
|
||||
|
||||
export const pantryItems = pgTable("pantry_items", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
ingredientId: text("ingredient_id").references(() => ingredients.id, { onDelete: "set null" }),
|
||||
rawName: text("raw_name").notNull(),
|
||||
quantity: decimal("quantity", { precision: 10, scale: 4 }),
|
||||
unit: text("unit"),
|
||||
expiresAt: timestamp("expires_at"),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const shoppingLists = pgTable("shopping_lists", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id").references(() => users.id, { onDelete: "cascade" }),
|
||||
name: text("name").notNull(),
|
||||
generatedAt: timestamp("generated_at"),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const shoppingListItems = pgTable("shopping_list_items", {
|
||||
id: text("id").primaryKey(),
|
||||
listId: text("list_id").notNull().references(() => shoppingLists.id, { onDelete: "cascade" }),
|
||||
ingredientId: text("ingredient_id").references(() => ingredients.id, { onDelete: "set null" }),
|
||||
rawName: text("raw_name").notNull(),
|
||||
quantity: decimal("quantity", { precision: 10, scale: 4 }),
|
||||
unit: text("unit"),
|
||||
aisle: text("aisle"),
|
||||
checked: boolean("checked").notNull().default(false),
|
||||
});
|
||||
|
||||
export const mealPlansRelations = relations(mealPlans, ({ one, many }) => ({
|
||||
user: one(users, { fields: [mealPlans.userId], references: [users.id] }),
|
||||
entries: many(mealPlanEntries),
|
||||
}));
|
||||
|
||||
export const mealPlanEntriesRelations = relations(mealPlanEntries, ({ one }) => ({
|
||||
mealPlan: one(mealPlans, { fields: [mealPlanEntries.mealPlanId], references: [mealPlans.id] }),
|
||||
recipe: one(recipes, { fields: [mealPlanEntries.recipeId], references: [recipes.id] }),
|
||||
}));
|
||||
|
||||
export const shoppingListsRelations = relations(shoppingLists, ({ one, many }) => ({
|
||||
user: one(users, { fields: [shoppingLists.userId], references: [users.id] }),
|
||||
items: many(shoppingListItems),
|
||||
}));
|
||||
|
||||
export const shoppingListItemsRelations = relations(shoppingListItems, ({ one }) => ({
|
||||
list: one(shoppingLists, { fields: [shoppingListItems.listId], references: [shoppingLists.id] }),
|
||||
}));
|
||||
@@ -0,0 +1,170 @@
|
||||
import {
|
||||
pgTable,
|
||||
text,
|
||||
timestamp,
|
||||
boolean,
|
||||
integer,
|
||||
decimal,
|
||||
jsonb,
|
||||
pgEnum,
|
||||
index,
|
||||
} from "drizzle-orm/pg-core";
|
||||
import { relations } from "drizzle-orm";
|
||||
import { users } from "./users";
|
||||
|
||||
export const visibilityEnum = pgEnum("visibility", ["private", "unlisted", "public"]);
|
||||
export const difficultyEnum = pgEnum("difficulty", ["easy", "medium", "hard"]);
|
||||
|
||||
export type DietaryTags = {
|
||||
vegan?: boolean;
|
||||
vegetarian?: boolean;
|
||||
glutenFree?: boolean;
|
||||
dairyFree?: boolean;
|
||||
nutFree?: boolean;
|
||||
halal?: boolean;
|
||||
kosher?: boolean;
|
||||
};
|
||||
|
||||
export const recipes = pgTable("recipes", {
|
||||
id: text("id").primaryKey(),
|
||||
authorId: text("author_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
title: text("title").notNull(),
|
||||
description: text("description"),
|
||||
baseServings: integer("base_servings").notNull().default(4),
|
||||
visibility: visibilityEnum("visibility").notNull().default("private"),
|
||||
sourceUrl: text("source_url"),
|
||||
aiGenerated: boolean("ai_generated").notNull().default(false),
|
||||
aiModel: text("ai_model"),
|
||||
aiPrompt: text("ai_prompt"),
|
||||
dietaryTags: jsonb("dietary_tags").$type<DietaryTags>().default({}),
|
||||
dietaryVerified: boolean("dietary_verified").notNull().default(false),
|
||||
nutritionData: jsonb("nutrition_data").$type<{ perServing: { calories: number; proteinG: number; carbsG: number; fatG: number; fiberG: number; sodiumMg: number } }>(),
|
||||
difficulty: difficultyEnum("difficulty"),
|
||||
prepMins: integer("prep_mins"),
|
||||
cookMins: integer("cook_mins"),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
}, (t) => [
|
||||
index("recipes_author_idx").on(t.authorId),
|
||||
index("recipes_visibility_idx").on(t.visibility),
|
||||
]);
|
||||
|
||||
export const ingredients = pgTable("ingredients", {
|
||||
id: text("id").primaryKey(),
|
||||
name: text("name").notNull().unique(),
|
||||
aliases: text("aliases").array().notNull().default([]),
|
||||
category: text("category"),
|
||||
knownAllergens: text("known_allergens").array().notNull().default([]),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const recipeIngredients = pgTable("recipe_ingredients", {
|
||||
id: text("id").primaryKey(),
|
||||
recipeId: text("recipe_id").notNull().references(() => recipes.id, { onDelete: "cascade" }),
|
||||
ingredientId: text("ingredient_id").references(() => ingredients.id, { onDelete: "set null" }),
|
||||
rawName: text("raw_name").notNull(),
|
||||
quantity: decimal("quantity", { precision: 10, scale: 4 }),
|
||||
unit: text("unit"),
|
||||
note: text("note"),
|
||||
order: integer("order").notNull().default(0),
|
||||
});
|
||||
|
||||
export const recipeSteps = pgTable("recipe_steps", {
|
||||
id: text("id").primaryKey(),
|
||||
recipeId: text("recipe_id").notNull().references(() => recipes.id, { onDelete: "cascade" }),
|
||||
order: integer("order").notNull(),
|
||||
instruction: text("instruction").notNull(),
|
||||
timerSeconds: integer("timer_seconds"),
|
||||
photoUrl: text("photo_url"),
|
||||
});
|
||||
|
||||
export const recipePhotos = pgTable("recipe_photos", {
|
||||
id: text("id").primaryKey(),
|
||||
recipeId: text("recipe_id").notNull().references(() => recipes.id, { onDelete: "cascade" }),
|
||||
storageKey: text("storage_key").notNull(),
|
||||
order: integer("order").notNull().default(0),
|
||||
isCover: boolean("is_cover").notNull().default(false),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const recipeNotes = pgTable("recipe_notes", {
|
||||
id: text("id").primaryKey(),
|
||||
recipeId: text("recipe_id").notNull().references(() => recipes.id, { onDelete: "cascade" }),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
content: text("content").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const recipeVariations = pgTable("recipe_variations", {
|
||||
id: text("id").primaryKey(),
|
||||
parentRecipeId: text("parent_recipe_id").notNull().references(() => recipes.id, { onDelete: "cascade" }),
|
||||
childRecipeId: text("child_recipe_id").notNull().references(() => recipes.id, { onDelete: "cascade" }),
|
||||
description: text("description"),
|
||||
aiGenerated: boolean("ai_generated").notNull().default(false),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const userAllergens = pgTable("user_allergens", {
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
allergenTag: text("allergen_tag").notNull(),
|
||||
});
|
||||
|
||||
export const recipesRelations = relations(recipes, ({ one, many }) => ({
|
||||
author: one(users, { fields: [recipes.authorId], references: [users.id] }),
|
||||
ingredients: many(recipeIngredients),
|
||||
steps: many(recipeSteps),
|
||||
photos: many(recipePhotos),
|
||||
notes: many(recipeNotes),
|
||||
childVariations: many(recipeVariations, { relationName: "parent" }),
|
||||
parentVariations: many(recipeVariations, { relationName: "child" }),
|
||||
}));
|
||||
|
||||
export const recipeIngredientsRelations = relations(recipeIngredients, ({ one }) => ({
|
||||
recipe: one(recipes, { fields: [recipeIngredients.recipeId], references: [recipes.id] }),
|
||||
ingredient: one(ingredients, { fields: [recipeIngredients.ingredientId], references: [ingredients.id] }),
|
||||
}));
|
||||
|
||||
export const recipePhotosRelations = relations(recipePhotos, ({ one }) => ({
|
||||
recipe: one(recipes, { fields: [recipePhotos.recipeId], references: [recipes.id] }),
|
||||
}));
|
||||
|
||||
export const recipeStepsRelations = relations(recipeSteps, ({ one }) => ({
|
||||
recipe: one(recipes, { fields: [recipeSteps.recipeId], references: [recipes.id] }),
|
||||
}));
|
||||
|
||||
export const recipeNotesRelations = relations(recipeNotes, ({ one }) => ({
|
||||
recipe: one(recipes, { fields: [recipeNotes.recipeId], references: [recipes.id] }),
|
||||
}));
|
||||
|
||||
export const recipeVariationsRelations = relations(recipeVariations, ({ one }) => ({
|
||||
parent: one(recipes, { fields: [recipeVariations.parentRecipeId], references: [recipes.id], relationName: "parent" }),
|
||||
child: one(recipes, { fields: [recipeVariations.childRecipeId], references: [recipes.id], relationName: "child" }),
|
||||
}));
|
||||
|
||||
export const recipeSnapshots = pgTable("recipe_snapshots", {
|
||||
id: text("id").primaryKey(),
|
||||
recipeId: text("recipe_id").notNull().references(() => recipes.id, { onDelete: "cascade" }),
|
||||
authorId: text("author_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
version: integer("version").notNull(),
|
||||
title: text("title").notNull(),
|
||||
snapshotData: jsonb("snapshot_data").notNull().$type<{
|
||||
title: string;
|
||||
description: string | null;
|
||||
baseServings: number;
|
||||
difficulty: string | null;
|
||||
prepMins: number | null;
|
||||
cookMins: number | null;
|
||||
dietaryTags: Record<string, boolean>;
|
||||
ingredients: Array<{ rawName: string; quantity: string | null; unit: string | null; note: string | null; order: number }>;
|
||||
steps: Array<{ instruction: string; timerSeconds: number | null; order: number }>;
|
||||
}>(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
}, (t) => [
|
||||
index("recipe_snapshots_recipe_idx").on(t.recipeId, t.version),
|
||||
]);
|
||||
|
||||
export const recipeSnapshotsRelations = relations(recipeSnapshots, ({ one }) => ({
|
||||
recipe: one(recipes, { fields: [recipeSnapshots.recipeId], references: [recipes.id] }),
|
||||
author: one(users, { fields: [recipeSnapshots.authorId], references: [users.id] }),
|
||||
}));
|
||||
@@ -0,0 +1,129 @@
|
||||
import {
|
||||
pgTable,
|
||||
text,
|
||||
timestamp,
|
||||
integer,
|
||||
boolean,
|
||||
pgEnum,
|
||||
index,
|
||||
} from "drizzle-orm/pg-core";
|
||||
import { relations } from "drizzle-orm";
|
||||
import { users } from "./users";
|
||||
import { recipes } from "./recipes";
|
||||
|
||||
export const feedItemTypeEnum = pgEnum("feed_item_type", [
|
||||
"new_recipe",
|
||||
"new_follow",
|
||||
"recipe_rated",
|
||||
]);
|
||||
|
||||
export const ratings = pgTable("ratings", {
|
||||
id: text("id").primaryKey(),
|
||||
recipeId: text("recipe_id").notNull().references(() => recipes.id, { onDelete: "cascade" }),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
score: integer("score").notNull(),
|
||||
reviewText: text("review_text"),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const favorites = pgTable("favorites", {
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
recipeId: text("recipe_id").notNull().references(() => recipes.id, { onDelete: "cascade" }),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const comments = pgTable("comments", {
|
||||
id: text("id").primaryKey(),
|
||||
recipeId: text("recipe_id").notNull().references(() => recipes.id, { onDelete: "cascade" }),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
parentId: text("parent_id"),
|
||||
content: text("content").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
}, (t) => [
|
||||
index("comments_recipe_idx").on(t.recipeId),
|
||||
]);
|
||||
|
||||
export const collections = pgTable("collections", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
name: text("name").notNull(),
|
||||
description: text("description"),
|
||||
isPublic: boolean("is_public").notNull().default(false),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const collectionRecipes = pgTable("collection_recipes", {
|
||||
collectionId: text("collection_id").notNull().references(() => collections.id, { onDelete: "cascade" }),
|
||||
recipeId: text("recipe_id").notNull().references(() => recipes.id, { onDelete: "cascade" }),
|
||||
addedAt: timestamp("added_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const cookingHistory = pgTable("cooking_history", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
recipeId: text("recipe_id").notNull().references(() => recipes.id, { onDelete: "cascade" }),
|
||||
cookedAt: timestamp("cooked_at").notNull().defaultNow(),
|
||||
servings: integer("servings"),
|
||||
notes: text("notes"),
|
||||
});
|
||||
|
||||
export const feedItems = pgTable("feed_items", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
type: feedItemTypeEnum("type").notNull(),
|
||||
actorId: text("actor_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
subjectId: text("subject_id").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
}, (t) => [
|
||||
index("feed_items_user_idx").on(t.userId, t.createdAt),
|
||||
]);
|
||||
|
||||
export const ratingsRelations = relations(ratings, ({ one }) => ({
|
||||
recipe: one(recipes, { fields: [ratings.recipeId], references: [recipes.id] }),
|
||||
user: one(users, { fields: [ratings.userId], references: [users.id] }),
|
||||
}));
|
||||
|
||||
export const collectionsRelations = relations(collections, ({ one, many }) => ({
|
||||
user: one(users, { fields: [collections.userId], references: [users.id] }),
|
||||
recipes: many(collectionRecipes),
|
||||
}));
|
||||
|
||||
export const collectionRecipesRelations = relations(collectionRecipes, ({ one }) => ({
|
||||
collection: one(collections, { fields: [collectionRecipes.collectionId], references: [collections.id] }),
|
||||
recipe: one(recipes, { fields: [collectionRecipes.recipeId], references: [recipes.id] }),
|
||||
}));
|
||||
|
||||
export const collectionMemberRoleEnum = pgEnum("collection_member_role", ["viewer", "editor"]);
|
||||
|
||||
export const collectionMembers = pgTable("collection_members", {
|
||||
id: text("id").primaryKey(),
|
||||
collectionId: text("collection_id").notNull().references(() => collections.id, { onDelete: "cascade" }),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
role: collectionMemberRoleEnum("role").notNull().default("viewer"),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const commentReactionTypeEnum = pgEnum("comment_reaction_type", ["like", "love", "laugh", "wow", "sad", "fire"]);
|
||||
|
||||
export const commentReactions = pgTable("comment_reactions", {
|
||||
id: text("id").primaryKey(),
|
||||
commentId: text("comment_id").notNull().references(() => comments.id, { onDelete: "cascade" }),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
type: commentReactionTypeEnum("type").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
}, (t) => [
|
||||
index("comment_reactions_comment_idx").on(t.commentId),
|
||||
]);
|
||||
|
||||
export const collectionMembersRelations = relations(collectionMembers, ({ one }) => ({
|
||||
collection: one(collections, { fields: [collectionMembers.collectionId], references: [collections.id] }),
|
||||
user: one(users, { fields: [collectionMembers.userId], references: [users.id] }),
|
||||
}));
|
||||
|
||||
export const commentReactionsRelations = relations(commentReactions, ({ one }) => ({
|
||||
comment: one(comments, { fields: [commentReactions.commentId], references: [comments.id] }),
|
||||
user: one(users, { fields: [commentReactions.userId], references: [users.id] }),
|
||||
}));
|
||||
@@ -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] }),
|
||||
}));
|
||||
@@ -0,0 +1,150 @@
|
||||
import {
|
||||
pgTable,
|
||||
text,
|
||||
timestamp,
|
||||
boolean,
|
||||
integer,
|
||||
pgEnum,
|
||||
} from "drizzle-orm/pg-core";
|
||||
import { relations } from "drizzle-orm";
|
||||
|
||||
export const userRoleEnum = pgEnum("user_role", ["user", "moderator", "admin"]);
|
||||
export const tierEnum = pgEnum("tier", ["free", "pro"]);
|
||||
export const unitPrefEnum = pgEnum("unit_pref", ["metric", "imperial"]);
|
||||
|
||||
export const users = pgTable("users", {
|
||||
id: text("id").primaryKey(),
|
||||
email: text("email").notNull().unique(),
|
||||
emailVerified: boolean("email_verified").notNull().default(false),
|
||||
name: text("name").notNull(),
|
||||
avatarUrl: text("avatar_url"),
|
||||
bio: text("bio"),
|
||||
username: text("username").unique(),
|
||||
role: userRoleEnum("role").notNull().default("user"),
|
||||
tier: tierEnum("tier").notNull().default("free"),
|
||||
unitPref: unitPrefEnum("unit_pref").notNull().default("metric"),
|
||||
locale: text("locale").notNull().default("en"),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
// Better Auth requires these tables
|
||||
export const sessions = pgTable("sessions", {
|
||||
id: text("id").primaryKey(),
|
||||
expiresAt: timestamp("expires_at").notNull(),
|
||||
token: text("token").notNull().unique(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
ipAddress: text("ip_address"),
|
||||
userAgent: text("user_agent"),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
});
|
||||
|
||||
export const accounts = pgTable("accounts", {
|
||||
id: text("id").primaryKey(),
|
||||
accountId: text("account_id").notNull(),
|
||||
providerId: text("provider_id").notNull(),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
accessToken: text("access_token"),
|
||||
refreshToken: text("refresh_token"),
|
||||
idToken: text("id_token"),
|
||||
accessTokenExpiresAt: timestamp("access_token_expires_at"),
|
||||
refreshTokenExpiresAt: timestamp("refresh_token_expires_at"),
|
||||
scope: text("scope"),
|
||||
password: text("password"),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const verifications = pgTable("verifications", {
|
||||
id: text("id").primaryKey(),
|
||||
identifier: text("identifier").notNull(),
|
||||
value: text("value").notNull(),
|
||||
expiresAt: timestamp("expires_at").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const userFollows = pgTable("user_follows", {
|
||||
followerId: text("follower_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
followingId: text("following_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const apiKeys = pgTable("api_keys", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
name: text("name").notNull(),
|
||||
keyHash: text("key_hash").notNull().unique(),
|
||||
lastUsedAt: timestamp("last_used_at"),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const userAiKeys = pgTable("user_ai_keys", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
provider: text("provider").notNull(),
|
||||
encryptedKey: text("encrypted_key").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const usersRelations = relations(users, ({ many }) => ({
|
||||
sessions: many(sessions),
|
||||
accounts: many(accounts),
|
||||
followers: many(userFollows, { relationName: "following" }),
|
||||
following: many(userFollows, { relationName: "follower" }),
|
||||
apiKeys: many(apiKeys),
|
||||
aiKeys: many(userAiKeys),
|
||||
}));
|
||||
|
||||
export const userAiKeysRelations = relations(userAiKeys, ({ one }) => ({
|
||||
user: one(users, { fields: [userAiKeys.userId], references: [users.id] }),
|
||||
}));
|
||||
|
||||
export const userFollowsRelations = relations(userFollows, ({ one }) => ({
|
||||
follower: one(users, { fields: [userFollows.followerId], references: [users.id], relationName: "follower" }),
|
||||
following: one(users, { fields: [userFollows.followingId], references: [users.id], relationName: "following" }),
|
||||
}));
|
||||
|
||||
export const pushSubscriptions = pgTable("push_subscriptions", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
endpoint: text("endpoint").notNull().unique(),
|
||||
p256dh: text("p256dh").notNull(),
|
||||
auth: text("auth").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const userNutritionGoals = pgTable("user_nutrition_goals", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }).unique(),
|
||||
caloriesKcal: integer("calories_kcal"),
|
||||
proteinG: integer("protein_g"),
|
||||
carbsG: integer("carbs_g"),
|
||||
fatG: integer("fat_g"),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const pushSubscriptionsRelations = relations(pushSubscriptions, ({ one }) => ({
|
||||
user: one(users, { fields: [pushSubscriptions.userId], references: [users.id] }),
|
||||
}));
|
||||
|
||||
export const userNutritionGoalsRelations = relations(userNutritionGoals, ({ one }) => ({
|
||||
user: one(users, { fields: [userNutritionGoals.userId], references: [users.id] }),
|
||||
}));
|
||||
|
||||
export const userModelPrefs = pgTable("user_model_prefs", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }).unique(),
|
||||
textProvider: text("text_provider"),
|
||||
textModel: text("text_model"),
|
||||
visionProvider: text("vision_provider"),
|
||||
visionModel: text("vision_model"),
|
||||
mealPlanProvider: text("meal_plan_provider"),
|
||||
mealPlanModel: text("meal_plan_model"),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const userModelPrefsRelations = relations(userModelPrefs, ({ one }) => ({
|
||||
user: one(users, { fields: [userModelPrefs.userId], references: [users.id] }),
|
||||
}));
|
||||
@@ -0,0 +1,33 @@
|
||||
import { pgTable, text, boolean, timestamp, integer, jsonb } from "drizzle-orm/pg-core";
|
||||
import { relations } from "drizzle-orm";
|
||||
import { users } from "./users";
|
||||
|
||||
export const webhooks = pgTable("webhooks", {
|
||||
id: text("id").primaryKey(),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
url: text("url").notNull(),
|
||||
events: text("events").array().notNull().default([]),
|
||||
secret: text("secret").notNull(),
|
||||
active: boolean("active").notNull().default(true),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const webhookDeliveries = pgTable("webhook_deliveries", {
|
||||
id: text("id").primaryKey(),
|
||||
webhookId: text("webhook_id").notNull().references(() => webhooks.id, { onDelete: "cascade" }),
|
||||
event: text("event").notNull(),
|
||||
payload: jsonb("payload"),
|
||||
statusCode: integer("status_code"),
|
||||
success: boolean("success").notNull().default(false),
|
||||
attempts: integer("attempts").notNull().default(0),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const webhooksRelations = relations(webhooks, ({ one, many }) => ({
|
||||
user: one(users, { fields: [webhooks.userId], references: [users.id] }),
|
||||
deliveries: many(webhookDeliveries),
|
||||
}));
|
||||
|
||||
export const webhookDeliveriesRelations = relations(webhookDeliveries, ({ one }) => ({
|
||||
webhook: one(webhooks, { fields: [webhookDeliveries.webhookId], references: [webhooks.id] }),
|
||||
}));
|
||||
Reference in New Issue
Block a user