更新依赖项,添加 flowme 插入触发器和监听器;重构数据库连接管理;优化用户路由和 SSE 处理
This commit is contained in:
47
src/routes/flowme/listener/index.ts
Normal file
47
src/routes/flowme/listener/index.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { schema, app, db } from '@/app.ts'
|
||||
import { Client } from 'pg'
|
||||
|
||||
let pgClient: Client | null = null
|
||||
|
||||
async function startFlowmeListener() {
|
||||
// 使用独立的数据库连接来监听
|
||||
pgClient = new Client({
|
||||
connectionString: process.env.DATABASE_URL || 'postgresql://postgres:postgres@localhost:5432/code_center',
|
||||
})
|
||||
|
||||
await pgClient.connect()
|
||||
console.log('🔌 已连接到 PostgreSQL 监听器')
|
||||
|
||||
// 订阅通知事件
|
||||
pgClient.on('notification', (data) => {
|
||||
if (!data.payload) return
|
||||
try {
|
||||
const parsed = JSON.parse(data.payload)
|
||||
console.log('📥 收到新 flowme 创建通知:', parsed)
|
||||
|
||||
// 在这里处理你的业务逻辑
|
||||
handleNewFlowme(parsed)
|
||||
} catch (err) {
|
||||
console.error('❌ 解析 flowme 通知失败:', err)
|
||||
}
|
||||
})
|
||||
|
||||
// 执行 LISTEN 命令订阅通道
|
||||
await pgClient.query('LISTEN flowme_insert')
|
||||
|
||||
console.log('👂 开始监听 flowme_insert 通道...')
|
||||
}
|
||||
|
||||
function handleNewFlowme(data: any) {
|
||||
// 根据新创建的 flowme 数据执行相应操作
|
||||
console.log('处理新 flowme:', data.id, data.title)
|
||||
|
||||
// 示例:可以通过 WebSocket 推送给前端
|
||||
// wsServer.emit('flowme:created', data)
|
||||
}
|
||||
|
||||
// 启动监听器(只启动一次)
|
||||
if (!global.__flowmeListenerStarted) {
|
||||
global.__flowmeListenerStarted = true
|
||||
startFlowmeListener().catch(console.error)
|
||||
}
|
||||
@@ -18,4 +18,6 @@ import './prompts/index.ts'
|
||||
|
||||
import './views/index.ts';
|
||||
|
||||
import './query-views/index.ts';
|
||||
import './query-views/index.ts';
|
||||
|
||||
import './flowme/index.ts'
|
||||
Reference in New Issue
Block a user