Files
promote/backend/scripts/check-postgres-projects.js
2025-03-11 00:36:22 +08:00

51 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
require('dotenv').config();
const { Pool } = require('pg');
// 创建PostgreSQL连接池
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
// 获取所有项目
async function getAllProjects() {
try {
const client = await pool.connect();
const result = await client.query('SELECT id, name, description, status FROM public.projects');
client.release();
return result.rows;
} catch (error) {
console.error('获取项目失败:', error);
return [];
}
}
// 主函数
async function main() {
console.log('查询PostgreSQL数据库中的项目...');
try {
// 获取所有项目
const projects = await getAllProjects();
if (projects.length === 0) {
console.log('没有找到任何项目,请先插入测试项目数据');
} else {
console.log(`找到 ${projects.length} 个项目:`);
console.table(projects);
// 提供一个示例项目ID用于漏斗接口
console.log('\n漏斗接口可以使用的项目ID示例:');
console.log(`项目ID: ${projects[0].id}`);
console.log(`接口URL示例: http://localhost:4000/api/analytics/project/${projects[0].id}/conversion-funnel?timeRange=30days`);
}
} catch (error) {
console.error('查询项目过程中发生错误:', error);
} finally {
// 关闭连接池
await pool.end();
}
}
// 执行主函数
main();