index.ts 867 B

12345678910111213141516171819202122232425262728293031
  1. import { drizzle } from 'drizzle-orm/node-postgres';
  2. import { Pool } from 'pg';
  3. import * as dotenv from 'dotenv';
  4. import * as authSchema from './schema/auth';
  5. import * as resourceSchema from './schema/resource';
  6. dotenv.config();
  7. const pool = new Pool({
  8. connectionString: process.env.DATABASE_URL,
  9. });
  10. export const db = drizzle(pool, { schema: { ...authSchema, ...resourceSchema } });
  11. // 用于测试连接的函数
  12. export async function testConnection() {
  13. try {
  14. const client = await pool.connect();
  15. console.log('✅ 成功连接到 PostgreSQL 数据库!');
  16. const res = await client.query('SELECT NOW()');
  17. console.log('🕒 数据库当前时间:', res.rows[0].now);
  18. client.release();
  19. } catch (err) {
  20. console.error('❌ 数据库连接失败:', err);
  21. process.exit(1);
  22. }
  23. }
  24. if (require.main === module) {
  25. testConnection();
  26. }