- 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
60 lines
2.0 KiB
TypeScript
60 lines
2.0 KiB
TypeScript
import { db, schema } from './db/connection'
|
|
import { sql, count } from 'drizzle-orm'
|
|
|
|
export async function optimizeDatabase(): Promise<void> {
|
|
try {
|
|
// PostgreSQL automatically creates indexes defined in schema.ts
|
|
// Run ANALYZE to update statistics for query optimization
|
|
const tables = [
|
|
'sources',
|
|
'reports',
|
|
'api_keys',
|
|
'source_categories',
|
|
'categories',
|
|
'users'
|
|
]
|
|
|
|
for (const tableName of tables) {
|
|
await db.execute(sql`ANALYZE ${sql.raw(tableName)}`)
|
|
}
|
|
|
|
// PostgreSQL equivalent of VACUUM - VACUUM ANALYZE updates statistics and cleans up
|
|
await db.execute(sql`VACUUM ANALYZE`)
|
|
|
|
console.log('Database optimization completed successfully')
|
|
|
|
} catch (error) {
|
|
console.error('Database optimization failed:', error)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export async function getDatabaseStats(): Promise<any> {
|
|
try {
|
|
// Get row counts for each table
|
|
const [sourcesCount] = await db.select({ count: count() }).from(schema.sources)
|
|
const [reportsCount] = await db.select({ count: count() }).from(schema.reports)
|
|
const [apiKeysCount] = await db.select({ count: count() }).from(schema.apiKeys)
|
|
const [categoriesCount] = await db.select({ count: count() }).from(schema.categories)
|
|
const [usersCount] = await db.select({ count: count() }).from(schema.users)
|
|
const [sourceCategoriesCount] = await db.select({ count: count() }).from(schema.sourceCategories)
|
|
|
|
const tables = [
|
|
{ table_name: 'sources', row_count: sourcesCount.count },
|
|
{ table_name: 'reports', row_count: reportsCount.count },
|
|
{ table_name: 'api_keys', row_count: apiKeysCount.count },
|
|
{ table_name: 'categories', row_count: categoriesCount.count },
|
|
{ table_name: 'users', row_count: usersCount.count },
|
|
{ table_name: 'source_categories', row_count: sourceCategoriesCount.count }
|
|
]
|
|
|
|
return {
|
|
tables,
|
|
optimized_at: new Date().toISOString()
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('Failed to get database stats:', error)
|
|
throw error
|
|
}
|
|
} |