mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-25 17:54:34 +08:00
fix(sql): restore SQL Lab CSV export (resolve #240)
This commit is contained in:
1 parent
733cd0db95
commit
68987617dc
2 files changed
+35
-1
No files matched your search
@@ -262,6 +262,33 @@ describe('registerSharedRoutes smoke tests', () => {
|
||||
assert.equal(countRow.count, 3)
|
||||
})
|
||||
|
||||
it('POST /_web/sessions/:id/sql returns rows in the SQL Lab columnar shape', async () => {
|
||||
const db = createSessionDb()
|
||||
const routeApp = Fastify()
|
||||
registerSharedRoutes(routeApp, createTestContext(new Map([['chat-1', db]])))
|
||||
await routeApp.ready()
|
||||
|
||||
const resp = await routeApp.inject({
|
||||
method: 'POST',
|
||||
url: '/_web/sessions/chat-1/sql',
|
||||
payload: { sql: 'SELECT id, content FROM message ORDER BY id LIMIT 2' },
|
||||
})
|
||||
|
||||
await routeApp.close()
|
||||
db.close()
|
||||
|
||||
assert.equal(resp.statusCode, 200)
|
||||
const body = resp.json()
|
||||
assert.deepEqual(body.columns, ['id', 'content'])
|
||||
assert.deepEqual(body.rows, [
|
||||
[1, 'alpha first'],
|
||||
[2, 'alpha from bob'],
|
||||
])
|
||||
assert.equal(body.rowCount, 2)
|
||||
assert.equal(body.limited, false)
|
||||
assert.equal(typeof body.duration, 'number')
|
||||
})
|
||||
|
||||
it('PATCH /_web/sessions/:id/name updates the shared session metadata', async () => {
|
||||
const db = createSessionDb()
|
||||
const routeApp = Fastify()
|
||||
|
||||
@@ -12,7 +12,14 @@ export function registerSqlRoutes(server: FastifyInstance, ctx: HttpRouteContext
|
||||
return reply.code(400).send({ error: 'Missing sql parameter' })
|
||||
}
|
||||
try {
|
||||
return executeSql(db, sql, { timing: true })
|
||||
const result = executeSql(db, sql, { columnar: true, timing: true, maxRows: 0 })
|
||||
return {
|
||||
columns: result.columns,
|
||||
rows: result.rows,
|
||||
rowCount: result.rowCount,
|
||||
duration: result.duration ?? 0,
|
||||
limited: result.truncated,
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : 'SQL execution error'
|
||||
return reply.code(400).send({ error: message })
|
||||
|
||||
Reference in new issue
Block a user