Files
waseel/scripts/seed-db.mjs
T
Krikorios fbe92c9d16 Add rider/driver role selection after sign-up
- users.role column (+ migration in seed script)
- /(api)/user: GET role by clerkId, PATCH to set role
- Role selection screen; drivers get placeholder driver-home
- Root index routes by role; hardened location handling on home
2026-08-22 16:13:59 +03:00

77 lines
2.7 KiB
JavaScript

// Creates and seeds the Waseel database tables on Neon.
// Usage: node scripts/seed-db.mjs (reads DATABASE_URL from .env)
import { neon } from "@neondatabase/serverless";
import { readFileSync } from "fs";
const env = readFileSync(new URL("../.env", import.meta.url), "utf8");
const databaseUrl = env
.split("\n")
.find((l) => l.startsWith("DATABASE_URL="))
?.split("=")
.slice(1)
.join("=")
.trim()
.replace(/^"|"$/g, "");
if (!databaseUrl) {
console.error("DATABASE_URL not found in .env");
process.exit(1);
}
const sql = neon(databaseUrl);
await sql`CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
clerk_id VARCHAR(255) NOT NULL,
role VARCHAR(20),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)`;
// For databases created before roles existed.
await sql`ALTER TABLE users ADD COLUMN IF NOT EXISTS role VARCHAR(20)`;
await sql`CREATE TABLE IF NOT EXISTS drivers (
id SERIAL PRIMARY KEY,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
profile_image_url TEXT,
car_image_url TEXT,
car_seats INTEGER NOT NULL,
rating NUMERIC(2,1) NOT NULL
)`;
await sql`CREATE TABLE IF NOT EXISTS rides (
ride_id SERIAL PRIMARY KEY,
origin_address TEXT NOT NULL,
destination_address TEXT NOT NULL,
origin_latitude DOUBLE PRECISION NOT NULL,
origin_longitude DOUBLE PRECISION NOT NULL,
destination_latitude DOUBLE PRECISION NOT NULL,
destination_longitude DOUBLE PRECISION NOT NULL,
ride_time INTEGER NOT NULL,
fare_price INTEGER NOT NULL,
payment_status VARCHAR(50) NOT NULL,
driver_id INTEGER NOT NULL REFERENCES drivers(id),
user_id VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)`;
const count = await sql`SELECT COUNT(*)::int AS n FROM drivers`;
if (count[0].n === 0) {
await sql`INSERT INTO drivers
(first_name, last_name, profile_image_url, car_image_url, car_seats, rating)
VALUES
('Karim', 'Haddad', 'https://randomuser.me/api/portraits/men/32.jpg', 'https://images.unsplash.com/photo-1555215695-3004980ad54e?w=600', 4, 4.8),
('Rana', 'Khalil', 'https://randomuser.me/api/portraits/women/44.jpg', 'https://images.unsplash.com/photo-1552519507-da3b142c6e3d?w=600', 4, 4.9),
('Omar', 'Chehab', 'https://randomuser.me/api/portraits/men/75.jpg', 'https://images.unsplash.com/photo-1580273916550-e323be2ae537?w=600', 4, 4.6),
('Layal', 'Abou-Jaoude', 'https://randomuser.me/api/portraits/women/68.jpg', 'https://images.unsplash.com/photo-1590362891991-f776e747a588?w=600', 2, 4.7)`;
console.log("Seeded 4 drivers.");
} else {
console.log(`Drivers table already has ${count[0].n} rows, skipping seed.`);
}
console.log("Database ready.");