2025-07-23 22:26:54 -04:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Neon Database Setup Script
|
|
|
|
|
*
|
|
|
|
|
* This script sets up the database tables in your Neon database.
|
|
|
|
|
* Make sure you have the POSTGRES_URL environment variable set.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Load environment variables from .env.local
|
|
|
|
|
require('dotenv').config({ path: '.env.local' });
|
|
|
|
|
|
|
|
|
|
const { neon } = require('@neondatabase/serverless');
|
|
|
|
|
|
|
|
|
|
async function setupNeonDatabase() {
|
2026-05-23 15:57:31 -04:00
|
|
|
const adminPassword = process.env.ADMIN_INITIAL_PASSWORD;
|
|
|
|
|
if (!adminPassword || !adminPassword.trim()) {
|
|
|
|
|
console.error(
|
|
|
|
|
'❌ ADMIN_INITIAL_PASSWORD environment variable is not set.\n' +
|
|
|
|
|
'\n' +
|
|
|
|
|
' Set it in .env.local for local dev, or as a CI secret if you run setup from CI.\n' +
|
|
|
|
|
' Generate a strong password with: openssl rand -base64 24\n' +
|
|
|
|
|
' See README.md → "First-time admin setup" for the full flow.\n'
|
|
|
|
|
);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 22:26:54 -04:00
|
|
|
const sql = neon(process.env.POSTGRES_URL);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
console.log('✅ Connecting to Neon database...');
|
|
|
|
|
|
|
|
|
|
// Create tables
|
|
|
|
|
await sql`
|
|
|
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
|
email VARCHAR(255) UNIQUE NOT NULL,
|
|
|
|
|
password VARCHAR(255) NOT NULL,
|
|
|
|
|
role VARCHAR(50) DEFAULT 'user',
|
|
|
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
|
|
|
)
|
|
|
|
|
`;
|
|
|
|
|
console.log('✅ Created users table');
|
|
|
|
|
|
|
|
|
|
await sql`
|
|
|
|
|
CREATE TABLE IF NOT EXISTS cards (
|
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
|
name VARCHAR(255) NOT NULL,
|
|
|
|
|
set_name VARCHAR(255),
|
|
|
|
|
set_code VARCHAR(50),
|
|
|
|
|
card_number VARCHAR(50),
|
|
|
|
|
rarity VARCHAR(50),
|
|
|
|
|
game VARCHAR(50) NOT NULL,
|
|
|
|
|
mana_cost VARCHAR(50),
|
|
|
|
|
cmc INTEGER,
|
|
|
|
|
card_type VARCHAR(255),
|
|
|
|
|
colors JSONB,
|
|
|
|
|
oracle_text TEXT,
|
|
|
|
|
power VARCHAR(10),
|
|
|
|
|
toughness VARCHAR(10),
|
|
|
|
|
image_url TEXT,
|
|
|
|
|
stock_image_url TEXT,
|
|
|
|
|
current_price DECIMAL(10,2),
|
|
|
|
|
market_price DECIMAL(10,2),
|
|
|
|
|
scryfall_id VARCHAR(255) UNIQUE,
|
|
|
|
|
verified BOOLEAN DEFAULT false,
|
|
|
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
|
|
|
)
|
|
|
|
|
`;
|
|
|
|
|
console.log('✅ Created cards table');
|
|
|
|
|
|
|
|
|
|
await sql`
|
|
|
|
|
CREATE TABLE IF NOT EXISTS user_cards (
|
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
|
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
|
|
|
|
|
card_id INTEGER REFERENCES cards(id) ON DELETE CASCADE,
|
|
|
|
|
quantity INTEGER DEFAULT 1,
|
|
|
|
|
condition VARCHAR(50) DEFAULT 'NM',
|
|
|
|
|
is_foil BOOLEAN DEFAULT false,
|
|
|
|
|
notes TEXT,
|
|
|
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
|
UNIQUE(user_id, card_id, is_foil)
|
|
|
|
|
)
|
|
|
|
|
`;
|
|
|
|
|
console.log('✅ Created user_cards table');
|
|
|
|
|
|
|
|
|
|
await sql`
|
|
|
|
|
CREATE TABLE IF NOT EXISTS collections (
|
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
|
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
|
|
|
|
|
name VARCHAR(255) NOT NULL,
|
|
|
|
|
description TEXT,
|
|
|
|
|
is_public BOOLEAN DEFAULT false,
|
|
|
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
|
|
|
)
|
|
|
|
|
`;
|
|
|
|
|
console.log('✅ Created collections table');
|
|
|
|
|
|
|
|
|
|
await sql`
|
|
|
|
|
CREATE TABLE IF NOT EXISTS collection_cards (
|
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
|
collection_id INTEGER REFERENCES collections(id) ON DELETE CASCADE,
|
|
|
|
|
card_id INTEGER REFERENCES cards(id) ON DELETE CASCADE,
|
|
|
|
|
quantity INTEGER DEFAULT 1,
|
|
|
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
|
UNIQUE(collection_id, card_id)
|
|
|
|
|
)
|
|
|
|
|
`;
|
|
|
|
|
console.log('✅ Created collection_cards table');
|
|
|
|
|
|
|
|
|
|
await sql`
|
|
|
|
|
CREATE TABLE IF NOT EXISTS decks (
|
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
|
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
|
|
|
|
|
name VARCHAR(255) NOT NULL,
|
|
|
|
|
description TEXT,
|
|
|
|
|
game VARCHAR(50),
|
|
|
|
|
is_public BOOLEAN DEFAULT false,
|
|
|
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
|
|
|
)
|
|
|
|
|
`;
|
|
|
|
|
console.log('✅ Created decks table');
|
|
|
|
|
|
|
|
|
|
await sql`
|
|
|
|
|
CREATE TABLE IF NOT EXISTS deck_cards (
|
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
|
deck_id INTEGER REFERENCES decks(id) ON DELETE CASCADE,
|
|
|
|
|
card_id INTEGER REFERENCES cards(id) ON DELETE CASCADE,
|
|
|
|
|
quantity INTEGER DEFAULT 1,
|
|
|
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
|
UNIQUE(deck_id, card_id)
|
|
|
|
|
)
|
|
|
|
|
`;
|
|
|
|
|
console.log('✅ Created deck_cards table');
|
|
|
|
|
|
|
|
|
|
// Create admin user
|
|
|
|
|
const bcrypt = require('bcryptjs');
|
2026-05-23 15:57:31 -04:00
|
|
|
const hashedPassword = await bcrypt.hash(adminPassword, 12);
|
2025-07-23 22:26:54 -04:00
|
|
|
|
|
|
|
|
await sql`
|
|
|
|
|
INSERT INTO users (email, password, role)
|
|
|
|
|
VALUES (${'admin@tcgvault.com'}, ${hashedPassword}, ${'admin'})
|
|
|
|
|
ON CONFLICT (email) DO NOTHING
|
|
|
|
|
`;
|
|
|
|
|
console.log('✅ Created admin user');
|
|
|
|
|
|
|
|
|
|
console.log('🎉 Neon database setup completed successfully!');
|
|
|
|
|
console.log('');
|
|
|
|
|
console.log('📋 Database Details:');
|
|
|
|
|
console.log(' Database: Neon PostgreSQL');
|
2026-05-23 15:57:31 -04:00
|
|
|
console.log(' Admin user ready (email: admin@tcgvault.com)');
|
2025-07-23 22:26:54 -04:00
|
|
|
console.log('');
|
|
|
|
|
console.log('🔧 Next Steps:');
|
|
|
|
|
console.log(' 1. Test the API endpoints');
|
|
|
|
|
console.log(' 2. Start building the frontend');
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('❌ Database setup failed:', error.message);
|
|
|
|
|
console.log('');
|
|
|
|
|
console.log('🔧 Troubleshooting:');
|
|
|
|
|
console.log(' 1. Make sure POSTGRES_URL is set in .env.local');
|
|
|
|
|
console.log(' 2. Check your Neon database connection');
|
|
|
|
|
console.log(' 3. Ensure the database URL is correct');
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setupNeonDatabase();
|