This commit is contained in:
Jp
2026-02-02 16:38:39 +08:00
parent e16e3ec387
commit 9442352a87

View File

@@ -1,19 +1,77 @@
require('reflect-metadata');
const { Sequelize } = require('sequelize-typescript');
const path = require('path');
const fs = require('fs');
// Try to find the model file
let FoodItem;
const distModelPath = path.join(__dirname, '../dist/models/food-item.model.js');
if (fs.existsSync(distModelPath)) {
const modelModule = require(distModelPath);
FoodItem = modelModule.FoodItem;
let MealFood;
let PlannedFood;
let Meal;
let User;
let WaterLog;
let WeightLog;
let MealPlan;
let UserGoal;
let DailySummary;
let APICache;
const foodItemPath = path.join(__dirname, '../dist/models/food-item.model.js');
const mealFoodPath = path.join(__dirname, '../dist/models/meal-food.model.js');
const plannedFoodPath = path.join(__dirname, '../dist/models/planned-food.model.js');
const mealPath = path.join(__dirname, '../dist/models/meal.model.js');
const userPath = path.join(__dirname, '../dist/models/user.model.js');
const waterLogPath = path.join(__dirname, '../dist/models/water-log.model.js');
const weightLogPath = path.join(__dirname, '../dist/models/weight-log.model.js');
const mealPlanPath = path.join(__dirname, '../dist/models/meal-plan.model.js');
const userGoalPath = path.join(__dirname, '../dist/models/user-goal.model.js');
const dailySummaryPath = path.join(__dirname, '../dist/models/daily-summary.model.js');
const apiCachePath = path.join(__dirname, '../dist/models/api-cache.model.js');
if (fs.existsSync(foodItemPath)) {
const mod = require(foodItemPath);
FoodItem = mod.FoodItem;
} else {
console.error('Could not find compiled FoodItem model at ' + distModelPath);
console.error('Please run "npm run build" first.');
console.error('Missing model: ' + foodItemPath);
process.exit(1);
}
if (fs.existsSync(mealFoodPath)) {
const mod = require(mealFoodPath);
MealFood = mod.MealFood;
}
if (fs.existsSync(plannedFoodPath)) {
const mod = require(plannedFoodPath);
PlannedFood = mod.PlannedFood;
}
if (fs.existsSync(mealPath)) {
const mod = require(mealPath);
Meal = mod.Meal;
}
if (fs.existsSync(userPath)) {
const mod = require(userPath);
User = mod.User;
}
if (fs.existsSync(waterLogPath)) {
const mod = require(waterLogPath);
WaterLog = mod.WaterLog;
}
if (fs.existsSync(weightLogPath)) {
const mod = require(weightLogPath);
WeightLog = mod.WeightLog;
}
if (fs.existsSync(mealPlanPath)) {
const mod = require(mealPlanPath);
MealPlan = mod.MealPlan;
}
if (fs.existsSync(userGoalPath)) {
const mod = require(userGoalPath);
UserGoal = mod.UserGoal;
}
if (fs.existsSync(dailySummaryPath)) {
const mod = require(dailySummaryPath);
DailySummary = mod.DailySummary;
}
if (fs.existsSync(apiCachePath)) {
const mod = require(apiCachePath);
APICache = mod.APICache;
}
// Database connection
const dbPath = process.env.DATABASE_URL
@@ -31,9 +89,10 @@ if (!process.env.DATABASE_URL) {
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: dbPath,
models: [FoodItem],
logging: false
});
const models = [FoodItem].concat([MealFood, PlannedFood, Meal, User, WaterLog, WeightLog, MealPlan, UserGoal, DailySummary, APICache].filter(Boolean));
sequelize.addModels(models);
const filipinoFoods = [
// Rice (Kanin)