-- Yamy (يامي) food ordering demo — SQLite schema -- Works with the bundled Node backend (node:sqlite) and with Cloudflare D1. -- All data in this demo is sample data. No real payments, no real orders. DROP VIEW IF EXISTS v_admin_stats; DROP TABLE IF EXISTS chat_messages; DROP TABLE IF EXISTS order_events; DROP TABLE IF EXISTS order_items; DROP TABLE IF EXISTS orders; DROP TABLE IF EXISTS reviews; DROP TABLE IF EXISTS menu_items; DROP TABLE IF EXISTS categories; DROP TABLE IF EXISTS restaurants; DROP TABLE IF EXISTS promos; DROP TABLE IF EXISTS addresses; DROP TABLE IF EXISTS sessions; DROP TABLE IF EXISTS couriers; DROP TABLE IF EXISTS users; CREATE TABLE users ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE, phone TEXT, password_hash TEXT NOT NULL, -- pbkdf2$$$ role TEXT NOT NULL DEFAULT 'user' CHECK (role IN ('user','admin')), status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active','blocked')), created_at TEXT NOT NULL ); CREATE TABLE sessions ( token TEXT PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, created_at TEXT NOT NULL, expires_at TEXT NOT NULL ); CREATE INDEX idx_sessions_user ON sessions(user_id); CREATE TABLE addresses ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, label TEXT NOT NULL, line TEXT NOT NULL, area TEXT, notes TEXT ); CREATE INDEX idx_addresses_user ON addresses(user_id); CREATE TABLE couriers ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, phone TEXT NOT NULL, vehicle TEXT NOT NULL, -- motorcycle | bicycle | car rating REAL NOT NULL DEFAULT 4.7, status TEXT NOT NULL DEFAULT 'available' CHECK (status IN ('available','busy','offline')), lat REAL, lon REAL ); CREATE TABLE restaurants ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, name_ar TEXT, cuisine TEXT NOT NULL, area TEXT NOT NULL, description TEXT, rating REAL NOT NULL DEFAULT 4.5, reviews_count INTEGER NOT NULL DEFAULT 0, eta_min INTEGER NOT NULL DEFAULT 30, delivery_fee REAL NOT NULL DEFAULT 15, min_order REAL NOT NULL DEFAULT 50, emoji TEXT NOT NULL DEFAULT '🍽️', lat REAL, lng REAL, tint TEXT NOT NULL DEFAULT '#fff4e6', tags TEXT NOT NULL DEFAULT '[]', -- JSON array of strings is_open INTEGER NOT NULL DEFAULT 1, created_at TEXT NOT NULL ); CREATE INDEX idx_restaurants_area ON restaurants(area); CREATE TABLE categories ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE, emoji TEXT NOT NULL DEFAULT '🍽️' ); CREATE TABLE menu_items ( id INTEGER PRIMARY KEY AUTOINCREMENT, restaurant_id INTEGER NOT NULL REFERENCES restaurants(id) ON DELETE CASCADE, category TEXT NOT NULL, name TEXT NOT NULL, name_ar TEXT, description TEXT, price REAL NOT NULL, emoji TEXT NOT NULL DEFAULT '🍽️', is_available INTEGER NOT NULL DEFAULT 1, is_popular INTEGER NOT NULL DEFAULT 0 ); CREATE INDEX idx_menu_restaurant ON menu_items(restaurant_id); CREATE TABLE promos ( code TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK (kind IN ('percent','flat','free_delivery')), value REAL NOT NULL DEFAULT 0, min_subtotal REAL NOT NULL DEFAULT 0, description TEXT, is_active INTEGER NOT NULL DEFAULT 1, uses INTEGER NOT NULL DEFAULT 0, max_uses INTEGER ); CREATE TABLE orders ( id TEXT PRIMARY KEY, -- ZD-8F3K2A user_id INTEGER REFERENCES users(id) ON DELETE SET NULL, restaurant_id INTEGER NOT NULL REFERENCES restaurants(id), restaurant_name TEXT NOT NULL, courier_id INTEGER REFERENCES couriers(id), customer_name TEXT NOT NULL, phone TEXT NOT NULL, address TEXT NOT NULL, note TEXT, payment_method TEXT NOT NULL CHECK (payment_method IN ('cash','visa')), card_last4 TEXT, subtotal REAL NOT NULL, delivery_fee REAL NOT NULL DEFAULT 0, discount REAL NOT NULL DEFAULT 0, total REAL NOT NULL, promo_code TEXT, status TEXT NOT NULL DEFAULT 'placed', eta_min INTEGER NOT NULL DEFAULT 30, delivery_lat REAL, delivery_lng REAL, created_at TEXT NOT NULL ); CREATE INDEX idx_orders_user ON orders(user_id); CREATE INDEX idx_orders_restaurant ON orders(restaurant_id); CREATE INDEX idx_orders_created ON orders(created_at); CREATE TABLE order_items ( id INTEGER PRIMARY KEY AUTOINCREMENT, order_id TEXT NOT NULL REFERENCES orders(id) ON DELETE CASCADE, menu_item_id INTEGER, name TEXT NOT NULL, price REAL NOT NULL, qty INTEGER NOT NULL DEFAULT 1 ); CREATE INDEX idx_order_items_order ON order_items(order_id); CREATE TABLE order_events ( id INTEGER PRIMARY KEY AUTOINCREMENT, order_id TEXT NOT NULL REFERENCES orders(id) ON DELETE CASCADE, status TEXT NOT NULL, ts TEXT NOT NULL, note TEXT ); CREATE INDEX idx_order_events_order ON order_events(order_id); CREATE TABLE reviews ( id INTEGER PRIMARY KEY AUTOINCREMENT, restaurant_id INTEGER NOT NULL REFERENCES restaurants(id) ON DELETE CASCADE, user_id INTEGER REFERENCES users(id) ON DELETE SET NULL, order_id TEXT, rating INTEGER NOT NULL CHECK (rating BETWEEN 1 AND 5), comment TEXT, created_at TEXT NOT NULL ); CREATE INDEX idx_reviews_restaurant ON reviews(restaurant_id); CREATE TABLE chat_messages ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, session_id TEXT, role TEXT NOT NULL CHECK (role IN ('user','bot')), text TEXT NOT NULL, ts TEXT NOT NULL ); CREATE VIEW v_admin_stats AS SELECT (SELECT COUNT(*) FROM restaurants) AS restaurants, (SELECT COUNT(*) FROM menu_items) AS menu_items, (SELECT COUNT(*) FROM users WHERE role='user') AS customers, (SELECT COUNT(*) FROM users WHERE role='admin') AS admins, (SELECT COUNT(*) FROM orders) AS orders, (SELECT COUNT(*) FROM orders WHERE status NOT IN ('delivered','canceled')) AS live_orders, (SELECT IFNULL(SUM(total),0) FROM orders WHERE status='delivered') AS revenue, (SELECT COUNT(*) FROM couriers) AS couriers, (SELECT COUNT(*) FROM reviews) AS reviews, (SELECT IFNULL(ROUND(AVG(rating),2),0) FROM reviews) AS avg_rating;