Skip to content

Commit

Permalink
fix: /queries endpoint should return arrays not number keyed results
Browse files Browse the repository at this point in the history
  • Loading branch information
christianmat committed Nov 22, 2024
1 parent a64b44f commit 2339970
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion apps/trench/src/queries/queries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common'
import { ClickHouseService } from '../services/data/click-house/click-house.service'
import {
convertJsonKeysToCamelCase,
convertObjectToArray,
convertToKebabCase,
isReadOnlyQuery,
parseJsonFields,
Expand Down Expand Up @@ -31,6 +32,8 @@ export class QueriesService {
this.clickhouseService.queryResults(convertToKebabCase(query), workspace.databaseName)
)
const results = await Promise.all(queryPromises)
return results.map((result) => parseJsonFields(convertJsonKeysToCamelCase(result)))
return results.map((result) =>
convertObjectToArray(parseJsonFields(convertJsonKeysToCamelCase(result)))
)
}
}
4 changes: 4 additions & 0 deletions apps/trench/src/queries/queries.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ export function parseJsonFields(json: Record<string, any>): Record<string, any>

return result
}

export function convertObjectToArray(object: Record<string, any>): any[] {
return Object.values(object)
}
32 changes: 32 additions & 0 deletions apps/trench/test/e2e/queries.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { authenticatedPost, PUBLIC_API_KEY, waitForQueryResults } from './utils'

describe('queries/', () => {
test('should create an event and execute a simple read-only query on it', async () => {
const newEvent = {
type: 'track',
event: 'User SignedUp',
timestamp: new Date().toISOString(),
}

// Create a new event
const createRes = await authenticatedPost('/events', PUBLIC_API_KEY).send({
events: [newEvent],
})
expect(createRes.statusCode).toEqual(201)
expect(createRes.body.results).toHaveLength(1)
const eventUuid = createRes.body.results[0].uuid
// Wait for the event to be created
const results = await waitForQueryResults(`uuid=${eventUuid}`)
expect(results).toHaveLength(1)
expect(results[0].uuid).toEqual(eventUuid)

// Execute the query
const query = `SELECT * FROM events WHERE uuid = '${eventUuid}'`
const executeRes = await authenticatedPost('/queries').send({
queries: [query],
})
expect(executeRes.statusCode).toEqual(201)
expect(executeRes.body.results).toHaveLength(1)
expect(executeRes.body.results[0][0].uuid).toEqual(eventUuid)
})
})

0 comments on commit 2339970

Please sign in to comment.