142 lines
5.4 KiB
JavaScript
142 lines
5.4 KiB
JavaScript
const sqlite3 = require('sqlite3').verbose();
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
const DB_DIR = path.join(__dirname, '..', 'database');
|
|
const DB_PATH = path.join(DB_DIR, 'gardentrack.db');
|
|
|
|
// Ensure database directory exists
|
|
if (!fs.existsSync(DB_DIR)) {
|
|
fs.mkdirSync(DB_DIR, { recursive: true });
|
|
}
|
|
|
|
const db = new sqlite3.Database(DB_PATH, (err) => {
|
|
if (err) {
|
|
console.log(DB_PATH);
|
|
console.error('Error creating database:', err.message);
|
|
process.exit(1);
|
|
} else {
|
|
console.log('Connected to SQLite database');
|
|
}
|
|
});
|
|
|
|
// Create tables
|
|
db.serialize(() => {
|
|
// Plants table
|
|
db.run(`
|
|
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
|
|
)
|
|
`);
|
|
|
|
// Plant history table
|
|
db.run(`
|
|
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
|
|
)
|
|
`);
|
|
|
|
// Harvest records table
|
|
db.run(`
|
|
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
|
|
)
|
|
`);
|
|
|
|
// Maintenance records table
|
|
db.run(`
|
|
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,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (plant_id) REFERENCES plants (id) ON DELETE CASCADE
|
|
)
|
|
`);
|
|
|
|
// Tasks table
|
|
db.run(`
|
|
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('Database tables created successfully!');
|
|
|
|
// Insert sample data
|
|
console.log('Inserting sample data...');
|
|
|
|
// Sample plants
|
|
const plantSql = `INSERT INTO plants (purchase_location, seedling_age, type, variety, seedling_height, planting_date, current_height, health_status, photo_url, current_photo_url, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
|
|
db.run(plantSql, ['Local Nursery', 12, 'tree', 'Apple - Honeycrisp', 45.0, '2022-04-15', 180.0, 'good', null, null, 'Growing well, good fruit production']);
|
|
db.run(plantSql, ['Garden Center', 8, 'shrub', 'Blueberry - Bluecrop', 25.0, '2023-03-20', 85.0, 'needs-attention', null, null, 'Leaves showing slight discoloration']);
|
|
db.run(plantSql, ['Online Store', 3, 'herb', 'Basil - Sweet Genovese', 8.0, '2024-05-10', 35.0, 'good', null, null, 'Producing well, regular harvests']);
|
|
|
|
// Sample tasks
|
|
const taskSql = `INSERT INTO tasks (plant_id, title, description, deadline, completed) VALUES (?, ?, ?, ?, ?)`;
|
|
db.run(taskSql, [1, 'Apply fertilizer', 'Apply spring fertilizer to apple trees', '2024-03-15', 0]);
|
|
db.run(taskSql, [2, 'Prune blueberry bushes', 'Annual pruning before spring growth', '2024-02-28', 1]);
|
|
db.run(taskSql, [3, 'Harvest basil', 'Regular harvest to encourage growth', '2024-06-01', 0]);
|
|
|
|
// Sample maintenance records
|
|
const maintenanceSql = `INSERT INTO maintenance_records (plant_id, date, type, description, amount) VALUES (?, ?, ?, ?, ?)`;
|
|
db.run(maintenanceSql, [1, '2024-01-10', 'pruning', 'Winter pruning - removed dead branches', null]);
|
|
db.run(maintenanceSql, [2, '2024-01-05', 'fertilizer', 'Applied organic compost', '2 cups']);
|
|
db.run(maintenanceSql, [3, '2024-05-15', 'watering', 'Deep watering during dry spell', '1 gallon']);
|
|
|
|
// Sample harvest records
|
|
const harvestSql = `INSERT INTO harvest_records (plant_id, date, quantity, unit, notes) VALUES (?, ?, ?, ?, ?)`;
|
|
db.run(harvestSql, [1, '2023-09-15', 25, 'lbs', 'Excellent harvest, apples were sweet and crisp']);
|
|
db.run(harvestSql, [2, '2023-07-20', 3, 'cups', 'First harvest of the season, berries were plump and sweet']);
|
|
db.run(harvestSql, [3, '2024-05-25', 0.5, 'cups', 'Fresh basil for cooking, very aromatic']);
|
|
|
|
console.log('Sample data inserted successfully!');
|
|
});
|
|
|
|
db.close((err) => {
|
|
if (err) {
|
|
console.error('Error closing database:', err.message);
|
|
} else {
|
|
console.log('Database initialization completed!');
|
|
}
|
|
}); |