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,43 @@
|
||||
import { z } from "zod";
|
||||
import { CreateRecipeSchema, DietaryTagsSchema } from "./recipes";
|
||||
|
||||
export const GenerateRecipeRequestSchema = z.object({
|
||||
prompt: z.string().min(1).max(1000),
|
||||
servings: z.number().int().positive().default(4),
|
||||
dietary: DietaryTagsSchema.optional(),
|
||||
difficulty: z.enum(["easy", "medium", "hard"]).optional(),
|
||||
model: z.string().optional(),
|
||||
});
|
||||
|
||||
export const SuggestVariationsRequestSchema = z.object({
|
||||
recipeId: z.string(),
|
||||
count: z.number().int().min(1).max(5).default(3),
|
||||
model: z.string().optional(),
|
||||
});
|
||||
|
||||
export const ImportFromUrlRequestSchema = z.object({
|
||||
url: z.string().url(),
|
||||
model: z.string().optional(),
|
||||
});
|
||||
|
||||
export const AiGeneratedRecipeSchema = CreateRecipeSchema.extend({
|
||||
nutrition: z.object({
|
||||
calories: z.number(),
|
||||
proteinG: z.number(),
|
||||
carbsG: z.number(),
|
||||
fatG: z.number(),
|
||||
fiberG: z.number(),
|
||||
}).optional(),
|
||||
});
|
||||
|
||||
export const VariationSuggestionSchema = z.object({
|
||||
title: z.string(),
|
||||
description: z.string(),
|
||||
changes: z.array(z.string()),
|
||||
recipe: CreateRecipeSchema,
|
||||
});
|
||||
|
||||
export type GenerateRecipeRequest = z.infer<typeof GenerateRecipeRequestSchema>;
|
||||
export type ImportFromUrlRequest = z.infer<typeof ImportFromUrlRequestSchema>;
|
||||
export type AiGeneratedRecipe = z.infer<typeof AiGeneratedRecipeSchema>;
|
||||
export type VariationSuggestion = z.infer<typeof VariationSuggestionSchema>;
|
||||
@@ -0,0 +1,26 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const PaginationSchema = z.object({
|
||||
page: z.coerce.number().int().positive().default(1),
|
||||
limit: z.coerce.number().int().min(1).max(100).default(20),
|
||||
});
|
||||
|
||||
export const PaginatedResponseSchema = <T extends z.ZodTypeAny>(itemSchema: T) =>
|
||||
z.object({
|
||||
data: z.array(itemSchema),
|
||||
pagination: z.object({
|
||||
page: z.number().int(),
|
||||
limit: z.number().int(),
|
||||
total: z.number().int(),
|
||||
pages: z.number().int(),
|
||||
}),
|
||||
});
|
||||
|
||||
export const ApiErrorSchema = z.object({
|
||||
error: z.string(),
|
||||
code: z.string().optional(),
|
||||
details: z.unknown().optional(),
|
||||
});
|
||||
|
||||
export type Pagination = z.infer<typeof PaginationSchema>;
|
||||
export type ApiError = z.infer<typeof ApiErrorSchema>;
|
||||
@@ -0,0 +1,4 @@
|
||||
export * from "./common";
|
||||
export * from "./recipes";
|
||||
export * from "./users";
|
||||
export * from "./ai";
|
||||
@@ -0,0 +1,88 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const DietaryTagsSchema = z.object({
|
||||
vegan: z.boolean().optional(),
|
||||
vegetarian: z.boolean().optional(),
|
||||
glutenFree: z.boolean().optional(),
|
||||
dairyFree: z.boolean().optional(),
|
||||
nutFree: z.boolean().optional(),
|
||||
halal: z.boolean().optional(),
|
||||
kosher: z.boolean().optional(),
|
||||
});
|
||||
|
||||
export const RecipeIngredientSchema = z.object({
|
||||
id: z.string(),
|
||||
rawName: z.string().min(1),
|
||||
quantity: z.number().positive().nullable(),
|
||||
unit: z.string().nullable(),
|
||||
note: z.string().nullable(),
|
||||
order: z.number().int(),
|
||||
});
|
||||
|
||||
export const RecipeStepSchema = z.object({
|
||||
id: z.string(),
|
||||
order: z.number().int(),
|
||||
instruction: z.string().min(1),
|
||||
timerSeconds: z.number().int().positive().nullable(),
|
||||
photoUrl: z.string().url().nullable(),
|
||||
});
|
||||
|
||||
export const RecipePhotoSchema = z.object({
|
||||
id: z.string(),
|
||||
storageKey: z.string(),
|
||||
order: z.number().int(),
|
||||
isCover: z.boolean(),
|
||||
});
|
||||
|
||||
export const RecipeSchema = z.object({
|
||||
id: z.string(),
|
||||
authorId: z.string(),
|
||||
title: z.string().min(1).max(200),
|
||||
description: z.string().nullable(),
|
||||
baseServings: z.number().int().positive(),
|
||||
visibility: z.enum(["private", "unlisted", "public"]),
|
||||
sourceUrl: z.string().url().nullable(),
|
||||
aiGenerated: z.boolean(),
|
||||
aiModel: z.string().nullable(),
|
||||
dietaryTags: DietaryTagsSchema,
|
||||
dietaryVerified: z.boolean(),
|
||||
difficulty: z.enum(["easy", "medium", "hard"]).nullable(),
|
||||
prepMins: z.number().int().positive().nullable(),
|
||||
cookMins: z.number().int().positive().nullable(),
|
||||
ingredients: z.array(RecipeIngredientSchema),
|
||||
steps: z.array(RecipeStepSchema),
|
||||
photos: z.array(RecipePhotoSchema),
|
||||
createdAt: z.string().datetime(),
|
||||
updatedAt: z.string().datetime(),
|
||||
});
|
||||
|
||||
export const CreateRecipeSchema = RecipeSchema.omit({
|
||||
id: true,
|
||||
authorId: true,
|
||||
aiGenerated: true,
|
||||
aiModel: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
}).extend({
|
||||
ingredients: z.array(RecipeIngredientSchema.omit({ id: true })),
|
||||
steps: z.array(RecipeStepSchema.omit({ id: true })),
|
||||
photos: z.array(RecipePhotoSchema.omit({ id: true })),
|
||||
});
|
||||
|
||||
export const UpdateRecipeSchema = CreateRecipeSchema.partial();
|
||||
|
||||
export const RecipeListQuerySchema = z.object({
|
||||
page: z.coerce.number().int().positive().default(1),
|
||||
limit: z.coerce.number().int().min(1).max(100).default(20),
|
||||
visibility: z.enum(["private", "unlisted", "public"]).optional(),
|
||||
authorId: z.string().optional(),
|
||||
q: z.string().optional(),
|
||||
dietary: z.string().optional(),
|
||||
difficulty: z.enum(["easy", "medium", "hard"]).optional(),
|
||||
});
|
||||
|
||||
export type Recipe = z.infer<typeof RecipeSchema>;
|
||||
export type CreateRecipe = z.infer<typeof CreateRecipeSchema>;
|
||||
export type UpdateRecipe = z.infer<typeof UpdateRecipeSchema>;
|
||||
export type RecipeListQuery = z.infer<typeof RecipeListQuerySchema>;
|
||||
export type DietaryTags = z.infer<typeof DietaryTagsSchema>;
|
||||
@@ -0,0 +1,40 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const UserSchema = z.object({
|
||||
id: z.string(),
|
||||
email: z.string().email(),
|
||||
name: z.string().min(1),
|
||||
username: z.string().min(3).max(30).regex(/^[a-z0-9_-]+$/).nullable(),
|
||||
avatarUrl: z.string().url().nullable(),
|
||||
bio: z.string().max(500).nullable(),
|
||||
role: z.enum(["user", "moderator", "admin"]),
|
||||
tier: z.enum(["free", "pro"]),
|
||||
unitPref: z.enum(["metric", "imperial"]),
|
||||
createdAt: z.string().datetime(),
|
||||
});
|
||||
|
||||
export const PublicUserSchema = UserSchema.pick({
|
||||
id: true,
|
||||
name: true,
|
||||
username: true,
|
||||
avatarUrl: true,
|
||||
bio: true,
|
||||
createdAt: true,
|
||||
});
|
||||
|
||||
export const UpdateUserSchema = z.object({
|
||||
name: z.string().min(1).max(100).optional(),
|
||||
username: z.string().min(3).max(30).regex(/^[a-z0-9_-]+$/).optional(),
|
||||
bio: z.string().max(500).optional(),
|
||||
unitPref: z.enum(["metric", "imperial"]).optional(),
|
||||
});
|
||||
|
||||
export const AdminUpdateUserSchema = UpdateUserSchema.extend({
|
||||
role: z.enum(["user", "moderator", "admin"]).optional(),
|
||||
tier: z.enum(["free", "pro"]).optional(),
|
||||
});
|
||||
|
||||
export type User = z.infer<typeof UserSchema>;
|
||||
export type PublicUser = z.infer<typeof PublicUserSchema>;
|
||||
export type UpdateUser = z.infer<typeof UpdateUserSchema>;
|
||||
export type AdminUpdateUser = z.infer<typeof AdminUpdateUserSchema>;
|
||||
Reference in New Issue
Block a user