migrate from SQLite to PostgreSQL with Drizzle ORM
- Updated all packages to latest versions (React 19, Next.js 14.2.32) - Replaced sqlite3 with pg and drizzle-orm dependencies - Created complete PostgreSQL schema with relationships and indexes - Migrated all API endpoints from SQLite to Drizzle queries - Added database seeding with sample data - Updated authentication to use bcrypt instead of pbkdf2 - Configured connection pooling for PostgreSQL - Updated app version to 1.0.0 - All endpoints tested and working correctly
This commit is contained in:
27
lib/db/connection.ts
Normal file
27
lib/db/connection.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||
import { Pool } from 'pg';
|
||||
import * as schema from './schema';
|
||||
|
||||
function createConnection() {
|
||||
if (!process.env.DATABASE_URL) {
|
||||
throw new Error('DATABASE_URL environment variable is required');
|
||||
}
|
||||
|
||||
const pool = new Pool({
|
||||
connectionString: process.env.DATABASE_URL,
|
||||
ssl: false,
|
||||
max: 10,
|
||||
idleTimeoutMillis: 30000,
|
||||
connectionTimeoutMillis: 2000
|
||||
});
|
||||
|
||||
// Test connection
|
||||
pool.on('error', (err) => {
|
||||
console.error('Unexpected error on idle client', err);
|
||||
});
|
||||
|
||||
return drizzle(pool, { schema });
|
||||
}
|
||||
|
||||
export const db = createConnection();
|
||||
export { schema };
|
||||
228
lib/db/schema.ts
Normal file
228
lib/db/schema.ts
Normal file
@@ -0,0 +1,228 @@
|
||||
import {
|
||||
pgTable,
|
||||
serial,
|
||||
varchar,
|
||||
text,
|
||||
boolean,
|
||||
integer,
|
||||
timestamp,
|
||||
decimal,
|
||||
pgEnum,
|
||||
uniqueIndex,
|
||||
index
|
||||
} from 'drizzle-orm/pg-core';
|
||||
import { relations } from 'drizzle-orm';
|
||||
|
||||
// Enums
|
||||
export const roleEnum = pgEnum('role', ['admin', 'moderator']);
|
||||
export const sourceTypeEnum = pgEnum('source_type', [
|
||||
'website', 'facebook_page', 'facebook_group', 'instagram',
|
||||
'blog', 'news_site', 'youtube', 'tiktok', 'telegram', 'other'
|
||||
]);
|
||||
export const sourceStatusEnum = pgEnum('source_status', [
|
||||
'pending', 'verified', 'rejected', 'under_review'
|
||||
]);
|
||||
export const languageEnum = pgEnum('language', ['sk', 'cs', 'en', 'other']);
|
||||
export const priorityEnum = pgEnum('priority', ['low', 'medium', 'high', 'urgent']);
|
||||
export const reportStatusEnum = pgEnum('report_status', [
|
||||
'pending', 'in_review', 'approved', 'rejected', 'duplicate'
|
||||
]);
|
||||
|
||||
// Users table
|
||||
export const users = pgTable('users', {
|
||||
id: serial('id').primaryKey(),
|
||||
email: varchar('email', { length: 255 }).notNull().unique(),
|
||||
passwordHash: varchar('password_hash', { length: 255 }).notNull(),
|
||||
name: varchar('name', { length: 100 }).notNull(),
|
||||
role: roleEnum('role').default('moderator'),
|
||||
isActive: boolean('is_active').default(true),
|
||||
lastLogin: timestamp('last_login'),
|
||||
createdAt: timestamp('created_at').defaultNow(),
|
||||
updatedAt: timestamp('updated_at').defaultNow()
|
||||
});
|
||||
|
||||
// Categories table
|
||||
export const categories = pgTable('categories', {
|
||||
id: serial('id').primaryKey(),
|
||||
name: varchar('name', { length: 100 }).notNull().unique(),
|
||||
slug: varchar('slug', { length: 100 }).notNull().unique(),
|
||||
description: text('description'),
|
||||
color: varchar('color', { length: 7 }).default('#6B7280'),
|
||||
priority: integer('priority').default(1),
|
||||
icon: varchar('icon', { length: 50 }),
|
||||
isActive: boolean('is_active').default(true),
|
||||
createdAt: timestamp('created_at').defaultNow(),
|
||||
updatedAt: timestamp('updated_at').defaultNow()
|
||||
}, (table) => {
|
||||
return {
|
||||
slugIdx: uniqueIndex('idx_categories_slug').on(table.slug),
|
||||
priorityIdx: index('idx_categories_priority').on(table.priority)
|
||||
};
|
||||
});
|
||||
|
||||
// Sources table
|
||||
export const sources = pgTable('sources', {
|
||||
id: serial('id').primaryKey(),
|
||||
url: varchar('url', { length: 1000 }).notNull().unique(),
|
||||
domain: varchar('domain', { length: 255 }).notNull(),
|
||||
title: varchar('title', { length: 500 }),
|
||||
description: text('description'),
|
||||
type: sourceTypeEnum('type').notNull(),
|
||||
status: sourceStatusEnum('status').default('pending'),
|
||||
riskLevel: integer('risk_level').default(1),
|
||||
language: languageEnum('language').default('sk'),
|
||||
evidenceUrls: text('evidence_urls'), // JSON
|
||||
reportedBy: varchar('reported_by', { length: 255 }),
|
||||
verifiedBy: integer('verified_by').references(() => users.id),
|
||||
rejectionReason: text('rejection_reason'),
|
||||
followerCount: integer('follower_count').default(0),
|
||||
lastChecked: timestamp('last_checked'),
|
||||
metadata: text('metadata').default('{}'), // JSON
|
||||
createdAt: timestamp('created_at').defaultNow(),
|
||||
updatedAt: timestamp('updated_at').defaultNow()
|
||||
}, (table) => {
|
||||
return {
|
||||
domainIdx: index('idx_sources_domain').on(table.domain),
|
||||
statusIdx: index('idx_sources_status').on(table.status),
|
||||
riskLevelIdx: index('idx_sources_risk_level').on(table.riskLevel),
|
||||
typeIdx: index('idx_sources_type').on(table.type),
|
||||
createdAtIdx: index('idx_sources_created_at').on(table.createdAt),
|
||||
verifiedByIdx: index('idx_sources_verified_by').on(table.verifiedBy),
|
||||
statusRiskIdx: index('idx_sources_status_risk').on(table.status, table.riskLevel)
|
||||
};
|
||||
});
|
||||
|
||||
// Source Categories junction table
|
||||
export const sourceCategories = pgTable('source_categories', {
|
||||
id: serial('id').primaryKey(),
|
||||
sourceId: integer('source_id').notNull().references(() => sources.id, { onDelete: 'cascade' }),
|
||||
categoryId: integer('category_id').notNull().references(() => categories.id, { onDelete: 'cascade' }),
|
||||
confidenceScore: decimal('confidence_score', { precision: 3, scale: 2 }).default('1.0'),
|
||||
addedBy: integer('added_by').references(() => users.id),
|
||||
createdAt: timestamp('created_at').defaultNow()
|
||||
}, (table) => {
|
||||
return {
|
||||
sourceIdIdx: index('idx_source_categories_source_id').on(table.sourceId),
|
||||
categoryIdIdx: index('idx_source_categories_category_id').on(table.categoryId),
|
||||
uniqueSourceCategory: uniqueIndex('unique_source_category').on(table.sourceId, table.categoryId)
|
||||
};
|
||||
});
|
||||
|
||||
// Reports table
|
||||
export const reports = pgTable('reports', {
|
||||
id: serial('id').primaryKey(),
|
||||
sourceUrl: varchar('source_url', { length: 1000 }).notNull(),
|
||||
sourceDomain: varchar('source_domain', { length: 255 }).notNull(),
|
||||
reporterEmail: varchar('reporter_email', { length: 255 }),
|
||||
reporterName: varchar('reporter_name', { length: 100 }),
|
||||
categorySuggestions: text('category_suggestions'), // JSON
|
||||
description: text('description').notNull(),
|
||||
evidenceUrls: text('evidence_urls'), // JSON
|
||||
priority: priorityEnum('priority').default('medium'),
|
||||
status: reportStatusEnum('status').default('pending'),
|
||||
assignedTo: integer('assigned_to').references(() => users.id),
|
||||
adminNotes: text('admin_notes'),
|
||||
processedAt: timestamp('processed_at'),
|
||||
ipAddress: varchar('ip_address', { length: 45 }),
|
||||
userAgent: text('user_agent'),
|
||||
createdAt: timestamp('created_at').defaultNow(),
|
||||
updatedAt: timestamp('updated_at').defaultNow()
|
||||
}, (table) => {
|
||||
return {
|
||||
statusIdx: index('idx_reports_status').on(table.status),
|
||||
sourceDomainIdx: index('idx_reports_source_domain').on(table.sourceDomain),
|
||||
priorityIdx: index('idx_reports_priority').on(table.priority),
|
||||
createdAtIdx: index('idx_reports_created_at').on(table.createdAt),
|
||||
assignedToIdx: index('idx_reports_assigned_to').on(table.assignedTo)
|
||||
};
|
||||
});
|
||||
|
||||
// API Keys table
|
||||
export const apiKeys = pgTable('api_keys', {
|
||||
id: serial('id').primaryKey(),
|
||||
keyHash: varchar('key_hash', { length: 255 }).notNull().unique(),
|
||||
name: varchar('name', { length: 100 }).notNull(),
|
||||
description: text('description'),
|
||||
ownerEmail: varchar('owner_email', { length: 255 }).notNull(),
|
||||
permissions: text('permissions').default('["read"]'), // JSON
|
||||
rateLimit: integer('rate_limit').default(1000),
|
||||
isActive: boolean('is_active').default(true),
|
||||
usageCount: integer('usage_count').default(0),
|
||||
lastUsed: timestamp('last_used'),
|
||||
expiresAt: timestamp('expires_at'),
|
||||
createdAt: timestamp('created_at').defaultNow(),
|
||||
updatedAt: timestamp('updated_at').defaultNow()
|
||||
}, (table) => {
|
||||
return {
|
||||
keyHashIdx: uniqueIndex('idx_api_keys_hash').on(table.keyHash),
|
||||
ownerIdx: index('idx_api_keys_owner').on(table.ownerEmail)
|
||||
};
|
||||
});
|
||||
|
||||
// Audit Logs table
|
||||
export const auditLogs = pgTable('audit_logs', {
|
||||
id: serial('id').primaryKey(),
|
||||
userId: integer('user_id').references(() => users.id),
|
||||
action: varchar('action', { length: 50 }).notNull(),
|
||||
resourceType: varchar('resource_type', { length: 50 }).notNull(),
|
||||
resourceId: integer('resource_id'),
|
||||
details: text('details'), // JSON
|
||||
ipAddress: varchar('ip_address', { length: 45 }),
|
||||
createdAt: timestamp('created_at').defaultNow()
|
||||
}, (table) => {
|
||||
return {
|
||||
userIdIdx: index('idx_audit_logs_user_id').on(table.userId),
|
||||
createdAtIdx: index('idx_audit_logs_created_at').on(table.createdAt),
|
||||
actionIdx: index('idx_audit_logs_action').on(table.action),
|
||||
resourceTypeIdx: index('idx_audit_logs_resource_type').on(table.resourceType)
|
||||
};
|
||||
});
|
||||
|
||||
// Relations
|
||||
export const usersRelations = relations(users, ({ many }) => ({
|
||||
verifiedSources: many(sources),
|
||||
sourceCategories: many(sourceCategories),
|
||||
assignedReports: many(reports),
|
||||
auditLogs: many(auditLogs)
|
||||
}));
|
||||
|
||||
export const categoriesRelations = relations(categories, ({ many }) => ({
|
||||
sourceCategories: many(sourceCategories)
|
||||
}));
|
||||
|
||||
export const sourcesRelations = relations(sources, ({ one, many }) => ({
|
||||
verifiedBy: one(users, {
|
||||
fields: [sources.verifiedBy],
|
||||
references: [users.id]
|
||||
}),
|
||||
sourceCategories: many(sourceCategories)
|
||||
}));
|
||||
|
||||
export const sourceCategoriesRelations = relations(sourceCategories, ({ one }) => ({
|
||||
source: one(sources, {
|
||||
fields: [sourceCategories.sourceId],
|
||||
references: [sources.id]
|
||||
}),
|
||||
category: one(categories, {
|
||||
fields: [sourceCategories.categoryId],
|
||||
references: [categories.id]
|
||||
}),
|
||||
addedBy: one(users, {
|
||||
fields: [sourceCategories.addedBy],
|
||||
references: [users.id]
|
||||
})
|
||||
}));
|
||||
|
||||
export const reportsRelations = relations(reports, ({ one }) => ({
|
||||
assignedTo: one(users, {
|
||||
fields: [reports.assignedTo],
|
||||
references: [users.id]
|
||||
})
|
||||
}));
|
||||
|
||||
export const auditLogsRelations = relations(auditLogs, ({ one }) => ({
|
||||
user: one(users, {
|
||||
fields: [auditLogs.userId],
|
||||
references: [users.id]
|
||||
})
|
||||
}));
|
||||
98
lib/db/seed.ts
Normal file
98
lib/db/seed.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import { db } from './connection';
|
||||
import { users, categories, sources, apiKeys, sourceCategories } from './schema';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
|
||||
export async function seedDatabase() {
|
||||
try {
|
||||
console.log('🌱 Seeding database...');
|
||||
|
||||
// Insert categories one by one
|
||||
const categoryData = [
|
||||
{ name: 'Hoax', slug: 'hoax', description: 'Šírenie nepravdivých informácií a hoaxov', color: '#EF4444', priority: 5, icon: 'AlertTriangle' },
|
||||
{ name: 'Hate Speech', slug: 'hate-speech', description: 'Nenávistné prejavy proti skupinám ľudí', color: '#DC2626', priority: 5, icon: 'MessageSquareX' },
|
||||
{ name: 'Violence', slug: 'violence', description: 'Povzbudzovanie k násiliu', color: '#B91C1C', priority: 5, icon: 'Sword' },
|
||||
{ name: 'Conspiracy', slug: 'conspiracy', description: 'Konšpiračné teórie', color: '#F59E0B', priority: 3, icon: 'Eye' },
|
||||
{ name: 'Propaganda', slug: 'propaganda', description: 'Politická propaganda a manipulácia', color: '#D97706', priority: 2, icon: 'Megaphone' }
|
||||
];
|
||||
|
||||
const insertedCategories = [];
|
||||
for (const cat of categoryData) {
|
||||
const result = await db.insert(categories).values(cat).returning();
|
||||
insertedCategories.push(result[0]);
|
||||
console.log(`✅ Inserted category: ${cat.name}`);
|
||||
}
|
||||
|
||||
// Insert admin user
|
||||
const hashedPassword = await bcrypt.hash('admin123', 12);
|
||||
const insertedUsers = await db.insert(users).values({
|
||||
email: 'admin@antihoax.sk',
|
||||
passwordHash: hashedPassword,
|
||||
name: 'System Admin',
|
||||
role: 'admin'
|
||||
}).returning();
|
||||
console.log(`✅ Inserted user: ${insertedUsers[0].name}`);
|
||||
|
||||
// Insert example sources
|
||||
const sourceData = [
|
||||
{
|
||||
url: 'https://example-hoax-site.com',
|
||||
domain: 'example-hoax-site.com',
|
||||
title: 'Example Hoax Site',
|
||||
description: 'Príklad hoax stránky pre testovanie',
|
||||
type: 'website' as const,
|
||||
status: 'verified' as const,
|
||||
riskLevel: 5,
|
||||
language: 'sk' as const,
|
||||
reportedBy: 'test@example.com',
|
||||
verifiedBy: insertedUsers[0].id,
|
||||
followerCount: 1500,
|
||||
metadata: JSON.stringify({ tags: ['test', 'example'] })
|
||||
},
|
||||
{
|
||||
url: 'https://example-conspiracy.com',
|
||||
domain: 'example-conspiracy.com',
|
||||
title: 'Conspiracy Theory Site',
|
||||
description: 'Stránka šíriaca konšpiračné teórie',
|
||||
type: 'blog' as const,
|
||||
status: 'verified' as const,
|
||||
riskLevel: 3,
|
||||
language: 'sk' as const,
|
||||
reportedBy: 'reporter@example.com',
|
||||
verifiedBy: insertedUsers[0].id,
|
||||
followerCount: 850,
|
||||
metadata: JSON.stringify({ tags: ['conspiracy', 'politics'] })
|
||||
}
|
||||
];
|
||||
|
||||
const insertedSources = [];
|
||||
for (const src of sourceData) {
|
||||
const result = await db.insert(sources).values(src).returning();
|
||||
insertedSources.push(result[0]);
|
||||
console.log(`✅ Inserted source: ${src.title}`);
|
||||
}
|
||||
|
||||
// Link sources with categories
|
||||
await db.insert(sourceCategories).values({
|
||||
sourceId: insertedSources[0].id,
|
||||
categoryId: insertedCategories[0].id, // Hoax
|
||||
confidenceScore: '1.0',
|
||||
addedBy: insertedUsers[0].id
|
||||
});
|
||||
|
||||
await db.insert(sourceCategories).values({
|
||||
sourceId: insertedSources[1].id,
|
||||
categoryId: insertedCategories[3].id, // Conspiracy
|
||||
confidenceScore: '0.9',
|
||||
addedBy: insertedUsers[0].id
|
||||
});
|
||||
|
||||
console.log(`✅ Linked sources with categories`);
|
||||
|
||||
console.log('🎉 Database seeded successfully!');
|
||||
console.log('📧 Admin login: admin@antihoax.sk / admin123');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error seeding database:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user