_db_fetch.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const { Client } = require('pg');
  2. const fs = require('fs');
  3. async function main() {
  4. const client = new Client({
  5. host: '39.98.38.2',
  6. port: 5866,
  7. database: 'highgo',
  8. user: 'highgo',
  9. password: 'Gw@141516',
  10. connectionTimeoutMillis: 30000,
  11. });
  12. try {
  13. await client.connect();
  14. console.log('Connected to database');
  15. // Get all tables in slaj schema
  16. const tableRes = await client.query(`
  17. SELECT table_name
  18. FROM information_schema.tables
  19. WHERE table_schema = 'slaj' AND table_type = 'BASE TABLE'
  20. ORDER BY table_name
  21. `);
  22. const tableNames = tableRes.rows.map(r => r.table_name);
  23. console.log(`Total tables in slaj schema: ${tableNames.length}`);
  24. // Get columns for each table
  25. const dbTables = {};
  26. for (const tname of tableNames) {
  27. const colRes = await client.query(`
  28. SELECT column_name, data_type,
  29. COALESCE(character_maximum_length, numeric_precision) AS max_length,
  30. is_nullable,
  31. column_default
  32. FROM information_schema.columns
  33. WHERE table_schema = 'slaj' AND table_name = $1
  34. ORDER BY ordinal_position
  35. `, [tname]);
  36. const cols = colRes.rows.map(c => ({
  37. name: c.column_name.toUpperCase(),
  38. type: c.data_type.toUpperCase(),
  39. nullable: c.is_nullable === 'YES',
  40. default: c.column_default,
  41. }));
  42. dbTables[tname] = cols;
  43. }
  44. // Save to JSON
  45. fs.writeFileSync('d:/Workspaces/slaj/_slaj_db_tables.json', JSON.stringify(dbTables, null, 2));
  46. console.log(`Saved ${Object.keys(dbTables).length} tables to _slaj_db_tables.json`);
  47. // Print table list
  48. console.log('\n=== 安监数据库 slaj schema 表列表 ===');
  49. for (const tname of Object.keys(dbTables).sort()) {
  50. console.log(`${tname} (${dbTables[tname].length} cols)`);
  51. }
  52. await client.end();
  53. } catch (err) {
  54. console.error('Error:', err.message);
  55. process.exit(1);
  56. }
  57. }
  58. main();