Files
web-antrean/scripts/view-db.js
2026-01-06 14:26:19 +07:00

74 lines
2.0 KiB
JavaScript

// Quick database viewer - shows all content and exits
// Usage: node scripts/view-db.js
import Database from 'better-sqlite3';
import path from 'path';
import fs from 'fs';
const dbPath = path.join(process.cwd(), 'data', 'users.db');
if (!fs.existsSync(dbPath)) {
console.error('❌ Database not found at:', dbPath);
process.exit(1);
}
const db = new Database(dbPath);
console.log('📊 Database Content Viewer\n');
console.log('Database path:', dbPath);
console.log('='.repeat(80));
// Get all tables
const tables = db.prepare(`
SELECT name FROM sqlite_master
WHERE type='table' AND name NOT LIKE 'sqlite_%'
`).all();
console.log('\n📋 Available Tables:');
tables.forEach((table, index) => {
console.log(` ${index + 1}. ${table.name}`);
});
// Show content for each table
tables.forEach((table) => {
const tableName = table.name;
// Get row count
const count = db.prepare(`SELECT COUNT(*) as total FROM ${tableName}`).get();
console.log('\n' + '='.repeat(80));
console.log(`\n📊 Table: ${tableName} (${count.total} rows)`);
console.log('-'.repeat(80));
// Get table schema
const schema = db.prepare(`PRAGMA table_info(${tableName})`).all();
console.log('\n📐 Schema:');
console.table(schema.map(col => ({
column: col.name,
type: col.type,
nullable: col.notnull === 0 ? 'YES' : 'NO',
default: col.dflt_value || '-'
})));
// Get all data
const data = db.prepare(`SELECT * FROM ${tableName}`).all();
if (data.length === 0) {
console.log('\n📭 No data in this table.');
} else {
console.log(`\n📄 Data (showing all ${data.length} rows):`);
console.table(data);
// If there are many rows, also show a summary
if (data.length > 10) {
console.log(`\n💡 Tip: Showing all ${data.length} rows. Use the interactive script for filtering.`);
}
}
});
console.log('\n' + '='.repeat(80));
console.log('\n✅ Done! Use `node scripts/db-access.js` for interactive queries.\n');
db.close();