118 lines
4.1 KiB
JavaScript
118 lines
4.1 KiB
JavaScript
/**
|
||
* Supabase 连接配置检查脚本
|
||
* 帮助验证 Supabase 连接配置是否正确
|
||
*/
|
||
|
||
require('dotenv').config();
|
||
|
||
class SupabaseConfigChecker {
|
||
constructor() {
|
||
this.config = {
|
||
SUPABASE_URL: process.env.SUPABASE_URL,
|
||
SUPABASE_ANON_KEY: process.env.SUPABASE_ANON_KEY,
|
||
SUPABASE_SERVICE_ROLE_KEY: process.env.SUPABASE_SERVICE_ROLE_KEY,
|
||
API_KEY: process.env.API_KEY
|
||
};
|
||
}
|
||
|
||
async checkConfig() {
|
||
console.log('🔍 检查 Supabase 配置...\n');
|
||
|
||
// 检查环境变量
|
||
console.log('📋 环境变量检查:');
|
||
for (const [key, value] of Object.entries(this.config)) {
|
||
if (value) {
|
||
const displayValue = value.length > 20 ?
|
||
value.substring(0, 20) + '...' : value;
|
||
console.log(`✅ ${key}: ${displayValue}`);
|
||
} else {
|
||
console.log(`❌ ${key}: 未设置`);
|
||
}
|
||
}
|
||
|
||
console.log('\n🌐 URL 格式检查:');
|
||
if (this.config.SUPABASE_URL) {
|
||
try {
|
||
const url = new URL(this.config.SUPABASE_URL);
|
||
console.log(`✅ 协议: ${url.protocol}`);
|
||
console.log(`✅ 主机: ${url.hostname}`);
|
||
console.log(`✅ 端口: ${url.port || '默认'}`);
|
||
} catch (error) {
|
||
console.log(`❌ URL 格式错误: ${error.message}`);
|
||
}
|
||
}
|
||
|
||
// 尝试基本连接测试
|
||
if (this.config.SUPABASE_URL && this.config.SUPABASE_ANON_KEY) {
|
||
console.log('\n🔗 尝试连接测试...');
|
||
await this.testConnection();
|
||
} else {
|
||
console.log('\n⚠️ 无法进行连接测试,缺少必要配置');
|
||
}
|
||
|
||
console.log('\n💡 配置建议:');
|
||
this.provideSuggestions();
|
||
}
|
||
|
||
async testConnection() {
|
||
try {
|
||
const { createClient } = require('@supabase/supabase-js');
|
||
|
||
const supabase = createClient(
|
||
this.config.SUPABASE_URL,
|
||
this.config.SUPABASE_ANON_KEY
|
||
);
|
||
// 尝试一个简单的查询
|
||
const { data, error } = await supabase
|
||
.from('ps_push_messages')
|
||
.select('count')
|
||
.limit(1);
|
||
|
||
if (error) {
|
||
console.log(`❌ 连接测试失败: ${error.message}`);
|
||
|
||
// 提供错误分析
|
||
if (error.message.includes('authentication')) {
|
||
console.log('💡 建议: 检查 SUPABASE_ANON_KEY 是否正确');
|
||
} if (error.message.includes('relation') && error.message.includes('does not exist')) {
|
||
console.log('💡 建议: 数据库中缺少 ps_push_messages 表,请先执行数据库初始化脚本');
|
||
}
|
||
if (error.message.includes('connection')) {
|
||
console.log('💡 建议: 检查 SUPABASE_URL 是否可访问');
|
||
}
|
||
} else {
|
||
console.log('✅ 基本连接测试成功');
|
||
}
|
||
|
||
} catch (error) {
|
||
console.log(`❌ 连接测试出错: ${error.message}`);
|
||
}
|
||
}
|
||
|
||
provideSuggestions() {
|
||
console.log('1. 确保 Supabase 实例正在运行并可访问');
|
||
console.log('2. 检查防火墙和网络配置');
|
||
console.log('3. 验证 API 密钥的有效性');
|
||
console.log('4. 确保数据库中存在必要的表结构');
|
||
console.log('5. 如使用自托管 Supabase,确认服务状态');
|
||
|
||
console.log('\n📝 下一步操作:');
|
||
console.log('1. 更新 .env 文件中的正确配置');
|
||
console.log('2. 运行: npm run setup-supabase');
|
||
console.log('3. 运行: npm test');
|
||
console.log('4. 运行: npm start');
|
||
}
|
||
}
|
||
|
||
// 执行检查
|
||
async function main() {
|
||
const checker = new SupabaseConfigChecker();
|
||
await checker.checkConfig();
|
||
}
|
||
|
||
if (require.main === module) {
|
||
main().catch(console.error);
|
||
}
|
||
|
||
module.exports = SupabaseConfigChecker;
|