import { Pool } from 'pg'; const pool = new Pool({ connectionString: 'postgresql://dutchie:dutchie_local_pass@localhost:54320/dutchie_menus' }); async function createBrandsTable() { try { console.log('Creating brands table...'); await pool.query(` CREATE TABLE IF NOT EXISTS brands ( id SERIAL PRIMARY KEY, store_id INTEGER NOT NULL REFERENCES stores(id) ON DELETE CASCADE, name VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) `); console.log('✅ Brands table created'); // Create index for faster queries await pool.query(` CREATE INDEX IF NOT EXISTS idx_brands_store_id ON brands(store_id) `); console.log('✅ Index created on store_id'); // Create unique constraint await pool.query(` CREATE UNIQUE INDEX IF NOT EXISTS idx_brands_store_name ON brands(store_id, name) `); console.log('✅ Unique constraint created on (store_id, name)'); console.log('\nCreating specials table...'); await pool.query(` CREATE TABLE IF NOT EXISTS specials ( id SERIAL PRIMARY KEY, store_id INTEGER NOT NULL REFERENCES stores(id) ON DELETE CASCADE, product_id INTEGER REFERENCES products(id) ON DELETE CASCADE, name VARCHAR(255) NOT NULL, description TEXT, discount_amount NUMERIC(10, 2), discount_percentage NUMERIC(5, 2), special_price NUMERIC(10, 2), original_price NUMERIC(10, 2), valid_date DATE NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) `); console.log('✅ Specials table created'); // Create composite index for fast date-based queries await pool.query(` CREATE INDEX IF NOT EXISTS idx_specials_store_date ON specials(store_id, valid_date DESC) `); console.log('✅ Index created on (store_id, valid_date)'); // Create index on product_id for joins await pool.query(` CREATE INDEX IF NOT EXISTS idx_specials_product_id ON specials(product_id) `); console.log('✅ Index created on product_id'); console.log('\n🎉 All tables and indexes created successfully!'); } catch (error: any) { console.error('❌ Error:', error.message); } finally { await pool.end(); } } createBrandsTable();