File: /var/www/indoadvisory_new/webapp/src/utils/database.ts
// Database utilities and types
export interface Article {
id: number;
title: string;
slug: string;
excerpt: string;
content: string;
featured_image?: string;
status: 'draft' | 'published' | 'archived';
category: string;
tags?: string;
author_id: number;
published_at?: string;
created_at: string;
updated_at: string;
}
export interface ContactInquiry {
id: number;
name: string;
email: string;
company?: string;
service?: string;
message: string;
status: 'new' | 'contacted' | 'closed';
created_at: string;
}
export interface SiteSetting {
id: number;
setting_key: string;
setting_value: string;
setting_type: 'text' | 'json' | 'boolean' | 'number';
description?: string;
updated_at: string;
}
export interface TeamMember {
id: number;
name: string;
position_id: string;
position_en: string;
bio_id?: string;
bio_en?: string;
email?: string;
linkedin_url?: string;
image_url?: string;
sort_order: number;
is_active: boolean;
created_at: string;
updated_at: string;
}
export interface Client {
id: number;
company_name: string;
industry: string;
logo_url?: string;
description_id?: string;
description_en?: string;
project_type: string;
project_value?: string;
completion_date?: string;
is_featured: boolean;
is_active: boolean;
sort_order: number;
created_at: string;
updated_at: string;
}
// Database helper functions
export async function initializeDatabase(db: D1Database): Promise<void> {
try {
// Create tables if they don't exist (migration simulation)
await db.batch([
db.prepare(`CREATE TABLE IF NOT EXISTS admin_users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
name TEXT NOT NULL,
role TEXT DEFAULT 'admin',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)`),
db.prepare(`CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
slug TEXT UNIQUE NOT NULL,
excerpt TEXT,
content TEXT NOT NULL,
featured_image TEXT,
status TEXT DEFAULT 'draft',
category TEXT DEFAULT 'news',
tags TEXT,
author_id INTEGER,
published_at DATETIME,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author_id) REFERENCES admin_users(id)
)`),
db.prepare(`CREATE TABLE IF NOT EXISTS contact_inquiries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT NOT NULL,
company TEXT,
service TEXT,
message TEXT NOT NULL,
status TEXT DEFAULT 'new',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)`),
db.prepare(`CREATE TABLE IF NOT EXISTS admin_sessions (
id TEXT PRIMARY KEY,
user_id INTEGER NOT NULL,
expires_at DATETIME NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES admin_users(id)
)`),
db.prepare(`CREATE TABLE IF NOT EXISTS site_settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
setting_key TEXT UNIQUE NOT NULL,
setting_value TEXT,
setting_type TEXT DEFAULT 'text',
description TEXT,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)`),
db.prepare(`CREATE TABLE IF NOT EXISTS team_members (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
position_id TEXT NOT NULL,
position_en TEXT NOT NULL,
bio_id TEXT,
bio_en TEXT,
email TEXT,
linkedin_url TEXT,
image_url TEXT,
sort_order INTEGER DEFAULT 0,
is_active BOOLEAN DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)`),
db.prepare(`CREATE TABLE IF NOT EXISTS clients (
id INTEGER PRIMARY KEY AUTOINCREMENT,
company_name TEXT NOT NULL,
industry TEXT NOT NULL,
logo_url TEXT,
description_id TEXT,
description_en TEXT,
project_type TEXT,
project_value TEXT,
completion_date DATE,
is_featured BOOLEAN DEFAULT 0,
is_active BOOLEAN DEFAULT 1,
sort_order INTEGER DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)`)
]);
console.log('Database initialized successfully');
} catch (error) {
console.error('Database initialization failed:', error);
}
}
export function createSlug(title: string): string {
return title
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, '') // Remove special characters
.replace(/[\s_-]+/g, '-') // Replace spaces and underscores with hyphens
.replace(/^-+|-+$/g, ''); // Remove leading/trailing hyphens
}
export function formatDate(dateString: string): string {
const date = new Date(dateString);
return date.toLocaleDateString('id-ID', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
}