All files / lib/supabase/types database.ts

0% Statements 0/5
0% Branches 0/24
0% Functions 0/5
0% Lines 0/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
/**
 * Supabase 数据库类型定义
 *
 * 说明:
 * - 这些类型对应数据库的实际响应结构(snake_case)
 * - 使用 convertKeysToCamel() 转换为前端使用的 camelCase 类型
 * - 所有日期字段为 ISO 8601 字符串格式
 *
 * @module database-types
 */
 
// ==================== 用户相关 ====================
 
export interface DbUser {
  id: string;
  username: string;
  avatar_url: string | null;
  bio?: string | null;
  phone?: string | null;
  is_admin?: boolean;
  created_at: string;
  updated_at?: string;
}
 
export interface DbUserProfile extends DbUser {
  follower_count?: number;
  following_count?: number;
  post_count?: number;
  reputation_score?: number;
}
 
// ==================== 宠物相关 ====================
 
export interface DbPet {
  id: number;
  user_id: string;
  name: string;
  species: 'dog' | 'cat' | 'bird' | 'fish' | 'rabbit' | 'hamster' | 'other';
  breed?: string | null;
  age?: number | null;
  photo_url?: string | null;
  description?: string | null;
  created_at: string;
  updated_at: string;
}
 
// ==================== 帖子相关 ====================
 
export interface DbPostMedia {
  id: number;
  post_id: number;
  media_type: 'image' | 'video';
  file_url: string;
  thumbnail_url?: string | null; // 视频缩略图 URL
  created_at: string;
}
 
export interface DbPost {
  id: number;
  content: string;
  author_id: string;
  author?: DbUser | null;
  post_media?: DbPostMedia[];
  created_at: string;
  updated_at: string;
}
 
export interface DbPostFavorite {
  id: number;
  user_id: string;
  post_id: number;
  post?: DbPost | null;
  created_at: string;
}
 
// ==================== 评论相关 ====================
 
export interface DbComment {
  id: number;
  content: string;
  author_id: string;
  author?: DbUser | null;
  target_type: 'post' | 'catfood' | 'report';
  target_id: number;
  parent_id: number | null;
  likes: number;
  created_at: string;
  updated_at: string;
}
 
export interface DbCommentLike {
  id: number;
  comment_id: number;
  user_id: string;
  created_at: string;
}
 
// ==================== 猫粮相关 ====================
 
export interface DbIngredient {
  id: number;
  name: string;
  type?: string | null;
  label?: string | null;
  description?: string | null;
}
 
export interface DbAdditive {
  id: number;
  name: string;
  en_name?: string | null;
  applicable_range?: string | null;
  type?: string | null;
}
 
export interface DbCatfoodTag {
  id: number;
  name: string;
  description?: string | null;
  created_at: string;
}
 
export interface DbCatfoodIngredientRelation {
  id: number;
  catfood_id: number;
  ingredient_id: number;
  amount?: string | null;
  order?: number;
  ingredient?: DbIngredient;
}
 
export interface DbCatfoodAdditiveRelation {
  id: number;
  catfood_id: number;
  additive_id: number;
  amount?: string | null;
  order?: number;
  additive?: DbAdditive;
}
 
export interface DbCatfoodTagRelation {
  id: number;
  catfood_id: number;
  tag_id: number;
  created_at?: string;
  tag?: DbCatfoodTag;
}
 
export interface DbCatfood {
  id: number;
  name: string;
  brand?: string | null;
  barcode?: string | null;
  image_url?: string | null;
  score?: number | null;
  count_num?: number | null;
  percentage?: boolean | null;
  // 营养成分
  crude_protein?: number | null;
  crude_fat?: number | null;
  crude_fiber?: number | null;
  crude_ash?: number | null;
  carbohydrates?: number | null;
  others?: number | null;
  // 分析结果
  safety?: string | null;
  nutrient?: string | null;
  // 关联数据
  catfood_ingredients?: DbCatfoodIngredientRelation[];
  catfood_additives?: DbCatfoodAdditiveRelation[];
  catfood_tag_relations?: DbCatfoodTagRelation[];
  created_at: string;
  updated_at: string;
}
 
export interface DbCatfoodFavorite {
  id: number;
  user_id: string;
  catfood_id: number;
  catfood?: DbCatfood | null;
  created_at: string;
}
 
export interface DbCatfoodLike {
  id: number;
  user_id: string;
  catfood_id: number;
  catfood?: DbCatfood | null;
  created_at: string;
}
 
export interface DbCatfoodRating {
  id: number;
  catfood_id: number;
  user_id: string;
  score: number;
  comment?: string | null;
  created_at: string;
  updated_at: string;
}
 
// ==================== 通知相关 ====================
 
export interface DbNotification {
  id: number;
  recipient_id: string;
  actor_id: string;
  actor?: DbUser | null;
  verb: 'comment_post' | 'reply_comment';
  post_id?: number | null;
  comment_id?: number | null;
  unread: boolean;
  created_at: string;
}
 
// ==================== 声望相关 ====================
 
export interface DbReputationSummary {
  id: number;
  user_id: string;
  score: number;
  profile_completeness: number;
  review_quality: number;
  community_contribution: number;
  compliance: number;
  level: string;
  updated_at: string;
}
 
export interface DbBadge {
  id: number;
  code: string;
  name: string;
  description?: string | null;
  icon?: string | null;
  enabled: boolean;
  rule?: Record<string, unknown> | null;
}
 
export interface DbUserBadge {
  id: number;
  user_id: string;
  badge_id: number;
  badge?: DbBadge;
  acquired_at: string;
  is_equipped: boolean;
}
 
// ==================== AI 报告相关 ====================
 
export interface DbAiReport {
  id: number;
  catfood_id: number;
  ingredients_text: string;
  tags?: unknown[] | null;
  additives?: unknown[] | null;
  ingredients?: unknown[] | null;
  safety?: string | null;
  nutrient?: string | null;
  percentage?: boolean | null;
  percent_data?: Record<string, number | null> | null;
  created_at: string;
  updated_at: string;
}
 
export interface DbFavoriteReport {
  id: number;
  user_id: string;
  report_id: number;
  report?: DbAiReport | null;
  created_at: string;
}
 
// ==================== 类型守卫 ====================
 
export function isDbPost(value: unknown): value is DbPost {
  return (
    typeof value === 'object' &&
    value !== null &&
    'id' in value &&
    'content' in value &&
    'author_id' in value
  );
}
 
export function isDbUser(value: unknown): value is DbUser {
  return typeof value === 'object' && value !== null && 'id' in value && 'username' in value;
}
 
export function isDbComment(value: unknown): value is DbComment {
  return (
    typeof value === 'object' &&
    value !== null &&
    'id' in value &&
    'content' in value &&
    'author_id' in value &&
    'target_type' in value
  );
}
 
export function isDbCatfood(value: unknown): value is DbCatfood {
  return typeof value === 'object' && value !== null && 'id' in value && 'name' in value;
}
 
export function isDbNotification(value: unknown): value is DbNotification {
  return (
    typeof value === 'object' &&
    value !== null &&
    'id' in value &&
    'recipient_id' in value &&
    'verb' in value
  );
}