const fs = require('fs'); const path = require('path'); const Database = require('better-sqlite3'); // Определяем путь const DB_DIR = path.join(__dirname, '..', 'database'); const DB_PATH = path.join(DB_DIR, 'gardentrack.db'); // Создаём папку, если не существует if (!fs.existsSync(DB_DIR)) { fs.mkdirSync(DB_DIR, { recursive: true }); console.log('📁 Папка database создана'); } // Подключаемся к базе const db = new Database(DB_PATH); console.log('✅ Подключено к базе:', DB_PATH); // Создаём таблицы db.exec(` CREATE TABLE IF NOT EXISTS plants ( id INTEGER PRIMARY KEY AUTOINCREMENT, purchase_location TEXT, seedling_age INTEGER, type TEXT NOT NULL, variety TEXT NOT NULL, seedling_height REAL, planting_date DATE NOT NULL, current_height REAL, health_status TEXT DEFAULT 'good' CHECK(health_status IN ('good', 'needs-attention', 'dead')), photo_url TEXT, current_photo_url TEXT, notes TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS plant_history ( id INTEGER PRIMARY KEY AUTOINCREMENT, plant_id INTEGER NOT NULL, year INTEGER NOT NULL, blooming_date DATE, fruiting_date DATE, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (plant_id) REFERENCES plants (id) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS harvest_records ( id INTEGER PRIMARY KEY AUTOINCREMENT, plant_id INTEGER NOT NULL, date DATE NOT NULL, quantity REAL NOT NULL, unit TEXT NOT NULL, notes TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (plant_id) REFERENCES plants (id) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS maintenance_records ( id INTEGER PRIMARY KEY AUTOINCREMENT, plant_id INTEGER NOT NULL, date DATE NOT NULL, type TEXT NOT NULL CHECK(type IN ('chemical', 'fertilizer', 'watering', 'pruning', 'transplanting', 'other')), description TEXT NOT NULL, amount TEXT, is_planned BOOLEAN DEFAULT 0, is_completed BOOLEAN DEFAULT 1, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (plant_id) REFERENCES plants (id) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS tasks ( id INTEGER PRIMARY KEY AUTOINCREMENT, plant_id INTEGER, title TEXT NOT NULL, description TEXT, deadline DATE NOT NULL, completed BOOLEAN DEFAULT 0, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (plant_id) REFERENCES plants (id) ON DELETE SET NULL ); `); console.log('✅ Таблицы успешно созданы'); // Вставка тестовых данных const insertPlant = db.prepare(` INSERT INTO plants (purchase_location, seedling_age, type, variety, seedling_height, planting_date, current_height, health_status, photo_url, current_photo_url, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `); insertPlant.run('Local Nursery', 12, 'tree', 'Apple - Honeycrisp', 45, '2022-04-15', 180, 'good', null, null, 'Growing well'); insertPlant.run('Garden Center', 8, 'shrub', 'Blueberry - Bluecrop', 25, '2023-03-20', 85, 'needs-attention', null, null, 'Needs fertilizer'); insertPlant.run('Online Store', 3, 'herb', 'Basil - Sweet Genovese', 8, '2024-05-10', 35, 'good', null, null, 'Harvested weekly'); const insertTask = db.prepare(` INSERT INTO tasks (plant_id, title, description, deadline, completed) VALUES (?, ?, ?, ?, ?) `); insertTask.run(1, 'Apply fertilizer', 'Spring fertilizer for apple trees', '2024-03-15', 0); insertTask.run(2, 'Prune blueberries', 'Spring pruning', '2024-02-28', 1); insertTask.run(3, 'Harvest basil', 'Every 2 weeks', '2024-06-01', 0); console.log('✅ Тестовые данные успешно вставлены'); console.log('📦 База данных готова');