TCA-Admin-Backend/setup-db.js
2026-08-14 11:22:07 +05:30

157 lines
6.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const mysql = require('mysql2/promise');
const bcrypt = require('bcryptjs');
require('dotenv').config();
async function setup() {
try {
const pool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
console.log('Connected to MySQL...');
// Create admin_users table
await pool.execute(`
CREATE TABLE IF NOT EXISTS admin_users (
id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
role VARCHAR(50) DEFAULT 'admin',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`);
console.log('Verified admin_users table exists.');
// Create events table
await pool.execute(`
CREATE TABLE IF NOT EXISTS events (
id INT AUTO_INCREMENT PRIMARY KEY,
year INT NOT NULL,
eventdate VARCHAR(100),
eventtitle VARCHAR(255) NOT NULL,
eventimageurl TEXT,
eventdescription TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`);
console.log('Verified events table exists.');
// Create event_images table
await pool.execute(`
CREATE TABLE IF NOT EXISTS event_images (
id INT AUTO_INCREMENT PRIMARY KEY,
eventid INT,
imageurl TEXT NOT NULL,
sort_order INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (eventid) REFERENCES events(id) ON DELETE CASCADE
)
`);
console.log('Verified event_images table exists.');
// Create upcoming_events table
await pool.execute(`
CREATE TABLE IF NOT EXISTS upcoming_events (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
slug VARCHAR(255),
date VARCHAR(255),
time VARCHAR(255),
location VARCHAR(255),
image TEXT,
link TEXT,
btn_text VARCHAR(255) DEFAULT 'Details Coming Soon',
description TEXT,
admission VARCHAR(255),
sort_order INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`);
console.log('Verified upcoming_events table exists.');
// Create blogs table
await pool.execute(`
CREATE TABLE IF NOT EXISTS blogs (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
slug VARCHAR(255) UNIQUE NOT NULL,
image TEXT,
description TEXT,
author VARCHAR(255) DEFAULT 'Admin',
profile VARCHAR(255) DEFAULT '/assets/images/profile-1.jpeg',
date VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`);
console.log('Verified blogs table exists.');
// Seed initial blogs if table is empty
const [blogRows] = await pool.execute('SELECT id FROM blogs LIMIT 1');
if (blogRows.length === 0) {
const initialBlogs = [
{
title: 'Save soil, save world Projects in 2020',
slug: 'save-soil-save-world',
image: '/assets/img/all-images/blog/blog-img4.png',
description: '<p>At Eventify 2024, you\'ll join an exclusive gathering of business leaders and innovators shaping the future of their industries. This one-day conference offers dynamic sessions on leadership, technology, and strategy to help you stay ahead in today\'s competitive market. Whether you\'re looking to unlock new opportunities or build lasting eventify partnerships, Eventify is where you need to be.</p>',
author: 'Beverly',
profile: '/assets/images/profile-1.jpeg',
date: '26 Jan 2025'
},
{
title: 'Civil Litigation papers Of Conference',
slug: 'civil-litigation-papers',
image: '/assets/img/all-images/blog/blog-img5.png',
description: '<p>Fuel your business growth with actionable insights from world-class experts at Eventify 2024. This premier event brings together forward-thinking professionals to explore the latest trends, technologies, and strategies for success. From keynote speeches to interactive workshops, Eventify provides you with the tools you need.</p>',
author: 'Gisselle',
profile: '/assets/images/profile-2.jpeg',
date: '26 Jan 2025'
},
{
title: 'Greetings and Opening Event of health',
slug: 'greetings-and-opening-event',
image: '/assets/img/all-images/blog/blog-img6.png',
description: '<p>Join us at Eventify 2024, where innovation meets opportunity. This conference is the ultimate destination for business leaders seeking to push the boundaries of what\'s possible. With sessions on disruptive technologies, leadership trends, and market strategies, you\'ll walk away with the knowledge and connections to lead.</p>',
author: 'Mertie',
profile: '/assets/images/profile-3.jpeg',
date: '26 Jan 2025'
}
];
for (const blog of initialBlogs) {
await pool.execute(
'INSERT IGNORE INTO blogs (title, slug, image, description, author, profile, date) VALUES (?, ?, ?, ?, ?, ?, ?)',
[blog.title, blog.slug, blog.image, blog.description, blog.author, blog.profile, blog.date]
);
}
console.log('Seeded initial blogs successfully.');
}
// Check if admin user exists
const [rows] = await pool.execute('SELECT * FROM admin_users WHERE email = ?', ['admin@example.com']);
if (rows.length === 0) {
const passwordStr = 'adminpassword';
const hash = await bcrypt.hash(passwordStr, 10);
await pool.execute('INSERT INTO admin_users (email, password_hash, role) VALUES (?, ?, ?)', [
'admin@example.com', hash, 'admin'
]);
console.log('Default admin user created: admin@example.com / adminpassword');
} else {
console.log('Default admin user already exists.');
}
console.log('Database setup complete.');
process.exit(0);
} catch (err) {
console.error('Setup failed:', err);
process.exit(1);
}
}
setup();